I am looking for a way to run all of the assertions in my unit tests in PyTest, even if some of them fail. I know there must be a simple way to do this. I checked the CLi options and looked through this site for similar questions/answers but didn't see anything. Sorry if this has already been answered.
For example, consider the following code snippet, with PyTest code alongside it:
def parrot(i):
return i
def test_parrot():
assert parrot(0) == 0
assert parrot(1) == 1
assert parrot(2) == 1
assert parrot(2) == 2
By default, the execution stops at the first failure:
$ python -m pytest fail_me.py
=================== test session starts ===================
platform linux2 -- Python 2.7.10, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
rootdir: /home/npsrt/Documents/repo/codewars, inifile:
collected 1 items
fail_me.py F
=================== FAILURES ===================
___________________ test_parrot ___________________
def test_parrot():
assert parrot(0) == 0
assert parrot(1) == 1
> assert parrot(2) == 1
E assert 2 == 1
E + where 2 = parrot(2)
fail_me.py:7: AssertionError
=================== 1 failed in 0.05 seconds ===================
What I'd like to do is to have the code continue to execute even after PyTest encounters the first failure.
unittest
, which is linked to by a bunch of very similar questions – Eric@pytest.mark.parametrize
is what you're looking for. it takes 2 arguments, the variable name that will be providing the data, and the data you wish to supply to the test. So, to achieve what you want, the following can be done. @pytest.mark.parametrize('parrot_num', (1, 2, 3, 4, 5)) def parrot(parrot_num): return parrot_num def test_parrot(): assert parrot(0) == 0 assert parrot(1) == 1 assert parrot(2) == 1 assert parrot(2) == 2 – Arthur Bowers