2010-10-14 07:25:09 +08:00
2011-03-04 06:40:38 +08:00
The writing and reporting of assertions in tests
==================================================
2010-10-14 07:25:09 +08:00
2012-06-17 16:59:30 +08:00
.. _`assertfeedback`:
2010-11-25 19:11:10 +08:00
.. _`assert with the assert statement`:
2012-10-12 20:52:36 +08:00
.. _`assert`:
2010-11-25 19:11:10 +08:00
2011-09-06 17:43:42 +08:00
Asserting with the `` assert `` statement
2010-10-14 07:25:09 +08:00
---------------------------------------------------------
2014-01-18 19:31:33 +08:00
`` pytest `` allows you to use the standard python `` assert `` for verifying
2010-10-14 07:25:09 +08:00
expectations and values in Python tests. For example, you can write the
2011-03-04 06:40:38 +08:00
following::
2010-10-14 07:25:09 +08:00
# content of test_assert1.py
def f():
return 3
def test_function():
assert f() == 4
2011-05-28 01:30:27 +08:00
to assert that your function returns a certain value. If this assertion fails
2011-06-19 04:30:46 +08:00
you will see the return value of the function call::
2010-10-14 07:25:09 +08:00
2016-06-21 22:16:57 +08:00
$ pytest test_assert1.py
2017-11-23 23:33:41 +08:00
=========================== test session starts ============================
2017-05-13 04:17:40 +08:00
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
2017-03-14 06:41:20 +08:00
rootdir: $REGENDOC_TMPDIR, inifile:
2017-07-04 07:29:13 +08:00
collected 1 item
2018-05-18 16:19:46 +08:00
2017-11-23 23:33:41 +08:00
test_assert1.py F [100%]
2018-05-18 16:19:46 +08:00
2017-11-23 23:33:41 +08:00
================================= FAILURES =================================
______________________________ test_function _______________________________
2018-05-18 16:19:46 +08:00
2010-10-14 07:25:09 +08:00
def test_function():
> assert f() == 4
E assert 3 == 4
E + where 3 = f()
2018-05-18 16:19:46 +08:00
2010-10-14 07:25:09 +08:00
test_assert1.py:5: AssertionError
2017-11-23 23:33:41 +08:00
========================= 1 failed in 0.12 seconds =========================
2010-10-14 07:25:09 +08:00
2014-01-18 19:31:33 +08:00
`` pytest `` has support for showing the values of the most common subexpressions
2011-06-19 04:30:46 +08:00
including calls, attributes, comparisons, and binary and unary
operators. (See :ref: `tbreportdemo` ). This allows you to use the
idiomatic python constructs without boilerplate code while not losing
introspection information.
2011-05-28 01:30:27 +08:00
2011-06-19 04:30:46 +08:00
However, if you specify a message with the assertion like this::
2011-06-19 04:07:36 +08:00
2011-06-19 04:30:46 +08:00
assert a % 2 == 0, "value was odd, should be even"
then no assertion introspection takes places at all and the message
will be simply shown in the traceback.
2011-05-28 01:30:27 +08:00
2011-06-19 04:30:46 +08:00
See :ref: `assert-details` for more information on assertion introspection.
2011-03-04 06:40:38 +08:00
2012-10-09 20:35:17 +08:00
.. _`assertraises`:
2011-09-06 17:43:42 +08:00
Assertions about expected exceptions
2010-10-14 07:25:09 +08:00
------------------------------------------
In order to write assertions about raised exceptions, you can use
2010-11-18 05:12:16 +08:00
`` pytest.raises `` as a context manager like this::
2010-10-14 07:25:09 +08:00
2011-03-04 06:40:38 +08:00
import pytest
2014-09-08 21:26:31 +08:00
def test_zero_division():
with pytest.raises(ZeroDivisionError):
1 / 0
2010-10-14 07:25:09 +08:00
and if you need to have access to the actual exception info you may use::
2014-09-08 21:26:31 +08:00
def test_recursion_depth():
with pytest.raises(RuntimeError) as excinfo:
def f():
f()
2010-10-14 07:25:09 +08:00
f()
2014-09-08 21:26:31 +08:00
assert 'maximum recursion' in str(excinfo.value)
2010-10-14 07:25:09 +08:00
2015-11-27 22:43:01 +08:00
`` excinfo `` is a `` ExceptionInfo `` instance, which is a wrapper around
2014-09-08 21:26:31 +08:00
the actual exception raised. The main attributes of interest are
`` .type `` , `` .value `` and `` .traceback `` .
2013-08-01 17:12:02 +08:00
2016-07-13 08:02:40 +08:00
.. versionchanged :: 3.0
2016-06-20 04:56:43 +08:00
2016-06-20 04:34:42 +08:00
In the context manager form you may use the keyword argument
`` message `` to specify a custom failure message::
>>> with raises(ZeroDivisionError, message="Expecting ZeroDivisionError"):
2018-06-13 01:39:51 +08:00
... pass
2016-06-20 04:34:42 +08:00
... Failed: Expecting ZeroDivisionError
2011-12-05 18:10:48 +08:00
If you want to write test code that works on Python 2.4 as well,
2010-10-14 07:25:09 +08:00
you may also use two other ways to test for an expected exception::
2010-11-18 05:12:16 +08:00
pytest.raises(ExpectedException, func, *args, * *kwargs)
pytest.raises(ExpectedException, "func(*args, * *kwargs)")
2010-10-14 07:25:09 +08:00
both of which execute the specified function with args and kwargs and
asserts that the given `` ExpectedException `` is raised. The reporter will
provide you with helpful output in case of failures such as *no
exception* or * wrong exception*.
2014-07-27 00:10:32 +08:00
Note that it is also possible to specify a "raises" argument to
`` pytest.mark.xfail `` , which checks that the test is failing in a more
specific way than just having any exception raised::
@pytest.mark.xfail(raises=IndexError)
def test_f():
f()
Using `` pytest.raises `` is likely to be better for cases where you are testing
exceptions your own code is deliberately raising, whereas using
`` @pytest.mark.xfail `` with a check function is probably better for something
like documenting unfixed bugs (where the test describes what "should" happen)
or bugs in dependencies.
2017-08-22 18:12:48 +08:00
Also, the context manager form accepts a `` match `` keyword parameter to test
that a regular expression matches on the string representation of an exception
(like the `` TestCase.assertRaisesRegexp `` method from `` unittest `` )::
2016-04-02 23:24:08 +08:00
import pytest
def myfunc():
raise ValueError("Exception 123 raised")
def test_match():
2017-08-22 18:12:48 +08:00
with pytest.raises(ValueError, match=r'.* 123 .* '):
2016-04-02 23:24:08 +08:00
myfunc()
The regexp parameter of the `` match `` method is matched with the `` re.search ``
2017-08-22 18:12:48 +08:00
function. So in the above example `` match='123' `` would have worked as
2016-04-02 23:24:08 +08:00
well.
2014-07-27 00:10:32 +08:00
2015-07-29 07:01:11 +08:00
.. _`assertwarns`:
Assertions about expected warnings
-----------------------------------------
.. versionadded :: 2.8
You can check that code raises a particular warning using
:ref: `pytest.warns <warns>` .
2014-07-27 00:10:32 +08:00
2010-11-26 03:06:42 +08:00
.. _newreport:
2010-10-14 07:25:09 +08:00
Making use of context-sensitive comparisons
-------------------------------------------------
.. versionadded :: 2.0
2014-01-18 19:31:33 +08:00
`` pytest `` has rich support for providing context-sensitive information
2010-10-14 07:25:09 +08:00
when it encounters comparisons. For example::
# content of test_assert2.py
def test_set_comparison():
set1 = set("1308")
set2 = set("8035")
assert set1 == set2
if you run this module::
2016-06-21 22:16:57 +08:00
$ pytest test_assert2.py
2017-11-23 23:33:41 +08:00
=========================== test session starts ============================
2017-05-13 04:17:40 +08:00
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
2017-03-14 06:41:20 +08:00
rootdir: $REGENDOC_TMPDIR, inifile:
2017-07-04 07:29:13 +08:00
collected 1 item
2018-05-18 16:19:46 +08:00
2017-11-23 23:33:41 +08:00
test_assert2.py F [100%]
2018-05-18 16:19:46 +08:00
2017-11-23 23:33:41 +08:00
================================= FAILURES =================================
___________________________ test_set_comparison ____________________________
2018-05-18 16:19:46 +08:00
2010-10-14 07:25:09 +08:00
def test_set_comparison():
set1 = set("1308")
set2 = set("8035")
> assert set1 == set2
2017-03-14 06:41:20 +08:00
E AssertionError: assert {'0', '1', '3', '8'} == {'0', '3', '5', '8'}
2010-10-14 07:25:09 +08:00
E Extra items in the left set:
E '1'
E Extra items in the right set:
E '5'
2014-10-24 21:08:43 +08:00
E Use -v to get the full diff
2018-05-18 16:19:46 +08:00
2010-10-14 07:25:09 +08:00
test_assert2.py:5: AssertionError
2017-11-23 23:33:41 +08:00
========================= 1 failed in 0.12 seconds =========================
2010-10-14 07:25:09 +08:00
Special comparisons are done for a number of cases:
* comparing long strings: a context diff is shown
* comparing long sequences: first failing indices
* comparing dicts: different entries
2010-11-26 03:06:42 +08:00
See the :ref: `reporting demo <tbreportdemo>` for many more examples.
2010-11-25 20:00:01 +08:00
2011-06-21 00:12:48 +08:00
Defining your own assertion comparison
----------------------------------------------
It is possible to add your own detailed explanations by implementing
the `` pytest_assertrepr_compare `` hook.
.. autofunction :: _pytest.hookspec.pytest_assertrepr_compare
2016-08-23 10:35:41 +08:00
:noindex:
2011-06-21 00:12:48 +08:00
2018-05-18 16:19:46 +08:00
As an example consider adding the following hook in a :ref: `conftest.py <conftest.py>`
2017-11-04 02:37:18 +08:00
file which provides an alternative explanation for `` Foo `` objects::
2011-06-21 00:12:48 +08:00
# content of conftest.py
from test_foocompare import Foo
def pytest_assertrepr_compare(op, left, right):
if isinstance(left, Foo) and isinstance(right, Foo) and op == "==":
2015-10-10 19:28:35 +08:00
return ['Comparing Foo instances:',
' vals: %s != %s' % (left.val, right.val)]
2011-06-21 00:12:48 +08:00
now, given this test module::
# content of test_foocompare.py
2017-02-17 02:41:51 +08:00
class Foo(object):
2011-06-21 00:12:48 +08:00
def __init__(self, val):
2015-09-21 21:23:26 +08:00
self.val = val
def __eq__(self, other):
return self.val == other.val
2011-06-21 00:12:48 +08:00
def test_compare():
f1 = Foo(1)
f2 = Foo(2)
assert f1 == f2
2014-01-18 19:31:33 +08:00
you can run the test module and get the custom output defined in
2011-06-21 00:12:48 +08:00
the conftest file::
2016-06-21 22:16:57 +08:00
$ pytest -q test_foocompare.py
2017-11-23 23:33:41 +08:00
F [100%]
================================= FAILURES =================================
_______________________________ test_compare _______________________________
2018-05-18 16:19:46 +08:00
2011-06-21 00:12:48 +08:00
def test_compare():
f1 = Foo(1)
f2 = Foo(2)
> assert f1 == f2
2016-08-04 04:31:44 +08:00
E assert Comparing Foo instances:
E vals: 1 != 2
2018-05-18 16:19:46 +08:00
2015-09-22 20:02:11 +08:00
test_foocompare.py:11: AssertionError
2015-06-07 05:30:49 +08:00
1 failed in 0.12 seconds
2011-05-27 10:08:55 +08:00
2011-05-28 01:30:27 +08:00
.. _assert-details:
2011-06-15 13:50:34 +08:00
.. _`assert introspection`:
2011-05-28 01:30:27 +08:00
2011-06-15 13:50:34 +08:00
Advanced assertion introspection
----------------------------------
.. versionadded :: 2.1
2011-05-27 10:08:55 +08:00
2011-06-19 04:30:46 +08:00
2017-01-13 23:52:02 +08:00
Reporting details about a failing assertion is achieved by rewriting assert
statements before they are run. Rewritten assert statements put introspection
information into the assertion failure message. `` pytest `` only rewrites test
modules directly discovered by its test collection process, so asserts in
supporting modules which are not themselves test modules will not be rewritten.
2011-05-27 10:08:55 +08:00
2011-05-29 08:00:47 +08:00
.. note ::
2017-04-19 19:19:19 +08:00
`` pytest `` rewrites test modules on import by using an import
hook to write new `` pyc `` files. Most of the time this works transparently.
2014-01-18 19:31:33 +08:00
However, if you are messing with import yourself, the import hook may
2017-04-19 19:19:19 +08:00
interfere.
If this is the case you have two options:
* Disable rewriting for a specific module by adding the string
`` PYTEST_DONT_REWRITE `` to its docstring.
* Disable rewriting for all modules by using `` --assert=plain `` .
Additionally, rewriting will fail silently if it cannot write new `` .pyc `` files,
i.e. in a read-only filesystem or a zipfile.
2011-05-27 10:08:55 +08:00
2014-01-18 19:31:33 +08:00
For further information, Benjamin Peterson wrote up `Behind the scenes of pytest's new assertion rewriting <http://pybites.blogspot.com/2011/07/behind-scenes-of-pytests-new-assertion.html> `_ .
2011-07-12 16:38:02 +08:00
2011-05-27 12:13:39 +08:00
.. versionadded :: 2.1
2011-05-29 05:01:02 +08:00
Add assert rewriting as an alternate introspection technique.
2011-05-27 12:13:39 +08:00
.. versionchanged :: 2.1
2011-07-05 23:29:53 +08:00
Introduce the `` --assert `` option. Deprecate `` --no-assert `` and
2011-05-27 12:13:39 +08:00
`` --nomagic `` .
2016-06-25 17:27:10 +08:00
.. versionchanged :: 3.0
2017-03-06 01:01:55 +08:00
Removes the `` --no-assert `` and `` --nomagic `` options.
2017-01-13 23:52:02 +08:00
Removes the `` --assert=reinterp `` option.