Hi, new to using pester and just trying the initial test script but getting the following error and no idea what it means. Any ideas ?
[-] Discovery in C:\users\********\pester\tests\worldclockapi.tests.ps1 failed with:
System.Management.Automation.RuntimeException: Test cannot be directly in the root.
If so I’m unable to reproduce so we need to know more about exactly what you’re running to produce that error.
I did a Invoke-Pester against that file and it fails, but more or less because the worldclockapi is not available, so it fails testing which is what it should do, as we do not get a 200 back we get a 503 back which is the first test.
Yes, it is the file from that tutorial. This is the output from the rest of the attempt.
at New-Test, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 1020
at It, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 10653
at <ScriptBlock>, C:\users\*****\pester\tests\worldclockapi.tests.ps1: line 8
at <ScriptBlock>, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 3034
at Invoke-File, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 3043
at Invoke-BlockContainer, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 2952
at Discover-Test, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 1468
at Invoke-Test, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 2462
at Invoke-Pester<End>, C:\Program Files\WindowsPowerShell\Modules\Pester\5.5.0\Pester.psm1: line 5046
at <ScriptBlock>, <No file>: line 1
Discovery found 0 tests in 160ms.
Running tests.
Tests completed in 159ms
Tests Passed: 0, Failed: 0, Skipped: 0 NotRun: 0
Container failed: 1
And this is the contents of the file
Describe 'Test worldclockapi.com' {
BeforeAll {
$response = Invoke-WebRequest -Method 'GET' -Uri 'http://worldclockapi.com/api/json/utc/now'
$responseContent = $response.Content | ConvertFrom-Json
}
}
It "It should respond with 200" {
$response.StatusCode | Should -Be 200
}
It "It should have a null service response" {
$responseContent.serviceResponse | Should -BeNullOrEmpty
}
It "It should be the right day of the week" {
$dayOfWeek = (Get-Date).DayOfWeek
$responseContent.dayOfTheWeek | Should -Be $dayOfWeek
}
It "It should be the right year" {
$year = Get-Date -Format "yyyy"
$responseContent.currentDateTime | Should -BeLike "*$year*"
}
It "It should be the right month" {
$month = Get-Date -Format "MM"
$responseContent.currentDateTime | Should -BeLike "*$month*"
}
# These two tests assume you are running this outside daylight savings (during the winter) .. hacky but good way to showcase the syntax ;)
It "It should not be daylight savings time" {
$responseContent.isDayLightSavingsTime | Should -Not -Be $true
}
It "It should not be daylight savings time another way" {
$responseContent.isDayLightSavingsTime | Should -BeFalse
}
The left is your code, the right is the code I pulled from that repo.
I reproduced the error with your code, and it has to do with those brackets being misplaced I think.
Again note that because that API is not available, it doesn’t pass the tests (as it should), but at least you’ll get proper output because you get a 503 instead of a 200.