Pester offers multiple ways to run code before, and after your tests to set them up, and clean up after them. The setup is represented by a
The teardown is used to clean up after test or a block and is guaranteed to run even if the test fails. Teardown is represented by
BeforeAll is used to share setup among all the tests in a
Describe / Context including all child blocks and tests.
BeforeAll runs during
Run phase and runs only once in the current block.
The typical usage is to setup the whole test script, most commonly to import the tested function, by dot-sourcing the script file that contains it:
Another typical usage is to do an expensive operation once, and then validate the result in multiple tests:
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. For example this is how we ensure that each test gets fresh file:
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. For example this is how we ensure that each test removes its test file:
AfterEach placement within the current block does not affect when it is run, you can place it on the top or on the bottom of the block, and it will still run last.
The teardown in
AfterEach should be prepared to run at any place of
It. For example when the
$file does not exist yet, because we got
AccessDenied when trying to write it.
AfterAll runs once after every
Context block. It is used to clean up common resources. It works the same as
AfterEach, except that it just runs once.
BeforeAll are defined, they run in the order in which they were defined.
AfterAll run in the opposite order.
BeforeEach are defined, they run in the order in which they were defined, and they run right before the test they setup.
AfterEach run right after the test is finished, in the opposite order.
There can be only one setup or teardown of each kind in a block.
Setups and teardowns are skipped when the current tree won't result in any test being run. For example when running tests with a tag filter "Acceptance", all unit test files will have all their tests filtered out. The setups and teardowns in those files won't run at all.
In the example below, Pester will see that there are not tests the pass filter
-ExcludeTag Acceptance, and so it will not run any of the or teardowns:
All variables defined in
BeforeAll are available to all child blocks and tests. But because all child blocks and tests run in their own scopes, the variables are not writeable to them. This is needed to isolate tests from each other.
AfterEach run in the same scope. All variables in them are shared. This way you can move code between
It without changes. And you can also base your
AfterEach on variables that were defined in
It, for example to delete a file you created in it.