Sub topic 13
Building the workflow into review and CI
This chapter assumes you can run pytest, read a GitHub Actions workflow file, and open a pull request. It also assumes you have already generated tests and docstrings for apply_discount as in earlier chapters. Here you will turn the habits from those chapters into rules that a team and a pipeline enforce.
Why the workflow needs enforcement
Generated tests and docs look finished when they are not. A reviewer who is in a hurry may approve them because they look tidy. A CI pipeline does not get tired, so it should catch the checkable problems. Humans should then spend their attention on the problems a machine cannot catch.
Split the checks into two groups:
- Mechanical checks (CI): do the tests pass, do the docstring examples run, is coverage above a floor.
- Judgment checks (review): do the assertions use values derived from the contract, do the mocks match real behavior, does the text say something true and useful.
Add the mechanical checks to CI
The workflow below runs the doctest examples from the docstring, then the test suite with a coverage floor. It assumes pricing.py is in the repository root and the tests are in tests/test_pricing.py.
name: checks
on: pull_request
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install pytest pytest-cov
- name: Run docstring examples
run: python -m doctest pricing.py
- name: Run tests with coverage floor
run: python -m pytest --cov=pricing --cov-fail-under=90Two details matter here. python -m doctest prints nothing when every example passes and fails the job when one does not, so a docstring that drifts from the code breaks the build. Running python -m pytest instead of bare pytest puts the current directory on the import path, so from pricing import apply_discount works.
Make review catch what CI cannot
Add a short checklist to your pull request template. Ask the author to confirm each item for any generated file:
- Expected values were derived from the documented contract, not copied from the function's own output.
- Every error path has a test that checks the exception type.
- Mocks replace only external dependencies, and the code under test is real.
- Each docstring claim, such as "raises ValueError", has a matching test or example.
- The pull request description says which parts were assistant-drafted.
The last item is about honesty with reviewers. A reviewer reads a drafted file more skeptically, and that is the right posture.
Prove that the tests can fail
One cheap review habit catches tautologies. Before merging, break the code on purpose, for example change percent / 100 to percent / 10, and run the tests. If nothing fails, the tests are not checking the behavior. Revert the change afterward. Some teams automate this with a mutation testing tool, but the manual version takes a minute and needs no setup.
Keep it maintainable
- Pin the assistant's job to drafts. Humans own the merged result, including its failures.
- Do not send secrets or private customer data in prompts. Use your organization's approved assistant settings.
- Regenerate docs when the function changes. The doctest step will tell you when they are stale.
- Keep your prompt templates in the repository so everyone asks for tests the same way.
Exercise
Your CI shows 100% coverage and all tests pass on a pull request with assistant-generated tests. Name one check you should still do before approving, and say how you would do it.
Show answer
Confirm the tests can fail. Temporarily change the function, for example swap percent / 100 for percent / 10, and run the suite. If every test still passes, the assertions are tautological or missing, and the coverage figure is giving false confidence. Revert the change afterward.