From 45bf24d3d03a52baf1910a531a36a11969d7cf00 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 19 Sep 2020 11:18:57 -0300 Subject: [PATCH 1/3] Add docs about reusing fixtures from other projects Fix #7754 --- doc/en/fixture.rst | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index b05f60876a2..896e174f079 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -1136,8 +1136,8 @@ and teared down after every test that used it. .. _`usefixtures`: -Using fixtures from classes, modules or projects ----------------------------------------------------------------------- +Use fixtures implicitly with @pytest.mark.usefixtures +----------------------------------------------------- .. regendoc:wipe @@ -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 `, +so just by 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. From 6d898185a6443f110a13164256c6b10f7c0e0e02 Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 19 Sep 2020 16:03:38 -0300 Subject: [PATCH 2/3] Update doc/en/fixture.rst Co-authored-by: Ran Benita --- doc/en/fixture.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index 896e174f079..3fd054d53ec 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -1537,7 +1537,7 @@ Using fixtures from other projects ---------------------------------- Usually projects that provide pytest support will use :ref:`entry points `, -so just by installing those projects into an environment will make those fixtures available for use. +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 From a8ac38f8afa377b854e94c99dbe4a662eb3daa3e Mon Sep 17 00:00:00 2001 From: Bruno Oliveira Date: Sat, 19 Sep 2020 16:06:27 -0300 Subject: [PATCH 3/3] Change title --- doc/en/fixture.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/en/fixture.rst b/doc/en/fixture.rst index 3fd054d53ec..90e88d87620 100644 --- a/doc/en/fixture.rst +++ b/doc/en/fixture.rst @@ -1136,8 +1136,8 @@ and teared down after every test that used it. .. _`usefixtures`: -Use fixtures implicitly with @pytest.mark.usefixtures ------------------------------------------------------ +Use fixtures in classes and modules with ``usefixtures`` +-------------------------------------------------------- .. regendoc:wipe