Skip to main content
Version: v5

BeforeEach

SYNOPSIS#

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

SYNTAX#

BeforeEach [-Scriptblock] <ScriptBlock> [<CommonParameters>]

DESCRIPTION#

BeforeEach runs once before every test in the current or any child blocks. Typically this is used to create all the prerequisites for the current test, such as writing content to a file.

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 "File parsing" {    BeforeEach {        # randomized path, to get fresh file for each test
        $file = "$([IO.Path]::GetTempPath())/$([Guid]::NewGuid())_form.xml"        Copy-Item -Source $template -Destination $file -Force | Out-Null    }
    It "Writes username" {        Write-XmlForm -Path $file -Field "username" -Value "nohwnd"        $content = Get-Content $file        # ...
    }
    It "Writes name" {        Write-XmlForm -Path $file -Field "name" -Value "Jakub"        $content = Get-Content $file        # ...
    }}

The example uses BeforeEach to ensure a clean sample-file is used for each test.

PARAMETERS#

-Scriptblock#

A scriptblock with steps to be executed during setup.

Type: ScriptBlockParameter Sets: (All)Aliases:
Required: TruePosition: 2Default value: NoneAccept pipeline input: FalseAccept wildcard characters: False

CommonParameters#

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

INPUTS#

OUTPUTS#

NOTES#

RELATED LINKS#

https://pester.dev/docs/commands/BeforeEach

https://pester.dev/docs/usage/setup-and-teardown

EDIT THIS PAGE#

This page was auto-generated using the comment based help in Pester 5.2.1. To edit the content of this page, change the corresponding help in the pester/Pester repository. See our contribution guide for more information.