

In practice, these limitations don't matter much. (Note that the return keyword itself does not trigger the breakpoint, so Pester would not report coverage analysis for a return statement which is not passed a value.) In the example script, the 5 analyzed commands are the expression evaluated by the if statement in FunctionOne, and the expressions after each of the four return statements. Breakpoints are not triggered by keywords such as else, try, or finally, or on opening or closing braces, but breakpoints can be triggered by expressions passed to certain keywords, such as the conditions evaluated by if statements and the various loop constructs, throw and return statements which are passed a value, and so on. Breakpoints can only be triggered by commands in PowerShell, which includes both calls to functions, Cmdlets and programs, as well as expressions and variable assignments. This is a limitation of the current implementation of the coverage analysis, which uses PSBreakpoints to track which commands are executed. You may have noticed that even though CoverageTest.ps1 is 17 lines long, Pester reports that only 5 commands are being analyzed for coverage.

Here is the CoverageTest.ps1 script file:Ī quick note on the "analyzed commands" numbers. Here are some examples of the various ways the -CodeCoverage parameter can be used, and their corresponding output. If you are using Invoke-Pester's -PassThru switch, the coverage analysis will also be available on the output object, under its CodeCoverage property. If this key is used and no corresponding value is assigned to StartLine, the entire file up to and including EndLine will be analyzed.Īfter Invoke-Pester finishes executing the test scripts, Pester will output a coverage report to the console.

Hashtables, which give you finer control over which sections of the file are analyzed for coverage.Strings, which refer to the path of the script files for which you want to generate coverage metrics.The arguments to this parameter may be one of the following: To generate Code Coverage metrics, pass one or more values to the -CodeCoverage parameter of the Invoke-Pester command. Note: Unlike most of Pester, use of the Code Coverage feature requires PowerShell version 3.0 or later. Pester can generate these code coverage metrics for you while it is executing unit tests. It's a good general indicator of how thoroughly your code has been tested, that all branches and edge cases are working properly, etc. Code Coverage refers to the percentage of lines of code that are tested by a suite of unit tests.
