Search code examples
pythonpytestfixtures

Can pytest fixtures and confest.py modules be shared across packages?


Suppose I have packageA which provides a class usefulClass, pytest fixtures in a test_stuff.py module, and test configurations in a conftest.py module.

Moreover, suppose that I have packageBand packageC both of which import packageA, installed via pip, and that they use usefulClass in the same way. Because they use usefulClass in the same way, packageB and packageC will require many identical pytest fixtures and configurations. In fact, their tests will primarily differ only in the sets of inputs over which they iterate.

Because the fixtures and configurations are identical and arise from the use of usefulClass, is it possible to define those fixtures and configurations in packageA, and then import them into the testing environments of packageB and packageC?

In particular, I would like to reuse the definition of pytest_generate_tests that appears in packageA's conftest.py module across dozens, if not hundreds of other packages. This way, I only need maintain one confest.py module, rather than hundreds.


Solution

  • The conftest.py file is not part of the module and cannot be imported from other modules.

    However you could create a module packageA.testutils, which you then can import in all conftest.py files, including packageA's:

    from packageA.testutils import *
    

    Maybe it even warrants creating an individual package that all your other packages depend on.