Skip to main content
Version: v5

AfterEach

This page was generated

Contributions are welcome in Pester v5-repo.

SYNOPSIS​

Defines a series of steps to perform at the end of every It block within the current Context or Describe block.

SYNTAX​

AfterEach [-Scriptblock] <scriptblock>

DESCRIPTION​

AfterEach runs once after every test in the current or any child blocks. Typically this is used to clean up resources created by the test or its setups. AfterEach runs in a finally block, and is guaranteed to run even if the test (or setup) fails.

BeforeEach and AfterEach are unique in that they apply to the entire Context or Describe block, regardless of the order of the statements in the Context or Describe.

EXAMPLES​

EXAMPLE 1​

Describe "Testing export formats" {
BeforeAll {
$filePath = "$([IO.Path]::GetTempPath())/$([Guid]::NewGuid())"
}
It "Test Export-CSV" {
Get-ChildItem | Export-CSV -Path $filePath -NoTypeInformation
$dir = Import-CSV -Path $filePath
# ...
}
It "Test Export-Clixml" {
Get-ChildItem | Export-Clixml -Path $filePath
$dir = Import-Clixml -Path $filePath
# ...
}

AfterEach {
if (Test-Path $file) {
Remove-Item $file -Force
}
}
}

The example uses AfterEach to remove a temporary file after each test.

PARAMETERS​

-Scriptblock​

A scriptblock with steps to be executed during teardown.

Type: System.Management.Automation.ScriptBlock
DefaultValue: ''
SupportsWildcards: false
Aliases: []
ParameterSets:
- Name: (All)
Position: 1
IsRequired: true
ValueFromPipeline: false
ValueFromPipelineByPropertyName: false
ValueFromRemainingArguments: false
DontShow: false
AcceptedValues: []
HelpMessage: ''

CommonParameters​

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutBuffer, -OutVariable, -PipelineVariable, -ProgressAction, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS​

OUTPUTS​

NOTES​

VERSION​

This page was generated using comment-based help in Pester 5.9.1.