Testing code that sets up your test scenario


This topic contains 1 reply, has 2 voices, and was last updated by  Adam Bertram 1 week ago.

  • Author
  • #77035

    JB Lewis

    I've been asked to rewrite a script that deals with log files. So for testing the script, I wrote a function in the .tests.ps1 that creates a bunch of log files, on the test drive, that my script can act on.

    My question is, how do people feel about the need to write a test to test that helper function?

  • #77058

    Adam Bertram

    You should avoid writing any kind of "code" in your tests. You should focus on keeping tests as simple as possible. That means avoiding any kind of logic like loops, if/then statements, etc. The question is do you trust that helper function? Is it as simple as possible? Can you see any possible way it can go wrong? If it's a super-simple helper function like function CreateLogFiles { param() 0..10 | % { Add-Content -Path "C:\Files\$_.log" -Value 'foo' }} then I wouldn't create a test for that.

    Just make sure to keep as simple as absolutely possible and don't worry about repeating yourself. It's better to keep it simple and easy to understand rather than needing tests for your tests.

You must be logged in to reply to this topic.