Passing the output of a test to the next test

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

Viewing 1 reply thread
  • Author
    Posts
    • #112171
      Participant
      Topics: 41
      Replies: 56
      Points: 119
      Rank: Participant

      Hi,

      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 "https://theapiaddress.me" `
                                     -Body ($Auth | ConvertTo-Json) `
                                     -ContentType "application/json" `
                                     -Method Post `
                                     -UseBasicParsing
              $Response = $R | ConvertFrom-Json
              $Response.tokenString  | Should Not BeNullOrEmpty
          }
          it 'Use token to access records' {
      
              $Headers = $Response.TokenString
              $T = Invoke-WebRequest -Uri "https://theapiaddress.me"`
                             -Method Get `
                             -Headers $Headers `
                             -UseBasicParsing
      
              $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.

      Thanks

      Tommy

    • #112174
      Participant
      Topics: 0
      Replies: 28
      Points: -7
      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.

Viewing 1 reply thread
  • The topic ‘Passing the output of a test to the next test’ is closed to new replies.