You might have heard of the recently added support for PowerShell development inside Visual Studio. This extension provides several features, with support for the Pester testing framework being one of them.
However, it seems that sometimes the Visual Studio 2013 just does not recognise your test scripts (even when they are named like SomeScript.Tests.ps1
).
As a quick workaround you can create an external tool under the Tools
menu and invoke Pester manually. You can do this by defining an External Tools...
with the following options:
- Title
Invoke-\&Pester
- Command
Powershell.exe
- Arguments
-command "$item = (Get-Item -Path $(TargetPath)); CD $item.Directory; Invoke-Pester;"
- [Optional] Initial directory
$(ProjectPath)
- Use Output window
TRUE
- Treat output as Unicode
FALSE
- Prompt for Arguments
FALSE
- [Inactive] Close on exit
TRUE
With in the test script we could load our module under test like this (with the help of BeforeEach
):
Describe "Do-Nothing" { Context "Function Returns Nothing" { BeforeEach { Write-Verbose "Before-Each"; Import-Module "..PoSHModulebinDebugPoSHModule.dll" -Verbose; } It "Should Return Nothing" { # Act $r = Do-Nothing; # Assert $r | Should Be $null; } } }
You can then run Pester by selecting Invoke-Pester
from the Tools
menu. The command will change to the project directory and invoke all tests (i.e. all files that match this filter: *.Tests.ps1
). The test result is shown on the Output
window (but without colouring and highlighting):
Describing Do-Nothing
Context Function Returns Nothing
VERBOSE: Before-Each
VERBOSE: Loading module from path '..\PoSHModule\bin\Debug\PoSHModule.dll'.
VERBOSE: Importing cmdlet 'Do-Nothing'.
[+] Should Return Nothing 498ms
Tests completed in 498ms
Passed: 1 Failed: 0 Skipped: 0 Pending: 0