Point Pester test file at different servers

Welcome Forums General PowerShell Q&A Point Pester test file at different servers

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
2 years, 2 months ago.

  • Author
    Posts
  • #51426

    Participant
    Points: 0
    Rank: Member

    Hi,

    so i have created a file full of pester tests, but at the start i had to hard code the server to connect to.

    So when i run the script it always points at that server. Is there a way to pass the server to the pester test file dynamically?

    ie can you do something like:

    Invoke-Pester "$testsFolder\*.ps1" -ServerName "myServer"

    or even

    Invoke-Pester "$testsFolder\*.ps1" -ServerName "myServer", "myServer2"

    • This topic was modified 2 years, 2 months ago by  staggerlee011.
  • #51449

    Member
    Points: 0
    Rank: Member

    Yep. You can put a param block in your tests.ps1 file, same as any other PowerShell script. When you run Invoke-Pester, you can tell it what parameters to pass to the test file like this (assuming you have a parameter named "ExampleParam" that's expecting a string):

    Invoke-Pester -Script @{ Path = '.\SomeFile.Tests.ps1'; Parameters = @{ ExampleParam = 'Value to pass' } }
    
  • #51459

    Participant
    Points: 0
    Rank: Member

    Hi Dave,

    Thanks for the reply!

    Any idea what im doing wrong with the below (guessing i've over simplified?), tried to make the simplest test to check im connecting to a passed through server.

    Calling script:

    Invoke-Pester -Script @{Path = "$testsFolder\Parameter.test.ps1"; Parameters = @{$ServerName = 'MyServerName'}}

    Paramter.test.ps1

    Param (
    [string]$ServerName
    )
    $Session = New-PSSession -ComputerName $ServerName

    Import-Module sqlserver

    Describe "test parameter in pester" {

    It "Test Server name" {
    Invoke-Command -Session $Session {$env:COMPUTERNAME} | Should be 'MyServerName'
    }
    }

    Im getting angry replies from powershell not liking where im taking this!

  • #51461

    Member
    Points: 0
    Rank: Member

    You've got one extra $ symbol in there. Should look like this:

    @{Path = "$testsFolder\Parameter.test.ps1"; Parameters = @{ServerName = 'MyServerName'}}

    (I removed the $ before ServerName)

  • #51464

    Participant
    Points: 0
    Rank: Member

    ha, awesome! thank you sir!

The topic ‘Point Pester test file at different servers’ is closed to new replies.