Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in BeforeAll of Data driven tests leads to missing info in the error output #2571

Open
3 tasks done
crlgb opened this issue Oct 23, 2024 · 4 comments
Open
3 tasks done

Comments

@crlgb
Copy link

crlgb commented Oct 23, 2024

Checklist

What is the issue?

Output does not resolve the current item $_ in the output.

Starting discovery in 1 files.
Discovery found 2 tests in 2ms.
Running tests.
[-] Describe Test <_> failed
 RuntimeException: something
[...]
Tests completed in 24ms
Tests Passed: 0, Failed: 2, Skipped: 0, Inconclusive: 0, NotRun: 0
BeforeAll \ AfterAll failed: 2
  - Test <_>
  - Test <_>

Expected Behavior

Starting discovery in 1 files.
Discovery found 2 tests in 2ms.
Running tests.
[-] Describe Test **a** failed
 RuntimeException: something
[...]
Tests completed in 24ms
Tests Passed: 0, Failed: 2, Skipped: 0, Inconclusive: 0, NotRun: 0
BeforeAll \ AfterAll failed: 2
  - Test **a**
  - Test **b**

Steps To Reproduce

Describe "Test <_>" -ForEach @("a","b") {
  BeforeAll {
    throw "something"
  }
  It "is a or b" {
    $_ | Should -Match "[ab]"
  }
}

Describe your environment

Pester version     : 5.6.1
PowerShell version : 7.4.1
OS version         : Unix 15.0.1

Possible Solution?

No response

@nohwnd
Copy link
Member

nohwnd commented Oct 23, 2024

This is by design. We evaluate the name just after BeforeEach, so values from BeforeAll and BeforeEach are reflected into the string. This seemed like the best place, since the Before* steps are setting up values and should hopefully have all the right data for the test.

We also need to evaluate in a scope that is on the same level as the BeforeEach.

The code here could probably be changed to capture the error from BeforeEach, run more code to evaluate the name of the test, and rethrow the error.

https://github.com/pester/Pester/blob/main/src/Pester.Runtime.ps1#L643

@fflaten
Copy link
Collaborator

fflaten commented Oct 23, 2024

This is known limitation and currently by design.

The variables are expanded after setup (BeforeAll/-Each) to have all variables/reference available. An exception during setup stops further processing to avoid unexpected results like old variables from parent scope being used etc.

@nohwnd Candidate for v6? Does anyone depend on BeforeEach/-All variables for templates?

@nohwnd
Copy link
Member

nohwnd commented Oct 23, 2024

IDK if we technically we can fix it.

There can also be a different complaint which is: the variables from my tests / AfterAll are not expanded.

@neo42JBR
Copy link

neo42JBR commented Nov 20, 2024

Chiming in on this topic:

I built a framework that can parse configuration files to pester tests. In it there is an option to specifiy if a test is terminating or not.
As you can use Set-ItResult (or its errorrecord) within BeforeEach the tests get the correct state but their title is not set correctly.

The variables might still be present (especially when coming from the testdata) and one could atleast try to expand them.

Edit:
Another case in which the above happens is when the Test is skipped via the -Skip parameter

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants