0
0
PowerShellscripting~20 mins

Pester testing framework basics in PowerShell - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pester Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Pester test block?
Consider this Pester test script:
Describe 'Math tests' {
  It 'adds numbers correctly' {
    (2 + 3) | Should -Be 5
  }
  It 'multiplies numbers correctly' {
    (4 * 2) | Should -Be 8
  }
}

What will Pester report after running this script?
PowerShell
Describe 'Math tests' {
  It 'adds numbers correctly' {
    (2 + 3) | Should -Be 5
  }
  It 'multiplies numbers correctly' {
    (4 * 2) | Should -Be 8
  }
}
A2 tests, 2 passed, 0 failed
B2 tests, 1 passed, 1 failed
C1 test, 1 passed, 0 failed
D0 tests run
Attempts:
2 left
💡 Hint
Both math operations are correct, so both tests should pass.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Pester test
Look at this Pester test code:
Describe 'String tests' {
  It 'checks string length' {
    'hello'.Length | Should -Be 5
  }
  It 'checks string content' 
    'hello' | Should -Be 'hello'
}

Which option correctly describes the syntax error?
PowerShell
Describe 'String tests' {
  It 'checks string length' {
    'hello'.Length | Should -Be 5
  }
  It 'checks string content' 
    'hello' | Should -Be 'hello'
}
AShould command is misspelled
BMissing closing brace '}' after first It block
CMissing opening brace '{' after second It block
DDescribe block is missing parentheses
Attempts:
2 left
💡 Hint
Each It block must have braces enclosing its code.
🔧 Debug
advanced
2:00remaining
Why does this Pester test fail unexpectedly?
Given this Pester test:
$value = 10
Describe 'Value tests' {
  It 'checks if value is greater than 5' {
    $value -gt 5 | Should -Be $true
  }
  It 'checks if value is less than 20' {
    $value -lt 20 | Should -Be $false
  }
}

Why does the second test fail?
PowerShell
$value = 10
Describe 'Value tests' {
  It 'checks if value is greater than 5' {
    $value -gt 5 | Should -Be $true
  }
  It 'checks if value is less than 20' {
    $value -lt 20 | Should -Be $false
  }
}
AThe variable $value is not accessible inside the Describe block
BThe second test expects $false but the condition is true
CThe Should command is used incorrectly with -Be
DThe first test modifies $value causing the second to fail
Attempts:
2 left
💡 Hint
Check the logic of the condition and the expected value in the second test.
🚀 Application
advanced
2:00remaining
How to test a function output with Pester?
You have this PowerShell function:
function Get-Greeting {
  param([string]$Name)
  "Hello, $Name!"
}

Which Pester test block correctly verifies that calling Get-Greeting with 'Alice' returns 'Hello, Alice!'?
PowerShell
function Get-Greeting {
  param([string]$Name)
  "Hello, $Name!"
}

Describe 'Get-Greeting function' {
  It 'returns correct greeting for Alice' {
    # Fill in the test here
  }
}
AGet-Greeting -Name 'Alice' | Should -Be 'Hello, Alice!'
B"Hello, Alice!" | Should -Be (Get-Greeting -Name 'Alice')
CGet-Greeting 'Alice' | Should -Be 'Hello, Alice!'
DGet-Greeting -Name Alice | Should -Be 'Hello Alice!'
Attempts:
2 left
💡 Hint
Use the function call on the left side of the Should command and the expected string on the right.
🧠 Conceptual
expert
2:00remaining
What is the purpose of the 'BeforeAll' block in Pester?
In Pester testing, what does the 'BeforeAll' block do compared to 'BeforeEach'?
A'BeforeAll' and 'BeforeEach' are interchangeable and run before each test
B'BeforeAll' runs before every It block; 'BeforeEach' runs once before all tests
C'BeforeAll' runs after all tests; 'BeforeEach' runs before all tests
D'BeforeAll' runs once before all tests in a Describe block; 'BeforeEach' runs before every It block
Attempts:
2 left
💡 Hint
Think about setup code that should run once versus before every test.