Reports and CI with GitHub Actions, with cost control

Study time
≈ 3 h (estimate)
Needs
L1-M07-L03
Updated
2026-11-06

Progress is saved in this browser only. No account, nothing is sent to a server.

Draft · reviewed by AI, not yet by a person. Content may change. English translation: AI-assisted, not human-reviewed.
On this page

Module: L1-M07 · Duration: ~165 min (estimate, including the independent exercise) · Previous: L03 · Next: L05 - Using AI tools in QA work safely (coding assistants, MCP)

1. Introduction

A test suite is valuable only when it runs automatically on every change and leaves a result that anyone can read. For an LLM feature there is one more question: which tests run automatically, and which ones must never run automatically because they cost money?

This lesson teaches how to produce a JUnit report and a readable summary, then how to write a GitHub Actions workflow with three tiers: offline tests on every push, UI tests on every push, and live tests only when someone clicks run and the run is approved.

The lesson's limit: the lab's workflow has had its structure checked by tests, and its commands have been run on a local machine. The course team has not run it on GitHub. Only GitHub can run it.

2. Learning objectives

  1. Produce a JUnit XML report and summarise it as Markdown, failing closed when the report is missing or empty.
  2. Read and write a GitHub Actions workflow with on, jobs, needs, if, env, concurrency.
  3. Design multi-tier CI for an LLM feature: offline runs automatically, live runs by hand, with approval and a budget.
  4. Use secrets correctly and explain the limits of masking secrets in logs.
  5. Check a workflow with tests, and distinguish that from running it.

3. Prerequisites and tools

  • L01-L03; L1-M03-L05 (multi-layer CI strategy, first version).
  • Basic YAML: indentation with spaces, key: value, lists with -.

4. Concepts

4.1 JUnit reports

pytest has the option --junit-xml=PATH (alias --junitxml) to "Create junit-xml style report file at given path" [R1]. Most CI systems can read this format. report.py (or python -m aiqa.reporting) turns it into a Markdown table, and fails closed: a report that does not exist, is not XML, or contains no tests is an error (exit code 2). Otherwise a "summary" step could show PASS for a run in which no test was run.

Everyday example. A form teacher receives the class's summary sheet of marks. If the sheet is empty, the right conclusion is "nobody has taken the exam yet", not "the whole class passed". A summary step that fails closed is the teacher who hands the empty sheet back instead of signing it.

Worked example: a real report and an empty one. From the lab folder:

python -m pytest -q tests/unit/test_reporting.py --junitxml=reports/reporting.xml
python report.py reports/reporting.xml
python -m pytest -q tests/unit -k khong_co_test_nao --junitxml=reports/empty.xml
python report.py reports/empty.xml

Output (excerpt, run times removed; copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-06; both versions gave identical output):

8 passed
| Total | Passed | Failed | Errors | Skipped |
|---|---|---|---|---|
| 8 | 8 | 0 | 0 | 0 |
44 deselected
report error: reports/empty.xml contains no test cases
  1. The first run has 8 tests; the summary counts exactly 8 and reports PASS (exit code 0). The block above leaves out the Markdown heading that report.py prints just before the table, ## Test report: PASS.
  2. The second run uses -k with a word that matches no test: 44 deselected, pytest ends with exit code 5 ("no tests collected") but still writes a report file, one that holds no test.
  3. report.py rejects that report with exit code 2. On CI, the summary step turns red, just as scenario 2 in §7 needs. (See the exit code with echo $? on macOS/Linux, echo $LASTEXITCODE in PowerShell.)

4.2 GitHub Actions workflows

A workflow is a YAML file in .github/workflows/ at the root of the repository. The lab's file is at ci/github-actions.yml for you to read; to use it, copy it to that location.

YAML key Meaning (according to the GitHub documentation)
on: push Runs "when a push is made to any branch in the workflow's repository" [R2]. The lab limits it to main, because pull requests already have their own trigger; otherwise every commit of a pull request would run twice
workflow_dispatch Allows manual runs; the "Run workflow" button is present "if the workflow file exists on the default branch" [R3]
concurrency "only a single job or workflow using the same concurrency group will run at a time" [R2]
jobs.<id>.environment The job references an environment, where reviewers can be set [R2][R4]
permissions The permissions of the automatic token; the lab grants only contents: read

