pester / Pester

Pester is the ubiquitous test and mock framework for PowerShell.
https://pester.dev/
Other
3.08k stars 470 forks source link

Ordering tests #534

Closed pvandervelde closed 8 years ago

pvandervelde commented 8 years ago

Is it currently possible to order tests in Pester? I'm using Pester for my integration / regression tests and would like to write tests like this

Describe 'My process' {
    Context 'executes without errors' {

        $processExitCode = -1
        It 'without throwing exceptions' {
            try
            {
                & myprocess.exe
                $processExitCode = $LASTEXITCODE
            }
            catch
            {
                # Process has failed :(
                $true | Should Be $false
            }
        }

        It 'with a zero exit code' {
            $processExitCode | Should be 0
        }
    }

    Context 'produces output' {
        It 'that is correct' {
            'c:\my\file.txt' | Should Exist
        }
    }
}

That does require that the first context block always gets executed first.

Is there a way to tell Pester to order my tests?

dlwyatt commented 8 years ago

They're always executed in the order they're defined, top to bottom. However, it's generally considered bad practice to write tests that depend on another test to work. In this case, I would just make a single It block with three assertions:

Describe 'My process' {
    It 'Executes properly' {
        & myprocess.exe
        $LASTEXITCODE | Should Be 0
        'c:\my\file.txt' | Should Exist
    }
}

There's no need for a try/catch, since any terminating error will fail the test anyway.

pvandervelde commented 8 years ago

I know it's not good practise but these are integration tests which may take a while so I don't really feel like using a test setup/teardown for each test because then it would really take ages. I can work with the fact that Pester executes from top to bottom :)