PowerShell Tester Tool Template
<# .SYNOPSIS PowerShell Tester Tool Template .DESCRIPTION This script provides a template for a PowerShell-based tester tool that can be customized for various testing scenarios. It includes functions for defining tests, running them, and generating reports. .NOTES File Name : TesterToolTemplate.ps1 Author : [Your Name] Prerequisite : PowerShell V5.1 or later Version : 1.0 Date : [Current Date] .EXAMPLE .\TesterToolTemplate.ps1 #> # Global variables $global:testResults = @() $global:reportPath = "$env:USERPROFILE\Desktop\Test_Report_$(Get-Date -Format 'yyyyMMdd_HHmmss').html" <# .SYNOPSIS Displays the main menu of the tester tool. #> function Show-Menu { Clear-Host Write-Host "=== PowerShell Tester Tool ===" -ForegroundColor Cyan Write-Host "1. Run All Tests" Write-Host "2. Run Specific Test" Write-Host "3. View Test Results" Write-Host "4. Generate HTML Report" Write-Host "5. Exit" } <# .SYNOPSIS Defines the test cases. Add your test functions here. #> function Define-Tests { $global:tests = @{ "Test1" = { Test-Case1 } "Test2" = { Test-Case2 } "Test3" = { Test-Case3 } # Add more test cases as needed } } <# .SYNOPSIS Example test case 1. Replace with your actual test logic. #> function Test-Case1 { $testName = "Test Case 1" $description = "Description of Test Case 1" try { # Your test logic here $result = $true $message = "Test Case 1 passed successfully" } catch { $result = $false $message = "Test Case 1 failed: $_" } return [PSCustomObject]@{ TestName = $testName Description = $description Result = $result Message = $message } } <# .SYNOPSIS Example test case 2. Replace with your actual test logic. #> function Test-Case2 { $testName = "Test Case 2" $description = "Description of Test Case 2" try { # Your test logic here $result = $true $message = "Test Case 2 passed successfully" } catch { $result = $false $message = "Test Case 2 failed: $_" } return [PSCustomObject]@{ TestName = $testName Description = $description Result = $result Message = $message } } <# .SYNOPSIS Example test case 3. Replace with your actual test logic. #> function Test-Case3 { $testName = "Test Case 3" $description = "Description of Test Case 3" try { # Your test logic here $result = $true $message = "Test Case 3 passed successfully" } catch { $result = $false $message = "Test Case 3 failed: $_" } return [PSCustomObject]@{ TestName = $testName Description = $description Result = $result Message = $message } } <# .SYNOPSIS Runs all defined tests. #> function Run-AllTests { Write-Host "`nRunning All Tests..." -ForegroundColor Yellow $global:testResults = @() foreach ($test in $global:tests.GetEnumerator()) { Write-Host "Running $($test.Key)..." -ForegroundColor Cyan $result = & $test.Value $global:testResults += $result if ($result.Result) { Write-Host " Passed: $($result.Message)" -ForegroundColor Green } else { Write-Host " Failed: $($result.Message)" -ForegroundColor Red } } } <# .SYNOPSIS Runs a specific test based on user input. #> function Run-SpecificTest { Write-Host "`nAvailable Tests:" -ForegroundColor Yellow $global:tests.Keys | ForEach-Object { Write-Host " $_" } $testName = Read-Host "`nEnter the name of the test to run" if ($global:tests.ContainsKey($testName)) { Write-Host "Running $testName..." -ForegroundColor Cyan $result = & $global:tests[$testName] $global:testResults += $result if ($result.Result) { Write-Host " Passed: $($result.Message)" -ForegroundColor Green } else { Write-Host " Failed: $($result.Message)" -ForegroundColor Red } } else { Write-Host "Test not found." -ForegroundColor Red } } <# .SYNOPSIS Displays the results of all run tests. #> function View-TestResults { if ($global:testResults.Count -eq 0) { Write-Host "`nNo tests have been run yet." -ForegroundColor Yellow } else { Write-Host "`nTest Results:" -ForegroundColor Yellow $global:testResults | Format-Table -AutoSize } } <# .SYNOPSIS Generates an HTML report of the test results. #> function Generate-HTMLReport { Write-Host "`nGenerating HTML Report..." -ForegroundColor Yellow $reportContent = @" <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Test Results Report</title> <style> body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; max-width: 800px; margin: 0 auto; padding: 20px; } h1 { color: #0078D4; } table { border-collapse: collapse; width: 100%; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f2f2f2; } .pass { color: green; } .fail { color: red; } </style> </head> <body> <h1>Test Results Report</h1> <p>Generated on: $(Get-Date)</p> <table> <tr> <th>Test Name</th> <th>Description</th> <th>Result</th> <th>Message</th> </tr> $(foreach ($result in $global:testResults) { $resultClass = if ($result.Result) { "pass" } else { "fail" } "<tr> <td>$($result.TestName)</td> <td>$($result.Description)</td> <td class='$resultClass'>$(if ($result.Result) { "Pass" } else { "Fail" })</td> <td>$($result.Message)</td> </tr>" }) </table> </body> </html> "@ $reportContent | Out-File -FilePath $global:reportPath Write-Host "Report generated and saved to: $global:reportPath" -ForegroundColor Green } # Main program loop Define-Tests do { Show-Menu $choice = Read-Host "`nEnter your choice (1-5)" switch ($choice) { "1" { Run-AllTests } "2" { Run-SpecificTest } "3" { View-TestResults } "4" { Generate-HTMLReport } "5" { Write-Host "Exiting program..." -ForegroundColor Yellow; break } default { Write-Host "Invalid choice. Please try again." -ForegroundColor Red } } if ($choice -ne "5") { Read-Host "`nPress Enter to continue..." } } while ($choice -ne "5")
This PowerShell Tester Tool Template includes:
- A menu-driven interface for easy navigation.
- Functions for defining and running tests.
- Placeholder test case functions that you can replace with your actual test logic.
- Ability to run all tests or a specific test.
- Function to view test results.
- HTML report generation for test results.
Key features:
- Flexible structure for defining multiple test cases
- Easy-to-use menu for running tests and viewing results
- Detailed test result tracking, including test name, description, result, and message
- HTML report generation for easy sharing and documentation of test results
- Modular design for easy expansion and modification
To use this template effectively:
- Replace the placeholder test case functions (Test-Case1, Test-Case2, Test-Case3) with your specific test logic.
- Add more test cases as needed in the Define-Tests function.
- Customize the test result object properties if you need to capture additional information.
- Modify the HTML report generation function if you want to change the report format or add more details.
- Add any necessary module imports or additional global variables at the beginning of the script.
This template provides a solid foundation for creating various types of testing tools in PowerShell, whether you’re testing system configurations, application functionality, or any other scenario that requires structured testing and reporting.