As part of using Jenkins for my CI im also looking at adding pester testing to certain builds.

First lets run a Pester test in a Build. You will need:

  • The Pester Module installed (In PowerShell 5 you can run Install-Module Pester -force)
  • The PowerShell Plugin for Jenkins Guide here

And have some tests. You can use my below script if you like which is a snippet of SQL Server Build tests, and have hard coded the $ServerName variable (change this to any SQL instance in your environment if your are playing along:

$ServerName = '.'

if(!($ServerName.contains("\")))
{
    $Server = $ServerName
}
else
{
    ## named instance needs to be split out
    $ServerInstance = $ServerName.Split("{\}")
    $ServerName = $ServerInstance.Item(0)
    $Instance = $ServerInstance.Item(1)
}

try
{
    $Session = New-PSSession -ComputerName $ServerName
}
catch
{
    write-host "Failed to create remote session to server: $ServerName"
    exit
}

Describe 'SQL Server Installation standards' {

    Context 'SQL Server Services' {
        It "The SQL Server engine service should be running" {
            IF($Server -eq $ServerName)
                {
                    (Invoke-Command -Session $Session {Get-Service -Name MSSQLSERVER}).status |
                    Should be 'Running'
                }
            ELSE
                {
                    $service = "MSSQL$"+ $Instance
                    (Invoke-Command -Session $Session {Get-Service -Name $args[0]} -ArgumentList $service).status |
                    Should be 'Running'
                }

        }
        It "The SQL Server agent service should be running" {
            IF($Server -eq $ServerName)
                {
                    (Invoke-Command -Session $Session {Get-Service -Name SQLSERVERAGENT}).status |
                    Should be 'Running'
                }
            ELSE
                {
                    $service = "SQLAgent$"+ $Instance
                    (Invoke-Command -Session $Session {Get-Service -Name $args[0]} -ArgumentList $service).status |
                    Should be 'Running'
                }

        }
    } # Context end SQL Server Installation Checks
}

Save the Pester file as: SQLServerBuild.test.ps1

Now open Jenkins, create a new Build using a Freestyle Project (I’ve called mine SQL Server Build Test)

Add a Windows PowerShell Build Step and add:

Import-Module "C:\Program Files\WindowsPowerShell\Modules\Pester"
Invoke-Pester -Script  U:\JenkinsScripts\Pester\DatabaseStd\SQLServerBuild.test.ps1

jenkinspester00

Note i had to use the full file path of the pester module in the import-module or it would fail (I believe this is because of a 32bit issue)

If we run the build now we get an output of:

jenkinspester01

This is ok, but its not greatest summary page and with more test can become rather difficult to read.

If we add another Test which fails

 Context 'SQL Server Browser Services' {
 It "The SQL Server Browser service should be running" {
 IF($Server -eq $ServerName)
 {
 (Invoke-Command -Session $Session {Get-Service -Name SQLBrowser}).status |
 Should be 'Running'
 }
 ELSE
 {
 $service = "SQLBrowser$"+ $Instance
 (Invoke-Command -Session $Session {Get-Service -Name $args[0]} -ArgumentList $service).status |
 Should be 'Running'
 }
 }
 }# context end SQL Server Browser Services

This test checks to see if the SQL Brower is running (Which it is not in my builds).  I get an output of:

jenkinspester02

Now thats upsetting! It says my test failed. but the build has passed.. This is because to Jenkins the build is simply running a powershell Script. it completed that. so all is good! this is major problem..

Solution

When you invoke-pester there are multiple parameters you can add to change the output. One of them is -OutputFormat which excepts “NunitXML” this is a well known unit testing xml format! and as you might expect.. Jenkins has a plugin for it.

First lets install the plugin via: Jenkins -> Manage Jenkins -> Manage Plugins -> Available and search for nunit tick and install the plugin (restarting if needing)

jenkinspester03

Next we can update our Build step to change the output of the Invoke-Pester command.

Import-Module "C:\Program Files\WindowsPowerShell\Modules\Pester"

Invoke-Pester -Script  U:\JenkinsScripts\Pester\DatabaseStd\SQLServerBuild.test.ps1 -OutputFile report.xml -OutputFormat NUnitXml

As you can see i did not give a file path to the output file. this is because the default location is the build steps workspace.. Leaving the file here lets the plugin pick it up and use it. It also stores all of your results in the individual build steps which is what we would want.

Next add a Post-build Action of “Publish NUnit test result report”

jenkinspester04

In here all we need to do is add the name of the file we choose for the XML output. (for me report.xml)

jenkinspester05

Save and run a build again!

Now we have an unstable build! perfect… and we also have a Test Result link added to our Build page:

jenkinspester06

As you can see it also shows the test that the build failed on. If we click on the Test Results link on the right hand side we have:

jenkinspester07

which gives us an either cleaner set of results (and with time shows the result trends along the top). We can also drill down from the Root link to see all test that have been run:

jenkinspester08

And there we go, Pester testing in Jenkins.

Advertisements