Good vs bad.

Weak trigger Good trigger
on: [push, pull_request]: every commit of a pull request runs twice push for main only, plus pull_request: every commit runs once

Why: a duplicate run catches no extra bug, it only doubles the machine time, and for a job that costs money it doubles the bill.

Worked example: the cron time. The schedule trigger uses a cron expression in UTC. To run at 8:30 in the morning, Vietnam time (UTC+7):

  1. Subtract 7 hours: 8:30 becomes 1:30 UTC, still the same day.
  2. A cron expression has 5 fields: minute, hour, day of the month, month, day of the week. Result: 30 1 * * *.
  3. If the subtraction gives a negative number, add 24 hours and go back one day. The exercise in §9.1 is exactly that case.

4.3 Three tiers

Job When Mode Cost
unit Push to main and every pull request AIQA_MODE=mock 0
ui After unit mock; Playwright installs the browser with python -m playwright install --with-deps chromium, as in Playwright's CI documentation [R5] 0 (machine time)
live Only on a manual run and with run_live selected AIQA_MODE=live, AIQA_MAX_USD=0.20 for the whole run Capped

The live job has three safeguards: the if condition (only workflow_dispatch with run_live); the live-api environment, where you configure reviewers ("Use required reviewers to require a specific person or team to approve workflow jobs that reference the environment" [R4]); and the framework's own budget guard (L02). On top of that comes the spend limit in the Claude Console (L1-M03-L01).

Everyday example. A restaurant lets its cooks taste every dish, every day, for free (unit, ui). Inviting a food critic to judge costs money, so it happens only when the manager approves it, and with a spending cap (live).

Worked example: the most a live run can cost. The two live tests are the two [live] parameters of the contract tests (L02), at the prices of claude-opus-5-5: $4 per million input tokens, $20 per million output tokens.

  1. test_contract_returns_a_chat_result sends "Xin chào" (8 characters) with max_tokens=50: 8 × 4 + 50 × 20 = 1032 millionths of a dollar, that is $0.001032.
  2. test_contract_reply_is_a_string_for_vietnamese_input sends a 29-character sentence with max_tokens=80: 29 × 4 + 80 × 20 = 1716 millionths of a dollar, that is $0.001716.
  3. The worst-case total is about $0.0027, far below the job's $0.20 budget. The budget is reached only when someone adds many live tests or raises max_tokens, and then the client stops before the call that would exceed it (L02 §5.1).

4.4 Secrets

  • A secret is used with the syntax ${{ secrets.NAME }} [R6].
  • "With the exception of GITHUB_TOKEN, secrets are not passed to the runner when a workflow is triggered from a forked repository" [R6]: a pull request from a fork cannot get your API key.
  • GitHub "automatically redacts the contents of all GitHub secrets that are printed to workflow logs", but also says "this redaction is not guaranteed" [R7]. Never print a secret, even when you believe it will be masked.
  • In the lab's workflow, only the pytest step of the live job sees the secret: the pip install step does not, so the install script of a library cannot read the API key. A test checks this.

Good vs bad. A secret set at workflow level:

env:                                   # workflow level: every job, every step
  ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}

compared with the lab's way: only the Live contract tests step has env: ANTHROPIC_API_KEY. In the first way, the unit job, the ui job and every pip install step all see the key; in the lab's way, only the one step that needs the key sees it.

4.5 Reports are always kept

The artifact upload step has if: ${{ !cancelled() }}: Playwright's reports and traces are saved even when tests fail, exactly when you need them most. The summary is written to $GITHUB_STEP_SUMMARY; GitHub uses job summaries to display content "such as test result summaries" [R8].

Everyday example. A plane's black box must record even when the flight goes wrong; a black box that records only smooth flights is useless. if: ${{ !cancelled() }} is the condition that keeps CI's "black box" (reports and traces) even when tests fail.

4.6 Testing the workflow's structure

tests/unit/test_ci_workflow.py reads the workflow with PyYAML and checks: the triggers, read-only permissions, that the offline jobs are never in live mode and never see a secret, that the live job has its condition, environment and budget, that every job writes a report and summarises it, that the unit and ui jobs keep their reports even on failure (the live job has no upload step), and that action versions are pinned by major version.

