Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixture with scope “class” running for every method #2938

Closed
grnmeira opened this issue Nov 21, 2017 · 6 comments
Closed

fixture with scope “class” running for every method #2938

grnmeira opened this issue Nov 21, 2017 · 6 comments
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed

Comments

@grnmeira
Copy link

I'm having a problem with fixture scopes and I wonder if this can be a bug. I'm running Pytest 3.2.5 with Python 3. I basically want to apply a fixture with scope "class" but parametrizing the fixture. The idea is to have groups of tests defined by classes, and to run those groups with different configurations. But when I apply also a fixture with scope "function", that seems to affect also the "class" fixture.

I have a question with an example code in Stack Overflow: https://stackoverflow.com/questions/47312835/pytest-fixture-with-scope-class-running-on-every-method

@grnmeira
Copy link
Author

After a bit more of research, I guess this may be a duplicated of issue #570.

@nicoddemus
Copy link
Member

Indeed seems like a duplicate, but let's keep this open because your example code might be useful.

@nicoddemus nicoddemus added topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed labels Nov 21, 2017
@ghost
Copy link

ghost commented Jul 12, 2018

I think that I run into similar issue while trying to figure out how the scopes work.
I'm not sure if this is the case, as I'm learning python and pytest and the error may be on my side, but:

in my conftest.py

@pytest.fixture(scope="session")
def session_node_fixture(request):
    print("\n", request.node, " session fixture")

@pytest.fixture(scope="class")
def class_node_fixture(request):
    print(request.node, " class fixture")

@pytest.fixture(scope="function")
def function_node_fixture(request):
    print(request.node, " function fixture")

@pytest.fixture(scope="module")
def module_node_fixture(request):
    print("\n", request.node, " module fixture")

in my pytest.ini
[pytest] usefixtures = class_node_fixture module_node_fixture session_node_fixture function_node_fixture

and in my test module 'test_module_b.py'

def test_testmodule_a():
    print("test testmodule_a")


def test_testmodule_b():
    print("test testmodule_b")

And the output of 'pytest -s' for that is:

collected 2 items                                                              

tests\fixture_tests\test_module_b.py
 <Session 'asta_automation_framework'>  session fixture

 <Module 'tests/fixture_tests/test_module_b.py'>  module fixture
<Function 'test_testmodule_a'>  class fixture
<Function 'test_testmodule_a'>  function fixture
test testmodule_a
.<Function 'test_testmodule_b'>  class fixture
<Function 'test_testmodule_b'>  function fixture
test testmodule_b
.

========================== 2 passed in 0.05 seconds ===========================

session and module fixtures are run as expected but I think that class fixtures shouldn't be run here.

@seppestas
Copy link

If this is a duplicate of #570, was this also fixed in #5798?

@rafaelcs
Copy link

rafaelcs commented Jun 8, 2020

Using pytest version 5.4.3 I have exactly the same issue. Do we have any update?

@bluetech
Copy link
Member

I cannot reproduce the original issue from the stack overflow post, so closing.

@fkudron-home please open a new issue if you still have this problem, however I think this is intended behavior (not sure).

@rafaelcs Please open a new issue with a way to reproduce the problem you're seeing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

5 participants