Setup and teardown
#
Setup and teardownPester 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 BeforeAll
, and BeforeEach
blocks.
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 AfterAll
and AfterEach
blocks.
#
BeforeAllBeforeAll
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:
#
BeforeEachBeforeEach
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:
#
AfterEachAfterEach
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 BeforeEach
or It
. For example when the $file
does not exist yet, because we got AccessDenied
when trying to write it.
#
AfterAllAfterAll
runs once after every Describe
or Context
block. It is used to clean up common resources. It works the same as AfterEach
, except that it just runs once.
#
Multiple setups and teardownsWhen multiple BeforeAll
are defined, they run in the order in which they were defined. AfterAll
run in the opposite order.
When multiple 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.
#
Skipping setups and teardownsSetups 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.
#
ScopingAll 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.
BeforeEach
, It
and AfterEach
run in the same scope. All variables in them are shared. This way you can move code between BeforeEach
and 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.