Passing the output of a test to the next test

Welcome Forums Pester Passing the output of a test to the next test

This topic contains 1 reply, has 2 voices, and was last updated by

7 months ago.

  • Author
  • #112171

    Topics: 35
    Replies: 44
    Points: 26
    Rank: Member


    I have a pester test that run against a API – the first test ensures i can get a authentication token from the api. This token then needs to pass onto the next test to check accessing records etc.

    It seems the the token doesn't get passed onto the next test?


    Describe 'Test API' {
        Context 'Testing API'{
        it 'Can generate a token'{
            $Auth = @{}
            $Auth.PresentedIdentifier = "asdbnefd"
            $Auth.PresentedSecret= "qweerty"
            $R = Invoke-WebRequest -Uri "" `
                                   -Body ($Auth | ConvertTo-Json) `
                                   -ContentType "application/json" `
                                   -Method Post `
            $Response = $R | ConvertFrom-Json
            $Response.tokenString  | Should Not BeNullOrEmpty
        it 'Use token to access records' {
            $Headers = $Response.TokenString
            $T = Invoke-WebRequest -Uri ""`
                           -Method Get `
                           -Headers $Headers `
            $Out = $T | ConvertFrom-Json
            $Out.webUserId | Should Not BeNullOrEmpty

    What is the best way to do it, i know it will work if i put all the tests under a single test, but i want to test each step, if you know what i mean.



  • #112174

    Topics: 0
    Replies: 26
    Points: -19
    Rank: Member

    Yes, you are right, setting the variable does not pass the data outside from the scope. This is how powershell works, setting variable in a scope only sets it in that scope, unless the variable has a script or global scope. So to share your data you need to either make the variable script scoped and share the data from the first test, or a more idiomatic solution: use BeforeAll block to setup for all tests in the Describe.

The topic ‘Passing the output of a test to the next test’ is closed to new replies.

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