A YAML detail worth remembering: PyYAML reads the on key as the boolean value True (a YAML 1.1 rule). The lab's test handles this with wf.get("on", wf.get(True)).

Worked example. Save as vd_l04.py in the lab folder and run python vd_l04.py:

import yaml

print(yaml.safe_load("on: push"))
print(yaml.safe_load('"on": push'))

Output (copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-06; both versions gave identical output):

{True: 'push'}
{'on': 'push'}

The unquoted key on becomes True; quoted, it stays the string 'on'. GitHub reads both spellings, so the test must accept both: that is the reason for wf.get("on", wf.get(True)).

5. Technical deep dive

5.1 Rerunning the CI commands locally

Before pushing, run exactly the commands that the workflow runs:

python -m pytest tests/unit --junitxml=reports/unit.xml
python report.py reports/unit.xml
python -m pytest tests/ui -m ui --tracing=retain-on-failure --junitxml=reports/ui.xml
python report.py reports/ui.xml

This catches most workflow bugs (wrong paths, missing libraries). It does not catch GitHub-specific errors (expression syntax, permissions, secrets not configured). Only a real run on GitHub can prove those.

5.2 Action versions

The workflow uses actions/checkout@v7, actions/setup-python@v7, actions/upload-artifact@v7, the latest major versions as of 2026-10-31. The example in Playwright's CI documentation uses older major versions [R5]; when you copy an example from documentation, check the current version of each action.

6. The QA perspective

  • What to check in a workflow: what runs automatically; what costs money and who allows it to run; where secrets go; whether reports are kept on failure; whether a run without any test is treated as green.
  • What can go wrong: the live job runs on every push; secrets at workflow level (every job sees them); reports uploaded only on success; a summary step that is green when the report is empty; a workflow "checked" only by eye.
  • Reporting: attach a link to the run, the JUnit report and the trace of the failing test.

7. Real-world scenarios

Scenario 1 (fictional): the end-of-month bill. A team sets the live tests to run on every push "to be sure". One week has many pull requests, and the API bill is ten times the forecast. Splitting the live job out, running it only by hand and with a budget, solves the problem without losing the check.

Scenario 2 (fictional): green because nothing ran. After a test directory is renamed, CI stays green for days. pytest finds no tests, and the summary step does not check for that. A summary step that fails closed when the report has no tests would turn red on the very first run.

8. Hands-on tutorial

8.1 Running the demo

python demo.py l04

Output (copied from a real run, 2026-10-31):

triggers: ['pull_request', 'push', 'workflow_dispatch']
job unit  needs=-     if=- env.AIQA_MODE=mock
job ui    needs=unit  if=- env.AIQA_MODE=mock
job live  needs=unit  if=${{ github.event_name == 'workflow_dispatch' && inputs.run_live }} env.AIQA_MODE=live
## Sample report: FAIL

| Total | Passed | Failed | Errors | Skipped |
|---|---|---|---|---|
| 4 | 2 | 1 | 0 | 1 |

### Failures

- `tests.ui.test_chat_ui::test_model_output_is_rendered_as_text_not_html[chromium]`: AssertionError: Locator expected to have count '0'

The first part is the structure of the workflow: three triggers, three jobs; only live has a condition and live mode. The second part is the Markdown summary of a sample report (ci/sample-junit.xml) with one failing test and one skipped test.

8.2 Running the tests

python -m pytest tests/unit/test_ci_workflow.py tests/unit/test_reporting.py -v

8.3 Troubleshooting

Symptom Cause Fix
report error: no report at ... The pytest command did not write a report, or the path is wrong Check --junitxml= and the reports/ directory
KeyError: 'on' when reading the workflow with PyYAML on is read as True wf.get("on", wf.get(True))
The workflow does not appear on GitHub The file is not in .github/workflows/ at the root of the repository Copy ci/github-actions.yml there

9. Independent exercise

9.1 Main exercise

Problem: The team wants the offline tests to also run every night, but the live job must stay manual.

