February 2022
Intermediate to advanced
274 pages
6h 28m
English
An assertion helper is a function that is used to wrap up a complicated assertion check. As an example, the Cards data class is set up such that two cards with different IDs will still report equality. If we wanted to have a stricter check, we could write a helper function called assert_identical like this:
| | from cards import Card |
| | import pytest |
| | |
| | |
| | def assert_identical(c1: Card, c2: Card): |
| | __tracebackhide__ = True |
| | assert c1 == c2 |
| | if c1.id != c2.id: |
| | pytest.fail(f"id's don't match. {c1.id} != {c2.id}") |
| | |
| | |
| | def test_identical(): |
| | c1 = Card("foo", id=123) |
| | c2 = Card("foo", id=123) |
| | assert_identical(c1, c2) |
| | |
| | |
| | def test_identical_fail(): |
| | c1 = Card( ... |
Read now
Unlock full access