I have the following Pester Test-script:
function Get-Name{
return "TestObject"
}
Describe{
BeforeAll{
$Script:ObjectID
$API::[API]::New()
}
It 'Get ObjectID By NAME'{
$Name = Get-Name
$Object = $API.GetObjectByName($Name)
$ObjectID = $Object.id
$ObjectID | Should -BeGreaterThan 0
Write-Host "ObjectID = " + $ObjectID
}
It 'Print Object ID'{
write-host $ObjectID
}
}
My issue is the following:
Issue 1: Function not recognized when the script is 'invoked'.
Whenever I run the script with the Invoke-Pester -Path c:\path\test.ps1 -Detailed -command, my first test fails due to the function not being recognized. However, when I compile and run my script in PowerShell ISE, the first test succeeds.
Issue 2: Pass ObjectID to another It-block
My second test never outputs the ID retrieved from the first test. I do not know how to pass the variable from one test, to another. I already have tried putting the BeforeALl above the Describe but without success.
What am I doing wrong in both cases? My pester version is 5.6.1
Invoke-Commandyou meantInvoke-Pester? As for "Issue 2" whats the point of this test? What is it trying to assert?Itblocks are supposed to be isolated tests, there shouldn't be a relation between 2ItblocksInvoke-Pesterand I must not be the first one trying to pass variables betweenIt-blocks right? What is the purpose of Script-wide variables then? thanks. It's important because in one script I might create and object (CreateObject()-method in API) and use its ID in another test (CreateOtherObject() with an API-call that 'builds' on the previous one.CreateUser()and a second API function namedUpdateUser(). Wouldn't it be smart to test and have 2 seperate tests and see if both functions work correctly? MyUpdateUser()-function would in this case 'depend' on theuserIDI got fromCreateUser().