2012-10-05 16:21:35 +08:00
|
|
|
.. _fixture:
|
2012-10-07 19:06:17 +08:00
|
|
|
.. _fixtures:
|
2012-10-05 16:21:35 +08:00
|
|
|
.. _`fixture functions`:
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
pytest fixtures: explicit, modular, scalable
|
2012-10-05 16:21:35 +08:00
|
|
|
========================================================
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
.. currentmodule:: _pytest.python
|
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
.. versionadded:: 2.0/2.3/2.4
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
.. _`xUnit`: http://en.wikipedia.org/wiki/XUnit
|
2013-05-08 03:37:08 +08:00
|
|
|
.. _`purpose of test fixtures`: http://en.wikipedia.org/wiki/Test_fixture#Software
|
2012-10-05 16:21:35 +08:00
|
|
|
.. _`Dependency injection`: http://en.wikipedia.org/wiki/Dependency_injection#Definition
|
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
The `purpose of test fixtures`_ is to provide a fixed baseline
|
|
|
|
upon which tests can reliably and repeatedly execute. pytest fixtures
|
2012-10-12 20:52:36 +08:00
|
|
|
offer dramatic improvements over the classic xUnit style of setup/teardown
|
|
|
|
functions:
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
* fixtures have explicit names and are activated by declaring their use
|
|
|
|
from test functions, modules, classes or whole projects.
|
|
|
|
|
|
|
|
* fixtures are implemented in a modular manner, as each fixture name
|
2013-05-08 03:37:08 +08:00
|
|
|
triggers a *fixture function* which can itself use other fixtures.
|
2012-10-12 20:52:36 +08:00
|
|
|
|
|
|
|
* fixture management scales from simple unit to complex
|
2012-10-20 15:52:03 +08:00
|
|
|
functional testing, allowing to parametrize fixtures and tests according
|
|
|
|
to configuration and component options, or to re-use fixtures
|
|
|
|
across class, module or whole test session scopes.
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2012-10-25 19:48:31 +08:00
|
|
|
In addition, pytest continues to support :ref:`xunitsetup`. You can mix
|
|
|
|
both styles, moving incrementally from classic to new style, as you
|
|
|
|
prefer. You can also start out from existing :ref:`unittest.TestCase
|
|
|
|
style <unittest.TestCase>` or :ref:`nose based <nosestyle>` projects.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
.. _`funcargs`:
|
|
|
|
.. _`funcarg mechanism`:
|
|
|
|
.. _`fixture function`:
|
2012-10-09 20:35:17 +08:00
|
|
|
.. _`@pytest.fixture`:
|
|
|
|
.. _`pytest.fixture`:
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
Fixtures as Function arguments (funcargs)
|
2012-10-07 19:06:17 +08:00
|
|
|
-----------------------------------------
|
|
|
|
|
|
|
|
Test functions can receive fixture objects by naming them as an input
|
2012-10-09 20:35:17 +08:00
|
|
|
argument. For each argument name, a fixture function with that name provides
|
2012-10-12 20:52:36 +08:00
|
|
|
the fixture object. Fixture functions are registered by marking them with
|
2012-10-20 15:52:03 +08:00
|
|
|
:py:func:`@pytest.fixture <_pytest.python.fixture>`. Let's look at a simple
|
2012-10-12 20:52:36 +08:00
|
|
|
self-contained test module containing a fixture and a test function
|
|
|
|
using it::
|
2012-10-09 20:35:17 +08:00
|
|
|
|
|
|
|
# content of ./test_smtpsimple.py
|
2012-10-05 16:21:35 +08:00
|
|
|
import pytest
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
@pytest.fixture
|
2012-10-09 20:35:17 +08:00
|
|
|
def smtp():
|
|
|
|
import smtplib
|
|
|
|
return smtplib.SMTP("merlinux.eu")
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
def test_ehlo(smtp):
|
|
|
|
response, msg = smtp.ehlo()
|
|
|
|
assert response == 250
|
|
|
|
assert "merlinux" in msg
|
|
|
|
assert 0 # for demo purposes
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-28 21:54:49 +08:00
|
|
|
Here, the ``test_ehlo`` needs the ``smtp`` fixture value. pytest
|
2012-10-20 15:52:03 +08:00
|
|
|
will discover and call the :py:func:`@pytest.fixture <_pytest.python.fixture>`
|
|
|
|
marked ``smtp`` fixture function. Running the test looks like this::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
$ py.test test_smtpsimple.py
|
2012-10-05 16:21:35 +08:00
|
|
|
=========================== test session starts ============================
|
2013-04-30 18:26:30 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 1 items
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
test_smtpsimple.py F
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
================================= FAILURES =================================
|
2012-10-09 20:35:17 +08:00
|
|
|
________________________________ test_ehlo _________________________________
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x226cc20>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
def test_ehlo(smtp):
|
|
|
|
response, msg = smtp.ehlo()
|
|
|
|
assert response == 250
|
|
|
|
assert "merlinux" in msg
|
|
|
|
> assert 0 # for demo purposes
|
|
|
|
E assert 0
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
test_smtpsimple.py:12: AssertionError
|
2013-04-30 18:26:30 +08:00
|
|
|
========================= 1 failed in 0.20 seconds =========================
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
In the failure traceback we see that the test function was called with a
|
|
|
|
``smtp`` argument, the ``smtplib.SMTP()`` instance created by the fixture
|
|
|
|
function. The test function fails on our deliberate ``assert 0``. Here is
|
|
|
|
an exact protocol of how py.test comes to call the test function this way:
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
1. pytest :ref:`finds <test discovery>` the ``test_ehlo`` because
|
2012-10-05 16:21:35 +08:00
|
|
|
of the ``test_`` prefix. The test function needs a function argument
|
2012-10-09 20:35:17 +08:00
|
|
|
named ``smtp``. A matching fixture function is discovered by
|
|
|
|
looking for a fixture-marked function named ``smtp``.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
2. ``smtp()`` is called to create an instance.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
3. ``test_ehlo(<SMTP instance>)`` is called and fails in the last
|
|
|
|
line of the test function.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
Note that if you misspell a function argument or want
|
|
|
|
to use one that isn't available, you'll see an error
|
|
|
|
with a list of available function arguments.
|
|
|
|
|
|
|
|
.. Note::
|
|
|
|
|
|
|
|
You can always issue::
|
|
|
|
|
|
|
|
py.test --fixtures test_simplefactory.py
|
|
|
|
|
|
|
|
to see available fixtures.
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
In versions prior to 2.3 there was no ``@pytest.fixture`` marker
|
2012-10-06 01:20:40 +08:00
|
|
|
and you had to use a magic ``pytest_funcarg__NAME`` prefix
|
2012-10-05 16:21:35 +08:00
|
|
|
for the fixture factory. This remains and will remain supported
|
2012-10-12 20:52:36 +08:00
|
|
|
but is not anymore advertised as the primary means of declaring fixture
|
2012-10-05 16:21:35 +08:00
|
|
|
functions.
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
Funcargs a prime example of dependency injection
|
|
|
|
---------------------------------------------------
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
When injecting fixtures to test functions, pytest-2.0 introduced the
|
|
|
|
term "funcargs" or "funcarg mechanism" which continues to be present
|
2013-05-08 03:37:08 +08:00
|
|
|
also in docs today. It now refers to the specific case of injecting
|
2012-10-18 21:06:55 +08:00
|
|
|
fixture values as arguments to test functions. With pytest-2.3 there are
|
2013-05-08 03:37:08 +08:00
|
|
|
more possibilities to use fixtures but "funcargs" remain as the main way
|
|
|
|
as they allow to directly state the dependencies of a test function.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
As the following examples show in more detail, funcargs allow test
|
|
|
|
functions to easily receive and work against specific pre-initialized
|
|
|
|
application objects without having to care about import/setup/cleanup
|
|
|
|
details. It's a prime example of `dependency injection`_ where fixture
|
|
|
|
functions take the role of the *injector* and test functions are the
|
|
|
|
*consumers* of fixture objects.
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
.. _smtpshared:
|
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
Working with a module-shared fixture
|
2012-10-09 20:35:17 +08:00
|
|
|
-----------------------------------------------------------------
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
.. regendoc:wipe
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
Fixtures requiring network access depend on connectivity and are
|
|
|
|
usually time-expensive to create. Extending the previous example, we
|
2012-10-20 15:52:03 +08:00
|
|
|
can add a ``scope='module'`` parameter to the
|
|
|
|
:py:func:`@pytest.fixture <_pytest.python.fixture>` invocation
|
2012-10-12 20:52:36 +08:00
|
|
|
to cause the decorated ``smtp`` fixture function to only be invoked once
|
2012-10-20 15:52:03 +08:00
|
|
|
per test module. Multiple test functions in a test module will thus
|
2013-05-08 03:37:08 +08:00
|
|
|
each receive the same ``smtp`` fixture instance. The next example puts
|
|
|
|
the fixture function into a separate ``conftest.py`` file so
|
|
|
|
that tests from multiple test modules in the directory can
|
|
|
|
access the fixture function::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
import pytest
|
|
|
|
import smtplib
|
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-10-05 16:21:35 +08:00
|
|
|
def smtp():
|
|
|
|
return smtplib.SMTP("merlinux.eu")
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
The name of the fixture again is ``smtp`` and you can access its result by
|
2012-11-09 02:05:46 +08:00
|
|
|
listing the name ``smtp`` as an input parameter in any test or fixture
|
2012-10-09 20:35:17 +08:00
|
|
|
function (in or below the directory where ``conftest.py`` is located)::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of test_module.py
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
def test_ehlo(smtp):
|
|
|
|
response = smtp.ehlo()
|
|
|
|
assert response[0] == 250
|
|
|
|
assert "merlinux" in response[1]
|
|
|
|
assert 0 # for demo purposes
|
|
|
|
|
|
|
|
def test_noop(smtp):
|
|
|
|
response = smtp.noop()
|
|
|
|
assert response[0] == 250
|
|
|
|
assert 0 # for demo purposes
|
|
|
|
|
|
|
|
We deliberately insert failing ``assert 0`` statements in order to
|
|
|
|
inspect what is going on and can now run the tests::
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
$ py.test test_module.py
|
|
|
|
=========================== test session starts ============================
|
2013-04-30 18:26:30 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
|
2012-10-09 20:35:17 +08:00
|
|
|
collected 2 items
|
|
|
|
|
|
|
|
test_module.py FF
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
================================= FAILURES =================================
|
|
|
|
________________________________ test_ehlo _________________________________
|
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x18a6368>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
def test_ehlo(smtp):
|
|
|
|
response = smtp.ehlo()
|
|
|
|
assert response[0] == 250
|
2012-10-18 18:24:50 +08:00
|
|
|
assert "merlinux" in response[1]
|
|
|
|
> assert 0 # for demo purposes
|
|
|
|
E assert 0
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
test_module.py:6: AssertionError
|
2012-10-05 16:21:35 +08:00
|
|
|
________________________________ test_noop _________________________________
|
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x18a6368>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
def test_noop(smtp):
|
|
|
|
response = smtp.noop()
|
|
|
|
assert response[0] == 250
|
|
|
|
> assert 0 # for demo purposes
|
|
|
|
E assert 0
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
test_module.py:11: AssertionError
|
2013-04-30 18:26:30 +08:00
|
|
|
========================= 2 failed in 0.26 seconds =========================
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
You see the two ``assert 0`` failing and more importantly you can also see
|
2012-10-20 15:52:03 +08:00
|
|
|
that the same (module-scoped) ``smtp`` object was passed into the two
|
2012-10-09 20:35:17 +08:00
|
|
|
test functions because pytest shows the incoming argument values in the
|
|
|
|
traceback. As a result, the two test functions using ``smtp`` run as
|
|
|
|
quick as a single one because they reuse the same instance.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
If you decide that you rather want to have a session-scoped ``smtp``
|
|
|
|
instance, you can simply declare it::
|
|
|
|
|
|
|
|
@pytest.fixture(scope=``session``)
|
|
|
|
def smtp(...):
|
|
|
|
# the returned fixture value will be shared for
|
|
|
|
# all tests needing it
|
2013-05-08 03:37:08 +08:00
|
|
|
|
|
|
|
.. _`contextfixtures`:
|
2012-10-20 15:52:03 +08:00
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
fixture finalization / teardowns
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
|
|
|
pytest supports two styles of fixture finalization:
|
|
|
|
|
|
|
|
- (new in pytest-2.4) by writing a contextmanager fixture
|
|
|
|
generator where a fixture value is "yielded" and the remainder
|
|
|
|
of the function serves as the teardown code. This integrates
|
|
|
|
very well with existing context managers.
|
|
|
|
|
|
|
|
- by making a fixture function accept a ``request`` argument
|
|
|
|
with which it can call ``request.addfinalizer(teardownfunction)``
|
|
|
|
to register a teardown callback function.
|
|
|
|
|
|
|
|
Both methods are strictly equivalent from pytest's view and will
|
|
|
|
remain supported in the future.
|
|
|
|
|
|
|
|
Because a number of people prefer the new contextmanager style
|
|
|
|
we describe it first::
|
|
|
|
|
|
|
|
# content of test_ctxfixture.py
|
|
|
|
|
|
|
|
import smtplib
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def smtp():
|
|
|
|
smtp = smtplib.SMTP("merlinux.eu")
|
|
|
|
yield smtp # provide the fixture value
|
|
|
|
print ("teardown smtp")
|
|
|
|
smtp.close()
|
|
|
|
|
|
|
|
pytest detects that you are using a ``yield`` in your fixture function,
|
|
|
|
turns it into a generator and:
|
|
|
|
|
|
|
|
a) iterates once into it for producing the value
|
|
|
|
b) iterates a second time for tearing the fixture down, expecting
|
|
|
|
a StopIteration (which is produced automatically from the Python
|
|
|
|
runtime when the generator returns).
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
The teardown will execute independently of the status of test functions.
|
|
|
|
You do not need to write the teardown code into a ``try-finally`` clause
|
|
|
|
like you would usually do with ``contextlib.contextmanager`` decorated
|
|
|
|
functions.
|
|
|
|
|
|
|
|
If the fixture generator yields a second value pytest will report
|
|
|
|
an error. Yielding cannot be used for parametrization. We'll describe
|
|
|
|
ways to implement parametrization further below.
|
|
|
|
|
|
|
|
Prior to pytest-2.4 you always needed to register a finalizer by accepting
|
|
|
|
a ``request`` object into your fixture function and calling
|
|
|
|
``request.addfinalizer`` with a teardown function::
|
|
|
|
|
|
|
|
import smtplib
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def smtp(request):
|
|
|
|
smtp = smtplib.SMTP("merlinux.eu")
|
|
|
|
def fin():
|
|
|
|
print ("teardown smtp")
|
|
|
|
smtp.close()
|
|
|
|
return smtp # provide the fixture value
|
|
|
|
|
|
|
|
This method of registering a finalizer reads more indirect
|
|
|
|
than the new contextmanager style syntax because ``fin``
|
|
|
|
is a callback function.
|
|
|
|
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
.. _`request-context`:
|
2012-10-06 01:20:40 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Fixtures can interact with the requesting test context
|
|
|
|
-------------------------------------------------------------
|
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
pytest provides a builtin :py:class:`request <FixtureRequest>` object,
|
2012-10-20 15:52:03 +08:00
|
|
|
which fixture functions can use to introspect the function, class or module
|
2013-05-08 03:37:08 +08:00
|
|
|
for which they are invoked.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
Further extending the previous ``smtp`` fixture example, let's
|
2013-05-08 03:37:08 +08:00
|
|
|
read an optional server URL from the module namespace::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
import pytest
|
|
|
|
import smtplib
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
@pytest.fixture(scope="module")
|
2012-10-05 16:21:35 +08:00
|
|
|
def smtp(request):
|
2012-10-07 19:06:17 +08:00
|
|
|
server = getattr(request.module, "smtpserver", "merlinux.eu")
|
|
|
|
smtp = smtplib.SMTP(server)
|
2013-05-08 03:37:08 +08:00
|
|
|
yield smtp # provide the fixture
|
|
|
|
print ("finalizing %s" % smtp)
|
|
|
|
smtp.close()
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
The finalizing part after the ``yield smtp`` statement will execute
|
|
|
|
when the last test using the ``smtp`` fixture has executed::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
$ py.test -s -q --tb=no
|
2012-10-06 01:20:40 +08:00
|
|
|
FF
|
2013-04-30 18:26:30 +08:00
|
|
|
finalizing <smtplib.SMTP instance at 0x1e10248>
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
We see that the ``smtp`` instance is finalized after the two
|
2013-05-08 03:37:08 +08:00
|
|
|
tests which use it finished executin. If we rather specify
|
|
|
|
``scope='function'`` then fixture setup and cleanup occurs
|
|
|
|
around each single test. Note that in either case the test
|
|
|
|
module itself does not need to change!
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
Let's quickly create another test module that actually sets the
|
2013-05-08 03:37:08 +08:00
|
|
|
server URL in its module namespace::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
# content of test_anothersmtp.py
|
|
|
|
|
2012-10-18 21:06:55 +08:00
|
|
|
smtpserver = "mail.python.org" # will be read by smtp fixture
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
def test_showhelo(smtp):
|
|
|
|
assert 0, smtp.helo()
|
|
|
|
|
|
|
|
Running it::
|
|
|
|
|
|
|
|
$ py.test -qq --tb=short test_anothersmtp.py
|
|
|
|
F
|
|
|
|
================================= FAILURES =================================
|
|
|
|
______________________________ test_showhelo _______________________________
|
|
|
|
test_anothersmtp.py:5: in test_showhelo
|
|
|
|
> assert 0, smtp.helo()
|
2012-10-19 16:53:28 +08:00
|
|
|
E AssertionError: (250, 'mail.python.org')
|
2012-10-09 20:35:17 +08:00
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
voila! The ``smtp`` fixture function picked up our mail server name
|
|
|
|
from the module namespace.
|
|
|
|
|
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
.. _`fixture-parametrize`:
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
Parametrizing a fixture
|
2012-10-05 16:21:35 +08:00
|
|
|
-----------------------------------------------------------------
|
|
|
|
|
2012-10-08 19:19:31 +08:00
|
|
|
Fixture functions can be parametrized in which case they will be called
|
2012-10-07 19:06:17 +08:00
|
|
|
multiple times, each time executing the set of dependent tests, i. e. the
|
|
|
|
tests that depend on this fixture. Test functions do usually not need
|
|
|
|
to be aware of their re-running. Fixture parametrization helps to
|
|
|
|
write exhaustive functional tests for components which themselves can be
|
|
|
|
configured in multiple ways.
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
Extending the previous example, we can flag the fixture to create two
|
|
|
|
``smtp`` fixture instances which will cause all tests using the fixture
|
|
|
|
to run twice. The fixture function gets access to each parameter
|
2013-04-04 20:36:44 +08:00
|
|
|
through the special :py:class:`request <FixtureRequest>` object::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
import pytest
|
|
|
|
import smtplib
|
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
@pytest.fixture(scope="module",
|
2012-10-05 16:21:35 +08:00
|
|
|
params=["merlinux.eu", "mail.python.org"])
|
|
|
|
def smtp(request):
|
|
|
|
smtp = smtplib.SMTP(request.param)
|
2013-05-08 03:37:08 +08:00
|
|
|
yield smtp
|
|
|
|
print ("finalizing %s" % smtp)
|
|
|
|
smtp.close()
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-20 15:52:03 +08:00
|
|
|
The main change is the declaration of ``params`` with
|
|
|
|
:py:func:`@pytest.fixture <_pytest.python.fixture>`, a list of values
|
2012-10-05 16:21:35 +08:00
|
|
|
for each of which the fixture function will execute and can access
|
|
|
|
a value via ``request.param``. No test function code needs to change.
|
|
|
|
So let's just do another run::
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
$ py.test -q test_module.py
|
2012-10-05 16:21:35 +08:00
|
|
|
FFFF
|
|
|
|
================================= FAILURES =================================
|
|
|
|
__________________________ test_ehlo[merlinux.eu] __________________________
|
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x1b38a28>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
def test_ehlo(smtp):
|
|
|
|
response = smtp.ehlo()
|
|
|
|
assert response[0] == 250
|
2012-10-18 18:24:50 +08:00
|
|
|
assert "merlinux" in response[1]
|
|
|
|
> assert 0 # for demo purposes
|
|
|
|
E assert 0
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
test_module.py:6: AssertionError
|
2012-10-05 16:21:35 +08:00
|
|
|
__________________________ test_noop[merlinux.eu] __________________________
|
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x1b38a28>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
def test_noop(smtp):
|
|
|
|
response = smtp.noop()
|
|
|
|
assert response[0] == 250
|
|
|
|
> assert 0 # for demo purposes
|
|
|
|
E assert 0
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
test_module.py:11: AssertionError
|
2012-10-05 16:21:35 +08:00
|
|
|
________________________ test_ehlo[mail.python.org] ________________________
|
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x1b496c8>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
def test_ehlo(smtp):
|
|
|
|
response = smtp.ehlo()
|
|
|
|
assert response[0] == 250
|
2012-10-18 18:24:50 +08:00
|
|
|
> assert "merlinux" in response[1]
|
2013-04-30 18:26:30 +08:00
|
|
|
E assert 'merlinux' in 'mail.python.org\nSIZE 25600000\nETRN\nSTARTTLS\nENHANCEDSTATUSCODES\n8BITMIME\nDSN'
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
test_module.py:5: AssertionError
|
2012-10-05 16:21:35 +08:00
|
|
|
________________________ test_noop[mail.python.org] ________________________
|
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
smtp = <smtplib.SMTP instance at 0x1b496c8>
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
def test_noop(smtp):
|
|
|
|
response = smtp.noop()
|
|
|
|
assert response[0] == 250
|
|
|
|
> assert 0 # for demo purposes
|
|
|
|
E assert 0
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
test_module.py:11: AssertionError
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
We see that our two test functions each ran twice, against the different
|
|
|
|
``smtp`` instances. Note also, that with the ``mail.python.org``
|
|
|
|
connection the second test fails in ``test_ehlo`` because a
|
|
|
|
different server string is expected than what arrived.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
.. _`interdependent fixtures`:
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
Modularity: using fixtures from a fixture function
|
2012-10-05 16:21:35 +08:00
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
You can not only use fixtures in test functions but fixture functions
|
|
|
|
can use other fixtures themselves. This contributes to a modular design
|
|
|
|
of your fixtures and allows re-use of framework-specific fixtures across
|
|
|
|
many projects. As a simple example, we can extend the previous example
|
|
|
|
and instantiate an object ``app`` where we stick the already defined
|
|
|
|
``smtp`` resource into it::
|
|
|
|
|
|
|
|
# content of test_appsetup.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
class App:
|
|
|
|
def __init__(self, smtp):
|
|
|
|
self.smtp = smtp
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def app(smtp):
|
|
|
|
return App(smtp)
|
|
|
|
|
|
|
|
def test_smtp_exists(app):
|
|
|
|
assert app.smtp
|
|
|
|
|
|
|
|
Here we declare an ``app`` fixture which receives the previously defined
|
|
|
|
``smtp`` fixture and instantiates an ``App`` object with it. Let's run it::
|
|
|
|
|
|
|
|
$ py.test -v test_appsetup.py
|
|
|
|
=========================== test session starts ============================
|
2013-04-30 18:26:30 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python
|
2012-10-05 16:21:35 +08:00
|
|
|
collecting ... collected 2 items
|
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
test_appsetup.py:12: test_smtp_exists[merlinux.eu] PASSED
|
|
|
|
test_appsetup.py:12: test_smtp_exists[mail.python.org] PASSED
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2013-04-30 18:26:30 +08:00
|
|
|
========================= 2 passed in 5.38 seconds =========================
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
Due to the parametrization of ``smtp`` the test will run twice with two
|
|
|
|
different ``App`` instances and respective smtp servers. There is no
|
|
|
|
need for the ``app`` fixture to be aware of the ``smtp`` parametrization
|
2012-10-20 15:52:03 +08:00
|
|
|
as pytest will fully analyse the fixture dependency graph.
|
|
|
|
|
|
|
|
Note, that the ``app`` fixture has a scope of ``module`` and uses a
|
|
|
|
module-scoped ``smtp`` fixture. The example would still work if ``smtp``
|
|
|
|
was cached on a ``session`` scope: it is fine for fixtures to use
|
|
|
|
"broader" scoped fixtures but not the other way round:
|
2012-10-19 17:12:13 +08:00
|
|
|
A session-scoped fixture could not use a module-scoped one in a
|
|
|
|
meaningful way.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
.. _`automatic per-resource grouping`:
|
|
|
|
|
|
|
|
Automatic grouping of tests by fixture instances
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc: wipe
|
|
|
|
|
|
|
|
pytest minimizes the number of active fixtures during test runs.
|
|
|
|
If you have a parametrized fixture, then all the tests using it will
|
|
|
|
first execute with one instance and then finalizers are called
|
|
|
|
before the next fixture instance is created. Among other things,
|
|
|
|
this eases testing of applications which create and use global state.
|
|
|
|
|
|
|
|
The following example uses two parametrized funcargs, one of which is
|
2012-10-09 20:35:17 +08:00
|
|
|
scoped on a per-module basis, and all the functions perform ``print`` calls
|
|
|
|
to show the setup/teardown flow::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of test_module.py
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture(scope="module", params=["mod1", "mod2"])
|
|
|
|
def modarg(request):
|
|
|
|
param = request.param
|
|
|
|
print "create", param
|
2013-05-08 03:37:08 +08:00
|
|
|
yield param
|
|
|
|
print ("fin %s" % param)
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
@pytest.fixture(scope="function", params=[1,2])
|
|
|
|
def otherarg(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
def test_0(otherarg):
|
|
|
|
print " test0", otherarg
|
|
|
|
def test_1(modarg):
|
|
|
|
print " test1", modarg
|
|
|
|
def test_2(otherarg, modarg):
|
|
|
|
print " test2", otherarg, modarg
|
|
|
|
|
|
|
|
Let's run the tests in verbose mode and with looking at the print-output::
|
|
|
|
|
|
|
|
$ py.test -v -s test_module.py
|
|
|
|
=========================== test session starts ============================
|
2013-04-30 18:26:30 +08:00
|
|
|
platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python
|
2012-10-05 16:21:35 +08:00
|
|
|
collecting ... collected 8 items
|
|
|
|
|
|
|
|
test_module.py:16: test_0[1] PASSED
|
|
|
|
test_module.py:16: test_0[2] PASSED
|
|
|
|
test_module.py:18: test_1[mod1] PASSED
|
|
|
|
test_module.py:20: test_2[1-mod1] PASSED
|
|
|
|
test_module.py:20: test_2[2-mod1] PASSED
|
|
|
|
test_module.py:18: test_1[mod2] PASSED
|
|
|
|
test_module.py:20: test_2[1-mod2] PASSED
|
|
|
|
test_module.py:20: test_2[2-mod2] PASSED
|
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
========================= 8 passed in 0.01 seconds =========================
|
2012-10-05 16:21:35 +08:00
|
|
|
test0 1
|
|
|
|
test0 2
|
|
|
|
create mod1
|
|
|
|
test1 mod1
|
|
|
|
test2 1 mod1
|
|
|
|
test2 2 mod1
|
|
|
|
fin mod1
|
|
|
|
create mod2
|
|
|
|
test1 mod2
|
|
|
|
test2 1 mod2
|
|
|
|
test2 2 mod2
|
|
|
|
fin mod2
|
|
|
|
|
|
|
|
You can see that the parametrized module-scoped ``modarg`` resource caused
|
|
|
|
an ordering of test execution that lead to the fewest possible "active" resources. The finalizer for the ``mod1`` parametrized resource was executed
|
|
|
|
before the ``mod2`` resource was setup.
|
|
|
|
|
|
|
|
|
2013-05-08 03:37:08 +08:00
|
|
|
.. _`usefixtures`:
|
2012-10-07 19:06:17 +08:00
|
|
|
|
|
|
|
using fixtures from classes, modules or projects
|
2012-10-05 16:21:35 +08:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
Sometimes test functions do not directly need access to a fixture object.
|
2012-10-09 20:35:17 +08:00
|
|
|
For example, tests may require to operate with an empty directory as the
|
|
|
|
current working directory but otherwise do not care for the concrete
|
|
|
|
directory. Here is how you can can use the standard `tempfile
|
|
|
|
<http://docs.python.org/library/tempfile.html>`_ and pytest fixtures to
|
2012-10-07 19:06:17 +08:00
|
|
|
achieve it. We separate the creation of the fixture into a conftest.py
|
|
|
|
file::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def cleandir():
|
|
|
|
newpath = tempfile.mkdtemp()
|
|
|
|
os.chdir(newpath)
|
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
and declare its use in a test module via a ``usefixtures`` marker::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of test_setenv.py
|
|
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
@pytest.mark.usefixtures("cleandir")
|
2012-10-05 16:21:35 +08:00
|
|
|
class TestDirectoryInit:
|
|
|
|
def test_cwd_starts_empty(self):
|
|
|
|
assert os.listdir(os.getcwd()) == []
|
|
|
|
with open("myfile", "w") as f:
|
|
|
|
f.write("hello")
|
|
|
|
|
|
|
|
def test_cwd_again_starts_empty(self):
|
|
|
|
assert os.listdir(os.getcwd()) == []
|
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
Due to the ``usefixtures`` marker, the ``cleandir`` fixture
|
2012-10-07 19:06:17 +08:00
|
|
|
will be required for the execution of each test method, just as if
|
2012-10-05 16:21:35 +08:00
|
|
|
you specified a "cleandir" function argument to each of them. Let's run it
|
2012-10-07 19:06:17 +08:00
|
|
|
to verify our fixture is activated and the tests pass::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
$ py.test -q
|
2012-10-06 01:20:40 +08:00
|
|
|
..
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
You can specify multiple fixtures like this::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
@pytest.mark.usefixtures("cleandir", "anotherfixture")
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-07 19:06:17 +08:00
|
|
|
and you may specify fixture usage at the test module level, using
|
2012-10-05 16:21:35 +08:00
|
|
|
a generic feature of the mark mechanism::
|
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
pytestmark = pytest.mark.usefixtures("cleandir")
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
Lastly you can put fixtures required by all tests in your project
|
|
|
|
into an ini-file::
|
|
|
|
|
|
|
|
# content of pytest.ini
|
|
|
|
|
|
|
|
[pytest]
|
2012-10-06 01:20:40 +08:00
|
|
|
usefixtures = cleandir
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
.. _`autouse fixtures`:
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
autouse fixtures (xUnit setup on steroids)
|
2012-10-05 16:21:35 +08:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
.. regendoc:wipe
|
|
|
|
|
|
|
|
Occasionally, you may want to have fixtures get invoked automatically
|
2012-10-07 19:06:17 +08:00
|
|
|
without a `usefixtures`_ or `funcargs`_ reference. As a practical
|
|
|
|
example, suppose we have a database fixture which has a
|
|
|
|
begin/rollback/commit architecture and we want to automatically surround
|
|
|
|
each test method by a transaction and a rollback. Here is a dummy
|
|
|
|
self-contained implementation of this idea::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of test_db_transact.py
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2012-10-08 17:22:31 +08:00
|
|
|
class DB:
|
2012-10-05 16:21:35 +08:00
|
|
|
def __init__(self):
|
2012-10-06 01:20:40 +08:00
|
|
|
self.intransaction = []
|
|
|
|
def begin(self, name):
|
|
|
|
self.intransaction.append(name)
|
|
|
|
def rollback(self):
|
|
|
|
self.intransaction.pop()
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-08 17:22:31 +08:00
|
|
|
@pytest.fixture(scope="module")
|
|
|
|
def db():
|
|
|
|
return DB()
|
|
|
|
|
2012-10-05 16:21:35 +08:00
|
|
|
class TestClass:
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2012-10-05 16:21:35 +08:00
|
|
|
def transact(self, request, db):
|
2012-10-06 01:20:40 +08:00
|
|
|
db.begin(request.function.__name__)
|
2012-10-05 16:21:35 +08:00
|
|
|
request.addfinalizer(db.rollback)
|
|
|
|
|
|
|
|
def test_method1(self, db):
|
2012-10-06 01:20:40 +08:00
|
|
|
assert db.intransaction == ["test_method1"]
|
|
|
|
|
|
|
|
def test_method2(self, db):
|
|
|
|
assert db.intransaction == ["test_method2"]
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
The class-level ``transact`` fixture is marked with *autouse=true*
|
2012-10-08 17:22:31 +08:00
|
|
|
which implies that all test methods in the class will use this fixture
|
2012-10-09 20:35:17 +08:00
|
|
|
without a need to state it in the test function signature or with a
|
|
|
|
class-level ``usefixtures`` decorator.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
If we run it, we get two passing tests::
|
|
|
|
|
|
|
|
$ py.test -q
|
|
|
|
..
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
Here is how autouse fixtures work in other scopes:
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
- if an autouse fixture is defined in a test module, all its test
|
2012-10-06 01:20:40 +08:00
|
|
|
functions automatically use it.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
- if an autouse fixture is defined in a conftest.py file then all tests in
|
2012-10-06 01:20:40 +08:00
|
|
|
all test modules belows its directory will invoke the fixture.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
- lastly, and **please use that with care**: if you define an autouse
|
2012-10-05 16:21:35 +08:00
|
|
|
fixture in a plugin, it will be invoked for all tests in all projects
|
|
|
|
where the plugin is installed. This can be useful if a fixture only
|
2012-10-06 01:20:40 +08:00
|
|
|
anyway works in the presence of certain settings e. g. in the ini-file. Such
|
|
|
|
a global fixture should always quickly determine if it should do
|
2012-10-05 16:21:35 +08:00
|
|
|
any work and avoid expensive imports or computation otherwise.
|
|
|
|
|
2012-10-08 17:22:31 +08:00
|
|
|
Note that the above ``transact`` fixture may very well be a fixture that
|
|
|
|
you want to make available in your project without having it generally
|
|
|
|
active. The canonical way to do that is to put the transact definition
|
2012-10-12 20:52:36 +08:00
|
|
|
into a conftest.py file **without** using ``autouse``::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
@pytest.fixture()
|
|
|
|
def transact(self, request, db):
|
|
|
|
db.begin()
|
|
|
|
request.addfinalizer(db.rollback)
|
|
|
|
|
2012-10-08 17:22:31 +08:00
|
|
|
and then e.g. have a TestClass using it by declaring the need::
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-06 01:20:40 +08:00
|
|
|
@pytest.mark.usefixtures("transact")
|
2012-10-05 16:21:35 +08:00
|
|
|
class TestClass:
|
|
|
|
def test_method1(self):
|
|
|
|
...
|
|
|
|
|
2012-10-08 17:22:31 +08:00
|
|
|
All test methods in this TestClass will use the transaction fixture while
|
2012-10-09 20:35:17 +08:00
|
|
|
other test classes or functions in the module will not use it unless
|
|
|
|
they also add a ``transact`` reference.
|
2012-10-05 16:21:35 +08:00
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
Shifting (visibility of) fixture functions
|
2012-10-07 19:06:17 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
If during implementing your tests you realize that you
|
|
|
|
want to use a fixture function from multiple test files you can move it
|
|
|
|
to a :ref:`conftest.py <conftest.py>` file or even separately installable
|
|
|
|
:ref:`plugins <plugins>` without changing test code. The discovery of
|
|
|
|
fixtures functions starts at test classes, then test modules, then
|
|
|
|
``conftest.py`` files and finally builtin and third party plugins.
|
|
|
|
|