Sub topic 13
Testing the action in its own repository
This chapter assumes you can write a basic workflow (jobs, steps, uses, run) and have read the earlier chapters on action.yml, inputs, outputs and github.action_path. It uses the greet action from those chapters.
A composite action has no compiler and no unit-test runner. The only way to run it is inside a workflow. So the standard way to test it is a workflow in the same repository that calls the action, then checks what came out.
How a local action test works
A step that uses uses: ./path loads the action from the checked-out workspace. The path is relative to the repository root, not to the workflow file. This leads to the main rule of testing a local action:
- The job must run
actions/checkoutbefore the step that uses the action. Without it, the runner finds noaction.ymland fails with an error saying it can't find the action. - If
action.ymlsits at the repository root, useuses: ./. - The action runs exactly as the checked-out commit defines it, so a pull request tests its own changes before merge.
A first test workflow
Save this as .github/workflows/test-greet.yml. It calls the action twice, once with the default input and once with a custom input. It then compares each output with the value you expect.
name: Test greet action
on:
push:
branches: [main]
pull_request:
jobs:
test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- id: default
uses: ./.github/actions/greet
- name: Check default greeting
shell: bash
env:
ACTUAL: ${{ steps.default.outputs.greeting }}
EXPECTED: Hello, World
run: |
if [ "$ACTUAL" != "$EXPECTED" ]; then
echo "::error::Expected '$EXPECTED' but got '$ACTUAL'"
exit 1
fi
- id: custom
uses: ./.github/actions/greet
with:
who-to-greet: Octocat
- name: Check custom greeting
shell: bash
env:
ACTUAL: ${{ steps.custom.outputs.greeting }}
EXPECTED: Hello, Octocat
run: |
if [ "$ACTUAL" != "$EXPECTED" ]; then
echo "::error::Expected '$EXPECTED' but got '$ACTUAL'"
exit 1
fiSeveral choices here matter:
- Assertions are ordinary
runsteps. A step that exits non-zero fails the job. No test framework is needed. - Actual and expected values go through
env. The same injection rule from the input-safety chapter applies to test code. If you put${{ ... }}directly in the script, a value containing quotes can break the script or change what it does. ::error::creates an annotation. The message shows on the workflow summary and the pull request, not only in the raw log.fail-fast: falselets every operating system finish. One failure then doesn't cancel the other jobs, so you see which platforms break.shell: bashis set on the assertion steps. Bash is available on all three hosted runner images. Thewindows-latestrunner uses Git Bash for this shell. Withoutshell: bash, a Windows job would run the script in PowerShell and theif [ ... ]syntax would fail.
Test on every runner you claim to support
Composite actions run on the runner of the calling job. If your scripts use sed -i, date -d, or readlink -f, the macOS runner may behave differently from Linux, because it ships BSD versions of these tools. A matrix catches this before users do.
If the action only supports Linux, say so in its description and test only ubuntu-latest. Failing early with a clear message is better than a confusing failure inside a script. You can check runner.os in an early step and exit with ::error::.
Test conditional steps
Conditions are easy to get wrong because inputs are strings. For the Node action from the conditionals chapter, run-tests: false (a boolean in the workflow) arrives as the string 'false'. Cover both branches:
- Call the action with
run-tests: "true"against a fixture project whose test passes. - Call it with
run-tests: "false"against a fixture whosenpm testwould fail. The job should still pass, which shows the step was skipped.
Keep fixtures in a directory such as test/fixtures/ and use working-directory on a wrapping step. You can also add a working-directory input to the action.
Test that the action fails when it should
A good test suite checks the failure path as well. Suppose the repository has a second action, .github/actions/require-token/action.yml, which rejects an empty token:
name: Require token
description: Fail when the token input is empty
inputs:
token:
description: A token value
required: false
default: ""
runs:
using: composite
steps:
- shell: bash
env:
TOKEN: ${{ inputs.token }}
run: |
if [ -z "$TOKEN" ]; then
echo "::error::The token input is required"
exit 1
fiTo test the failure, let the step fail without failing the job, then assert that it did fail:
name: Test require-token
on: [pull_request]
jobs:
expect-failure:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- id: no-token
uses: ./.github/actions/require-token
continue-on-error: true
- name: Confirm the action failed
if: ${{ steps.no-token.outcome != 'failure' }}
shell: bash
run: |
echo "::error::Expected the action to fail without a token"
exit 1The step context has two result fields, and they differ when continue-on-error is set:
| Field | Meaning | Value for the step above |
|---|---|---|
| `outcome` | Result before `continue-on-error` is applied | `failure` |
| `conclusion` | Result after `continue-on-error` is applied | `success` |
Check outcome in tests like this one. Checking conclusion would never see the failure.
Test the scripts you bundle
If the action ships scripts under github.action_path, you can test them in two layers:
- Run the script directly in a plain
runstep in the test workflow, with controlled arguments. This is fast and shows the script's own errors. - Run the action that calls it. This confirms the path
${{ github.action_path }}/scripts/check.shresolves, and that the script has the executable permission or is called throughbash, as the earlier example does.
Calling scripts as bash "$path" avoids losing the executable bit, which can happen when files are created on Windows.
Choose triggers that match what you change
pull_requestruns the workflow from the merge result of the pull request, so changes to the action and to the tests are tested together.pushtomaincatches problems introduced by a merge.- A
pathsfilter such as.github/actions/**and the test workflow file itself keeps the matrix from running on unrelated changes. Remember to include the workflow file in the filter, or edits to the tests won't run the tests. workflow_dispatchlets you run the tests by hand when debugging.
Testing the published form
A uses: ./ test runs the action from the same checkout. It doesn't test how another repository would reference it. The reference form owner/repo/path@ref can't be built from an expression, because uses does not accept ${{ }}. You have two options:
- Keep a small second repository, or a scheduled workflow, that calls
owner/repo@v1and runs the same assertions. This catches problems with tags and with files missing from the release. - Before a release, push a branch and reference it literally, for example
uses: owner/repo/.github/actions/greet@my-branch, from a throwaway workflow.
One specific problem this finds: a local test passes because the checkout contains the whole repository, while a remote caller only gets the action's own directory path in the downloaded repository. Scripts that reach outside the action directory using .. can work locally and still fail for other users, so keep everything the action needs inside it.
Common mistakes
- Forgetting
actions/checkout. The error appears at theusesstep and names the missingaction.yml. - Using the wrong relative path.
./greetfails if the action lives in.github/actions/greet. - Missing
idon the action step. Without it, you can't readsteps.<id>.outputs. - Interpolating outputs into scripts. Pass them through
env. - Asserting only the success path. Each input, default and condition deserves at least one case.
- Testing only on Linux while documenting support for all three platforms.
Exercise
In a test workflow, a step that calls an action has continue-on-error: true and the action exits with an error. Which expression in a later step's if correctly detects that the action failed: steps.x.conclusion == 'failure' or steps.x.outcome == 'failure'? Explain why.
Show answer
steps.x.outcome == 'failure'. The outcome field holds the result before continue-on-error is applied, so it is failure. The conclusion field holds the result after it is applied, so it is success, and a check for 'failure' on it would never match.
Exercise
Your test workflow calls uses: ./.github/actions/greet but the job fails immediately with a message that the action can't be found, even though the file exists on the branch. What is the most likely cause and the fix?
Show answer
The job has no checkout step before the action call. Local uses: ./path reads from the runner's workspace, which is empty until actions/checkout runs. Add - uses: actions/checkout@v4 as an earlier step in the same job.