forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Beginning to update comment-based help and wiki pages to match each other (and be accurate for v3.0 usage.)
- Loading branch information
Showing
4 changed files
with
125 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
BeforeEach and AfterEach | ||
------------------------------- | ||
|
||
The BeforeEach and AfterEach commands allow you to define setup and teardown tasks that are | ||
performed at the beginning and end of every It block. This can eliminate duplication of code | ||
in test scripts, ensure that each test is performed on a pristine state regardless of their | ||
order, and perform any necessary cleanup tasks after each test. | ||
|
||
BeforeEach and AfterEach blocks may be defined inside of any Describe or Context. If they | ||
are present in both a Context and its parent Describe, BeforeEach blocks in the Describe scope | ||
are executed first, followed by BeforeEach blocks in the Context scope. AfterEach blocks are | ||
the reverse of this, with the Context AfterEach blocks executing before Describe. | ||
|
||
The script blocks assigned to BeforeEach and AfterEach are dot-sourced in the Context or Describe | ||
which contains the current It statement, so you don't have to worry about the scope of variable | ||
assignments. Any variables that are assigned values within a BeforeEach block can be used inside | ||
the body of the It block. | ||
|
||
Note about syntax and placement | ||
------------------------------- | ||
|
||
Unlike most of the commands in a Pester script, BeforeEach and AfterEach blocks apply to the | ||
entire Describe or Context scope in which they are defined, regardless of the order of commands | ||
inside the Describe or Context. In other words, even if an It block appears before BeforeEach | ||
or AfterEach in the tests file, the BeforeEach and AfterEach will still be executed. | ||
|
||
Examples | ||
------------------------------- | ||
|
||
Describe 'Testing BeforeEach and AfterEach' { | ||
$afterEachVariable = 'AfterEach has not been executed yet' | ||
|
||
It 'Demonstrates that BeforeEach may be defined after the It command' { | ||
$beforeEachVariable | Should Be 'Set in a describe-scoped BeforeEach' | ||
$afterEachVariable | Should Be 'AfterEach has not been executed yet' | ||
} | ||
|
||
It 'Demonstrates that AfterEach has executed after the end of the first test' { | ||
$afterEachVariable | Should Be 'AfterEach has been executed' | ||
} | ||
|
||
BeforeEach { | ||
$beforeEachVariable = 'Set in a describe-scoped BeforeEach' | ||
} | ||
|
||
AfterEach { | ||
$afterEachVariable = 'AfterEach has been executed' | ||
} | ||
} |