Why use parallel testing?
- Faster Execution: Run multiple tests at the same time to reduce overall test duration.
- Scalability: Easily scale your testing efforts as your test suite grows.
- Dynamic Distribution: Automatically distribute tests across jobs using GitHub’s matrix strategy.
- Cost Efficiency: Optimize resource usage by running tests in parallel.
- Reliability: Avoid failures from going beyond your plan’s max instances.
Setting Up parallel testing with a matrix strategy
1
Ensure your test files are stored in a directory (for example,
testdriver/acceptance/
) and follow a consistent naming convention (for example, login.yaml
, signup.yaml
, etc.).2
Define the GitHub Action Workflow
Here’s an example of a GitHub Action workflow that uses the matrix strategy to run tests in parallel:
Explanation of the workflow
-
gather-test-files
Job:- Collects all test files in the
testdriver/acceptance/
directory. - Uses
basename
to get just the filenames without the full path. - Outputs the list of test files as a JSON array for the matrix strategy.
- Collects all test files in the
-
run-tests
Job:- Uses the matrix strategy to dynamically create a job for each test file.
- Runs each test file in parallel using
npx testdriverai@latest
with the--headless
flag. - The
max-parallel
setting limits the number of tests that can run in parallel to8
(the max instances for the Medium Team Plan).
-
Matrix Strategy:
- The
matrix.test
variable represents each test filename. - Each job runs a single test file from the
testdriver/acceptance/
directory.
- The
-
Environment Variables:
TD_API_KEY
: Your TestDriver API key for authentication.TD_WEBSITE
: The target website URL for testing if its a web application.TD_THIS_FILE
: The current test file being executed, this will also be the title of the test recording.
-
Fail-Fast:
- Set to
false
to ensure all tests run even if one fails.
- Set to
Benefits of using the matrix strategy
- Dynamic Test Distribution: Automatically adapts to the number of test files.
- Scalable: Easily handles large test suites by distributing tests across multiple jobs.
- Efficient Resource Usage: Runs tests in parallel, reducing idle time.
Example output
When this workflow runs:- Each test file in
testdriver/acceptance/
(for example,login.yaml
,signup.yaml
) is executed in its own job. - The results of all tests are displayed in the GitHub Actions dashboard with clear matrix job names.
- Failed tests can be easily identified and debugged individually.
Best practices
- Organize Test Files: Use a consistent naming convention for test files to simplify management.
- Monitor Test Results: Review the GitHub Actions dashboard to identify and debug failing tests.
- Optimize Test Files: Ensure each test file is self-contained and doesn’t depend on the execution of other tests.
- Use Fail-Fast Judiciously: Enable
fail-fast: true
only if you want to stop all tests when one fails.