Invoke-Pester
Invoke-Pester [[-Script] <Object[]>] [[-TestName] <String[]>] [[-EnableExit]] [[-OutputXml] <String>] [[-Tag]<String[]>] [-ExcludeTag <String[]>] [-PassThru] [-CodeCoverage <Object[]>] [-Strict] [-Quiet] [-PesterOption<Object>] [<CommonParameters>]
Invoke-Pester [[-Script] <Object[]>] [[-TestName] <String[]>] [[-EnableExit]] [[-Tag] <String[]>] [-ExcludeTag<String[]>] [-PassThru] [-CodeCoverage <Object[]>] [-Strict] -OutputFile* <String> -OutputFormat* <String> [-Quiet][-PesterOption <Object>] [<CommonParameters>]
Upon calling Invoke-Pester, all files that have a name containing"*.Tests.ps1" will have the tests defined in their Describe blocksexecuted. Invoke-Pester begins at the location of Path andruns recursively through each sub directory looking for"*.Tests.ps1" files containing tests. If a TestName is provided,Invoke-Pester will only run tests that have a describe block with amatching name. By default, Invoke-Pester will end the test run with asimple report of the number of tests passed and failed output to theconsole. One may want pester to "fail a build" in the event that anytests fail. To accomodate this, Invoke-Pester will return an exitcode equal to the number of failed tests if the EnableExit switch isset. Invoke-Pester will also write a NUnit style log of test resultsif the OutputXml parameter is provided. In these cases, Invoke-Pesterwill write the result log to the path provided in the OutputXmlparameter.
Optionally, Pester can generate a report of how much code is coveredby the tests, and information about any commands which were notexecuted.
Parameters
-Script <Object[]>
- Default value is .
This parameter indicates which test scripts should be run.This parameter may be passed simple strings (wildcards are allowed), or hashtables containing Path, Arguments and Parameters keys.If hashtables are used, the Parameters key must refer to a hashtable, and the Arguments key must refer to an array; these will be splatted to the test script(s) indicated in the Path key.
Note: If the path contains any wildcards, or if it refers to a directory, then Pester will search for and execute all test scripts named *.Tests.ps1 in the target path; the search is recursive. If the path contains no wildcards and refers to a file, Pester will just try to execute that file regardless of its name.
Aliased to 'Path' and 'relative_path' for backwards compatibility.
-TestName <String[]>
Informs Invoke-Pester to only run Describe blocks that match this name.
-EnableExit [<SwitchParameter>]
- Default value is False
Will cause Invoke-Pester to exit with a exit code equal to the number of failed tests once all tests have been run. Use this to "fail" a build when any tests fail.
-OutputXml <String>
The path where Invoke-Pester will save a NUnit formatted test results log file. If this path is not provided, no log will be generated.
-Tag <String[]>
Informs Invoke-Pester to only run Describe blocks tagged with the tags specified. Aliased 'Tags' for backwards compatibility.
-ExcludeTag <String[]>
Informs Invoke-Pester to not run blocks tagged with the tags specified.
-PassThru [<SwitchParameter>]
- Default value is False
Returns a Pester result object containing the information about the whole test run, and each test.
-CodeCoverage <Object[]>
- Default value is @()
Instructs Pester to generate a code coverage report in addition to running tests. You may pass either hashtables or strings to this parameter.If strings are used, they must be paths (wildcards allowed) to source files, and all commands in the files are analyzed for code coverage.By passing hashtables instead, you can limit the analysis to specific lines or functions within a file.Hashtables must contain a Path key (which can be abbreviated to just "P"), and may contain Function (or "F"), StartLine (or "S"), and EndLine ("E") keys to narrow down the commands to be analyzed.If Function is specified, StartLine and EndLine are ignored.If only StartLine is defined, the entire script file starting with StartLine is analyzed.If only EndLine is present, all lines in the script file up to and including EndLine are analyzed.Both Function and Path (as well as simple strings passed instead of hashtables) may contain wildcards.
-Strict [<SwitchParameter>]
- Default value is False
Makes Pending and Skipped tests to Failed tests. Useful for continuous integration where you need to make sure all tests passed.
-OutputFile <String>
- This value is required
-OutputFormat <String>
- This value is required
-Quiet [<SwitchParameter>]
- Default value is False
Disables the output Pester writes to screen. No other output is generated unless you specify PassThru, or one of the Output parameters.
-PesterOption <Object>
Sets advanced options for the test execution. Enter a PesterOption object, such as one that you create by using the New-PesterOption cmdlet, or a hash table in which the keys are option names and the values are option values.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,ErrorAction, ErrorVariable, WarningAction, WarningVariable,OutBuffer, PipelineVariable, and OutVariable.
Examples
-
PS C:\> Invoke-Pester This will find all *.Tests.ps1 files and run their tests. No exit code will be returned and no log file will be saved.
-
PS C:\> Invoke-Pester -Script ./tests/Utils* This will run all tests in files under ./Tests that begin with Utils and alsocontains .Tests.
-
PS C:\> Invoke-Pester -Script @{ Path = './tests/Utils*'; Parameters = @{ NamedParameter = 'Passed By Name' }; Arguments = @('Passed by position') } Executes the same tests as in Example 1, but will run them with the equivalent of the following command line: & $testScriptPath -NamedParameter 'Passed By Name' 'Passed by position'
-
PS C:\> Invoke-Pester -TestName "Add Numbers" This will only run the Describe block named "Add Numbers"
-
PS C:\> Invoke-Pester -EnableExit -OutputXml "./artifacts/TestResults.xml" This runs all tests from the current directory downwards and writes the results according to the NUnit schema to artifacts/TestResults.xml just below the current directory. The test run will return an exit code equal to the number of test failures.
-
PS C:\> Invoke-Pester -EnableExit -OutputFile "./artifacts/TestResults.xml" -OutputFormat NUnitxml This runs all tests from the current directory downwards and writes the results to an output file and NUnitxml output format
-
PS C:\> Invoke-Pester -CodeCoverage 'ScriptUnderTest.ps1' Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage report for all commands in the "ScriptUnderTest.ps1" file.
-
PS C:\> Invoke-Pester -CodeCoverage @{ Path = 'ScriptUnderTest.ps1'; Function = 'FunctionUnderTest' } Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage report for all commands in the "FunctionUnderTest" function in the "ScriptUnderTest.ps1" file.
-
PS C:\> Invoke-Pester -CodeCoverage @{ Path = 'ScriptUnderTest.ps1'; StartLine = 10; EndLine = 20 } Runs all *.Tests.ps1 scripts in the current directory, and generates a coverage report for all commands on lines 10 through 20 in the "ScriptUnderTest.ps1" file.
This work is licensed under a Creative Commons Attribution 4.0 International. It is attributed to Microsoft Corporation and can be found here.
PowerShell Commands