Skip to content

Commit 2fb6675

Browse files
📖 [Docs]: Add best practices for shared test infrastructure (#285)
The README now includes actionable guidance on how to structure `BeforeAll.ps1` and `AfterAll.ps1` scripts for modules that run integration tests across multiple OS runners. This helps module authors avoid common pitfalls like rate limits, naming collisions, and stale resources from failed runs. - Fixes #286 ## Best practices for shared test infrastructure A new section under "Setup and Teardown Scripts" covers four key patterns: - **Deterministic naming with `$env:GITHUB_RUN_ID`** — use the stable run ID for resource names instead of `[guid]::NewGuid()` or `Get-Random`, so test files on different runners can reference shared resources by name. - **Stale resource cleanup** — remove leftover resources matching the naming prefix before creating new ones, to handle incomplete teardown from previous failed runs. - **Tests reference, don't create** — test files should fetch shared resources by their deterministic name rather than provisioning their own. - **Naming conventions** — a table of recommended patterns for shared resources, secrets, variables, and environments, including guidance for multi-context scenarios.
1 parent 60bdf8a commit 2fb6675

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,70 @@ Write-Host "Cleaning up test environment..."
205205
Write-Host "Cleanup completed!"
206206
```
207207

208+
##### Best practices for shared test infrastructure
209+
210+
Tests run in parallel across multiple OS runners. To avoid rate limits or conflicts from excessive resource creation,
211+
provision shared infrastructure once in `BeforeAll.ps1` and tear it down in `AfterAll.ps1`. Individual test files
212+
should consume the shared infrastructure instead of creating their own.
213+
214+
###### Use deterministic naming with `$env:GITHUB_RUN_ID`
215+
216+
Use `$env:GITHUB_RUN_ID` (stable per workflow run, shared across OS runners) to build deterministic resource names.
217+
This lets test files reference shared resources by name without passing state between jobs.
218+
219+
```powershell
220+
# BeforeAll.ps1
221+
$os = $env:RUNNER_OS
222+
$id = $env:GITHUB_RUN_ID
223+
$resourceName = "Test-$os-$id"
224+
```
225+
226+
Do **not** use `[guid]::NewGuid()` or `Get-Random` for shared resource names — these produce different values on
227+
each runner and cannot be referenced by other jobs.
228+
229+
###### Clean up stale resources from previous failed runs
230+
231+
If a previous workflow run failed before teardown completed, stale resources may remain. Start `BeforeAll.ps1` by
232+
removing any resources matching your naming prefix before creating new ones:
233+
234+
```powershell
235+
# Remove stale resources from previous failed runs
236+
Get-Resources -Filter "Test-$os-*" | Remove-Resource
237+
238+
# Create fresh shared resources
239+
New-Resource -Name "Test-$os-$id"
240+
```
241+
242+
###### Tests reference shared resources — they do not create them
243+
244+
Test files should fetch the shared resource by its deterministic name, not create new resources:
245+
246+
```powershell
247+
# Inside a test file
248+
BeforeAll {
249+
$os = $env:RUNNER_OS
250+
$id = $env:GITHUB_RUN_ID
251+
$resource = Get-Resource -Name "Test-$os-$id"
252+
}
253+
```
254+
255+
Test-specific ephemeral resources (for example, secrets, variables, or temporary items) can still be created and
256+
cleaned up within each test file. Only long-lived or expensive resources should be shared.
257+
258+
###### Naming conventions
259+
260+
Use a consistent naming scheme so that resources are easy to identify and clean up. A recommended pattern:
261+
262+
| Resource | Pattern | Example |
263+
|-------------------|---------------------------------------|----------------------------|
264+
| Shared resource | `Test-{OS}-{RunID}` | `Test-Linux-1234` |
265+
| Extra resource | `Test-{OS}-{RunID}-{N}` | `Test-Linux-1234-1` |
266+
| Secret / variable | `{TestName}_{OS}_{RunID}` | `Secrets_Linux_1234` |
267+
| Environment | `{TestName}-{OS}-{RunID}` | `Secrets-Linux-1234` |
268+
269+
When tests use multiple authentication contexts that share the same runner, include a token or context identifier in
270+
the name to avoid collisions (for example, `Test-{OS}-{ContextID}-{RunID}`).
271+
208272

209273
#### Module tests
210274

0 commit comments

Comments
 (0)