Skip to content

🚀[Feature]: Add support for running setup and teardown scripts during tests #177

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions .github/workflows/Test-ModuleLocal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ jobs:
"name=$name" >> $env:GITHUB_OUTPUT
"path=$path" >> $env:GITHUB_OUTPUT

- name: Run BeforeAll Setup Script
shell: pwsh
working-directory: ${{ inputs.WorkingDirectory }}
env:
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
GITHUB_TOKEN: ${{ github.token }}
run: |
$testPath = '${{ inputs.TestPath }}'
$beforeAllScript = Join-Path $testPath "BeforeAll.ps1"
if (Test-Path $beforeAllScript) {
Write-Host "Running BeforeAll setup script: $beforeAllScript"
& $beforeAllScript
} else {
Write-Host "No BeforeAll.ps1 script found in $testPath"
}

- name: Test-ModuleLocal
uses: PSModule/Invoke-Pester@v4
env:
Expand Down Expand Up @@ -133,3 +155,26 @@ jobs:
Prescript: | # This is to speed up module loading in Pester.
Install-PSResource -Repository PSGallery -TrustRepository -Name PSCustomObject
Import-Module -Name '${{ steps.import-module.outputs.name }}' -RequiredVersion 999.0.0

- name: Run AfterAll Teardown Script
if: always()
shell: pwsh
working-directory: ${{ inputs.WorkingDirectory }}
env:
TEST_APP_ENT_CLIENT_ID: ${{ secrets.TEST_APP_ENT_CLIENT_ID }}
TEST_APP_ENT_PRIVATE_KEY: ${{ secrets.TEST_APP_ENT_PRIVATE_KEY }}
TEST_APP_ORG_CLIENT_ID: ${{ secrets.TEST_APP_ORG_CLIENT_ID }}
TEST_APP_ORG_PRIVATE_KEY: ${{ secrets.TEST_APP_ORG_PRIVATE_KEY }}
TEST_USER_ORG_FG_PAT: ${{ secrets.TEST_USER_ORG_FG_PAT }}
TEST_USER_USER_FG_PAT: ${{ secrets.TEST_USER_USER_FG_PAT }}
TEST_USER_PAT: ${{ secrets.TEST_USER_PAT }}
GITHUB_TOKEN: ${{ github.token }}
run: |
$testPath = '${{ inputs.TestPath }}'
$afterAllScript = Join-Path $testPath "AfterAll.ps1"
if (Test-Path $afterAllScript) {
Write-Host "Running AfterAll teardown script: $afterAllScript"
& $afterAllScript
} else {
Write-Host "No AfterAll.ps1 script found in $testPath"
}
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ Depending on the labels in the pull requests, the workflow will result in differ
- This produces a json based report that is used to later evaluate the results of the tests.
- [Test module](./.github/workflows/Test-ModuleLocal.yml)
- Import and tests the module in parallel (matrix) using Pester tests from the module repository.
- Supports setup and teardown scripts:
- **BeforeAll.ps1**: Runs before each test execution to set up test environment (e.g., deploy infrastructure, download test data)
- **AfterAll.ps1**: Runs after each test execution to clean up test environment (e.g., remove test resources, cleanup databases)
- Setup/teardown scripts are automatically detected in test directories and executed with the same environment variables as tests
- This produces a json based report that is used to later evaluate the results of the tests.
- [Get test results](./.github/workflows/Get-TestResults.yml)
- Gathers the test results from the previous steps and creates a summary of the results.
Expand Down Expand Up @@ -257,6 +261,46 @@ Build:
| `Verbose` | `boolean` | Whether to enable verbose output. | `false` | `false` |
| `WorkingDirectory` | `string` | The path to the root of the repo. | `false` | `.` |

### Setup and Teardown Scripts

The workflow supports automatic execution of setup and teardown scripts for module tests:

#### BeforeAll.ps1
- **Location**: Place in your test directories (e.g., `tests/BeforeAll.ps1`)
- **Purpose**: Runs before each test execution to prepare the test environment
- **Use cases**: Deploy test infrastructure, download test data, initialize databases, configure services
- **Environment**: Has access to the same environment variables as your tests (secrets, GitHub token, etc.)

