Powershell-Pester-5
Powershell-Pester-5是一款code方向的AI技能,核心价值是PowerShell Pester testing best practices based on Pester v5 conventions,可用于解决开发者在code领域的实际问题,帮助用户提升效率、自动化重复任务或优化工作流。
PowerShell Pester testing best practices based on Pester v5 conventions
mkdir -p ./skills/powershell-pester-5 && curl -sfL https://raw.githubusercontent.com/github/awesome-copilot/main/skills/powershell-pester-5/SKILL.md -o ./skills/powershell-pester-5/SKILL.md Run in terminal / PowerShell. Requires curl (Unix) or PowerShell 5+ (Windows).
Skill Content
# PowerShell Pester v5 Testing Guidelines
This guide provides PowerShell-specific instructions for creating automated tests using PowerShell Pester v5 module. Follow PowerShell cmdlet development guidelines in [powershell.instructions.md](./powershell.instructions.md) for general PowerShell scripting best practices.
File Naming and Structure
- **File Convention:** Use `*.Tests.ps1` naming pattern
- **Placement:** Place test files next to tested code or in dedicated test directories
- **Import Pattern:** Use `BeforeAll { . $PSScriptRoot/FunctionName.ps1 }` to import tested functions
- **No Direct Code:** Put ALL code inside Pester blocks (`BeforeAll`, `Describe`, `Context`, `It`, etc.)
Test Structure Hierarchy
BeforeAll { # Import tested functions }
Describe 'FunctionName' {
Context 'When condition' {
BeforeAll { # Setup for context }
It 'Should behavior' { # Individual test }
AfterAll { # Cleanup for context }
}
}Core Keywords
- **`Describe`**: Top-level grouping, typically named after function being tested
- **`Context`**: Sub-grouping within Describe for specific scenarios
- **`It`**: Individual test cases, use descriptive names
- **`Should`**: Assertion keyword for test validation
- **`BeforeAll/AfterAll`**: Setup/teardown once per block
- **`BeforeEach/AfterEach`**: Setup/teardown before/after each test
Setup and Teardown
- **`BeforeAll`**: Runs once at start of containing block, use for expensive operations
- **`BeforeEach`**: Runs before every `It` in block, use for test-specific setup
- **`AfterEach`**: Runs after every `It`, guaranteed even if test fails
- **`AfterAll`**: Runs once at end of block, use for cleanup
- **Variable Scoping**: `BeforeAll` variables available to child blocks (read-only), `BeforeEach/It/AfterEach` share same scope
Assertions (Should)
- **Basic Comparisons**: `-Be`, `-BeExactly`, `-Not -Be`
- **Collections**: `-Contain`, `-BeIn`, `-HaveCount`
- **Numeric**: `-BeGreaterThan`, `-BeLessThan`, `-BeGreaterOrEqual`
- **Strings**: `-Match`, `-Like`, `-BeNullOrEmpty`
- **Types**: `-BeOfType`, `-BeTrue`, `-BeFalse`
- **Files**: `-Exist`, `-FileContentMatch`
- **Exceptions**: `-Throw`, `-Not -Throw`
Mocking
- **`Mock CommandName { ScriptBlock }`**: Replace command behavior
- **`-ParameterFilter`**: Mock only when parameters match condition
- **`-Verifiable`**: Mark mock as requiring verification
- **`Should -Invoke`**: Verify mock was called specific number of times
- **`Should -InvokeVerifiable`**: Verify all verifiable mocks were called
- **Scope**: Mocks default to containing block scope
Mock Get-Service { @{ Status = 'Running' } } -ParameterFilter { $Name -eq 'TestService' }
Should -Invoke Get-Service -Exactly 1 -ParameterFilter { $Name -eq 'TestService' }Test Cases (Data-Driven Tests)
Use `-TestCases` or `-ForEach` for parameterized tests:
It 'Should return <Expected> for <Input>' -TestCases @(
@{ Input = 'value1'; Expected = 'result1' }
@{ Input = 'value2'; Expected = 'result2' }
) {
Get-Function $Input | Should -Be $Expected
}Data-Driven Tests
- **`-ForEach`**: Available on `Describe`, `Context`, and `It` for generating multiple tests from data
- **`-TestCases`**: Alias for `-ForEach` on `It` blocks (backwards compatibility)
- **Hashtable Data**: Each item defines variables available in test (e.g., `@{ Name = 'value'; Expected = 'result' }`)
- **Array Data**: Uses `$_` variable for current item
- **Templates**: Use `<variablename>` in test names for dynamic expansion
# Hashtable approach
It 'Returns <Expected> for <Name>' -ForEach @(
@{ Name = 'test1'; Expected = 'result1' }
@{ Name = 'test2'; Expected = 'result2' }
) { Get-Function $Name | Should -Be $Expected }
# Array approach
It 'Contains <_>' -ForEach 'item1', 'item2' { Get-Collection | Should -Contain $_ }Tags
- **Available on**: `Describe`, `Context`, and `It` block
🎯 Best For
- QA engineers
- Developers writing unit tests
- Claude users
- GitHub Copilot users
- Software engineers
💡 Use Cases
- Generating test cases for edge conditions
- Writing integration test suites
- Code quality improvement
- Best practice enforcement
📖 How to Use This Skill
- 1
Install the Skill
Copy the install command from the Terminal tab and run it. The SKILL.md file downloads to your local skills directory.
- 2
Load into Your AI Assistant
Open Claude or GitHub Copilot and reference the skill. Paste the SKILL.md content or use the system prompt tab.
- 3
Apply Powershell-Pester-5 to Your Work
Open your project in the AI assistant and ask it to apply the skill. Start with a small module to verify the output quality.
- 4
Review and Refine
Review AI suggestions before committing. Run tests, check for regressions, and iterate on the skill output.
❓ Frequently Asked Questions
Does this generate test mocks?
Many testing skills include mock generation. Check the install command and skill content for details.
Is Powershell-Pester-5 compatible with Cursor and VS Code?
Yes — this skill works with any AI coding assistant including Cursor, VS Code with Copilot, and JetBrains IDEs.
Do I need specific dependencies for Powershell-Pester-5?
Check the install command and Works With section. Most code skills only require the AI assistant and your codebase.
How do I install Powershell-Pester-5?
Copy the install command from the Terminal tab and run it. The skill downloads to ./skills/powershell-pester-5/SKILL.md, ready to use.
Can I customize this skill for my team?
Absolutely. Edit the SKILL.md file to add team-specific instructions, examples, or workflows.
⚠️ Common Mistakes to Avoid
Not testing edge cases
AI tends to generate happy-path tests. Manually review for boundary conditions.
Skipping validation
Always test AI-generated code changes, even for simple refactors.
Missing dependency updates
Check if the skill requires updated dependencies or new packages.