Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Invoke-Pester -Path "C:\GIT\GITHUB\dbatools.Workspace\Test-Examples.ps1" -OutputFile 'C:\Temp\dbatools\pester.xml' -OutputFormat NUnitXml
- #$testRes = [XML](Get-Content -Path "C:\Temp\dbatools\pester.xml")
- #$testRes.SelectNodes("//test-suite/results/test-case/failure/..").OuterXml | Out-File 'C:\Temp\dbatools\Analysis\Res.xml'
- Import-Module "C:\GIT\GITHUB\dbatools.Workspace\dbatools" -Force
- $excludeCommands = @(
- "Import-ModuleFile"
- , "Get-DeepClone"
- )
- $commandsRaw = Get-Command -Module dbatools
- if ($excludeCommands.Count -gt 0) {
- $commands = $commandsRaw | Select-String -Pattern $excludeCommands -SimpleMatch -NotMatch
- } else {
- $commands = $commandsRaw
- }
- foreach ( $commandName in $commands) {
- # command to be tested
- #$commandName = 'New-D365Bacpac'
- #$commandName = 'Get-D365PackageLabelFile'
- # get all examples from the help
- $examples = Get-Help $commandName -Examples
- # make a describe block that will contain tests for this
- Describe "Examples from $commandName" {
- $examples.Examples.Example | foreach {
- # examples have different format,
- # at least the ones I used that MS provided
- # so you need to either standardize them,
- # or provide some hints about what to do
- # such as putting the code first
- # followed by
- # #output: the desired output
- # here I am simply taking the first line and removing 'PS C:\>'
- # which makes some of the tests fail
- $example = $_.Code -replace "`n.*" -replace "PS C:\\>"
- if ( ($example -like "*|*" ) -or (-not ($example -match $commandName)) ) {
- It "Example - $example" -Skip { $true }
- } else {
- # for every example we want a single It block
- It "Example - $example" {
- # mock the tested command so we don't actually do anything
- # because it can be unsafe and we don't have the environment setup
- # (so the only thing we are testing is that the code is semantically
- # correct and provides all the needed params)
- Mock $commandName {
- # I am returning true here,
- # but some of the examples drill down to the returned object
- # so in strict mode we would fail
- $true
- }
- # here simply invoke the example
- $result = Invoke-Expression $example
- # and check that we got result from the mock
- $result | Should -BeTrue
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment