Using assert Statements
When you write test functions, the normal Python assert statement is your primary tool to communicate test failure. The simplicity of this within pytest is brilliant. It’s what drives a lot of developers to use pytest over other frameworks.
If you’ve used any other testing framework, you’ve probably seen various assert helper functions. For example, following is a list of a few of the assert forms and assert helper functions from unittest:
pytest | unittest |
|---|---|
assert something | assertTrue(something) |
assert not something | assertFalse(something) |
assert a == b | assertEqual(a, b) |
assert a != b | assertNotEqual(a, b) |
assert a is None | assertIsNone(a) |
assert a is not None | assertIsNotNone(a) |
assert a <= b | assertLessEqual(a, b) |
… | … |
With pytest, you can ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access