Skip to content

Commit

Permalink
Merge pull request #7776 from nicoddemus/backport-7772
Browse files Browse the repository at this point in the history
[6.0.x] Add docs about reusing fixtures from other projects (#7772)
  • Loading branch information
nicoddemus committed Sep 19, 2020
2 parents 89d03b2 + 5e14e7d commit f5b1f44
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions doc/en/fixture.rst
Expand Up @@ -1136,8 +1136,8 @@ and teared down after every test that used it.

.. _`usefixtures`:

Using fixtures from classes, modules or projects
----------------------------------------------------------------------
Use fixtures in classes and modules with ``usefixtures``
--------------------------------------------------------

.. regendoc:wipe
Expand Down Expand Up @@ -1531,3 +1531,37 @@ Given the tests file structure is:
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.


Using fixtures from other projects
----------------------------------

Usually projects that provide pytest support will use :ref:`entry points <setuptools entry points>`,
so just installing those projects into an environment will make those fixtures available for use.

In case you want to use fixtures from a project that does not use entry points, you can
define :globalvar:`pytest_plugins` in your top ``conftest.py`` file to register that module
as a plugin.

Suppose you have some fixtures in ``mylibrary.fixtures`` and you want to reuse them into your
``app/tests`` directory.

All you need to do is to define :globalvar:`pytest_plugins` in ``app/tests/conftest.py``
pointing to that module.

.. code-block:: python
pytest_plugins = "mylibrary.fixtures"
This effectively registers ``mylibrary.fixtures`` as a plugin, making all its fixtures and
hooks available to tests in ``app/tests``.

.. note::

Sometimes users will *import* fixtures from other projects for use, however this is not
recommended: importing fixtures into a module will register them in pytest
as *defined* in that module.

This has minor consequences, such as appearing multiple times in ``pytest --help``,
but it is not **recommended** because this behavior might change/stop working
in future versions.

0 comments on commit f5b1f44

Please sign in to comment.