Testing code that sets up your test scenario

Welcome Forums Pester Testing code that sets up your test scenario

This topic contains 3 replies, has 2 voices, and was last updated by

1 year, 1 month ago.

  • Author
  • #77035


    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


    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.

    • #79453


      Adam, you pretty much hit the nail on the head: dead simple, no logic. I don't even bother putting anything in the files, I just create x number of files with distinct names. So much of what I do revolves around dealing with files, so for testing, I need to make a known set of files available to work against.

      Thanks for your feedback!

  • #79454


    For unit tests, I don't even create the files. I mock out everything that won't run in memory. That way the I'm purely testing the code and no other environmental factors. Chances are that creating a temp file on the drive is going to work just fine but it's always safer, when unit testing, to mock all environmental dependencies.

The topic ‘Testing code that sets up your test scenario’ is closed to new replies.