Skip to content

Commit e23af00

Browse files
committed
Introduce a dedicated section about conftest.py
Also reorganize conftest-related content a bit. Closes #1505, closes #1770.
1 parent 531e0dc commit e23af00

File tree

5 files changed

+35
-19
lines changed

5 files changed

+35
-19
lines changed

changelog/1505.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Introduce a dedicated section about conftest.py.

doc/en/assert.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ the ``pytest_assertrepr_compare`` hook.
209209
.. autofunction:: _pytest.hookspec.pytest_assertrepr_compare
210210
:noindex:
211211

212-
As an example consider adding the following hook in a conftest.py which
213-
provides an alternative explanation for ``Foo`` objects::
212+
As an example consider adding the following hook in a :ref:`conftest.py <conftest.py>`
213+
file which provides an alternative explanation for ``Foo`` objects::
214214

215215
# content of conftest.py
216216
from test_foocompare import Foo

doc/en/fixture.rst

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,39 @@ It's a prime example of `dependency injection`_ where fixture
127127
functions take the role of the *injector* and test functions are the
128128
*consumers* of fixture objects.
129129

130+
.. _`conftest.py`:
131+
.. _`conftest`:
132+
133+
``conftest.py``: sharing fixture functions
134+
------------------------------------------
135+
136+
If during implementing your tests you realize that you
137+
want to use a fixture function from multiple test files you can move it
138+
to a ``conftest.py`` file.
139+
You don't need to import the fixture you want to use in a test, it
140+
automatically gets discovered by pytest. The discovery of
141+
fixture functions starts at test classes, then test modules, then
142+
``conftest.py`` files and finally builtin and third party plugins.
143+
144+
You can also use the ``conftest.py`` file to implement
145+
:ref:`local per-directory plugins <conftest.py plugins>`.
146+
147+
Sharing test data
148+
-----------------
149+
150+
If you want to make test data from files available to your tests, a good way
151+
to do this is by loading these data in a fixture for use by your tests.
152+
This makes use of the automatic caching mechanisms of pytest.
153+
154+
Another good approach is by adding the data files in the ``tests`` folder.
155+
There are also community plugins available to help managing this aspect of
156+
testing, e.g. `pytest-datadir <https://github.com/gabrielcnr/pytest-datadir>`__
157+
and `pytest-datafiles <https://pypi.python.org/pypi/pytest-datafiles>`__.
158+
130159
.. _smtpshared:
131160

132-
Scope: Sharing a fixture across tests in a class, module or session
133-
-------------------------------------------------------------------
161+
Scope: sharing a fixture instance across tests in a class, module or session
162+
----------------------------------------------------------------------------
134163

135164
.. regendoc:wipe
136165
@@ -878,17 +907,6 @@ All test methods in this TestClass will use the transaction fixture while
878907
other test classes or functions in the module will not use it unless
879908
they also add a ``transact`` reference.
880909

881-
882-
Shifting (visibility of) fixture functions
883-
----------------------------------------------------
884-
885-
If during implementing your tests you realize that you
886-
want to use a fixture function from multiple test files you can move it
887-
to a :ref:`conftest.py <conftest.py>` file or even separately installable
888-
:ref:`plugins <plugins>` without changing test code. The discovery of
889-
fixtures functions starts at test classes, then test modules, then
890-
``conftest.py`` files and finally builtin and third party plugins.
891-
892910
Overriding fixtures on various levels
893911
-------------------------------------
894912

doc/en/plugins.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ environment you can type::
9494

9595
and will get an extended test header which shows activated plugins
9696
and their names. It will also print local plugins aka
97-
:ref:`conftest.py <conftest>` files when they are loaded.
97+
:ref:`conftest.py <conftest.py plugins>` files when they are loaded.
9898

9999
.. _`cmdunregister`:
100100

@@ -155,4 +155,3 @@ in the `pytest repository <https://github.com/pytest-dev/pytest>`_.
155155
_pytest.terminal
156156
_pytest.tmpdir
157157
_pytest.unittest
158-

doc/en/writing_plugins.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ Plugin discovery order at tool startup
5757

5858
.. _`pytest/plugin`: http://bitbucket.org/pytest-dev/pytest/src/tip/pytest/plugin/
5959
.. _`conftest.py plugins`:
60-
.. _`conftest.py`:
6160
.. _`localplugin`:
62-
.. _`conftest`:
6361
.. _`local conftest plugins`:
6462

6563
conftest.py: local per-directory plugins

0 commit comments

Comments
 (0)