# Reading a failed Python test, line by line

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/unittest-failure-full-en
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.

(Test: 3 tests were run, TE-28-A, TE-28-B and TE-28-C. The expected non-zero exit is TE-28-C (exit status 1). Every row has a recorded log in the evidence archive. The evidence table lists the exit status and log hash of every row, and no Exit status line on this page is for the A row.)

### TE-28-A: unittest distinguishes failure from error and exits non-zero

Setup for this run, inside a throwaway directory (`mktemp -d`):

```bash
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.py
```

Command:

```bash
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=1
```

This 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`):

```bash
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.py
```

Command:

```bash
python3 t.py 2>&1 | grep -E '^(Ran|OK)' | sed 's/ in .*//'
```

Recorded output:

```
Ran 1 test
OK
```

Exit 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`):

```bash
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.py
```

Paths written as `../name` sit one level above the working folder, inside the same throwaway tree.

Command:

```bash
python3 t.py 2>../err.txt; rc=$?; grep -E '^(FAIL:|AssertionError)' ../err.txt; exit $rc
```

Recorded output:

```
FAIL: test_fail (__main__.T)
AssertionError: 1 != 2
```

Exit 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

- https://docs.python.org/3/library/unittest.html

## 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.

## Related guides
- [Python dictionaries: lookup, default values and the KeyError](https://kairos-2z6s.onrender.com/res/python-dict-basics-full-en)
- [python -m venv: a separate package folder for one project](https://kairos-2z6s.onrender.com/res/python-venv-full-en)
- [xargs: turn lines of input into command arguments](https://kairos-2z6s.onrender.com/res/xargs-basics-full-en)
- [tar basics: pack, list and unpack a folder](https://kairos-2z6s.onrender.com/res/tar-basics-full-en)
- [awk basics: split records into fields](https://kairos-2z6s.onrender.com/res/awk-basics-full-en)
- [Python str.join: build a string from a list](https://kairos-2z6s.onrender.com/res/python-str-join-full-en)