#### AfterAll.ps1
- **Location**: Place in your test directories (e.g., `tests/AfterAll.ps1`)
- **Purpose**: Runs after each test execution to clean up the test environment
- **Use cases**: Remove test resources, cleanup databases, stop services, upload artifacts
- **Environment**: Has access to the same environment variables as your tests

**Example BeforeAll.ps1:**
```powershell
Write-Host "Setting up test environment..."
# Deploy test infrastructure
# Download test data
# Initialize test databases
Write-Host "Test environment ready!"
```

**Example AfterAll.ps1:**
```powershell
Write-Host "Cleaning up test environment..."
# Remove test resources
# Cleanup databases
# Stop services
Write-Host "Cleanup completed!"
```

**Notes:**
- Scripts are automatically detected and executed if present
- Each unique test directory path is processed only once
- Scripts run with PowerShell and have access to PSModuleHelpers
- If no scripts are found, the workflow continues normally

### Secrets

The following secrets are used by the workflow. They can be automatically provided (if available) by setting the `secrets: inherit`
Expand Down
12 changes: 12 additions & 0 deletions tests/srcTestRepo/tests/AfterAll.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write-Host "=== AFTERALL TEARDOWN SCRIPT EXECUTING ==="
Write-Host "Tearing down test environment..."

# Example teardown tasks:
# - Clean up test infrastructure
# - Remove test data
# - Cleanup test environment
# - Drop test databases
# - Stop test services

Write-Host "Test environment teardown completed successfully!"
Write-Host "=== AFTERALL TEARDOWN SCRIPT COMPLETED ==="
12 changes: 12 additions & 0 deletions tests/srcTestRepo/tests/BeforeAll.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Write-Host "=== BEFOREALL SETUP SCRIPT EXECUTING ==="
Write-Host "Setting up test environment..."

# Example setup tasks:
# - Deploy test infrastructure
# - Download test data
# - Initialize test environment
# - Set up test databases
# - Configure test services

Write-Host "Test environment setup completed successfully!"
Write-Host "=== BEFOREALL SETUP SCRIPT COMPLETED ==="
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Write-Host "=== AFTERALL TEARDOWN SCRIPT (Environments) EXECUTING ==="
Write-Host "Tearing down test environment for Environments..."

# Example teardown tasks for Environments directory:
Write-Host "Cleaning up Environments test environment..."

Write-Host "Environments environment teardown completed successfully!"
Write-Host "=== AFTERALL TEARDOWN SCRIPT (Environments) COMPLETED ==="
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Write-Host "=== BEFOREALL SETUP SCRIPT (Environments) EXECUTING ==="
Write-Host "Setting up test environment for Environments..."

# Example setup tasks for Environments directory:
Write-Host "Initializing Environments test environment..."

Write-Host "Environments environment setup completed successfully!"
Write-Host "=== BEFOREALL SETUP SCRIPT (Environments) COMPLETED ==="
8 changes: 8 additions & 0 deletions tests/srcWithManifestTestRepo/tests/MyTests/AfterAll.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Write-Host "=== AFTERALL TEARDOWN SCRIPT (MyTests) EXECUTING ==="
Write-Host "Tearing down test environment for MyTests..."

# Example teardown tasks for MyTests directory:
Write-Host "Cleaning up MyTests test environment..."

Write-Host "MyTests environment teardown completed successfully!"
Write-Host "=== AFTERALL TEARDOWN SCRIPT (MyTests) COMPLETED ==="
8 changes: 8 additions & 0 deletions tests/srcWithManifestTestRepo/tests/MyTests/BeforeAll.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Write-Host "=== BEFOREALL SETUP SCRIPT (MyTests) EXECUTING ==="
Write-Host "Setting up test environment for MyTests..."

# Example setup tasks for MyTests directory:
Write-Host "Initializing MyTests test environment..."

Write-Host "MyTests environment setup completed successfully!"
Write-Host "=== BEFOREALL SETUP SCRIPT (MyTests) COMPLETED ==="