I'm more familiar with the google test framework and know about the primary behavior pair they support about ASSERT_*
vs EXPECT_*
which are the fatal and non-fatal assert modes.
From the documentation:
The assertions come in pairs that test the same thing but havedifferent effects on the current function. ASSERT_*
versions generatefatal failures when they fail, and abort the current function.EXPECT_*
versions generate nonfatal failures, which don't abort thecurrent function. Usually EXPECT_* are preferred, as they allow morethan one failures to be reported in a test. However, you should useASSERT_* if it doesn't make sense to continue when the assertion inquestion fails.
Question: does pytest also have a non fatal assert flavor or mode I can enable?
It's nice to allow a full range of tests to maximally execute to get the richest failure history rather than abort at the first failure and potentially hide subsequent failures that have to be discovered piecewise by running multiple instances of the test application.
I use pytest-assume
for non-fatal assertions. It does the job pretty well.
Installation
As usual,
$ pip install pytest-assume
Usage example
import pytestdef test_spam():pytest.assume(True)pytest.assume(False)a, b = True, Falsepytest.assume(a == b)pytest.assume(1 == 0)pytest.assume(1 < 0)pytest.assume('')pytest.assume([])pytest.assume({})
If you feel writing pytest.assume
is a bit too much, just alias the import:
import pytest.assume as expectdef test_spam():expect(True)...
Running the above test yields:
$ pytest -v
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.6.0, py-1.5.3, pluggy-0.6.0 -- /data/gentoo64-prefix/u0_a82/projects/stackoverflow/so-50630845
cachedir: .pytest_cache
rootdir: /data/gentoo64-prefix/u0_a82/projects/stackoverflow/so-50630845, inifile:
plugins: assume-1.2
collecting ... collected 1 itemtest_spam.py::test_spam FAILED [100%]=================================== FAILURES ===================================
__________________________________ test_spam ___________________________________
test_spam.py:6: AssumptionFailurepytest.assume(False)test_spam.py:9: AssumptionFailurepytest.assume(a == b)test_spam.py:11: AssumptionFailurepytest.assume(1 == 0)test_spam.py:12: AssumptionFailurepytest.assume(1 < 0)test_spam.py:13: AssumptionFailurepytest.assume('')test_spam.py:14: AssumptionFailurepytest.assume([])test_spam.py:14: AssumptionFailurepytest.assume([])test_spam.py:15: AssumptionFailurepytest.assume({})------------------------------------------------------------
Failed Assumptions: 7
=========================== 1 failed in 0.18 seconds ===========================