Reading a failed Python test, line by line
unittest reports failures and errors separately and exits non-zero when tests fail. Output order and detail vary by version; unittest ships with Python.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-28-A: unittest distinguishes failure from error and exits non-zero
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'import unittest\nclass T(unittest.TestCase):\n def test_fail(self):\n self.assertEqual(1, 2)\n def test_err(self):\n raise RuntimeError("boom")\nif __name__ == "__main__":\n unittest.main()\n' > t.pyCommand:
python3 t.py 2>&1 | tail -4; python3 t.py >/dev/null 2>&1; echo exit=$?Recorded output:
----------------------------------------------------------------------
Ran 2 tests in 0.000s
FAILED (failures=1, errors=1)
exit=1This run checks the exit status and records the output; it asserts no expected text.
TE-28-B: a passing unittest prints Ran 1 test and OK
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'import unittest\nclass T(unittest.TestCase):\n def test_ok(self):\n self.assertEqual(1, 1)\nif __name__ == "__main__":\n unittest.main()\n' > t.pyCommand:
python3 t.py 2>&1 | grep -E '^(Ran|OK)' | sed 's/ in .*//'Recorded output:
Ran 1 test
OKExit status: 0. This status and the output match what was written before the run. This is the status of sed, the last command in the pipe.
TE-28-C: expected non-zero exit: a failing assertEqual makes unittest exit with status 1 and report FAIL and AssertionError
Setup for this run, inside a throwaway directory (mktemp -d):
printf 'import unittest\nclass T(unittest.TestCase):\n def test_fail(self):\n self.assertEqual(1, 2)\nif __name__ == "__main__":\n unittest.main()\n' > t.pyPaths written as ../name sit one level above the working folder, inside the same throwaway tree.
Command:
python3 t.py 2>../err.txt; rc=$?; grep -E '^(FAIL:|AssertionError)' ../err.txt; exit $rcRecorded output:
FAIL: test_fail (__main__.T)
AssertionError: 1 != 2Exit status: 1. This status matches what was written before the run. The expected output text was corrected after the first run: the first prediction's grep also caught the line FAILED (failures=1).
TE-28-B: by the setup, the one test asserts that 1 equals 1. The lines kept by grep are Ran 1 test and OK, with the timing cut off by sed. TE-28-C: the standard error went to ../err.txt. The lines kept from it are FAIL: test_fail (__main__.T) and AssertionError: 1 != 2, and the script exited with status 1.
Limits
Output order and detail vary by version; unittest ships with Python.
Source
Evidence
TE-28-A: unittest distinguishes failure from error and exits non-zero
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-28-A | unittest distinguishes failure from error and exits non-zero | 0 | PASS | c263cc7af952 | | TE-28-B | a passing unittest prints Ran 1 test and OK | 0 | PASS | 3d4b9dc6d5f5 | | TE-28-C | expected non-zero exit: a failing assertEqual makes unittest exit with status 1 and report FAIL and AssertionError | 1 | PASS | 2cab09a81af6 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides