2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
.. highlightlang:: python
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Basic patterns and examples
|
2010-11-06 06:37:25 +08:00
|
|
|
==========================================================
|
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Pass different values to a test function, depending on command line options
|
2010-11-06 06:37:25 +08:00
|
|
|
----------------------------------------------------------------------------
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
.. regendoc:wipe
|
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
Suppose we want to write a test that depends on a command line option.
|
|
|
|
Here is a basic pattern how to achieve this::
|
|
|
|
|
|
|
|
# content of test_sample.py
|
|
|
|
def test_answer(cmdopt):
|
|
|
|
if cmdopt == "type1":
|
|
|
|
print ("first")
|
|
|
|
elif cmdopt == "type2":
|
|
|
|
print ("second")
|
|
|
|
assert 0 # to see what was printed
|
|
|
|
|
|
|
|
|
|
|
|
For this to work we need to add a command line option and
|
2012-10-07 19:06:17 +08:00
|
|
|
provide the ``cmdopt`` through a :ref:`fixture function <fixture function>`::
|
2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
2012-10-07 19:06:17 +08:00
|
|
|
import pytest
|
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--cmdopt", action="store", default="type1",
|
|
|
|
help="my option: type1 or type2")
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def cmdopt(request):
|
2012-11-06 21:09:12 +08:00
|
|
|
return request.config.getoption("--cmdopt")
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Let's run this without supplying our new option::
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
$ py.test -q test_sample.py
|
2010-11-06 06:37:25 +08:00
|
|
|
F
|
|
|
|
================================= FAILURES =================================
|
|
|
|
_______________________________ test_answer ________________________________
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
cmdopt = 'type1'
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
def test_answer(cmdopt):
|
|
|
|
if cmdopt == "type1":
|
|
|
|
print ("first")
|
|
|
|
elif cmdopt == "type2":
|
|
|
|
print ("second")
|
|
|
|
> assert 0 # to see what was printed
|
|
|
|
E assert 0
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
test_sample.py:6: AssertionError
|
|
|
|
----------------------------- Captured stdout ------------------------------
|
|
|
|
first
|
|
|
|
|
|
|
|
And now with supplying a command line option::
|
|
|
|
|
|
|
|
$ py.test -q --cmdopt=type2
|
|
|
|
F
|
|
|
|
================================= FAILURES =================================
|
|
|
|
_______________________________ test_answer ________________________________
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
cmdopt = 'type2'
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
def test_answer(cmdopt):
|
|
|
|
if cmdopt == "type1":
|
|
|
|
print ("first")
|
|
|
|
elif cmdopt == "type2":
|
|
|
|
print ("second")
|
|
|
|
> assert 0 # to see what was printed
|
|
|
|
E assert 0
|
2010-11-06 18:38:53 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
test_sample.py:6: AssertionError
|
|
|
|
----------------------------- Captured stdout ------------------------------
|
|
|
|
second
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
You can see that the command line option arrived in our test. This
|
|
|
|
completes the basic pattern. However, one often rather wants to process
|
|
|
|
command line options outside of the test and rather pass in different or
|
|
|
|
more complex objects.
|
2010-11-21 04:35:55 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Dynamically adding command line options
|
2010-12-07 19:14:12 +08:00
|
|
|
--------------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
Through :confval:`addopts` you can statically add command line
|
|
|
|
options for your project. You can also dynamically modify
|
|
|
|
the command line arguments before they get processed::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
import sys
|
2010-12-07 19:34:18 +08:00
|
|
|
def pytest_cmdline_preparse(args):
|
2010-12-07 19:14:12 +08:00
|
|
|
if 'xdist' in sys.modules: # pytest-xdist plugin
|
|
|
|
import multiprocessing
|
|
|
|
num = max(multiprocessing.cpu_count() / 2, 1)
|
|
|
|
args[:] = ["-n", str(num)] + args
|
|
|
|
|
|
|
|
If you have the :ref:`xdist plugin <xdist>` installed
|
|
|
|
you will now always perform test runs using a number
|
|
|
|
of subprocesses close to your CPU. Running in an empty
|
|
|
|
directory with the above conftest.py::
|
|
|
|
|
|
|
|
$ py.test
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 0 items
|
2010-12-07 19:14:12 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
============================= in 0.00 seconds =============================
|
2010-11-21 04:35:55 +08:00
|
|
|
|
2011-02-09 21:55:21 +08:00
|
|
|
.. _`excontrolskip`:
|
2010-11-21 04:35:55 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Control skipping of tests according to command line option
|
2010-11-21 04:35:55 +08:00
|
|
|
--------------------------------------------------------------
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
.. regendoc:wipe
|
|
|
|
|
2010-11-21 04:35:55 +08:00
|
|
|
Here is a ``conftest.py`` file adding a ``--runslow`` command
|
|
|
|
line option to control skipping of ``slow`` marked tests::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--runslow", action="store_true",
|
|
|
|
help="run slow tests")
|
|
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
2012-11-06 21:09:12 +08:00
|
|
|
if 'slow' in item.keywords and not item.config.getoption("--runslow"):
|
2010-11-21 04:35:55 +08:00
|
|
|
pytest.skip("need --runslow option to run")
|
|
|
|
|
|
|
|
We can now write a test module like this::
|
|
|
|
|
|
|
|
# content of test_module.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
slow = pytest.mark.slow
|
|
|
|
|
|
|
|
def test_func_fast():
|
|
|
|
pass
|
|
|
|
|
|
|
|
@slow
|
|
|
|
def test_func_slow():
|
|
|
|
pass
|
|
|
|
|
|
|
|
and when running it will see a skipped "slow" test::
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
$ py.test -rs # "-rs" means report details on the little 's'
|
2010-11-21 04:35:55 +08:00
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 2 items
|
2010-11-21 04:35:55 +08:00
|
|
|
|
|
|
|
test_module.py .s
|
|
|
|
========================= short test summary info ==========================
|
2012-11-20 21:01:31 +08:00
|
|
|
SKIP [1] /tmp/doc-exec-138/conftest.py:9: need --runslow option to run
|
2010-11-21 04:35:55 +08:00
|
|
|
|
2012-10-18 21:06:55 +08:00
|
|
|
=================== 1 passed, 1 skipped in 0.01 seconds ====================
|
2010-11-21 04:35:55 +08:00
|
|
|
|
|
|
|
Or run it including the ``slow`` marked test::
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
$ py.test --runslow
|
2010-11-21 04:35:55 +08:00
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 2 items
|
2010-11-21 04:35:55 +08:00
|
|
|
|
|
|
|
test_module.py ..
|
|
|
|
|
2012-05-23 00:30:34 +08:00
|
|
|
========================= 2 passed in 0.01 seconds =========================
|
2010-11-21 04:35:55 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Writing well integrated assertion helpers
|
2010-11-21 04:35:55 +08:00
|
|
|
--------------------------------------------------
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
.. regendoc:wipe
|
|
|
|
|
2010-11-21 04:35:55 +08:00
|
|
|
If you have a test helper function called from a test you can
|
|
|
|
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.
|
|
|
|
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::
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
$ py.test -q test_checkconfig.py
|
2010-11-21 04:35:55 +08:00
|
|
|
F
|
|
|
|
================================= FAILURES =================================
|
|
|
|
______________________________ test_something ______________________________
|
|
|
|
|
|
|
|
def test_something():
|
|
|
|
> checkconfig(42)
|
|
|
|
E Failed: not configured: 42
|
|
|
|
|
|
|
|
test_checkconfig.py:8: Failed
|
|
|
|
|
|
|
|
Detect if running from within a py.test run
|
|
|
|
--------------------------------------------------------------
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
.. regendoc:wipe
|
|
|
|
|
2010-11-21 04:35:55 +08:00
|
|
|
Usually it is a bad idea to make application code
|
|
|
|
behave differently if called from a test. But if you
|
|
|
|
absolutely must find out if your application code is
|
|
|
|
running from a test you can do something like this::
|
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
# content of conftest.py
|
2010-11-21 04:35:55 +08:00
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
import sys
|
|
|
|
sys._called_from_test = True
|
|
|
|
|
|
|
|
def pytest_unconfigure(config):
|
|
|
|
del sys._called_from_test
|
|
|
|
|
|
|
|
and then check for the ``sys._called_from_test`` flag::
|
|
|
|
|
|
|
|
if hasattr(sys, '_called_from_test'):
|
|
|
|
# called from within a test run
|
|
|
|
else:
|
|
|
|
# called "normally"
|
2011-12-05 18:10:48 +08:00
|
|
|
|
2010-11-21 04:35:55 +08:00
|
|
|
accordingly in your application. It's also a good idea
|
2011-12-05 18:10:48 +08:00
|
|
|
to use your own application module rather than ``sys``
|
2010-11-21 04:35:55 +08:00
|
|
|
for handling flag.
|
|
|
|
|
2011-01-13 02:39:36 +08:00
|
|
|
Adding info to test report header
|
|
|
|
--------------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
It's easy to present extra information in a py.test run::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
def pytest_report_header(config):
|
|
|
|
return "project deps: mylib-1.1"
|
|
|
|
|
|
|
|
which will add the string to the test header accordingly::
|
|
|
|
|
|
|
|
$ py.test
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2011-01-13 02:39:36 +08:00
|
|
|
project deps: mylib-1.1
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 0 items
|
2011-01-13 02:39:36 +08:00
|
|
|
|
2012-05-23 00:30:34 +08:00
|
|
|
============================= in 0.00 seconds =============================
|
2011-01-13 02:39:36 +08:00
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
You can also return a list of strings which will be considered as several
|
|
|
|
lines of information. You can of course also make the amount of reporting
|
|
|
|
information on e.g. the value of ``config.option.verbose`` so that
|
|
|
|
you present more information appropriately::
|
|
|
|
|
|
|
|
# content of conftest.py
|
2011-12-05 18:10:48 +08:00
|
|
|
|
2011-01-13 02:39:36 +08:00
|
|
|
def pytest_report_header(config):
|
|
|
|
if config.option.verbose > 0:
|
|
|
|
return ["info1: did you know that ...", "did you?"]
|
|
|
|
|
|
|
|
which will add info only when run with "--v"::
|
|
|
|
|
|
|
|
$ py.test -v
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4 -- /home/hpk/p/pytest/.tox/regen/bin/python
|
2011-01-13 02:39:36 +08:00
|
|
|
info1: did you know that ...
|
|
|
|
did you?
|
|
|
|
collecting ... collected 0 items
|
|
|
|
|
2012-05-23 00:30:34 +08:00
|
|
|
============================= in 0.00 seconds =============================
|
2011-01-13 02:39:36 +08:00
|
|
|
|
|
|
|
and nothing when run plainly::
|
|
|
|
|
|
|
|
$ py.test
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 0 items
|
2011-01-13 02:39:36 +08:00
|
|
|
|
2012-05-23 00:30:34 +08:00
|
|
|
============================= in 0.00 seconds =============================
|
2011-11-09 01:20:56 +08:00
|
|
|
|
|
|
|
profiling test duration
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
.. versionadded: 2.2
|
|
|
|
|
|
|
|
If you have a slow running large test suite you might want to find
|
2011-12-05 18:10:48 +08:00
|
|
|
out which tests are the slowest. Let's make an artifical test suite::
|
2011-11-09 01:20:56 +08:00
|
|
|
|
|
|
|
# content of test_some_are_slow.py
|
2011-12-05 18:10:48 +08:00
|
|
|
|
|
|
|
import time
|
2011-11-09 01:20:56 +08:00
|
|
|
|
|
|
|
def test_funcfast():
|
|
|
|
pass
|
2011-12-05 18:10:48 +08:00
|
|
|
|
2011-11-09 01:20:56 +08:00
|
|
|
def test_funcslow1():
|
|
|
|
time.sleep(0.1)
|
2011-12-05 18:10:48 +08:00
|
|
|
|
2011-11-09 01:20:56 +08:00
|
|
|
def test_funcslow2():
|
|
|
|
time.sleep(0.2)
|
2011-12-05 18:10:48 +08:00
|
|
|
|
|
|
|
Now we can profile which test functions execute the slowest::
|
2011-11-09 01:20:56 +08:00
|
|
|
|
|
|
|
$ py.test --durations=3
|
2011-11-19 02:32:11 +08:00
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 3 items
|
2011-11-19 02:32:11 +08:00
|
|
|
|
|
|
|
test_some_are_slow.py ...
|
|
|
|
|
|
|
|
========================= slowest 3 test durations =========================
|
|
|
|
0.20s call test_some_are_slow.py::test_funcslow2
|
|
|
|
0.10s call test_some_are_slow.py::test_funcslow1
|
2012-10-25 19:48:31 +08:00
|
|
|
0.00s call test_some_are_slow.py::test_funcfast
|
2012-05-23 00:30:34 +08:00
|
|
|
========================= 3 passed in 0.31 seconds =========================
|
2012-10-18 18:24:50 +08:00
|
|
|
|
|
|
|
incremental testing - test steps
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
Sometimes you may have a testing situation which consists of a series
|
|
|
|
of test steps. If one step fails it makes no sense to execute further
|
|
|
|
steps as they are all expected to fail anyway and their tracebacks
|
|
|
|
add no insight. Here is a simple ``conftest.py`` file which introduces
|
|
|
|
an ``incremental`` marker which is to be used on classes::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_runtest_makereport(item, call):
|
2012-10-18 21:06:55 +08:00
|
|
|
if "incremental" in item.keywords:
|
2012-10-18 18:24:50 +08:00
|
|
|
if call.excinfo is not None:
|
|
|
|
parent = item.parent
|
|
|
|
parent._previousfailed = item
|
|
|
|
|
|
|
|
def pytest_runtest_setup(item):
|
2012-10-18 21:06:55 +08:00
|
|
|
if "incremental" in item.keywords:
|
2012-10-18 18:24:50 +08:00
|
|
|
previousfailed = getattr(item.parent, "_previousfailed", None)
|
|
|
|
if previousfailed is not None:
|
|
|
|
pytest.xfail("previous test failed (%s)" %previousfailed.name)
|
|
|
|
|
|
|
|
These two hook implementations work together to abort incremental-marked
|
|
|
|
tests in a class. Here is a test module example::
|
|
|
|
|
|
|
|
# content of test_step.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.incremental
|
|
|
|
class TestUserHandling:
|
|
|
|
def test_login(self):
|
|
|
|
pass
|
|
|
|
def test_modification(self):
|
|
|
|
assert 0
|
|
|
|
def test_deletion(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_normal():
|
|
|
|
pass
|
|
|
|
|
|
|
|
If we run this::
|
|
|
|
|
|
|
|
$ py.test -rx
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-10-18 18:24:50 +08:00
|
|
|
collected 4 items
|
|
|
|
|
|
|
|
test_step.py .Fx.
|
|
|
|
|
|
|
|
================================= FAILURES =================================
|
|
|
|
____________________ TestUserHandling.test_modification ____________________
|
|
|
|
|
2012-11-20 21:01:31 +08:00
|
|
|
self = <test_step.TestUserHandling instance at 0x193bc68>
|
2012-10-18 18:24:50 +08:00
|
|
|
|
|
|
|
def test_modification(self):
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_step.py:9: AssertionError
|
|
|
|
========================= short test summary info ==========================
|
|
|
|
XFAIL test_step.py::TestUserHandling::()::test_deletion
|
|
|
|
reason: previous test failed (test_modification)
|
2012-11-20 21:01:31 +08:00
|
|
|
============== 1 failed, 2 passed, 1 xfailed in 0.02 seconds ===============
|
2012-10-18 18:24:50 +08:00
|
|
|
|
|
|
|
We'll see that ``test_deletion`` was not executed because ``test_modification``
|
|
|
|
failed. It is reported as an "expected failure".
|
|
|
|
|
2012-11-07 18:11:40 +08:00
|
|
|
|
|
|
|
Package/Directory-level fixtures (setups)
|
|
|
|
-------------------------------------------------------
|
|
|
|
|
|
|
|
If you have nested test directories, you can have per-directory fixture scopes
|
|
|
|
by placing fixture functions in a ``conftest.py`` file in that directory
|
|
|
|
You can use all types of fixtures including :ref:`autouse fixtures
|
|
|
|
<autouse fixtures>` which are the equivalent of xUnit's setup/teardown
|
|
|
|
concept. It's however recommended to have explicit fixture references in your
|
|
|
|
tests or test classes rather than relying on implicitely executing
|
|
|
|
setup/teardown functions, especially if they are far away from the actual tests.
|
|
|
|
|
|
|
|
Here is a an example for making a ``db`` fixture available in a directory::
|
|
|
|
|
|
|
|
# content of a/conftest.py
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
class DB:
|
|
|
|
pass
|
|
|
|
|
|
|
|
@pytest.fixture(scope="session")
|
|
|
|
def db():
|
|
|
|
return DB()
|
|
|
|
|
|
|
|
and then a test module in that directory::
|
|
|
|
|
|
|
|
# content of a/test_db.py
|
|
|
|
def test_a1(db):
|
|
|
|
assert 0, db # to show value
|
|
|
|
|
|
|
|
another test module::
|
|
|
|
|
|
|
|
# content of a/test_db2.py
|
|
|
|
def test_a2(db):
|
|
|
|
assert 0, db # to show value
|
|
|
|
|
|
|
|
and then a module in a sister directory which will not see
|
|
|
|
the ``db`` fixture::
|
|
|
|
|
|
|
|
# content of b/test_error.py
|
|
|
|
def test_root(db): # no db here, will error out
|
|
|
|
pass
|
|
|
|
|
|
|
|
We can run this::
|
|
|
|
|
|
|
|
$ py.test
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-11-09 06:36:16 +08:00
|
|
|
collected 7 items
|
2012-11-07 18:11:40 +08:00
|
|
|
|
2012-11-09 06:36:16 +08:00
|
|
|
test_step.py .Fx.
|
2012-11-07 18:11:40 +08:00
|
|
|
a/test_db.py F
|
|
|
|
a/test_db2.py F
|
|
|
|
b/test_error.py E
|
|
|
|
|
|
|
|
================================== ERRORS ==================================
|
|
|
|
_______________________ ERROR at setup of test_root ________________________
|
2012-11-20 21:01:31 +08:00
|
|
|
file /tmp/doc-exec-138/b/test_error.py, line 1
|
2012-11-07 18:11:40 +08:00
|
|
|
def test_root(db): # no db here, will error out
|
|
|
|
fixture 'db' not found
|
|
|
|
available fixtures: pytestconfig, recwarn, monkeypatch, capfd, capsys, tmpdir
|
|
|
|
use 'py.test --fixtures [testpath]' for help on them.
|
|
|
|
|
2012-11-20 21:01:31 +08:00
|
|
|
/tmp/doc-exec-138/b/test_error.py:1
|
2012-11-07 18:11:40 +08:00
|
|
|
================================= FAILURES =================================
|
2012-11-09 06:36:16 +08:00
|
|
|
____________________ TestUserHandling.test_modification ____________________
|
|
|
|
|
2012-11-20 21:01:31 +08:00
|
|
|
self = <test_step.TestUserHandling instance at 0x1492d88>
|
2012-11-09 06:36:16 +08:00
|
|
|
|
|
|
|
def test_modification(self):
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_step.py:9: AssertionError
|
2012-11-07 18:11:40 +08:00
|
|
|
_________________________________ test_a1 __________________________________
|
|
|
|
|
2012-11-20 21:01:31 +08:00
|
|
|
db = <conftest.DB instance at 0x1498e60>
|
2012-11-07 18:11:40 +08:00
|
|
|
|
|
|
|
def test_a1(db):
|
|
|
|
> assert 0, db # to show value
|
2012-11-20 21:01:31 +08:00
|
|
|
E AssertionError: <conftest.DB instance at 0x1498e60>
|
2012-11-07 18:11:40 +08:00
|
|
|
|
|
|
|
a/test_db.py:2: AssertionError
|
|
|
|
_________________________________ test_a2 __________________________________
|
|
|
|
|
2012-11-20 21:01:31 +08:00
|
|
|
db = <conftest.DB instance at 0x1498e60>
|
2012-11-07 18:11:40 +08:00
|
|
|
|
|
|
|
def test_a2(db):
|
|
|
|
> assert 0, db # to show value
|
2012-11-20 21:01:31 +08:00
|
|
|
E AssertionError: <conftest.DB instance at 0x1498e60>
|
2012-11-07 18:11:40 +08:00
|
|
|
|
|
|
|
a/test_db2.py:2: AssertionError
|
2012-11-20 21:01:31 +08:00
|
|
|
========== 3 failed, 2 passed, 1 xfailed, 1 error in 0.04 seconds ==========
|
2012-11-07 18:11:40 +08:00
|
|
|
|
|
|
|
The two test modules in the ``a`` directory see the same ``db`` fixture instance
|
|
|
|
while the one test in the sister-directory ``b`` doesn't see it. We could of course
|
|
|
|
also define a ``db`` fixture in that sister directory's ``conftest.py`` file.
|
|
|
|
Note that each fixture is only instantiated if there is a test actually needing
|
|
|
|
it (unless you use "autouse" fixture which are always executed ahead of the first test
|
|
|
|
executing).
|
|
|
|
|
|
|
|
|
2012-11-09 06:36:16 +08:00
|
|
|
post-process test reports / failures
|
|
|
|
---------------------------------------
|
|
|
|
|
|
|
|
If you want to postprocess test reports and need access to the executing
|
|
|
|
environment you can implement a hook that gets called when the test
|
|
|
|
"report" object is about to be created. Here we write out all failing
|
|
|
|
test calls and also access a fixture (if it was used by the test) in
|
|
|
|
case you want to query/look at it during your post processing. In our
|
|
|
|
case we just write some informations out to a ``failures`` file::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
@pytest.mark.tryfirst
|
|
|
|
def pytest_runtest_makereport(item, call, __multicall__):
|
|
|
|
# execute all other hooks to obtain the report object
|
|
|
|
rep = __multicall__.execute()
|
|
|
|
|
|
|
|
# we only look at actual failing test calls, not setup/teardown
|
|
|
|
if rep.when == "call" and rep.failed:
|
|
|
|
mode = "a" if os.path.exists("failures") else "w"
|
|
|
|
with open("failures", mode) as f:
|
|
|
|
# let's also access a fixture for the fun of it
|
|
|
|
if "tmpdir" in item.funcargs:
|
|
|
|
extra = " (%s)" % item.funcargs["tmpdir"]
|
|
|
|
else:
|
|
|
|
extra = ""
|
|
|
|
|
|
|
|
f.write(rep.nodeid + extra + "\n")
|
|
|
|
return rep
|
|
|
|
|
|
|
|
if you then have failing tests::
|
|
|
|
|
|
|
|
# content of test_module.py
|
|
|
|
def test_fail1(tmpdir):
|
|
|
|
assert 0
|
|
|
|
def test_fail2():
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
and run them::
|
|
|
|
|
|
|
|
$ py.test test_module.py
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-11-09 06:36:16 +08:00
|
|
|
collected 2 items
|
|
|
|
|
|
|
|
test_module.py FF
|
|
|
|
|
|
|
|
================================= FAILURES =================================
|
|
|
|
________________________________ test_fail1 ________________________________
|
|
|
|
|
2012-11-20 21:01:31 +08:00
|
|
|
tmpdir = local('/tmp/pytest-543/test_fail10')
|
2012-11-09 06:36:16 +08:00
|
|
|
|
|
|
|
def test_fail1(tmpdir):
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_module.py:2: AssertionError
|
|
|
|
________________________________ test_fail2 ________________________________
|
|
|
|
|
|
|
|
def test_fail2():
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_module.py:4: AssertionError
|
2012-11-20 20:42:00 +08:00
|
|
|
========================= 2 failed in 0.02 seconds =========================
|
2012-11-09 06:36:16 +08:00
|
|
|
|
|
|
|
you will have a "failures" file which contains the failing test ids::
|
|
|
|
|
|
|
|
$ cat failures
|
2012-11-20 21:01:31 +08:00
|
|
|
test_module.py::test_fail1 (/tmp/pytest-543/test_fail10)
|
2012-11-09 06:36:16 +08:00
|
|
|
test_module.py::test_fail2
|
2012-11-14 16:39:21 +08:00
|
|
|
|
|
|
|
Making test result information available in fixtures
|
|
|
|
-----------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
If you want to make test result reports available in fixture finalizers
|
|
|
|
here is a little example implemented via a local plugin::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.tryfirst
|
|
|
|
def pytest_runtest_makereport(item, call, __multicall__):
|
|
|
|
# execute all other hooks to obtain the report object
|
|
|
|
rep = __multicall__.execute()
|
|
|
|
|
|
|
|
# set an report attribute for each phase of a call, which can
|
|
|
|
# be "setup", "call", "teardown"
|
|
|
|
|
|
|
|
setattr(item, "rep_" + rep.when, rep)
|
|
|
|
return rep
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def something(request):
|
|
|
|
def fin():
|
|
|
|
# request.node is an "item" because we use the default
|
|
|
|
# "function" scope
|
|
|
|
if request.node.rep_setup.failed:
|
|
|
|
print "setting up a test failed!", request.node.nodeid
|
|
|
|
elif request.node.rep_setup.passed:
|
|
|
|
if request.node.rep_call.failed:
|
|
|
|
print "executing test failed", request.node.nodeid
|
|
|
|
request.addfinalizer(fin)
|
|
|
|
|
|
|
|
|
|
|
|
if you then have failing tests::
|
|
|
|
|
|
|
|
# content of test_module.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def other():
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test_setup_fails(something, other):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_call_fails(something):
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test_fail2():
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
and run it::
|
|
|
|
|
|
|
|
$ py.test -s test_module.py
|
|
|
|
=========================== test session starts ============================
|
2012-11-20 21:01:31 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.4
|
2012-11-14 16:39:21 +08:00
|
|
|
collected 3 items
|
|
|
|
|
|
|
|
test_module.py EFF
|
|
|
|
|
|
|
|
================================== ERRORS ==================================
|
|
|
|
____________________ ERROR at setup of test_setup_fails ____________________
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def other():
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_module.py:6: AssertionError
|
|
|
|
================================= FAILURES =================================
|
|
|
|
_____________________________ test_call_fails ______________________________
|
|
|
|
|
|
|
|
something = None
|
|
|
|
|
|
|
|
def test_call_fails(something):
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_module.py:12: AssertionError
|
|
|
|
________________________________ test_fail2 ________________________________
|
|
|
|
|
|
|
|
def test_fail2():
|
|
|
|
> assert 0
|
|
|
|
E assert 0
|
|
|
|
|
|
|
|
test_module.py:15: AssertionError
|
|
|
|
==================== 2 failed, 1 error in 0.01 seconds =====================
|
|
|
|
setting up a test failed! test_module.py::test_setup_fails
|
|
|
|
executing test failed test_module.py::test_call_fails
|
|
|
|
|
|
|
|
You'll see that the fixture finalizers could use the precise reporting
|
|
|
|
information.
|
|
|
|
|