Where communities thrive

  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
Repo info
    Felix Becker
    got it
    Jakub Jareš
    Is it possible to mock the call operator &?
    I’m not sure. I never have. But I have mocked commands called by the call operator.
    Hello! I'm sort of confused about a problem i'm running into.
    Basically, I'm getting odd behavior when I'm trying to assert that an array is either Empty/Null or NOT empty/null
    as an example, see that code.
    test-PesterPipelineIssue /Users/me/testingDir/testPester.txt | Should beNullOrEmpty
    if you change that line to "should not beNullOrEmpty"` you'll see the behavior change, which i really don't understand.
    (also, sorry if I'm asking in the wrong place. Please let me know if I should move my question to a different location!)
    aaaand nevermind. I nuked my install of psc and pester and resintalled and now I'm getting different behavior
    Hi Folks,
    Pester gives code coverage for lines, do we have a way to get the same for functions ?
    Jakub Jareš
    @kvprasoon No, I don't think there is an option for that.
    $Thanks @nohwnd
    hello, anybody used the BeforeEachScenario -Tags statement as described in https://kevinmarquette.github.io/2017-04-30-Powershell-Gherkin-advanced-features/#beforeeachscenario. I get the following error: "The expression after '&' in a pipeline element produced an object that was not valid."
    Hello, anybody mocked get-environment variable function either user or machine
    I have a function to get environment variable, where I have used Get-EnvironmentVariable function to get the value. While writing pester test, I want to mock [System.Environment]::Get-EnvironmentVariable('XXXX', 'User') inorder to test empty scenario and with value scenrio. Can anyone help on this
    Johan Ljunggren
    @routhrirajan Don't think it is possible to mock that, easiest it to make a wrapper (powershell function) for the method [System.Environment]::GetEnvironmentVariable() and mock that. Another way might be to make a stub class and load with Add-Type, but not sure it works in this case since the type is already loaded.
    Thanks @johlju I will try with the suggested options. Let you know if it helps
    Chris Forbes
    Hi everyone. How do I test a module that has RequiredModules in the manifest. if the dependent modules are not installed, the tests will fail.
    Chris Forbes
    Or this may be due to fully qualified function names, I'm not sure.
    Sotiris Nanopoulos
    Hey @nohwnd I want to take a stab at this pester/Pester#301 . Is it ok? Is there anyone working on something conflicting with this?
    Rushikesh Vyas

    Hey guys I am trying to use Pester (4.8.1) for our azure devops pipeline CI build. I have a "publish test" task and
    this is what I am doing right now
    Invoke-Pester -Script $testFilePath -OutputFile $outputFilePath -OutputFormat 'NUnitXML' -CodeCoverage $psFiles -CodeCoverageOutputFile $codeCoverageOutputFIlePath -EnableExit

    My questions is: Is there a way I can put the result in a variable using passthru and and still save all the outputs in the specified files? I want the task to fail if any tests fail but I also have following tasks that shows test result and I want to see which tests failed. With -EnableExit fail it fails the testing task but it skips publishing test result task too

    Heber Alejandro
    Hello everyone! how can I send parameters for each test case in a suite? currently I had just one test case to call and I do it like this Invoke-Pester -Script @{Path = 'C:\Turing\Test\Frida-launcher.ps1'; Parameters = @{frida_process_id = '807028544'; frida_user = 'heber.solis@softtek.com'; frida_psw = 'password'}} -OutputFile "frida-results.xml" -OutputFormat "NUnitXML" but I don't know how to do it for every test case in the suite :(. Thanks for any help :)
    is there a way to escape special characters in gherkin step names?
    i found that trying to use parenthesis or square brackets causes it to stop matching the names of the steps in the features to the names of the actual steps
    I tried escaping these with backslash and and the backtick but still can't get it to work
    Carl in 't Veld
    is there a way to define functions in the scope of a unit test that can be called from a MockWith code block?
    @kitforbes I have a related requirement. I have a custom module that is requiring AzureAD and AD to be imported. But obviously these are very large and I want to leave them out. I just mock all the Azure calls. Currently the only way I manage this is to exclude the required modules from the custom module's manifest psd1 file
    Andy AO
    hello everyone
    I don't feel very active here. There are only 69 members.
    Michael Ablassmeier
    How is it possible to use Variables for Describe and It blocks in V5? It is not necessarily the -TestCases parameter that helps me out. Im testing various command line tools, each test tool goes into its own file (command.Tests.ps1) so it would be neat to use the "command" as variable from the filename in the it and Describe copes (Descript "test ${command}" ..)
    Using BeforeAll with $global:command="foo" is problematic because if pester is called with -Path on a directory with multiple tests. the global variable overrides during execution all other files too,.. i had weird behavior.
    Daniel Groh

    I have a function which uses some Azure Cli commands, for example:

        $appRegistration = az ad app create `
            --display-name $name `
            --reply-urls $replyUrls `
            --oauth2-allow-implicit-flow true `
            --available-to-other-tenants false `
            -o json | 

    How can I mock az ad app create and assert the parameters with Assert-MockCalled? I don't find anything about that

    Marc van Gorp
    Mocking an executable is not possible. At least not to my knowledge.
    In this scenario I create a 'wrapper' function that calls the executable. This 'wrapper' function can be mocked.
    function Invoke-AzureCli {
        switch ($isWindows) {
            $True {
                $AssertPath = Test-Path -Path 'C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin\az.cmd'
            $False {
                switch ($IsLinux) {
                    $true {
                        $AssertPath = Test-Path -Path '/usr/bin/az'
                    Default { throw "Unsuported operating system detected '$PSVersionTable.OS'" }
        if ($AssertPath -eq $False) {
            throw "Azure CLI not found!`nThis function requires Azure CLI to be available on this system.`nhttps://docs.microsoft.com/en-us/cli/azure/install-azure-cli"
        az $args
        if ($LASTEXITCODE -ne 0) {
            throw 'Execution failed'
    Matthew Parsons

    (This is only like my second time using Gitter, and first time trying to Pester 5 in ernest)

    The pester.dev documenation isn't clear to me what exactly $PesterPreference is compared to a PesterConfiguration, and why I would want to use one method over the other, or why either are even necessary when I just want to run a test and see the actual output?

    1 reply
    Just doublechecking before raising an issue: in Pester 5.3 there seems to be no way to get codecoverage parameters like function/startline/endline passed into Pester anymore? The legacy parameter set doesn't accept a hashtable for -CodeCoverage (well it does but it turns it into the string "System.Collections.HashTable" and PesterConfiguration.CodeCoverage only has Paths and enforces that to be a string array. Is this something which is being worked on?
    Miodrag Milić
    Hi there, since recently I cant execute tests from vscode, I get this when I click any run/debug
    Cannot read property '0' of undefined
    Does anybody know what seems to be the problem ?
    I reinstalled powershell extension just to make sure, but no bueno. Tests work fine when executed outside of vscode
    Using latest Pester, vscode, Powershell extensin, pwsh
    Powershell extension logs show nothing interesting
    Miodrag Milić
    Actually found 1 error in Powershell Editor Services
    [Error - 16:32:42] Microsoft.PowerShell.EditorServices.Services.PowerShellContextService: Execution of the following command(s) completed with errors:
        Microsoft.PowerShell.Core\Get-Command Send-Ping -ErrorAction Ignore
    Anyway, when I remove all the code and leave just Describe 'Pinging' {} it still fails, but without any error
    Miodrag Milić
    Here debug log for Powershell editor services , executing empty test from above
    Chris Simon

    Given my powershell code (not in a function, as the last few parts are the script running, but i want to check some stuff for my script, using pester) My powershell code WinVersion.ps1

    $WindowsVersion = Get-CimInstance -ClassName Win32_Operatingsystem | select -expand Caption

    My Pester script:

    BeforeAll {
    . $PSCommandPath.Replace('.Tests.ps1', '.ps1')

    Describe "Test Server 2012" {
    It "Given Server 2012, return correct data" {
    Mock -CommandName Get-CimInstance -ParameterFilter {$ClassName -eq "Win32_Operatingsystem"} -MockWith {
    Write-Host "CIM"
    return [Microsoft.Management.Infrastructure.CimInstanc]@{
    Caption = "Microsoft Windows Server 2012 Datacenter"
    write-host $WindowsVersion

    My Write-host (in my pester script) should return Microsoft Windows Server 2012 Datacenter but it returns my own windows version. It thus ignores my mock.

    I also tried the mock without the ParameterFilter, but that also didn't work.

    This is the result that Pester gives me:

    Starting discovery in 1 files.
    Discovery found 2 tests in 82ms.
    Running tests.
    [-] Test Server 2012.Given Server 2012, return correct data 46ms (45ms|1ms)
    Expected strings to be the same, but they were different.
    Expected length: 40
    Actual length: 31
    Strings differ at index 18.
    Expected: 'Microsoft Windows Server 2012 Datacenter'
    But was: 'Microsoft Windows 11 Enterprise'
    at $WindowsVersion | Should -Be "Microsoft Windows Server 2012 Datacenter"

    Any clue why?

    Jin Kang

    Hello, can anyone tell me what I am doing wrong here?

    Here’s a script I want to test using Pester (e.g. run3.ps1):

    using namespace System.Net
    $ok = $true
    $errorMsg = ""
    $status = "In Progress"
    Write-Host "Function: myFunction. Applying tags.."
    Write-Host $Subscription
    $CreationState = $Subscription.CreationState | ConvertFrom-Json 
    if ($CreationState.Tags -eq "Complete") {
        Write-Host "Tags are already applied."
        return @{
            OK               = $ok
            Status           = $status
            SubscriptionInfo = $Subscription    

    And here’s my Pester unit test (e.g. run3.Tests.ps1):

    BeforeAll {
        . $PSScriptRoot/run3.ps1
    Describe 'run-orig.ps1' {
        It 'Tags are applied' {
            $tags = [pscustomobject]@{
                Tags = "Complete"
            $tagsJson = $tags | ConvertTo-Json
            $value = [pscustomobject]@{
                Status = 'Active'
            $value | Add-Member -MemberType NoteProperty  -Name CreationState -Value $tagsJson
            $res = . $PSScriptRoot/run3.ps1 -Subscription $value
            $res | Should -BeOfType [System.Collections.Hashtable]

    When I execute my pester tests, this is the error message I receive. Error is on ConvertFrom-Json on line #10 and error message is Cannot bind argument to parameter 'InputObject' because it is null.

    I would greatly appreciate any help on this. Thank you!

    Pester v5.3.3
    Starting discovery in 1 files.
    Discovery found 1 tests in 5ms.
    Running tests.
    Running tests from '/Users/dfdf/Downloads/azfunc/HelloWorldProj/HttpExample/run3.Tests.ps1'
    Function: myFunction. Applying tags..
    ConvertFrom-Json: /Users/dfdf/Downloads/azfunc/HelloWorldProj/HttpExample/run3.ps1:10:48
    Line |
      10 |  $CreationState = $Subscription.CreationState | ConvertFrom-Json
         |                                                 ~~~~~~~~~~~~~~~~
         | Cannot bind argument to parameter 'InputObject' because it is null.
    Describing run-orig.ps1
    Function: myFunction. Applying tags..
    @{Status=Active; CreationState={
      "Tags": "Complete"
      [-] Tags are applied 6ms (6ms|1ms)
       Expected the value to have type [hashtable] or any of its subtypes, but got @{Tags=Complete} with type [PSCustomObject].
       at $res | Should -BeOfType [System.Collections.Hashtable], /Users/jink/Downloads/azfunc/HelloWorldProj/HttpExample/run3.Tests.ps1:19
       at <ScriptBlock>, /Users/dfdf/Downloads/azfunc/HelloWorldProj/HttpExample/run3.Tests.ps1:19
    Tests completed in 36ms
    Tests Passed: 0, Failed: 1, Skipped: 0 NotRun: 0