Code Monkey home page Code Monkey logo

Comments (3)

fflaten avatar fflaten commented on May 25, 2024 1

IIRC there's no difference between BeforeAll and BeforeDisovery regarding this behavior. So I'm not sure why one would work differently. Hard to troubleshoot without seeing the specific pipeline.

As mentioned the exit code will be positive (non-zero) when either block types fail. Ex.

# demoCI.ps1
param([switch]$FailFirst = $false)

$sbFailed = {
    BeforeDiscovery {
        throw 'oh nooo'
    }
    Describe 'Fail in BeforeDiscovery' {
        It 'never runs' { 1 | Should -Be 1 }
    }
}

$sbWorks = {
    Describe 'Working file' {
        It 'works' { 1 | Should -Be 1 }
    }
}

$conf = New-PesterConfiguration
$conf.Run.ScriptBlock = @(if ($FailFirst) { $sbFailed }) + @($sbWorks)
# This exits the script/powershell with the exit code.
# Without it you'd usually have to check $LASTEXITCODE after Invoke-Pester and throw/exit if non-zero.
$conf.Run.Exit = $true 
$conf.Output.Verbosity = 'None' # To shorten output

Invoke-Pester -Configuration $conf

Demo:

# Run with only successful container ($sbWorks)
> $proc = Start-Process pwsh -ArgumentList "-NoProfile -File ./demoCI.ps1" -PassThru -Wait
> $proc.ExitCode
0

# Run with failing container first ($sbFailed, $sbWorks)
> $proc = Start-Process pwsh -ArgumentList "-NoProfile -File ./demoCI.ps1 -FailFirst" -PassThru -Wait
> $proc.ExitCode
1 # This would usually fail a pipeline step unless configured to continue on error. YMMW depending on CI

from pester.

fflaten avatar fflaten commented on May 25, 2024

Thanks for the feature request.

This can lead to a false indication that the stage has passed successfully, even though it has encountered a failure.

$LASTEXITCODE will always be non-zero when a container has failed, but you might have to check it in your step? Try the Run.Exit option (or -CI switch if you don't use advanced configuration) which will exit the powershell-prosess with the exit code.

It won't exit though, but it will skip the file where BeforeDiscovery failed.

Implement a mechanism within Pester that checks the exit code of the BeforeDiscovery script and ensures that if it fails, the entire Pester test suite is aborted and the pipeline stage is marked as failed.

Not sure if you meant it this way, but checking exit codes from console applications etc. are unlikely to be supported. Atm. we only consider PowerShell exceptions and terminating errors to be a failure, which will stop the current file only.

from pester.

Nadiahansen15 avatar Nadiahansen15 commented on May 25, 2024

When I execute the tests and encounter a failure in the "beforeDiscovery" phase, the system reports a failed container with the following message:

Container failed: 1
/azp/_work/1/s/gov/tests/there.Tests.ps1

Interestingly, even though the tests within that container are not executed, the pipeline does not mark it as a failure. As a result, we won't be alerted to any potential issues with the tests unless we manually inspect them after each run.

However, I've observed that if a failure occurs in the "beforeAll" phase, it is correctly flagged as a test failure.

Is it possible to make "beforeDiscovery" cause the test to fail or take an action that flags it in the CI pipeline stage, so that the stage doesn't return as "all good"?

from pester.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.