Setup & Teardown
How to handle pre and post test tasks.
Overview
Prerun scripts are commands executed on a TestDriver virtual machine (VM) before each test in a CI/CD pipeline. They are used to prepare the environment by provisioning the VM, installing dependencies, configuring settings, or building the application. This ensures a consistent and reproducible environment for every test execution.
By using prerun scripts, you can:
- Speed up test setup.
- Prevent test failures caused by inconsistent environments.
- Promote reproducible builds for reliable test results.
Use cases
Prerun scripts are ideal for:
- Installing necessary dependencies (for example, browsers, libraries, or tools).
- Building your application or running setup scripts.
- Configuring the VM to match specific test requirements.
- Preparing staging environments or accessing private resources.
Example: Installing Arc Browser
The following example demonstrates how to use a prerun script within the GitHub Actions workflow folder to download and install the Arc Browser on a Windows VM before running tests.
v5
onward.Example workflow with advanced setup and teardown
Workflow file
Workflow overview
-
Setup Tasks:
- Use a dedicated action before the TestDriver action to prepare the environment (for example, create a test user via an API).
- Pass the created user credentials to the TestDriver action using environment variables.
-
Run Tests:
- Execute the tests using TestDriver, leveraging the setup data (for example, the test user).
-
Teardown Tasks:
- Use a dedicated action after the TestDriver action to clean up (for example, delete the test user).
- Ensure the teardown step runs no matter the result of the TestDriver action.
Workflow steps explained
Setup test user
- Purpose: Create a test user via an API before running the tests.
- How It Works:
- The
Setup Test User
step sends aPOST
request to the API to create a new user. - The user ID, email, and password are extracted from the API response and stored as environment variables (
USER_ID
,USER_EMAIL
,USER_PASSWORD
).
- The
- Example Output:
USER_ID
:12345
USER_EMAIL
:[email protected]
USER_PASSWORD
:password123
Run tests with TestDriver
- Purpose: Execute tests using the TestDriver action.
- How It Works:
- The
USER_EMAIL
andUSER_PASSWORD
environment variables are passed to theprerun
script. - The test prompts use these credentials to log in and perform actions.
- The
Teardown test user
- Purpose: Delete the test user via an API after the tests are complete.
- How It Works:
- The
Teardown Test User
step sends aDELETE
request to the API to remove the test user. - The
if: always()
condition ensures this step runs even if the TestDriver action fails.
- The
Best practices for setup and teardown
-
Use APIs for Setup and Teardown:
- Use APIs to create and delete test data dynamically, ensuring a clean environment for each test run.
-
Pass Data via Environment Variables:
- Store setup data (for example, user credentials) in environment variables and pass them to the TestDriver action.
-
Ensure Teardown Always Runs:
- Use
if: always()
to ensure teardown tasks are executed regardless of the test results.
- Use
-
Log Setup and Teardown Steps:
- Add
echo
statements to log the progress of setup and teardown tasks for easier debugging.
- Add
-
Test Locally:
- Verify setup and teardown scripts locally before integrating them into the workflow.
Advanced configuration
If you need to perform more complex setup or teardown tasks, you can use the lifecycle
folder in your repository. This folder can contain multiple files, each with a specific purpose.
Example use cases
1. User management
- Create a test user during setup.
- Delete the test user during teardown.
2. Database operations
- Insert test data into a database during setup.
- Remove the test data during teardown.
3. Mock services
- Start a mock API server during setup.
- Stop the mock server during teardown.
Conclusion
By structuring your GitHub Actions workflow to handle setup before the TestDriver action and teardown after it, you can ensure a clean and reliable test environment for every run. This approach also ensures that teardown tasks are executed regardless of the test results, maintaining a consistent state for subsequent runs.