QA AND TEST AUTOMATION · PART 8 · CI/CD AND TEST INFRASTRUCTURE · CI · 74 OF 112

Running Tests in GitHub Actions, Step by Step

A GitHub Actions workflow for ShopRung's tests, line by line: triggers for pull requests, main, nightly and manual runs, jobs and steps, starting the app inside the job and waiting for its health check, caching, an API job and a UI job with a browser matrix, secrets, artifacts kept on failure, and protecting the main branch with required checks. The workflow passes actionlint.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 67
  63. 68
  64. 69
  65. 70
  66. 71
  67. 72
  68. 73
  69. 74
  70. 75
  71. 76
  72. 77
  73. 78
  74. 79
  75. 80
  76. 81
  77. 82
  78. 83
  79. 84
  80. 85
  81. 86
  82. 87
  83. 88
  84. 89
  85. 90
  86. 91
  87. 92
  88. 93
  89. 94
  90. 95
  91. 96
  92. 97
  93. 98
  94. 99
  95. 100
  96. 101
  97. 102
  98. 103
  99. 104
  100. 105
  101. 106
  102. 107
  103. 108
  104. 109
  105. 110
  106. 111
  107. 112
1 / 12

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Triggers
  • jobs and steps
  • start the app, wait on health
  • cache
  • matrix
  • secrets
  • artifacts
  • required checks

Notes

Triggers

The file lives in dot github slash workflows. The on section lists triggers: every pull request, every push to main, a nightly schedule written as cron, here two seventeen in the morning UTC, and workflow dispatch, which adds a run button in the GitHub interface. Then come jobs. Each job runs on a fresh virtual machine, so nothing leaks between runs.

.github/workflows/tests.yml
name: tests
on:
  pull_request:
  push:
    branches: [main]
  schedule:
    - cron: "17 2 * * *"  # nightly
  workflow_dispatch:  # a Run button

jobs:
  api: ...

Start the app

The API job runs on Ubuntu with a fifteen minute timeout, so a hung test cannot burn hours. Steps run in order. Checkout fetches the code. Setup node installs Node twenty two. Then ShopRung starts in the background, with its output saved to a log file, and wait on polls the health endpoint until the shop answers, up to a minute. Never use a fixed sleep here; wait for the health check.

Job: api, first steps
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
  - uses: actions/checkout@v4
  - uses: actions/setup-node@v4
    with: { node-version: 22 }
  - name: Start ShopRung
    working-directory: shoprung
    run: |
      npm start > shoprung.log 2>&1 &
      npx --yes wait-on@8 http-get://...

Run the suite

Next, setup python installs Python three point twelve, and its cache option keeps downloaded packages between runs, which saves time on every pull request. The API tests step installs the pinned requirements and runs pytest with a JUnit XML report. If any test fails, pytest exits with an error, the step fails, the job fails, and GitHub marks the check red.

Job: api, the tests
  - uses: actions/setup-python@v5
    with:
      python-version: "3.12"
      cache: pip
  - name: API tests
    working-directory: api-tests
    run: |
      pip install -r requirements.txt
      pytest --junitxml=results/api.xml

Artifacts

A failed step normally stops the job, so the upload step says if always: run it whatever happened. Upload artifact stores the JUnit report and ShopRung's own log with the run, where anyone can download them. The application's log is often the fastest way to understand a failing API test: it shows the error from the server side.

Upload results
  - name: Keep results and logs
    if: always()
    uses: actions/upload-artifact@v4
    with:
      name: api-results
      path: |
        api-tests/results/
        shoprung/shoprung.log

Matrix

The UI job needs the API job: it only starts if the API tests passed, which saves expensive browser minutes when the basics are broken. A matrix runs the same job once per browser, Chromium and Firefox, in parallel. Fail fast false keeps the other browser running when one fails, so you see the full picture. Each copy installs its browser with its system libraries, then runs only that browser's Playwright project.

