Skip to content

Clarify the documentation of fixture scopes #2848

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/538.doc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Clarify the documentation of available fixture scopes.
18 changes: 11 additions & 7 deletions doc/en/fixture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ functions:
* fixture management scales from simple unit to complex
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.
across function, class, module or whole test session scopes.

In addition, pytest continues to support :ref:`xunitsetup`. You can mix
both styles, moving incrementally from classic to new style, as you
Expand Down Expand Up @@ -129,8 +129,8 @@ functions take the role of the *injector* and test functions are the

.. _smtpshared:

Sharing a fixture across tests in a module (or class/session)
-----------------------------------------------------------------
Scope: Sharing a fixture across tests in a class, module or session
-------------------------------------------------------------------

.. regendoc:wipe

Expand All @@ -139,10 +139,12 @@ usually time-expensive to create. Extending the previous example, we
can add a ``scope='module'`` parameter to the
:py:func:`@pytest.fixture <_pytest.python.fixture>` invocation
to cause the decorated ``smtp`` fixture function to only be invoked once
per test module. Multiple test functions in a test module will thus
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
per test *module* (the default is to invoke once per test *function*).
Multiple test functions in a test module will thus
each receive the same ``smtp`` fixture instance, thus saving time.

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::

# content of conftest.py
Expand Down Expand Up @@ -223,6 +225,8 @@ instance, you can simply declare it:
# the returned fixture value will be shared for
# all tests needing it

Finally, the ``class`` scope will invoke the fixture once per test *class*.

.. _`finalization`:

Fixture finalization / executing teardown code
Expand Down