why doesn't this work? Pester Beforeeach not applying variable change

Welcome Forums Pester why doesn't this work? Pester Beforeeach not applying variable change

This topic contains 12 replies, has 3 voices, and was last updated by

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #133613

    Participant
    Points: 24
    Rank: Member

    Hi

    I'm trying to be able to switch the -skip option on or off based on what I set in a variable. Please see below.

    $skip = $true
    Describe "test describe" {
        Context "test"{
            BeforeEach {
                                   $skip="true"
                                   write-host $skip
             }
             write-host $skip
             It -name "testIT" -skip:$skip {
             1 | should be 1
             }
        }
    }
    If I set the $skip to true it works and although it reads the Beforeeach block it doesn't apply the change to the $skip variable.
    Why?
  • #133623

    Participant
    Points: 1,620
    Helping Hand
    Rank: Community Hero

    Are you trying to skip the testcase (it block) ? What you would need is BeforeAll instead of BeforeEach, my understanding is Before each is executed for each test case once its triggered, hence skipping logic cannot be put there.

  • #133641

    Participant
    Points: 24
    Rank: Member

    Hi,

    Thanks for the quick reply and help.

    Yes I'm trying to skip the testcase.

    But if I set it to the BeforeAll wont that set the skip for all the tests?

    The full picture of what I'm trying to do is to put an if statement in the beforeeach example below, this checks against a json file loaded in to a variable.

    beforeeach{

    if ($config.test.skip -eq "True"){

    $skip = $true

    }

    }

     

     

  • #133644

    Participant
    Points: 1,620
    Helping Hand
    Rank: Community Hero

    It will skip all. If you are using the if statement to convert to $true, you can directly do it via conversion [bool]::Parse($config.test.skip)

    it "test name" -skip:([bool]::Parse($config.test.skip)) {
        1 | should -be 1
    }
    
  • #133661

    Participant
    Points: 24
    Rank: Member

    Hi,

    Cool, thanks. Just tried it but I get this error

    FormatException: String was not recognized as a valid Boolean.
    MethodInvocationException: Exception calling "Parse" with "1" argument(s): "String was not recognized as a valid Boolean."

  • #133683

    Participant
    Points: 1,620
    Helping Hand
    Rank: Community Hero

    Make sure you pass only true or false to parse method as those are the only supported values.

  • #133853

    Participant
    Points: 24
    Rank: Member

    Hi, I struggling here, still can't get it working.

    here is an example of the json

    {

    "tests": {

    "test1": {

    "name": "test1",

    "skip": "$true"

    }

    }

     

    would it be possible to give me an example where this works, as I cannot get it to work the way you have said.

     

    Thanks

  • #134001

    Participant
    Points: 1,620
    Helping Hand
    Rank: Community Hero

    remove dollar from '$true', parser will convert true to boolean. $ is variable invoker and is not required here.

  • #134049

    Participant
    Points: 24
    Rank: Member

    Thanks, Kvprasoon

    Really appreciate the time you are spending with me to fix this issue. I'm still getting an error.

    Would it be possible to actually put an example down that works? Also, can you check it works when just ran as a script. I've found that when I run things from a script, it fails.

    I'm not sure if it's the way I'm setting the variable. Heres my setup.

    I have a config.json file which contains a list of my tests see below

    {

    "tests": {
    "test1": {
    "name": "test1",
    "skip": "$true"
    }
    "test2": {
    "name": "test2",
    "skip": "$true"
    }
    }
    }

    I have a powershell script that load that config into a variable called $tests

    $file = "../config/tests.json"
    $tests = get-content $file | convertFrom-json
    $testargument = "test1"

    describe "describe block" {
    beforeeach{
    if ($tests.$testargument.skip = "True")
    {
    $skip = $true
    }
    }
    context "Context block"{
    it -name "test1" -skip:$skip {
    1 | should be 1
    }
    context "Another Context block" {
    it -name "test2" -skip$skip {
    1 | should be 1
    }
    }
    }

    So where am I going wrong?

    Thanks
    Russell

  • #134082

    Participant
    Points: 1,620
    Helping Hand
    Rank: Community Hero

    I would do it like below.

    #json
    {
        "tests": 
            [
                {
                    "name": "test1",
                    "skip": "true"
                },
                {
                    "name": "test2",
                    "skip": "true"
                }
            ]
        
    }
    
    $file = "../config/tests.json"
    $tests = Get-Content -Path $file | convertFrom-json
    
    describe "describe block" {
    
        Foreach ($TestCase in $tests.tests) {
            context "Context block" {
                it -name $TestCase.Name -skip:([Bool]::Parse($TestCase.Skip)) {
                    1 | should be 1
                }
            }
        }
    }
    

    I would recommend you to format code when posting in forum. It will help anyone to understand the code better. The instructions will be shown at the top while posting/replying.

    And I would suggest you to take some time reading the Docs for pester . github.com/pester/pester/wiki

  • #134466

    Participant
    Points: 24
    Rank: Member

    Hi Kvprasoon,
    Thanks for writing that out, I've tried what you have written and it works, which is great. However, you aren't using the BeforeEach block. Could you see if you can get this working with a BeforeEach block, please? I can't get this working.

    thanks
    Russell

  • #134516

    Participant
    Points: 1,620
    Helping Hand
    Rank: Community Hero

    As I mentioned you before. As far as I know, BeforeEach is executed inside "It" block. Hence it cannot be used for this logic.

  • #136266

    Participant
    Points: 74
    Rank: Member

    For what it's worth Jakub Jareš has a really good rundown of scoping and execution order in the Pester v5 readme. (covers both v4 and v5).

You must be logged in to reply to this topic.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort