Skip to content

Commit

Permalink
Clarify the documentation of fixture scopes. Closes pytest-dev#538.
Browse files Browse the repository at this point in the history
  • Loading branch information
bilderbuchi committed Oct 17, 2017
1 parent ae4e596 commit baadd56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
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

0 comments on commit baadd56

Please sign in to comment.