pester/docs

Cannot execute code in Describe before the ITs

steevessaillant opened this issue · 3 comments

Why I cannot doe this?

Describe "My Suite" {
#call a function
MyFunction - param $x
It("test case 1){
# MyFunction not called before ....
}
It("test case 1){
#must do this
MyFunction - param $x
}
}

Thank you!

In Pester v5 code outside It (and a few other blocks) is executed during a initial Discovery-phase. So it is executed once, but in a different scope that's lost before your It-blocked is executed.

You could place it in a BeforeAll (executed once) or BeforeEach (executed before each test) block. Ex.

Describe "My Suite" {
  BeforeAll {
    MyFunction -param $x
  }
  It "test case 1" {
    # MyFunction called before ....
  }
}

See https://pester.dev/docs/usage/discovery-and-run for more details.

@steevessaillant can we close this ticket?

@steevessaillant can we close this ticket?

I'd say yes 🙂