pytest : Sharing a fixture across tests in a module

As an extension to the last article on getting started with pytest , we introduce the concept of 'scope' in fixtures. We can add a scope='module' parameter to the @pytest.fixture invocation to cause the decorated mySetUp fixture function to only be invoked once per test module. Multiple test functions in a test module will thus each receive the same mySetUp 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 access the fixture function.
conftest.py looks like















module "test_one.py" looks like











module "test_two.py" looks like












Output:


Comments

Popular Posts