pytest-dev / pytest-dev/pytest

@pytest.fixture(scope="package") works like session if placed in one file and used with pytest_plugins in conftest.py

Open
#7,232 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

topic: fixtures
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Hello.
I'm using pytest==5.4.1.
I have conftest.py file in root of the directory with content like this:

pytest_plugins = ['fixtures.configuration', 'fixtures.global']

my fixtures are placed in separate folder 'fixtures'.

When I run some fixture, which is defined in fixtures.configuration with scope "package" it's executed as session fixture. So I suppose this package scope realted to where fixtures are placed, than to where they're called. Is it expected behavior? if I want to use pytest_plugins in conftest.py this looks a bit inconvenient.

This can be tested by.

Creating conftest.py in root of directory with content:

pytest_plugins = ['fixtures.myfixtures']

Creating fixtures folder with myfxitures.py file inside of it with content.

@pytest.fixture(scope="package")
def hello_package():
      print('hello')
      yield
      print('world')

Creating package 'some' with two packages inside: 'hello', 'world'.

with files inside with content like
hello_test.py

def test_hello(hello_package):
        pass

world_test.py

def test_world(hello_package):
        pass

running: pytest -sv some

result:

collected 2 items                                                                                                                                                                                                                        

some/hello/hello_test.py::test_hello setup
PASSED
some/hworld/world_test.py::test_world PASSEDteardown

expected result:

collected 2 items                                                                                                                                                                                                                        

some/hello/hello_test.py::test_hello setup
PASSEDteardown

some/hworld/world_test.py::test_world setup
PASSEDteardown

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior from the root conftest.py, fixtures/myfixtures.py, and the hello/world test packages using pytest -sv some. Compare the observed fixture setup and teardown with the expected package-scoped behavior, then trace how pytest_plugins affects fixture scope. Done means the behavior is explained or corrected and the two package runs show separate setup and teardown.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.