Adding Results to Test Output?

43 views
Skip to first unread message

Justin Grote

unread,
Jan 21, 2016, 3:02:56 AM1/21/16
to Pester
Hello,

I've been using Pester and have been look at using it for operational validation of managed devices, e.g. run down a sheet of if a system is accessible via WMI, RPC, Registry, WinRM, SNMP, etc.

I have a proof-of-concept Gist here:

My one question is, is there a native ability in pester to include output with a test?

For instance, say I have this test output:

Describing Windows Remote Management
 
[+] Responds to RPC TCP Port within 3 seconds 46ms
 
[+] Returns a WMI Computer Make and Model within 3 seconds 163ms


Is there any way to add what the Computer Make and Model actually were to the test result information? I care less about having it in the "write-host" output as I do having it in the XML or as an output object that comes out via -PassThru for instance.

The closest I've gotten so far is using write-verbose in the test and running with $VerbosePreference as "Continue", so it looks like this:

Describing Windows Remote Management
 [+] Responds to RPC TCP Port within 3 seconds 46ms
 [+] Returns a WMI Computer Make and Model within 3 seconds 163ms
VERBOSE: Host Manufacturer: Microsoft Corporation
VERBOSE: Host Model: Surface Pro 3

But what I'd really like is something like this:

 [+] Responds to RPC TCP Port within 3 seconds 46ms
 [+] Returns a WMI Computer Make and Model within 3 seconds 163ms
          OUTPUT: Host Manufacturer: Microsoft Corporation
          OUTPUT: Host Model: Surface Pro 3

That would also include that information into the XML output file.

Is there something native in Pester for this? I just can't find it in the documentation so I thought I'd ask before I go trying to code it myself.

Dave Wyatt

unread,
Jan 21, 2016, 8:31:08 AM1/21/16
to Pester
There's nothing like that at a the moment (and I'd need to find a valid spot for it in the XML file anyway; we can't just make up new fields in NUnit.  :) )

In the short term, though, you could just make another test case which contains that information in its name.  It's a bit of a hack, but should work.  Something along these lines:

    Describe 'Windows Remote Management' {
        $hashtable = @{}

        It 'Returns a WMI Computer Make and Model within 3 seconds' {
            $make = Get-ManufacturerCodeHere
            $model = Get-ModelCodeHere

            # Assertion for this test case
       
            # These lines only get run if this test was successful; above assertions didn't fail
            $hashtable.Make = $make
            $hashtable.Model = $model
        }

        if ($hashtable.Count -gt 0)
        {
            It "Host Manufacturer: $($hashtable.Make)" {
                $null = $null # Needs to be some non-comment line here or Pester marks the test as Pending
            }

            It "Host Model: $($hashtable.Model)" {
                $null = $null # Needs to be some non-comment line here or Pester marks the test as Pending
            }
        }
    }

Justin Grote

unread,
Jan 21, 2016, 10:58:39 AM1/21/16
to Dave Wyatt, Pester
Thanks Dave! That's a clever idea and should work for my very limited needs at the moment. Glad to hear its on the radar and I appreciate the fast response.

--
You received this message because you are subscribed to a topic in the Google Groups "Pester" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pester/PN6KsKaTfCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pester+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Justin Grote

unread,
Jan 21, 2016, 11:39:45 AM1/21/16
to Pester
Here's a working example. Note that I had to explicity use the Script scope when assigning the variable inside It, because it appears the Mock cleanup was wiping out the variable even if I defined it outside the script beforehand. Maybe there's a better way to handle this?

$ComputerName = "localhost"
$Timeout = 3

$MakeModelResult = $null
describe "WMI Computer Test" {
    New-Variable MakeModelResult -Scope Script -force
    It "Returns a WMI Computer Manufacturer and Model within $Timeout seconds" {
        #If you don't use Script: here, $MakeModelResult will be null at "Show Make and Model Information"
        #Using Job to implement a time limit for Get-WMIObject which doesn't have a native timeout functionality
        $Script:MakeModelResult = Get-WMIObject -Computername $ComputerName Win32_ComputerSystem -asjob | wait-job -timeout $Timeout | Receive-Job
        $MakeModelResult.Manufacturer | Should Not BeNullOrEmpty
        $MakeModelResult.Model | Should Not BeNullOrEmpty
    }

    #Show Make and Model Information
    if ($MakeModelResult) {
        It "  OUTPUT: WMI Computer Manufacturer: $($MakeModelResult.Manufacturer)" {
            #Workaround to keep Pester from marking the test as "Pending"
            $null = $null
        }
        It "  OUTPUT: WMI Computer Model: $($MakeModelResult.Model)" {
            #Workaround to keep Pester from marking the test as "Pending"
            $null = $null
        }
    }
}


Justin Grote

unread,
Jan 21, 2016, 11:41:31 AM1/21/16
to Pester
And the resulting working output which does what I wanted:

Describing WMI Computer Test
 [+] Returns a WMI Computer Manufacturer and Model within 3 seconds 60ms
 [+]   OUTPUT: WMI Computer Manufacturer: Microsoft Corporation 15ms
 [+]   OUTPUT: WMI Computer Model: Surface Pro 3 8ms

Justin Grote

unread,
Jan 21, 2016, 11:59:03 AM1/21/16
to Pester
Err sorry my example has some redundant variable definition. Here is a cleaned up version that also adds a toggle to skip showing the output if not explicitly specified, and skip showing output if the dependent test fails.

$ComputerName = "localhost"
$Timeout = 3
$ShowTestOutputs = $true


describe "WMI Computer Test" {
    It "Returns a WMI Computer Manufacturer and Model within $Timeout seconds" {
        $Script:MakeModelResult = Get-WMIObject -Computername $ComputerName Win32_ComputerSystem -asjob | 
            Wait-Job -Timeout $Timeout | 
            Receive-Job
        $MakeModelResult.Manufacturer | Should Not BeNullOrEmpty
        $MakeModelResult.Model | Should Not BeNullOrEmpty
    }

    #Show Make and Model Output if enabled and present
    if ($ShowTestOutputs) {
        It -skip:(!$MakeModelResult) "  OUTPUT: WMI Computer Manufacturer: $($MakeModelResult.Manufacturer)" {
            #Workaround to keep Pester from marking the test as "Pending"
            $null = $null
        }
        It -skip:(!$MakeModelResult) "  OUTPUT: WMI Computer Model: $($MakeModelResult.Model)" {
Reply all
Reply to author
Forward
0 new messages