Job: ui
ui:
  needs: api
  strategy:
    fail-fast: false
    matrix:
      browser: [chromium, firefox]
  steps: ... start ShopRung ...
    - run: npx playwright install
        --with-deps ${{ matrix.browser }}
    - run: npx playwright test
        --project=${{ matrix.browser }}

Secrets

Real credentials never go in the file. They are stored as repository secrets in GitHub's settings and passed to a step as environment variables; GitHub masks them in logs. ShopRung's practice passwords are public, but the habit matters. On failure, the Playwright report with its traces is uploaded per browser and kept for fourteen days.

Secrets and failure artifacts
    - run: npx playwright test ...
      env:
        ALICE_PASSWORD:
          ${{ secrets.ALICE_PASSWORD }}
    - if: failure()
      uses: actions/upload-artifact@v4
      with:
        name: playwright-report-
          ${{ matrix.browser }}
        path: e2e/playwright-report/
        retention-days: 14

Block bad merges

A workflow only reports. To stop broken code, protect the main branch in the repository settings: require pull requests, and require the status checks to pass, here the API job and both UI matrix jobs. Then a red check blocks the merge button. This is where tests turn into a quality gate, which a later episode covers in depth.

Repository settings
  • branch protection (or rulesets) on main: require a pull request require status checks: api, ui (chromium), ui (firefox) require branches up to date
  • red check → the Merge button is blocked

Check it before pushing

A few habits. Run actionlint on workflow files before pushing; it catches syntax and expression mistakes, and the workflow in this episode passes it. Pin action versions. Cache dependencies. Put a timeout on every job. And keep slow suites for the nightly run by checking the event name in an if condition, so pull requests stay fast.

Good habits
  • actionlint: checks syntax and expressions before you push (this file passes it)
  • pin action versions (@v4)
  • cache dependencies
  • timeouts on every job
  • run the slow nightly suite only on schedule: if: github.event_name == 'schedule'

Common mistakes

Mistakes. Sleep thirty instead of waiting for a health check. Secrets written into the YAML. Uploading artifacts only when everything passed, which is exactly when nobody needs them. No timeout, so a hung test runs for hours. Every suite on every pull request. And checks that are not required, so red pull requests can still be merged.

Avoid these
  • sleep 30 instead of a health check
  • secrets in the YAML
  • artifacts only on success
  • no timeout (hung job, hours)
  • every suite on every PR
  • checks not required, so red PRs still merge

Say it like this

In the interview: a workflow triggered by pull requests, pushes to main and a nightly schedule. Each job checks out the code, sets up Node or Python with caching, starts the app and waits for its health check, runs the suite with a JUnit report, and uploads results, traces and the app's logs even on failure. UI tests use a browser matrix, secrets come from repository settings, and branch protection makes the checks required.

In the interview
  • "A workflow on PRs, main and nightly. Each job checks out, sets up Node or Python with caching, starts the app and waits on its health check, runs
  • the suite with a JUnit report, and uploads results, traces and app logs with if: always(). UI
  • uses a browser matrix; secrets
  • from settings; checks required."

Interview questions

Q1Where do GitHub Actions workflows live?

YAML files in the .github/workflows/ folder of the repository, one file per workflow.

Q2How do you start the application under test in a workflow?

Start it in the background in a step (for example npm start &) and wait for its health endpoint with a tool like wait-on, not a fixed sleep.

Q3How do you keep artifacts when tests fail?

Use actions/upload-artifact in a step with if: always() or if: failure().

Q4What is a matrix in GitHub Actions?

A strategy that runs the same job once per combination of values, such as browsers or Node versions, in parallel.

Q5How do you pass secrets to tests?

Store them as repository or environment secrets and map them to environment variables with ${{ secrets.NAME }}; they are masked in logs.

Q6How do you make tests block merging?

Enable branch protection or rulesets on main and mark the workflow jobs as required status checks.

Q7How do you speed up workflows?

Cache dependencies, run jobs in parallel (matrix, sharding), order cheap jobs first with needs, and move slow suites to scheduled runs.

Q8How do you validate a workflow file?

With actionlint, which checks YAML syntax, expressions and known action inputs before pushing.