Treat the exit status as the test gate
How buildspec test commands propagate test failures through exit codes and publish unit-test and coverage results through report groups. · advanced
CodeBuild runs the commands in your buildspec; it does not infer command success from text such as “tests failed” in a log. A test command must return a nonzero exit status for CodeBuild to recognize its failure. Configure the runner to fail on failed assertions, and do not use a phase failure policy that continues past a failure you intend to block the build.
The relevant status is the one returned by the shell command CodeBuild executes. In Bash, pytest || true masks pytest's failure. A wrapper that runs pytest and then successfully runs echo done can also return success unless it explicitly preserves the failure. Likewise, pytest | tee tests.log normally returns tee's status; set -o pipefail makes a failed pipeline member produce a nonzero pipeline status.
Keep two responsibilities separate: exit codes enforce the gate, while report files explain the results. A failed test report does not replace correct exit-code propagation, and a failing command does not guarantee that a usable report file exists.
| Bash command | Runner fails | Status seen by CodeBuild |
|---|---|---|
| pytest | Nonzero | Runner's failure |
| pytest || true | Nonzero | Zero |
| pytest | tee tests.log | Nonzero; tee succeeds | Zero without pipefail |