This document describes the comprehensive testing framework for the PowerShell-Tools repository.
The testing framework uses Pester, the standard PowerShell testing framework, to ensure all scripts work correctly and maintain high code quality. The framework includes:
- Unit Tests: Test individual functions and components in isolation
- Integration Tests: Test complete workflows and cross-script functionality
- Security Analysis: Automated security scanning and validation
- Code Coverage: Measure test coverage across all scripts
- CI/CD Integration: Automated testing on every commit and pull request
- PowerShell 5.1 or later (PowerShell 7.x recommended)
- Pester module (automatically installed if missing)
# Run all tests
.\RunTests.ps1
# Run only unit tests
.\RunTests.ps1 -TestType Unit
# Run only integration tests
.\RunTests.ps1 -TestType Integration
# Run with detailed output
.\RunTests.ps1 -ShowDetails
# Run in CI mode with coverage
.\RunTests.ps1 -TestType All -OutputFormat NUnitXml -CIPowerShell-Tools/
├── Tests/
│ ├── Unit/ # Unit tests for individual scripts
│ │ ├── System-Administration.Tests.ps1
│ │ ├── Network-Management.Tests.ps1
│ │ ├── Security.Tests.ps1
│ │ ├── Development-Tools.Tests.ps1
│ │ └── Automation.Tests.ps1
│ ├── Integration/ # Integration and end-to-end tests
│ │ └── End-to-End.Tests.ps1
│ └── TestHelpers/ # Shared test utilities
│ └── TestSetup.ps1
├── TestResults/ # Test output and reports
├── RunTests.ps1 # Main test runner
├── pester.config.ps1 # Pester configuration
└── TESTING.md # This documentation
Unit tests verify individual functions and components work correctly in isolation:
- Script Validation: Syntax checking and help validation
- Parameter Validation: Input validation and error handling
- Core Functionality: Main script logic and output validation
- Error Handling: Exception handling and edge cases
- Performance: Execution time and resource usage
Example unit test:
Describe "Get-SystemInfo.ps1" {
Context "Parameter validation" {
It "should accept valid computer names" {
{ Get-SystemInfo -ComputerName "localhost" -WhatIf } | Should -Not -Throw
}
}
Context "Output format validation" {
It "should return system information object" {
$result = Get-SystemInfo -ComputerName "localhost"
$result | Should -Not -BeNullOrEmpty
$result.ComputerName | Should -Be "TEST-COMPUTER"
}
}
}Integration tests verify complete workflows and cross-script functionality:
- Cross-script Integration: Scripts working together
- Realistic Scenarios: Real-world usage patterns
- End-to-end Workflows: Complete business processes
- Error Propagation: How errors cascade between scripts
Example integration test:
Describe "End-to-End Integration Tests" {
Context "Cross-script functionality" {
It "should generate system report and schedule task to run it" {
# Create system report
$systemInfo = Get-SystemInfo -ComputerName "localhost"
$systemInfo | Should -Not -BeNullOrEmpty
# Schedule task to run report
$task = New-ScheduledTask -TaskName "SystemReportTask" -ScriptPath $script:SystemInfoScript
$task | Should -Not -BeNullOrEmpty
}
}
}The TestSetup.ps1 file provides common utilities:
- Environment Setup: Initialize and clean up test environment
- Mock Helpers: Common mock objects and responses
- Test Data: Generate test data and fixtures
- Validation Helpers: Common validation functions
The pester.config.ps1 file contains:
- Test discovery paths
- Code coverage settings
- Output formats and paths
- Coverage thresholds (80% target)
- Verbosity and reporting options
The RunTests.ps1 script supports:
- TestType: All, Unit, Integration
- OutputFormat: NUnitXml, JUnitXml, Console
- ShowDetails: Detailed output
- CI: Continuous integration mode with coverage
The .github/workflows/test.yml workflow:
- Runs on Windows with PowerShell 5.1 and 7.x
- Executes syntax validation
- Runs unit and integration tests
- Generates code coverage reports
- Performs security analysis with PSScriptAnalyzer
- Uploads test results and coverage data
Tests generate multiple report formats:
- NUnit XML: For CI/CD integration
- JUnit XML: For test reporting systems
- JaCoCo XML: For code coverage
- Console: For local development
-
Test Structure:
- Use descriptive
Describe,Context, andItblocks - Group related tests logically
- Use meaningful test names that describe expected behavior
- Use descriptive
-
Test Isolation:
- Each test should be independent
- Use
BeforeEachandAfterEachfor setup/cleanup - Mock external dependencies
-
Assertions:
- Use specific assertions (
Should -Be,Should -Contain) - Test both positive and negative cases
- Verify error conditions
- Use specific assertions (
-
Mock Strategy:
- Mock external dependencies (CIM, network, file system)
- Use realistic mock data
- Verify mock interactions when needed
-
Keep Tests Updated:
- Update tests when functionality changes
- Add tests for new features
- Remove obsolete tests
-
Test Performance:
- Keep tests fast (< 30 seconds for unit tests)
- Use timeouts for network operations
- Optimize slow tests
-
Test Coverage:
- Aim for 80%+ code coverage
- Focus on critical paths
- Test error handling paths
The framework includes automated security analysis:
- PSScriptAnalyzer: Static code analysis
- Credential Scanning: Detect hardcoded passwords
- Dangerous Cmdlet Detection: Identify risky operations
- External URL Scanning: Find external dependencies
-
Test Security Features:
- Verify encryption/decryption
- Test password generation
- Validate secure storage
-
Test Error Handling:
- Verify no secrets in error messages
- Test permission denied scenarios
- Validate input sanitization
-
Pester Not Found:
Install-Module -Name Pester -Force -SkipPublisherCheck -Scope CurrentUser
-
Test Failures:
- Check test output for specific errors
- Run with
-ShowDetailsfor more information - Verify script syntax with PowerShell parser
-
Coverage Issues:
- Ensure all scripts are in coverage paths
- Check for excluded files
- Verify test execution actually calls functions
For debugging tests:
# Run specific test file
Invoke-Pester -Path ".\Tests\Unit\System-Administration.Tests.ps1" -Output Detailed
# Run specific test
Invoke-Pester -Path ".\Tests\Unit\System-Administration.Tests.ps1" -FullName "*should accept valid computer names*"When adding new scripts or features:
- Write Tests First: Use TDD approach when possible
- Maintain Coverage: Ensure new code is tested
- Update Documentation: Keep this file updated
- Run Full Test Suite: Verify all tests pass before committing
Target performance goals:
- Unit Tests: < 30 seconds total
- Integration Tests: < 2 minutes total
- Full Test Suite: < 5 minutes total
- Individual Script Tests: < 10 seconds each
Planned improvements:
- Property-based Testing: Add randomized test data
- Load Testing: Test scripts with large datasets
- Cross-platform Testing: Linux and macOS support
- Performance Profiling: Detailed performance metrics
- Mutation Testing: Verify test quality
For testing issues:
- Check this documentation
- Review test output and logs
- Check GitHub Issues for known problems
- Create new issue with detailed error information