Requirements:

  1. In exercises/my-workflow.yml, add a schedule trigger with a cron expression for 2 a.m. Vietnam time. Note: GitHub's cron uses UTC.
  2. Make sure the live job does not run on the schedule.
  3. Run python -m pytest exercises/test_l04_my_workflow.py.
  4. Write 3-5 sentences: whom should a scheduled run notify when it fails, and why?

Deliverable: the modified workflow file and your answer.

Acceptance criteria: the exercise test passes; the cron time is the correct UTC time for 2 a.m. Vietnam time.

Hint: Vietnam time is UTC+7.

9.2 Extra exercise: which tests vanished? (20-30 minutes)

Problem: A PASS summary cannot tell you that 40 tests vanished (scenario 1 of L01 §7) or that a test folder was renamed (scenario 2 in §7). Comparing the JUnit reports of two runs can.

Requirements: in exercises/test_l04_extra_lost_tests.py:

  1. TODO 1: write lost_tests(before_xml, after_xml): return the sorted list of ids classname::name of the tests that ran in the earlier report (passed, failed or error) but did not run in the later one (missing, or skipped).
  2. Fail closed: call summarize() from aiqa.reporting on both files before comparing, so that a missing or broken report raises ReportError.

Run: python -m pytest exercises/test_l04_extra_lost_tests.py. Until you do the work, the tests fail with TODO 1.

Acceptance criteria: 7 tests pass.

Hint: compare sets of ids, not counts: a renamed test keeps the count the same. Why does an id need the classname too? Read test_same_name_in_another_file_does_not_count.

9.3 Real-world scenario: reviewing an e-wallet's workflow

Context (fictional): The PayNhanh team builds the support chatbot of an e-wallet and asks you to review their CI workflow:

on: [push, pull_request, schedule]
env:
  ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v7
      - run: pip install -r requirements.txt
      - run: AIQA_MODE=live python -m pytest --junitxml=reports/all.xml || true
      - run: python report.py reports/all.xml >> "$GITHUB_STEP_SUMMARY"
      - uses: actions/upload-artifact@v7
        if: ${{ success() }}
        with:
          name: report
          path: reports/

Your task: write review comments, each pointing to the exact line and stating the risk (money, secrets, evidence, trust in the result) and a fix. Then rewrite the workflow with the three tiers of §4.3. Finally, state clearly what you can verify on your machine and what only a real run on GitHub can prove.

A good answer has:

  • Every comment points to a specific line and a section of the lesson (§4.1-§4.5), with a concrete consequence.
  • The comments are ranked by severity, with reasons.
  • The rewritten workflow has separate tiers, each with a clear run condition and mode.
  • At least one structure test (in the style of test_ci_workflow.py, §4.6) for the new workflow.
  • A clear line between what was verified on the machine and what needs GitHub to prove (§5.1).

10. Knowledge check

0/20 done · Results are kept in this browser only.

P02 · True or false

Because GitHub masks secrets in logs, printing the API key to the log for debugging is safe.

P03 · Short answer

Why does the report upload step use if: ${{ !cancelled() }}?

P04 · Short answer

What can a test of the workflow's structure prove, and what can it not prove?

P05 · Short answer

(Scenario) A contributor opens a pull request from a fork and asks why the live tests did not run. How do you explain it?

P07 · Multiple choice

For which file below does the lab's python report.py <file> end with exit code 0?

P08 · Spot the bug

The job below is copied from ci/github-actions.yml (comments and the Summary step removed), with one line moved. Find the bug.

  live:
    needs: unit
    if: ${{ github.event_name == 'workflow_dispatch' && inputs.run_live }}
    runs-on: ubuntu-latest
    timeout-minutes: 10
    environment: live-api
    env:
      AIQA_MODE: live
      AIQA_MAX_USD: "0.20"
      ANTHROPIC_API_KEY: ${{ secrets.ANTHROPIC_API_KEY }}
    steps:
      - uses: actions/checkout@v7
      - uses: actions/setup-python@v7
        with:
          python-version: "3.12"
      - run: python -m pip install -r requirements.txt
      - name: Live contract tests
        run: python -m pytest -m live --junitxml=reports/live.xml

P09 · Recall

Lesson: L1-M07-L01

L01 §4.3: which command selects the group of tests that call the real API (costing money), the group that this lesson's live job runs?

