2008-08-16 23:26:59 +08:00
|
|
|
===============================================
|
2010-07-27 03:15:15 +08:00
|
|
|
ATTIC documentation
|
2008-08-16 23:26:59 +08:00
|
|
|
===============================================
|
|
|
|
|
2009-04-02 21:12:26 +08:00
|
|
|
XXX REVIEW and remove the below XXX
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Customizing the testing process
|
2008-08-16 23:26:59 +08:00
|
|
|
===============================
|
|
|
|
|
|
|
|
writing conftest.py files
|
|
|
|
-----------------------------------
|
|
|
|
|
|
|
|
You may put conftest.py files containing project-specific
|
|
|
|
configuration in your project's root directory, it's usually
|
2010-07-27 03:15:15 +08:00
|
|
|
best to put it just into the same directory level as your
|
2014-01-18 19:31:33 +08:00
|
|
|
topmost ``__init__.py``. In fact, ``pytest`` performs
|
2010-07-27 03:15:15 +08:00
|
|
|
an "upwards" search starting from the directory that you specify
|
|
|
|
to be tested and will lookup configuration values right-to-left.
|
|
|
|
You may have options that reside e.g. in your home directory
|
2008-08-16 23:26:59 +08:00
|
|
|
but note that project specific settings will be considered
|
|
|
|
first. There is a flag that helps you debugging your
|
|
|
|
conftest.py configurations::
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2016-03-21 00:12:50 +08:00
|
|
|
py.test --trace-config
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
customizing the collecting and running process
|
2008-08-16 23:26:59 +08:00
|
|
|
-----------------------------------------------
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
To introduce different test items you can create
|
|
|
|
one or more ``conftest.py`` files in your project.
|
|
|
|
When the collection process traverses directories
|
|
|
|
and modules the default collectors will produce
|
|
|
|
custom Collectors and Items if they are found
|
|
|
|
in a local ``conftest.py`` file.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
Customizing the collection process in a module
|
2010-07-27 03:15:15 +08:00
|
|
|
----------------------------------------------
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
If you have a module where you want to take responsibility for
|
|
|
|
collecting your own test Items and possibly even for executing
|
2010-07-27 03:15:15 +08:00
|
|
|
a test then you can provide `generative tests`_ that yield
|
2008-08-21 18:18:58 +08:00
|
|
|
callables and possibly arguments as a tuple. This is especially
|
|
|
|
useful for calling application test machinery with different
|
|
|
|
parameter sets but counting each of the calls as a separate
|
2010-07-27 03:15:15 +08:00
|
|
|
tests.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2009-04-13 21:58:26 +08:00
|
|
|
.. _`generative tests`: features.html#generative-tests
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
The other extension possibility is about
|
|
|
|
specifying a custom test ``Item`` class which
|
|
|
|
is responsible for setting up and executing an underlying
|
|
|
|
test. Or you can extend the collection process for a whole
|
|
|
|
directory tree by putting Items in a ``conftest.py`` configuration file.
|
|
|
|
The collection process dynamically consults the *chain of conftest.py*
|
|
|
|
modules to determine collectors and items at ``Directory``, ``Module``,
|
|
|
|
``Class``, ``Function`` or ``Generator`` level respectively.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Customizing execution of Items and Functions
|
2009-04-02 21:12:26 +08:00
|
|
|
----------------------------------------------------
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
- ``pytest.Function`` test items control execution
|
2009-04-02 21:12:26 +08:00
|
|
|
of a test function through its ``function.runtest()`` method.
|
2010-07-27 03:15:15 +08:00
|
|
|
This method is responsible for performing setup and teardown
|
|
|
|
("Test Fixtures") for a test Function.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
- ``Function.execute(target, *args)`` methods are invoked by
|
2010-07-27 03:15:15 +08:00
|
|
|
the default ``Function.run()`` to actually execute a python
|
|
|
|
function with the given (usually empty set of) arguments.
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
.. _`py-dev mailing list`: http://codespeak.net/mailman/listinfo/py-dev
|
2009-02-27 18:18:27 +08:00
|
|
|
|
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
.. _`test generators`: funcargs.html#test-generators
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
.. _`generative tests`:
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
generative tests: yielding parametrized tests
|
|
|
|
====================================================
|
|
|
|
|
|
|
|
Deprecated since 1.0 in favour of `test generators`_.
|
|
|
|
|
|
|
|
*Generative tests* are test methods that are *generator functions* which
|
|
|
|
``yield`` callables and their arguments. This is useful for running a
|
|
|
|
test function multiple times against different parameters. Example::
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_generative():
|
|
|
|
for x in (42,17,49):
|
|
|
|
yield check, x
|
|
|
|
|
|
|
|
def check(arg):
|
2009-08-19 01:04:57 +08:00
|
|
|
assert arg % 7 == 0 # second generated tests fails!
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Note that ``test_generative()`` will cause three tests
|
2009-08-19 01:04:57 +08:00
|
|
|
to get run, notably ``check(42)``, ``check(17)`` and ``check(49)``
|
2010-07-27 03:15:15 +08:00
|
|
|
of which the middle one will obviously fail.
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
To make it easier to distinguish the generated tests it is possible to specify an explicit name for them, like for example::
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_generative():
|
|
|
|
for x in (42,17,49):
|
|
|
|
yield "case %d" % x, check, x
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
|
|
|
|
disabling a test class
|
2010-07-27 03:15:15 +08:00
|
|
|
----------------------
|
2009-08-19 01:04:57 +08:00
|
|
|
|
|
|
|
If you want to disable a complete test class you
|
2010-07-27 03:15:15 +08:00
|
|
|
can set the class-level attribute ``disabled``.
|
|
|
|
For example, in order to avoid running some tests on Win32::
|
2009-08-19 01:04:57 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestPosixOnly:
|
2009-08-19 01:04:57 +08:00
|
|
|
disabled = sys.platform == 'win32'
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-08-19 01:04:57 +08:00
|
|
|
def test_xxx(self):
|
2010-07-27 03:15:15 +08:00
|
|
|
...
|