about beforeeach aftereach.help

Download About BeforeEach AfterEach.help

If you can't read please download the document

Upload: satish-kumar-maurya

Post on 06-Dec-2015

213 views

Category:

Documents


1 download

DESCRIPTION

About BeforeEach AfterEach.help

TRANSCRIPT

BeforeEach, AfterEach, BeforeAll, and AfterAll-------------------------------The BeforeEach and AfterEach commands allow you to define setup and teardown tasks that areperformed at the beginning and end of every It block. This can eliminate duplication of codein test scripts, ensure that each test is performed on a pristine state regardless of theirorder, and perform any necessary cleanup tasks after each test.BeforeEach and AfterEach blocks may be defined inside of any Describe or Context. If theyare present in both a Context and its parent Describe, BeforeEach blocks in the Describe scopeare executed first, followed by BeforeEach blocks in the Context scope. AfterEach blocks arethe 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 Describewhich contains the current It statement, so you don't have to worry about the scope of variableassignments. Any variables that are assigned values within a BeforeEach block can be used insidethe body of the It block.BeforeAll and AfterAll are used the same way as BeforeEach and AfterEach, except that they areexecuted at the beginning and end of their containing Describe or Context block. This isessentially syntactic sugar for the following arrangement of code:Describe 'Something' { try { } finally { }}Note about syntax and placement-------------------------------Unlike most of the commands in a Pester script, BeforeEach, AfterEach, BeforeAll and AfterAll blocksapply to the entire Describe or Context scope in which they are defined, regardless of the order ofcommands inside the Describe or Context. In other words, even if an It block appears before BeforeEachor AfterEach in the tests file, the BeforeEach and AfterEach will still be executed. Likewise, BeforeAllcode 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' }}