Pester icon indicating copy to clipboard operation
Pester copied to clipboard

v5: BeforeEach/AfterEach -Context?

Open nvarscar opened this issue 5 years ago • 7 comments

1. General summary of the issue

I tend to write a chain of different It tests that expect a certain resource to be available during the whole execution of the Context in persistent state and then get removed once the tests in this particular context are over. Right now, I'm using BeforeAll and AfterAll for each Context to create/remove such resources. It would be much easier to have a BeforeEach -Context so that I could define it on a Describe level just once.

2. Describe Your Environment

Powershell 5.1/Core, Windows and Linux

3. Desired Behavior

Describe "my tests" {
  BeforeEach -Context {
    # create new file here
  }
  AfterEach -Context {
    # remove file here
  }
  Context "context 1" {
    It "it 1" {
      # doing some activities on the file
      $true | Should -Be $true
    }
    It "it 2" {
      # continuing working on the same file and expecting it to have results from "it 1"
      $true | Should -Be $true
    }
  }
  Context "context 2" {
    It "it 1" {
      # doing some activities on the file
      $true | Should -Be $true
    }
    It "it 2" {
      # continuing working on the same file and expecting it to have results from "it 1"
      $true | Should -Be $true
    }
  }
}

4.Current Behavior

Describe "my tests" {
  Context "context 1" {
    BeforeAll {
      # create new file here
    }
    AfterAll {
      # remove file here
    }
    It "it 1" {
      # doing some activities on the file
      $true | Should -Be $true
    }
    It "it 2" {
      # continuing working on the same file and expecting it to have results from "it 1"
      $true | Should -Be $true
    }
  }
  Context "context 2" {
    # defining all the same logic again
    BeforeAll {
      # create new file here again
    }
    AfterAll {
      # remove file here again
    }
    It "it 1" {
      # doing some activities on the file
      $true | Should -Be $true
    }
    It "it 2" {
      # continuing working on the same file and expecting it to have results from "it 1"
      $true | Should -Be $true
    }
  }
}

5. Possible Solution

As suggested, add something like a -Context switch to the BeforeEach/AfterEach blocks so that they only trigger on Contexts

6. Context

Want to get rid of excessive code and making changes on 10 different places instead of a single block.

nvarscar avatar Jan 19 '19 18:01 nvarscar

Thanks for the suggestion 👍 I think this is similar to #335 and I plan to support this, I actually already do internally, I just need to figure out what should every of those setups do and how they should scope in relation to each other.

nohwnd avatar Jan 20 '19 07:01 nohwnd

Coming back to this. I think the best way to implement this is indeed adding switches to BeforeEach. We are already distinguishing between Context and Describe in Mocks and so we need to be consistent and distinuish between them here as well. The switches make this easy to do and are easy to discover to the user.

BeforeEach { 
    # runs before each test, like in v4
}

BeforeEach -It { 
    # runs before each test, like in v4
}

BeforeEach -Describe {
    # runs before each Describe but not Context
}

BeforeEach -Describe -Context {
    # runs before every block
}

BeforeEach -Describe -Context -It {
    # runs before every block and test
}

nohwnd avatar Mar 24 '19 10:03 nohwnd

Nice! thanks @nohwnd, looking forward to v5

nvarscar avatar Mar 24 '19 19:03 nvarscar

@nohwnd this is not working for me. In my test i have the following code:

Describe "Main tests"{
    BeforeEach -Context {...}
    Context "A"{...}
    Context "B"{...}
}

When I run it I get the following output:

Pester v5.3.1

Starting discovery in 1 files.
[-] Discovery in X:\Programmieren\Powershell\PSWinRAR\Tests\PSWinRAR.Tests.ps1 failed with:
System.Management.Automation.ParameterBindingException: Es wurde kein Parameter gefunden, der dem Parameternamen "Context" entspricht.
   bei System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   bei System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   bei System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   bei System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   bei System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   bei System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   bei System.Management.Automation.PSScriptCmdlet.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   bei System.Management.Automation.PSScriptCmdlet.DoEndProcessing()
   bei System.Management.Automation.CommandProcessorBase.Complete()

Which is thrown on the BeforeEach line and translated it means that there was no Parameter found with the name "Context". The output shows that I am running Pester 5.3.1. Did I do something wrong or is there a problem with this feature?

kevinholtkamp avatar Apr 22 '22 18:04 kevinholtkamp

Yeah that is not implemented yet.

nohwnd avatar Apr 22 '22 18:04 nohwnd

Oh, sorry for the stupid question, I am pretty new to how github works. Since it has v5 in the title I thought it would be in version 5. Is there a way to know roughly when this will be in the live version?

kevinholtkamp avatar Apr 22 '22 18:04 kevinholtkamp

I can't tell. It was planned to be done in the future when Pester v4 was the current version, but then I never got to actually implement it for v5.

Oh, sorry for the stupid question, I am pretty new to how github works.

Not a stupid question and no problem. You would not be able to easily tell, even if it was implemented, unless this issue would be closed and we explicitly wrote it here. 🙂

nohwnd avatar Apr 22 '22 18:04 nohwnd