Skip to content

🩹 [Patch]: Consolidate test setup/teardown tasks #534

@MariusStorhaug

Description

@MariusStorhaug

Describe the change

Now that Process-PSModule supports BeforeAll and AfterAll scripts, that run once before and after all the parallel tests, we should move some of the setup/teardown tasks to these. This will hopefully speed up the testing process, and result in fewer failures due to ratelimiting.

Current Test Structure

Each test file currently performs repeated setup/teardown in individual Context blocks:

  • Creates unique test resources (repos, teams, orgs) per Context
  • Connects/disconnects GitHub accounts multiple times
  • Cleans up resources after each Context

Proposed Changes

tests/BeforeAll.ps1 - Infrastructure Setup

Create shared test infrastructure that ALL tests can use:

  • Test Organizations - Pre-create test organizations for different auth scenarios
  • Test Repositories - Pre-create base repositories for testing
  • Test Environments - Pre-create deployment environments
  • Test Teams - Pre-create teams with different permission levels
  • Cleanup old artifacts - Remove stale resources from previous failed runs

Important: This does NOT replace the BeforeAll blocks in test files, which are still needed for:

  • Authentication (Connect-GitHubAccount, Connect-GitHubApp)
  • Context-specific setup
  • Loading test data (AuthCases.ps1)

tests/AfterAll.ps1 - Infrastructure Teardown

Clean up all shared test infrastructure:

  • Remove all test organizations created in BeforeAll
  • Remove all test repositories
  • Remove all test teams and environments
  • Generate cleanup report

Test Files - Keep Existing BeforeAll/AfterAll Blocks

Test file Context blocks should continue to:

  • BeforeAll: Connect to GitHub with appropriate credentials (this stages the runner)
  • AfterAll: Disconnect GitHub sessions
  • Use the pre-created infrastructure instead of creating their own

Benefits

  1. Speed: Infrastructure created once instead of repeatedly per Context
  2. Rate Limiting: Significantly fewer API calls during test execution
  3. Reliability: Consistent test infrastructure across all test runs
  4. Cost: Fewer parallel operations hitting API limits
  5. Maintainability: Centralized infrastructure definitions

Implementation Strategy

  1. Analyze current test patterns to identify common infrastructure needs
  2. Create tests/BeforeAll.ps1 with infrastructure provisioning
  3. Create tests/AfterAll.ps1 with cleanup logic
  4. Update test files to consume shared infrastructure instead of creating their own
  5. Maintain existing authentication logic in test file BeforeAll/AfterAll blocks

Test Files Affected

All 15 test files will need updates to use shared infrastructure:

  • Apps.Tests.ps1
  • Artifacts.Tests.ps1
  • Emojis.Tests.ps1
  • Enterprise.Tests.ps1
  • Environments.Tests.ps1
  • GitHub.Tests.ps1
  • GitHubFormatter.Tests.ps1
  • Organizations.Tests.ps1
  • Permissions.Tests.ps1
  • Releases.Tests.ps1
  • Repositories.Tests.ps1
  • Secrets.Tests.ps1
  • Teams.Tests.ps1
  • Users.Tests.ps1
  • Variables.Tests.ps1

Metadata

Metadata

Labels

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions