Skip to content

Commit a76a068

Browse files
Add PowerShell function test specification documentation
1 parent a6c7da7 commit a76a068

2 files changed

Lines changed: 222 additions & 0 deletions

File tree

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# PowerShell Function Test Specification
2+
3+
## Overview
4+
5+
This document defines the structure and guidelines for writing Pester tests for PowerShell functions. The goal is to ensure consistency and comprehensive test coverage while maintaining clarity.
6+
7+
## Test Structure
8+
9+
Each function is tested within a structured Pester `Describe` block that follows this hierarchy:
10+
11+
### 1. Module-Level `Describe`
12+
13+
- The outermost `Describe` block corresponds to the module.
14+
- The module name is derived from the prefix of the function name's noun part.
15+
16+
### 2. Function-Level `Describe`
17+
18+
- Each function within the module gets its own `Describe` block.
19+
- Named using the full function name.
20+
21+
### 3. Use Case `Context`
22+
23+
- Each use case is grouped within a `Context` block.
24+
- The context name starts with the function name followed by a descriptive test case identifier.
25+
- Example: `Get-Uri - simple usage`
26+
- Example: `Get-Uri - Pipeline usage`
27+
- Example: `Get-Uri - ParameterSet: Detailed`
28+
29+
### 4. Functional `It` Statements
30+
31+
- Each `It` block tests a specific aspect of the function's behavior.
32+
- The name follows the format: `FunctionName - <expected behavior>`
33+
- Example: `Get-Uri - gets the URI object when provided a string`
34+
- Example: `Get-Uri - returns $null when input is empty`
35+
- Example: `Get-Uri - throws error on invalid input`
36+
37+
### 5. Test Guidelines
38+
39+
- **No Mocks**: We do not use mocks in tests.
40+
- **Real Inputs**: Tests should use actual inputs and expected outputs.
41+
- **Comprehensive Coverage**: All function functionality, including edge cases, must be tested.
42+
43+
## Example Test Structure
44+
45+
```powershell
46+
Describe 'Uri' {
47+
Describe 'Get-Uri' {
48+
Dontext 'Get-Uri - simple usage' {
49+
It 'Get-Uri - gets the URI object when provided a string' {
50+
$result = Get-Uri -InputString 'https://example.com'
51+
$result | Should -BeOfType [System.Uri]
52+
}
53+
}
54+
55+
Context 'Get-Uri - Pipeline usage' {
56+
It 'Get-Uri - processes input from the pipeline' {
57+
$result = 'https://example.com' | Get-Uri
58+
$result | Should -BeOfType [System.Uri]
59+
}
60+
}
61+
}
62+
}
63+
```
64+
65+
This ensures our tests are structured, maintainable, and adhere to best practices.
66+

0 commit comments

Comments
 (0)