The assertion is going to call our function and then define what the output should be. There are many Should operators, but again, for simplicity, start with the Be operator. If I call the function while using the ThingToGet parameter, then the output should change the message to include this parameter value. If you call the function Get-Something without any parameters, the output should be "I got something!". For the purposes of this article, you are going to focus on the Should keyword. Assertions are defining what you expect to happen (and this should be reflected in the name of the It block).
Next, you need to write the code that will replace our # Assertion comment in the code above. Now that the Pester test file is organized, how do you actually write the tests? Writing Assertions If the ThingToGet is used, then the output changes to reflect the string passed to the parameter. If the ThingToGet parameter is not used, then the output should be "I got something!". Each Context block covers if we use a parameter or not when calling the function.įinally, within each Context block is the It block describing the test and the expected result. Here we have a single Describe block for the Get-Something function, and within that block are two Context blocks. Take a look at a Describe block with Context and It blocks for testing the Get-Something function: 'PesterMockcbaa9725-8267-4c27-8605-6a94eaf92de7' which ultimately calls your mock script block, and it then generates an alias for the function you're mocking which resolves to this function. This is very much like variable scoping in regular PowerShell code when dealing with if-then statements or loops. To implement a Mock, Pester creates a randomly-named function in the appropriate scope (e.g. However, information defined in an It block will not be available to the parent Context or Describe blocks. Therefore, the synonym for 'pester' is 'bother.' 100 9 Gratify refers to giving (someone). A variable or other information defined in a Describe block will be available to any nested Context or It blocks. Just like describe, each block has its own scope. The It block is the actual test that you want to perform against the code. It blocks are the actual test and can be located under Describe or Context blocks. Synonyms for MOCK: burlesque, caricature, do, imitate, mimic, parody, send up, spoof Antonyms for MOCK: comply (with), conform (to), follow, mind, obey.
Context blocks are optional and just provide an additional group of It blocks within the Describe block. A Describe block can contain any number of Context and It blocks scoped to it. The Describe block contains a logical grouping of tests, and anything defined in that Describe block will no longer exist when it exits. There are six types of blocks we can use, and this article focuses on three:ĭescribe blocks are the highest level of organization in the Pester test file. Open up the 1 in your favorite editor and we’ll explore how tests are organized. With Pester installed and module code available, it’s time to write your first Pester test.