Understanding Pester

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Robbie Courtney Robbie Courtney 1 year, 3 months ago.

  • Author
  • #27731
    Profile photo of Robbie Courtney
    Robbie Courtney

    I've been trying to use pester to start automating test of my PowerShell scripts but keep getting stuck. I think a lot of it is due to the design of my scripts not being as testable as they should be. All of the examples that I see for Pester is basically testing a library of functions. What about testing a controller script is that possible? Or should you just test the helper functions of the controller script that is stored in ps1 or module files? It seems to me that Pester is great for testing individual functions, but can't figure out how to use for testing the logic of a controller script since pester has to load the script first, this causes the controller script to then run.

    If anyone can give me pointers I would appreciate it. I have watched 2 sessions from Dave Wyatt on pester, one was “Beyond Syntax: Pester Testing”, and another was a recording from the 2015 PowerShell summit I think. I have also read countless blogs on the subject to include reading the GitHub examples on Pester, but still can't quite figure out how to apply it to my scripts.

  • #27733
    Profile photo of Dave Wyatt
    Dave Wyatt

    Controller scripts can definitely be tricky to test, particularly if they both define functions and then execute code in the same file. Jakub made a blog post on this topic a while back: http://jakubjares.com/blog/2015/01/10/test-powershell-scripts-end-to-end-with-pester/

    If you have the option, moving the function definitions into a separate ps1 file or script module will make testing easier.

  • #27737
    Profile photo of Robbie Courtney
    Robbie Courtney

    Thank you for clearing that up. I kept thinking I was missing something, I'm just going to need to get creative in creating test or just focus on on testing the most important logic. It's nice to get an answer from the person that wrote a lot of the current Pester code. Thanks Dave

You must be logged in to reply to this topic.