2010-11-06 06:37:31 +08:00
|
|
|
|
|
|
|
writing well integrated assertion helpers
|
|
|
|
========================================================
|
|
|
|
|
|
|
|
If you have a test helper function called from a test you can
|
2010-11-06 18:38:53 +08:00
|
|
|
use the ``pytest.fail`` marker to fail a test with a certain message.
|
|
|
|
The test support function will not show up in the traceback if you
|
|
|
|
set the ``__tracebackhide__`` option somewhere in the helper function.
|
2010-11-06 06:37:31 +08:00
|
|
|
Example::
|
|
|
|
|
|
|
|
# content of test_checkconfig.py
|
|
|
|
import pytest
|
|
|
|
def checkconfig(x):
|
|
|
|
__tracebackhide__ = True
|
|
|
|
if not hasattr(x, "config"):
|
|
|
|
pytest.fail("not configured: %s" %(x,))
|
|
|
|
|
|
|
|
def test_something():
|
|
|
|
checkconfig(42)
|
|
|
|
|
|
|
|
The ``__tracebackhide__`` setting influences py.test showing
|
|
|
|
of tracebacks: the ``checkconfig`` function will not be shown
|
|
|
|
unless the ``--fulltrace`` command line option is specified.
|
|
|
|
Let's run our little function::
|
|
|
|
|
|
|
|
$ py.test -q
|
|
|
|
F
|
|
|
|
================================= FAILURES =================================
|
|
|
|
______________________________ test_something ______________________________
|
|
|
|
|
|
|
|
def test_something():
|
|
|
|
> checkconfig(42)
|
|
|
|
E Failed: not configured: 42
|
|
|
|
|
|
|
|
test_checkconfig.py:8: Failed
|
|
|
|
1 failed in 0.02 seconds
|