P10 · Match

According to §4.2 and the demo in §8.1, match each YAML key of the workflow with its effect.

P11 · Short answer

In the lab, you run python -m pytest tests/unit -k khong_co_test --junitxml=reports/empty.xml (no test matches), then python report.py reports/empty.xml. What does the summary step report, and why does that matter?

P12 · True or false

In the lab's workflow, the live job also uploads its report with actions/upload-artifact.

P13 · Multiple choice

When the lab's workflow is read with yaml.safe_load, wf["on"] raises KeyError: 'on'. Why?

P14 · Select all that apply

According to §6, which configurations in a workflow for an LLM feature are bad signs? Select every correct option.

P15 · Put in order

Put the steps of the unit job in the order they appear in ci/github-actions.yml.

  1. actions/setup-python@v7 with python-version: "3.12"
  2. python -m pytest tests/unit --junitxml=reports/unit.xml
  3. actions/upload-artifact@v7 with if: ${{ !cancelled() }}
  4. The Summary step: summarises reports/unit.xml into $GITHUB_STEP_SUMMARY
  5. actions/checkout@v7
  6. python -m pip install -r requirements.txt

P17 · Apply now

In code-labs/l1-m07, do the extra exercise of §9.2: write lost_tests(before_xml, after_xml) in exercises/test_l04_extra_lost_tests.py, then run python -m pytest exercises/test_l04_extra_lost_tests.py.

Checklist

P18 · True or false

In the worked example of §4.1, the command python -m pytest -q tests/unit -k khong_co_test_nao --junitxml=reports/empty.xml still writes a report file, even though no test runs.

P19 · Multiple choice

Worked example of §4.2: which cron expression runs at 8:30 in the morning, Vietnam time (UTC+7), every day?

P20 · True or false

According to the worked example of §4.3, the lab's two live tests cost at most about $0.0027 per run, so the AIQA_MAX_USD=0.20 budget of the live job is not reached.

11. Summary

  • Concepts: JUnit reports; a summary that fails closed; workflows, triggers, jobs, needs, if; three-tier CI; environments with reviewers; secrets and the limits of masking them; keeping reports on failure.
  • Skills: producing and summarising reports; reading, writing and testing the structure of a workflow; rerunning the CI commands locally.
  • Pitfalls: automatic live tests; secrets at workflow level; green because there are no tests; believing that secret masking always works.
  • You can now: put an LLM test framework on CI without unintended spending.

12. References

ID Title Publisher URL Accessed Relevance
R1 API Reference (--junit-xml) pytest https://docs.pytest.org/en/stable/reference/reference.html 2026-10-31 JUnit reports
R2 Workflow syntax for GitHub Actions GitHub https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-syntax 2026-10-31 on: push, concurrency, environment
R3 Events that trigger workflows GitHub https://docs.github.com/en/actions/reference/workflows-and-actions/events-that-trigger-workflows 2026-10-31 workflow_dispatch
R4 Deployments and environments GitHub https://docs.github.com/en/actions/reference/workflows-and-actions/deployments-and-environments 2026-10-31 Required reviewers
R5 Continuous Integration Playwright (Python) https://playwright.dev/python/docs/ci 2026-10-31 playwright install --with-deps on CI
R6 Using secrets in GitHub Actions GitHub https://docs.github.com/en/actions/how-tos/write-workflows/choose-what-workflows-do/use-secrets 2026-10-31 Syntax; secrets do not reach workflows from forks
R7 Secrets GitHub https://docs.github.com/en/actions/concepts/security/secrets 2026-10-31 Masking secrets in logs; "not guaranteed"
R8 Workflow commands for GitHub Actions GitHub https://docs.github.com/en/actions/reference/workflows-and-actions/workflow-commands 2026-10-31 $GITHUB_STEP_SUMMARY

Key terms (English ↔ Vietnamese)

English Tiếng Việt
CI (continuous integration) Tích hợp liên tục
Workflow / job / step Quy trình / công việc / bước
Trigger Sự kiện kích hoạt
Artifact Tệp kết quả được lưu lại
Environment (protected) Môi trường có bảo vệ
Fail closed Lỗi thì coi là không đạt