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

Get Python Testing with pytest now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.