Table of Contents – Should
I finished the Should chapter in The Pester Book. As I’ve already been doing some testing with Pester, Should is a keyword, or command, in which I’m familiar, and in which I’ve used. It works this way: the Describe block, houses optional Context block(s), which house It block(s), which include an assertion. This assertion includes a PowerShell command piped to Should, that either passes or fails.
It it’s simplest form, a nested It block might look something like this example, taken almost exactly from The Pester Book.
... It ‘Testing 1 is an Integer:’ { 1 | Should BeOfType [int] } ...
Because the numeric value of 1 is in fact an integer, this test will succeed. When Pester runs against this assertion, it’s going to pass, and produce output such as this:
... [+] Testing 1 is an Integer: 28ms ...
Those test results will be in green — the color of success.
The piping of our command to Should turns useable PowerShell, into Pester testing. Previously, we’ve discussed the keywords necessary to create the test framework. Now, we’re adding to our test, what’s really making it a test.
The Should keyword, or command, requires operators. We saw the -BeOfType operator in the above example. These operators, according to The Pester Book, are Be, BeExactly, BeLessThan, BeGreaterThan, BeLike, BeLikeExactly, Match, MatchExactly, BeOfType, Throw, Exist, Contain, ContainExactly, BeNullOrEmpty, and BeIn. Keep your eyes out for new ones, as Pester is continually being developed.
Remember, Should takes our assertion and determines whether it’s accurate or not. If the test is accurate, or is true, the test has passed. In reverse, if the test isn’t accurate, or is false, it has failed. Remember however, that you can have a successful test, if you’re testing for failure.