forked from pester/Pester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathabout_BeforeEach_AfterEach.help.txt
88 lines (71 loc) · 3.43 KB
/
about_BeforeEach_AfterEach.help.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
TOPIC
about_BeforeEach_AfterEach
SHORT DESCRIPTION
Describes the BeforeEach and AfterEach commands, which run a set of commands that you specify
before or after every It block.
LONG DESCRIPTION
The the BeforeEach and AfterEach commands in the Pester module let you 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 clean-up 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.
BeforeAll and AfterAll are used the same way as BeforeEach and AfterEach, except that they are
executed at the beginning and end of their containing Describe or Context block. This is
essentially syntactic sugar for the following arrangement of code:
Describe 'Something' {
try
{
<BeforeAll Code Here>
<Describe Body>
}
finally
{
<AfterAll Code Here>
}
}
SYNTAX AND PLACEMENT
Unlike most of the commands in a Pester script, BeforeEach, AfterEach, BeforeAll and AfterAll 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. Likewise, BeforeAll
code will be executed at the beginning of a Context or Describe block regardless of where it is found,
and AfterAll code will execute at the end of the Context or Describe.
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'
$beforeAllVariable | Should Be 'BeforeAll has been executed'
}
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'
}
BeforeAll {
$beforeAllVariable = 'BeforeAll has been executed'
}
}
SEE ALSO
about_Pester
about_Should
about_Mocking
about_TestDrive
about_about_Try_Catch_Finally
Describe
Context
Should
It
Invoke-Pester