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
2017-01-03 06:07:41 +08:00
.. _`Dependency injection`: http://en.wikipedia.org/wiki/Dependency_injection
2012-10-05 16:21:35 +08:00
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
2014-01-18 19:31:33 +08:00
offer dramatic improvements over the classic xUnit style of setup/teardown
2012-10-12 20:52:36 +08:00
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
2013-10-01 18:37:11 +08:00
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
2013-09-26 18:57:21 +08:00
Fixtures as Function arguments
2012-10-07 19:06:17 +08:00
-----------------------------------------
Test functions can receive fixture objects by naming them as an input
2014-01-18 19:31:33 +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
2017-06-29 11:00:14 +08:00
return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
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 0 # for demo purposes
2012-10-05 16:21:35 +08:00
2014-01-18 19:31:33 +08:00
Here, the `` test_ehlo `` needs the `` smtp `` fixture value. pytest
will discover and call the :py:func: `@pytest.fixture <_pytest.python.fixture>`
2012-10-20 15:52:03 +08:00
marked `` smtp `` fixture function. Running the test looks like this::
2012-10-05 16:21:35 +08:00
2016-06-21 22:16:57 +08:00
$ pytest test_smtpsimple.py
2015-06-07 05:30:49 +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
2012-10-09 20:35:17 +08:00
test_smtpsimple.py F
2017-07-04 07:29:13 +08:00
2015-06-07 05:30:49 +08:00
======= FAILURES ========
_______ test_ehlo ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-09 20:35:17 +08:00
def test_ehlo(smtp):
response, msg = smtp.ehlo()
assert response == 250
2015-03-26 16:34:10 +08:00
> assert 0 # for demo purposes
E assert 0
2017-07-04 07:29:13 +08:00
2015-03-26 16:34:10 +08:00
test_smtpsimple.py:11: AssertionError
2015-06-07 05:30:49 +08:00
======= 1 failed in 0.12 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
2014-01-18 19:31:33 +08:00
the exact protocol used by `` pytest `` 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::
2016-06-21 22:16:57 +08:00
pytest --fixtures test_simplefactory.py
2012-10-05 16:21:35 +08:00
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
2014-01-18 19:31:33 +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.
2017-06-21 10:43:34 +08:00
Fixtures: a prime example of dependency injection
2012-10-09 20:35:17 +08:00
---------------------------------------------------
2012-10-05 16:21:35 +08:00
2017-06-21 10:43:34 +08:00
Fixtures 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
2012-10-09 20:35:17 +08:00
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:
2013-09-26 18:57:21 +08:00
Sharing a fixture across tests in a module (or class/session)
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
2014-01-18 19:31:33 +08:00
Fixtures requiring network access depend on connectivity and are
2012-10-09 20:35:17 +08:00
usually time-expensive to create. Extending the previous example, we
2014-01-18 19:31:33 +08:00
can add a `` scope='module' `` parameter to the
2012-10-20 15:52:03 +08:00
:py:func: `@pytest.fixture <_pytest.python.fixture>` invocation
2014-01-18 19:31:33 +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
2014-01-18 19:31:33 +08:00
that tests from multiple test modules in the directory can
2013-05-08 03:37:08 +08:00
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():
2017-06-29 11:00:14 +08:00
return smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
2012-10-05 16:21:35 +08:00
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):
2015-07-31 10:46:25 +08:00
response, msg = smtp.ehlo()
assert response == 250
2015-09-22 22:52:35 +08:00
assert b"smtp.gmail.com" in msg
2017-06-21 20:06:52 +08:00
assert 0 # for demo purposes
2012-10-05 16:21:35 +08:00
def test_noop(smtp):
2015-07-31 10:46:25 +08:00
response, msg = smtp.noop()
assert response == 250
2012-10-05 16:21:35 +08:00
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::
2016-06-21 22:16:57 +08:00
$ pytest test_module.py
2015-06-07 05:30:49 +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:
2012-10-09 20:35:17 +08:00
collected 2 items
2017-07-04 07:29:13 +08:00
2012-10-09 20:35:17 +08:00
test_module.py FF
2017-07-04 07:29:13 +08:00
2015-06-07 05:30:49 +08:00
======= FAILURES ========
_______ test_ehlo ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-05 16:21:35 +08:00
def test_ehlo(smtp):
2015-09-22 20:02:11 +08:00
response, msg = smtp.ehlo()
assert response == 250
2015-09-22 22:52:35 +08:00
assert b"smtp.gmail.com" in msg
2015-09-22 21:05:56 +08:00
> assert 0 # for demo purposes
E assert 0
2017-07-04 07:29:13 +08:00
2015-09-22 21:05:56 +08:00
test_module.py:6: AssertionError
2015-06-07 05:30:49 +08:00
_______ test_noop ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-05 16:21:35 +08:00
def test_noop(smtp):
2015-09-22 20:02:11 +08:00
response, msg = smtp.noop()
assert response == 250
2012-10-05 16:21:35 +08:00
> assert 0 # for demo purposes
E assert 0
2017-07-04 07:29:13 +08:00
2012-10-07 19:06:17 +08:00
test_module.py:11: AssertionError
2015-06-07 05:30:49 +08:00
======= 2 failed in 0.12 seconds ========
2012-10-05 16:21:35 +08:00
2014-01-18 19:31:33 +08:00
You see the two `` assert 0 `` failing and more importantly you can also see
that the same (module-scoped) `` smtp `` object was passed into the two
test functions because pytest shows the incoming argument values in the
2012-10-09 20:35:17 +08:00
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 ``
2015-07-10 08:50:38 +08:00
instance, you can simply declare it:
.. code-block :: python
2012-10-20 15:52:03 +08:00
2013-10-21 19:36:22 +08:00
@pytest.fixture(scope="session")
2012-10-20 15:52:03 +08:00
def smtp(...):
2014-01-18 19:31:33 +08:00
# the returned fixture value will be shared for
2012-10-20 15:52:03 +08:00
# all tests needing it
2013-05-08 03:37:08 +08:00
2013-09-26 18:57:21 +08:00
.. _`finalization`:
2014-01-18 19:31:33 +08:00
2015-09-25 21:54:03 +08:00
Fixture finalization / executing teardown code
2013-05-08 03:37:08 +08:00
-------------------------------------------------------------
2014-01-18 19:31:33 +08:00
pytest supports execution of fixture specific finalization code
2016-06-08 07:59:58 +08:00
when the fixture goes out of scope. By using a `` yield `` statement instead of `` return `` , all
2017-02-25 23:06:51 +08:00
the code after the *yield* statement serves as the teardown code:
.. code-block :: python
2013-05-08 03:37:08 +08:00
2013-09-26 18:57:21 +08:00
# content of conftest.py
2013-05-08 03:37:08 +08:00
import smtplib
import pytest
@pytest.fixture(scope="module")
2017-04-26 16:31:53 +08:00
def smtp():
2017-06-29 11:00:14 +08:00
smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
2016-06-08 07:59:58 +08:00
yield smtp # provide the fixture value
print("teardown smtp")
smtp.close()
2013-05-08 03:37:08 +08:00
2016-11-29 17:25:57 +08:00
The `` print `` and `` smtp.close() `` statements will execute when the last test in
the module has finished execution, regardless of the exception status of the
tests.
2013-05-08 03:37:08 +08:00
2013-09-26 18:57:21 +08:00
Let's execute it::
2016-06-21 22:16:57 +08:00
$ pytest -s -q --tb=no
2013-10-04 01:09:18 +08:00
FFteardown smtp
2017-07-04 07:29:13 +08:00
2015-06-07 05:30:49 +08:00
2 failed in 0.12 seconds
2013-09-26 18:57:21 +08:00
We see that the `` smtp `` instance is finalized after the two
tests finished execution. Note that if we decorated our fixture
function with `` scope='function' `` then fixture setup and cleanup would
2014-01-18 19:31:33 +08:00
occur around each single test. In either case the test
2013-09-26 18:57:21 +08:00
module itself does not need to change or know about these details
of fixture setup.
2017-02-25 23:06:51 +08:00
Note that we can also seamlessly use the `` yield `` syntax with `` with `` statements:
.. code-block :: python
2016-06-08 07:59:58 +08:00
# content of test_yield2.py
2017-02-25 23:06:51 +08:00
import smtplib
2016-06-08 07:59:58 +08:00
import pytest
2017-02-25 23:06:51 +08:00
@pytest.fixture(scope="module")
2017-04-25 22:35:19 +08:00
def smtp():
2017-06-29 11:00:14 +08:00
with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp:
2017-02-25 23:06:51 +08:00
yield smtp # provide the fixture value
2016-06-08 07:59:58 +08:00
2017-02-25 23:06:51 +08:00
The `` smtp `` connection will be closed after the test finished execution
because the `` smtp `` object automatically closes when
2016-06-08 07:59:58 +08:00
the `` with `` statement ends.
2017-06-21 10:43:34 +08:00
Note that if an exception happens during the *setup* code (before the `` yield `` keyword), the
2017-06-21 20:06:52 +08:00
*teardown* code (after the `` yield `` ) will not be called.
2017-06-21 10:43:34 +08:00
2013-05-08 03:37:08 +08:00
2016-06-08 07:59:58 +08:00
.. note ::
Prior to version 2.10, in order to use a `` yield `` statement to execute teardown code one
had to mark a fixture using the `` yield_fixture `` marker. From 2.10 onward, normal
fixtures can use `` yield `` directly so the `` yield_fixture `` decorator is no longer needed
and considered deprecated.
2017-06-21 10:43:34 +08:00
An alternative option for executing *teardown* code is to
make use of the `` addfinalizer `` method of the `request-context`_ object to register
2017-06-21 20:06:52 +08:00
finalization functions.
2016-06-08 07:59:58 +08:00
2017-06-21 10:43:34 +08:00
Here's the `` smtp `` fixture changed to use `` addfinalizer `` for cleanup:
.. code-block :: python
# content of conftest.py
import smtplib
import pytest
@pytest.fixture(scope="module")
def smtp(request):
2017-06-29 11:00:14 +08:00
smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
2017-06-21 10:43:34 +08:00
def fin():
print ("teardown smtp")
smtp.close()
request.addfinalizer(fin)
return smtp # provide the fixture value
2017-06-21 20:06:52 +08:00
Both `` yield `` and `` addfinalizer `` methods work similarly by calling their code after the test
2017-06-21 10:43:34 +08:00
ends, but `` addfinalizer `` has two key differences over `` yield `` :
1. It is possible to register multiple finalizer functions.
2. Finalizers will always be called regardless if the fixture *setup* code raises an exception.
This is handy to properly close all resources created by a fixture even if one of them
fails to be created/acquired::
2016-06-08 07:59:58 +08:00
2017-06-21 10:43:34 +08:00
@pytest.fixture
def equipments(request):
r = []
for port in ('C1', 'C3', 'C28'):
equip = connect(port)
request.addfinalizer(equip.disconnect)
r.append(equip)
return r
2016-03-20 21:10:20 +08:00
2017-06-21 10:43:34 +08:00
In the example above, if `` "C28" `` fails with an exception, `` "C1" `` and `` "C3" `` will still
2017-06-21 20:06:52 +08:00
be properly closed. Of course, if an exception happens before the finalize function is
registered then it will not be executed.
2016-03-20 21:10:20 +08:00
2012-10-12 20:52:36 +08:00
.. _`request-context`:
2012-10-06 01:20:40 +08:00
2013-09-26 18:57:21 +08:00
Fixtures can introspect the requesting test context
2012-10-07 19:06:17 +08:00
-------------------------------------------------------------
2013-09-26 18:57:21 +08:00
Fixture function can accept the :py:class: `request <FixtureRequest>` object
to introspect the "requesting" test function, class or module context.
2014-01-18 19:31:33 +08:00
Further extending the previous `` smtp `` fixture example, let's
2013-09-26 18:57:21 +08:00
read an optional server URL from the test module which uses our fixture::
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):
2015-07-31 10:46:25 +08:00
server = getattr(request.module, "smtpserver", "smtp.gmail.com")
2017-06-29 11:00:14 +08:00
smtp = smtplib.SMTP(server, 587, timeout=5)
2016-06-08 07:59:58 +08:00
yield smtp
print ("finalizing %s (%s)" % (smtp, server))
smtp.close()
2012-10-05 16:21:35 +08:00
2013-09-26 18:57:21 +08:00
We use the `` request.module `` attribute to optionally obtain an
`` smtpserver `` attribute from the test module. If we just execute
again, nothing much has changed::
2012-10-05 16:21:35 +08:00
2016-06-21 22:16:57 +08:00
$ pytest -s -q --tb=no
2015-09-22 22:52:35 +08:00
FFfinalizing <smtplib.SMTP object at 0xdeadbeef> (smtp.gmail.com)
2017-07-04 07:29:13 +08:00
2017-03-14 06:41:20 +08:00
2 failed in 0.12 seconds
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::
2014-01-18 19:31:33 +08:00
2012-10-07 19:06:17 +08:00
# content of test_anothersmtp.py
2014-01-18 19:31:33 +08:00
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::
2016-06-21 22:16:57 +08:00
$ pytest -qq --tb=short test_anothersmtp.py
2012-10-07 19:06:17 +08:00
F
2015-06-07 05:30:49 +08:00
======= FAILURES ========
_______ test_showhelo ________
2012-10-07 19:06:17 +08:00
test_anothersmtp.py:5: in test_showhelo
2014-07-17 22:55:24 +08:00
assert 0, smtp.helo()
2015-09-22 22:52:35 +08:00
E AssertionError: (250, b'mail.python.org')
2014-09-24 20:46:56 +08:00
E assert 0
2016-11-10 05:44:58 +08:00
------------------------- Captured stdout teardown -------------------------
finalizing <smtplib.SMTP object at 0xdeadbeef> (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
2016-06-08 07:59:58 +08:00
Parametrizing fixtures
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
2014-01-18 19:31:33 +08:00
write exhaustive functional tests for components which themselves can be
2012-10-07 19:06:17 +08:00
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
2014-01-18 19:31:33 +08:00
@pytest.fixture(scope="module",
2015-07-31 10:46:25 +08:00
params=["smtp.gmail.com", "mail.python.org"])
2012-10-05 16:21:35 +08:00
def smtp(request):
2017-06-29 11:00:14 +08:00
smtp = smtplib.SMTP(request.param, 587, timeout=5)
2016-06-08 07:59:58 +08:00
yield smtp
print ("finalizing %s" % smtp)
smtp.close()
2012-10-05 16:21:35 +08:00
2014-01-18 19:31:33 +08:00
The main change is the declaration of `` params `` with
2012-10-20 15:52:03 +08:00
: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
2014-01-18 19:31:33 +08:00
a value via `` request.param `` . No test function code needs to change.
2012-10-05 16:21:35 +08:00
So let's just do another run::
2016-06-21 22:16:57 +08:00
$ pytest -q test_module.py
2012-10-05 16:21:35 +08:00
FFFF
2015-06-07 05:30:49 +08:00
======= FAILURES ========
2015-09-22 20:02:11 +08:00
_______ test_ehlo[smtp.gmail.com] ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-05 16:21:35 +08:00
def test_ehlo(smtp):
2015-09-22 20:02:11 +08:00
response, msg = smtp.ehlo()
assert response == 250
2015-09-22 22:52:35 +08:00
assert b"smtp.gmail.com" in msg
2015-09-22 21:05:56 +08:00
> assert 0 # for demo purposes
E assert 0
2017-07-04 07:29:13 +08:00
2015-09-22 21:05:56 +08:00
test_module.py:6: AssertionError
2015-09-22 20:02:11 +08:00
_______ test_noop[smtp.gmail.com] ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-05 16:21:35 +08:00
def test_noop(smtp):
2015-09-22 20:02:11 +08:00
response, msg = smtp.noop()
assert response == 250
2012-10-05 16:21:35 +08:00
> assert 0 # for demo purposes
E assert 0
2017-07-04 07:29:13 +08:00
2012-10-07 19:06:17 +08:00
test_module.py:11: AssertionError
2015-06-07 05:30:49 +08:00
_______ test_ehlo[mail.python.org] ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-05 16:21:35 +08:00
def test_ehlo(smtp):
2015-09-22 20:02:11 +08:00
response, msg = smtp.ehlo()
assert response == 250
2015-09-22 22:52:35 +08:00
> assert b"smtp.gmail.com" in msg
2017-07-04 07:29:13 +08:00
E AssertionError: assert b'smtp.gmail.com' in b'mail.python.org\nPIPELINING\nSIZE 51200000\nETRN\nSTARTTLS\nAUTH DIGEST-MD5 NTLM CRAM-MD5\nENHANCEDSTATUSCODES\n8BITMIME\nDSN\nSMTPUTF8'
2015-09-22 21:05:56 +08:00
test_module.py:5: AssertionError
2015-09-22 20:02:11 +08:00
-------------------------- Captured stdout setup ---------------------------
2015-09-22 22:52:35 +08:00
finalizing <smtplib.SMTP object at 0xdeadbeef>
2015-06-07 05:30:49 +08:00
_______ test_noop[mail.python.org] ________
2017-07-04 07:29:13 +08:00
2015-09-22 22:52:35 +08:00
smtp = <smtplib.SMTP object at 0xdeadbeef>
2017-07-04 07:29:13 +08:00
2012-10-05 16:21:35 +08:00
def test_noop(smtp):
2015-09-22 20:02:11 +08:00
response, msg = smtp.noop()
assert response == 250
2012-10-05 16:21:35 +08:00
> assert 0 # for demo purposes
E assert 0
2017-07-04 07:29:13 +08:00
2012-10-07 19:06:17 +08:00
test_module.py:11: AssertionError
2016-11-10 05:44:58 +08:00
------------------------- Captured stdout teardown -------------------------
finalizing <smtplib.SMTP object at 0xdeadbeef>
2015-06-07 05:30:49 +08:00
4 failed in 0.12 seconds
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
2014-01-18 19:31:33 +08:00
`` smtp `` instances. Note also, that with the `` mail.python.org ``
connection the second test fails in `` test_ehlo `` because a
2012-10-09 20:35:17 +08:00
different server string is expected than what arrived.
2012-10-05 16:21:35 +08:00
2014-10-08 07:43:27 +08:00
pytest will build a string that is the test ID for each fixture value
2015-07-31 10:46:25 +08:00
in a parametrized fixture, e.g. `` test_ehlo[smtp.gmail.com] `` and
2014-10-08 07:43:27 +08:00
`` test_ehlo[mail.python.org] `` in the above examples. These IDs can
be used with `` -k `` to select specific cases to run, and they will
also identify the specific case when one is failing. Running pytest
with `` --collect-only `` will show the generated IDs.
Numbers, strings, booleans and None will have their usual string
representation used in the test ID. For other objects, pytest will
make a string based on the argument name. It is possible to customise
the string used in a test ID for a certain fixture value by using the
`` ids `` keyword argument::
2015-07-31 10:21:19 +08:00
# content of test_ids.py
2014-10-08 07:43:27 +08:00
import pytest
@pytest.fixture(params=[0, 1], ids=["spam", "ham"])
def a(request):
return request.param
def test_a(a):
pass
def idfn(fixture_value):
if fixture_value == 0:
return "eggs"
else:
return None
@pytest.fixture(params=[0, 1], ids=idfn)
def b(request):
return request.param
def test_b(b):
pass
The above shows how `` ids `` can be either a list of strings to use or
a function which will be called with the fixture value and then
has to return a string to use. In the latter case if the function
return `` None `` then pytest's auto-generated ID will be used.
Running the above tests results in the following test IDs being used::
2016-06-21 22:16:57 +08:00
$ pytest --collect-only
2015-06-07 05:30:49 +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:
collected 10 items
2015-03-26 16:34:10 +08:00
<Module 'test_anothersmtp.py'>
2015-09-22 20:02:11 +08:00
<Function 'test_showhelo[smtp.gmail.com]'>
2015-03-26 16:34:10 +08:00
<Function 'test_showhelo[mail.python.org]'>
2015-09-22 20:02:11 +08:00
<Module 'test_ids.py'>
<Function 'test_a[spam]'>
<Function 'test_a[ham]'>
<Function 'test_b[eggs]'>
<Function 'test_b[1]'>
2015-03-26 16:34:10 +08:00
<Module 'test_module.py'>
2015-09-22 20:02:11 +08:00
<Function 'test_ehlo[smtp.gmail.com]'>
<Function 'test_noop[smtp.gmail.com]'>
2015-03-26 16:34:10 +08:00
<Function 'test_ehlo[mail.python.org]'>
<Function 'test_noop[mail.python.org]'>
2017-07-04 07:29:13 +08:00
2015-12-06 23:14:23 +08:00
======= no tests ran in 0.12 seconds ========
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
2014-01-18 19:31:33 +08:00
2012-10-05 16:21:35 +08:00
import pytest
2017-02-17 02:41:51 +08:00
class App(object):
2012-10-05 16:21:35 +08:00
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::
2016-06-21 22:16:57 +08:00
$ pytest -v test_appsetup.py
2015-06-07 05:30:49 +08:00
======= test session starts ========
2017-05-13 19:25:52 +08:00
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5
2015-09-22 20:02:11 +08:00
cachedir: .cache
2017-03-14 06:41:20 +08:00
rootdir: $REGENDOC_TMPDIR, inifile:
2012-10-05 16:21:35 +08:00
collecting ... collected 2 items
2017-07-04 07:29:13 +08:00
2015-09-22 20:02:11 +08:00
test_appsetup.py::test_smtp_exists[smtp.gmail.com] PASSED
2014-08-08 03:41:51 +08:00
test_appsetup.py::test_smtp_exists[mail.python.org] PASSED
2017-07-04 07:29:13 +08:00
2015-06-07 05:30:49 +08:00
======= 2 passed in 0.12 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
2014-01-18 19:31:33 +08:00
need for the `` app `` fixture to be aware of the `` smtp `` parametrization
as pytest will fully analyse the fixture dependency graph.
2012-10-20 15:52:03 +08:00
Note, that the `` app `` fixture has a scope of `` module `` and uses a
module-scoped `` smtp `` fixture. The example would still work if `` smtp ``
2014-01-18 19:31:33 +08:00
was cached on a `` session `` scope: it is fine for fixtures to use
2012-10-20 15:52:03 +08:00
"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
2014-01-18 19:31:33 +08:00
first execute with one instance and then finalizers are called
2012-10-05 16:21:35 +08:00
before the next fixture instance is created. Among other things,
this eases testing of applications which create and use global state.
2016-06-28 00:09:18 +08:00
The following example uses two parametrized fixture, 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
2016-03-20 22:53:32 +08:00
print (" SETUP modarg %s" % param)
2016-06-08 07:59:58 +08:00
yield param
print (" TEARDOWN modarg %s" % param)
2012-10-05 16:21:35 +08:00
@pytest.fixture(scope="function", params=[1,2])
def otherarg(request):
2016-03-20 22:53:32 +08:00
param = request.param
print (" SETUP otherarg %s" % param)
2016-06-08 07:59:58 +08:00
yield param
print (" TEARDOWN otherarg %s" % param)
2012-10-05 16:21:35 +08:00
def test_0(otherarg):
2016-03-20 22:53:32 +08:00
print (" RUN test0 with otherarg %s" % otherarg)
2012-10-05 16:21:35 +08:00
def test_1(modarg):
2016-03-20 22:53:32 +08:00
print (" RUN test1 with modarg %s" % modarg)
2012-10-05 16:21:35 +08:00
def test_2(otherarg, modarg):
2016-03-20 22:53:32 +08:00
print (" RUN test2 with otherarg %s and modarg %s" % (otherarg, modarg))
2012-10-05 16:21:35 +08:00
Let's run the tests in verbose mode and with looking at the print-output::
2016-06-21 22:16:57 +08:00
$ pytest -v -s test_module.py
2016-05-31 17:15:57 +08:00
======= test session starts ========
2017-05-13 19:25:52 +08:00
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python3.5
2015-09-22 20:02:11 +08:00
cachedir: .cache
2017-03-14 06:41:20 +08:00
rootdir: $REGENDOC_TMPDIR, inifile:
2016-05-31 17:15:57 +08:00
collecting ... collected 8 items
2017-07-04 07:29:13 +08:00
2016-03-20 22:53:32 +08:00
test_module.py::test_0[1] SETUP otherarg 1
RUN test0 with otherarg 1
PASSED TEARDOWN otherarg 1
2017-07-04 07:29:13 +08:00
2016-03-20 22:53:32 +08:00
test_module.py::test_0[2] SETUP otherarg 2
RUN test0 with otherarg 2
PASSED TEARDOWN otherarg 2
2017-07-04 07:29:13 +08:00
2016-03-20 22:53:32 +08:00
test_module.py::test_1[mod1] SETUP modarg mod1
RUN test1 with modarg mod1
2014-08-08 21:20:37 +08:00
PASSED
2016-03-20 22:53:32 +08:00
test_module.py::test_2[1-mod1] SETUP otherarg 1
RUN test2 with otherarg 1 and modarg mod1
PASSED TEARDOWN otherarg 1
2017-07-04 07:29:13 +08:00
2016-03-20 22:53:32 +08:00
test_module.py::test_2[2-mod1] SETUP otherarg 2
RUN test2 with otherarg 2 and modarg mod1
PASSED TEARDOWN otherarg 2
2017-07-04 07:29:13 +08:00
2016-03-20 22:53:32 +08:00
test_module.py::test_1[mod2] TEARDOWN modarg mod1
SETUP modarg mod2
RUN test1 with modarg mod2
2014-08-08 21:20:37 +08:00
PASSED
2016-03-20 22:53:32 +08:00
test_module.py::test_2[1-mod2] SETUP otherarg 1
RUN test2 with otherarg 1 and modarg mod2
PASSED TEARDOWN otherarg 1
2017-07-04 07:29:13 +08:00
2016-03-20 22:53:32 +08:00
test_module.py::test_2[2-mod2] SETUP otherarg 2
RUN test2 with otherarg 2 and modarg mod2
PASSED TEARDOWN otherarg 2
TEARDOWN modarg mod2
2017-07-04 07:29:13 +08:00
2016-05-31 17:15:57 +08:00
======= 8 passed in 0.12 seconds ========
2016-03-20 22:53:32 +08:00
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.
In particular notice that test_0 is completely independent and finishes first.
Then test_1 is executed with `` mod1 `` , then test_2 with `` mod1 `` , then test_1
with `` mod2 `` and finally test_2 with `` mod2 `` .
2012-10-05 16:21:35 +08:00
2016-03-20 22:53:32 +08:00
The `` otherarg `` parametrized resource (having function scope) was set up before
and teared down after every test that used it.
2012-10-05 16:21:35 +08:00
2013-05-08 03:37:08 +08:00
.. _`usefixtures`:
2012-10-07 19:06:17 +08:00
2015-09-25 21:54:03 +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
2016-10-06 01:49:28 +08:00
directory. Here is how you can use the standard `tempfile
2012-10-09 20:35:17 +08:00
<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
2014-01-18 19:31:33 +08:00
2012-10-05 16:21:35 +08:00
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")
2017-02-17 02:41:51 +08:00
class TestDirectoryInit(object):
2012-10-05 16:21:35 +08:00
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
2016-06-21 22:16:57 +08:00
$ pytest -q
2012-10-06 01:20:40 +08:00
..
2015-06-07 05:30:49 +08:00
2 passed in 0.12 seconds
2012-10-05 16:21:35 +08:00
2015-07-10 08:50:38 +08:00
You can specify multiple fixtures like this:
.. code-block :: python
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
2014-01-18 19:31:33 +08:00
and you may specify fixture usage at the test module level, using
2015-07-10 08:50:38 +08:00
a generic feature of the mark mechanism:
.. code-block :: python
2012-10-05 16:21:35 +08:00
2012-10-06 01:20:40 +08:00
pytestmark = pytest.mark.usefixtures("cleandir")
2012-10-05 16:21:35 +08:00
2015-09-25 21:54:03 +08:00
Note that the assigned variable *must* be called `` pytestmark `` , assigning e.g.
`` foomark `` will not activate the fixtures.
2014-01-18 19:31:33 +08:00
Lastly you can put fixtures required by all tests in your project
2015-07-10 08:50:38 +08:00
into an ini-file:
2012-10-05 16:21:35 +08:00
2015-07-10 08:50:38 +08:00
.. code-block :: ini
2012-10-05 16:21:35 +08:00
2015-07-10 08:50:38 +08:00
# content of pytest.ini
2012-10-05 16:21:35 +08:00
[pytest]
2012-10-06 01:20:40 +08:00
usefixtures = cleandir
2012-10-09 20:35:17 +08:00
2013-12-17 14:58:49 +08:00
.. _`autouse`:
2012-10-12 20:52:36 +08:00
.. _`autouse fixtures`:
2012-10-05 16:21:35 +08:00
2015-09-25 21:54:03 +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
2017-06-21 10:43:34 +08:00
without declaring a function argument explicitly or a `usefixtures`_ decorator.
As a practical example, suppose we have a database fixture which has a
2012-10-07 19:06:17 +08:00
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
2014-01-18 19:31:33 +08:00
2012-10-05 16:21:35 +08:00
import pytest
2017-02-17 02:41:51 +08:00
class DB(object):
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()
2017-02-17 02:41:51 +08:00
class TestClass(object):
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__)
2016-06-08 07:59:58 +08:00
yield
db.rollback()
2012-10-05 16:21:35 +08:00
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::
2016-06-21 22:16:57 +08:00
$ pytest -q
2012-10-06 01:20:40 +08:00
..
2015-06-07 05:30:49 +08:00
2 passed in 0.12 seconds
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
2016-06-30 04:51:35 +08:00
- autouse fixtures obey the `` scope= `` keyword-argument: if an autouse fixture
has `` scope='session' `` it will only be run once, no matter where it is
defined. `` scope='class' `` means it will be run once per class, etc.
2014-01-18 19:31:33 +08:00
- if an autouse fixture is defined in a test module, all its test
functions automatically use it.
2012-10-05 16:21:35 +08:00
2014-01-18 19:31:33 +08:00
- if an autouse fixture is defined in a conftest.py file then all tests in
2015-07-08 07:35:21 +08:00
all test modules below 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
2015-07-08 07:35:21 +08:00
any work and avoid otherwise expensive imports or computation.
2012-10-05 16:21:35 +08:00
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
2014-01-18 19:31:33 +08:00
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
2016-06-08 07:59:58 +08:00
@pytest.fixture
2012-10-05 16:21:35 +08:00
def transact(self, request, db):
db.begin()
2016-06-08 07:59:58 +08:00
yield
db.rollback()
2012-10-05 16:21:35 +08:00
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")
2017-02-17 02:41:51 +08:00
class TestClass(object):
2012-10-05 16:21:35 +08:00
def test_method1(self):
...
2012-10-08 17:22:31 +08:00
All test methods in this TestClass will use the transaction fixture while
2014-01-18 19:31:33 +08:00
other test classes or functions in the module will not use it unless
2012-10-09 20:35:17 +08:00
they also add a `` transact `` reference.
2012-10-05 16:21:35 +08:00
2016-06-28 00:09:18 +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.
2015-03-02 01:54:24 +08:00
Overriding fixtures on various levels
-------------------------------------
In relatively large test suite, you most likely need to `` override `` a `` global `` or `` root `` fixture with a `` locally ``
defined one, keeping the test code readable and maintainable.
Override a fixture on a folder (conftest) level
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Given the tests file structure is:
::
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture
def username():
return 'username'
test_something.py
# content of tests/test_something.py
def test_username(username):
assert username == 'username'
subfolder/
__init__.py
conftest.py
# content of tests/subfolder/conftest.py
import pytest
@pytest.fixture
def username(username):
return 'overridden-' + username
test_something.py
# content of tests/subfolder/test_something.py
def test_username(username):
assert username == 'overridden-username'
As you can see, a fixture with the same name can be overridden for certain test folder level.
Note that the `` base `` or `` super `` fixture can be accessed from the `` overriding ``
fixture easily - used in the example above.
Override a fixture on a test module level
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Given the tests file structure is:
::
tests/
__init__.py
conftest.py
# content of tests/conftest.py
@pytest.fixture
def username():
return 'username'
test_something.py
# content of tests/test_something.py
import pytest
@pytest.fixture
def username(username):
return 'overridden-' + username
def test_username(username):
assert username == 'overridden-username'
test_something_else.py
# content of tests/test_something_else.py
import pytest
@pytest.fixture
def username(username):
return 'overridden-else-' + username
def test_username(username):
assert username == 'overridden-else-username'
In the example above, a fixture with the same name can be overridden for certain test module.
Override a fixture with direct test parametrization
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Given the tests file structure is:
::
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture
def username():
return 'username'
@pytest.fixture
def other_username(username):
return 'other-' + username
test_something.py
# content of tests/test_something.py
import pytest
@pytest.mark.parametrize('username', ['directly-overridden-username'])
def test_username(username):
assert username == 'directly-overridden-username'
@pytest.mark.parametrize('username', ['directly-overridden-username-other'])
def test_username_other(other_username):
2016-12-20 08:48:32 +08:00
assert other_username == 'other-directly-overridden-username-other'
2015-03-02 01:54:24 +08:00
In the example above, a fixture value is overridden by the test parameter value. Note that the value of the fixture
can be overridden this way even if the test doesn't use it directly (doesn't mention it in the function prototype).
Override a parametrized fixture with non-parametrized one and vice versa
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Given the tests file structure is:
::
tests/
__init__.py
conftest.py
# content of tests/conftest.py
import pytest
@pytest.fixture(params=['one', 'two', 'three'])
def parametrized_username(request):
return request.param
@pytest.fixture
def non_parametrized_username(request):
return 'username'
test_something.py
# content of tests/test_something.py
import pytest
@pytest.fixture
def parametrized_username():
return 'overridden-username'
@pytest.fixture(params=['one', 'two', 'three'])
def non_parametrized_username(request):
return request.param
def test_username(parametrized_username):
assert parametrized_username == 'overridden-username'
def test_parametrized_username(non_parametrized_username):
assert non_parametrized_username in ['one', 'two', 'three']
test_something_else.py
# content of tests/test_something_else.py
def test_username(parametrized_username):
assert parametrized_username in ['one', 'two', 'three']
def test_username(non_parametrized_username):
assert non_parametrized_username == 'username'
2013-09-26 18:57:21 +08:00
2015-03-02 01:54:24 +08:00
In the example above, a parametrized fixture is overridden with a non-parametrized version, and
a non-parametrized fixture is overridden with a parametrized version for certain test module.
The same applies for the test folder level obviously.