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

Duplicated fixture setup even though the previous cached fixture can be used #3392

Closed
gavincyi opened this issue Apr 12, 2018 · 4 comments
Closed
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed

Comments

@gavincyi
Copy link

gavincyi commented Apr 12, 2018

Consider the following code,

import pytest                                               
                                                            
def pytest_generate_tests(metafunc):                        
    metafunc.parametrize('f1', list('abc'), scope="session")
    metafunc.parametrize('f2', [1, 2])                      
                                                            
                                                            
@pytest.fixture(scope="session")                            
def f1(request):                                            
    pass                                                    
                                                            
@pytest.fixture(scope="session")                            
def f1_another(f1):                                         
    print("Initialize f1 %s" % f1)                          
                                                            
@pytest.fixture                                             
def f2(request):                                            
    pass                                                    
                                                            
def test(f1_another, f2):                                   
    pass                                                    

with flag pytest -vs, the output is

collected 6 items                                                                                                                                                                                                                                                                         

pytest_example.py::test[a-1] Initialize f1 a
PASSED
pytest_example.py::test[a-2] Initialize f1 a
PASSED
pytest_example.py::test[b-1] Initialize f1 b
PASSED
pytest_example.py::test[b-2] Initialize f1 b
PASSED
pytest_example.py::test[c-1] Initialize f1 c
PASSED
pytest_example.py::test[c-2] Initialize f1 c
PASSED

Fixture f1_another takes the same f1 fixture but is initialized twice. The expected output should be

collected 6 items                                                                                                                                                                                                                                                                         

pytest_example.py::test[a-1] Initialize f1 a
PASSED
pytest_example.py::test[a-2] PASSED
pytest_example.py::test[b-1] Initialize f1 b
PASSED
pytest_example.py::test[b-2] PASSED
pytest_example.py::test[c-1] Initialize f1 c
PASSED
pytest_example.py::test[c-2] PASSED

The pytest version is 3.5.0. It looks to me the issue may relate to #2846.

@pytestbot
Copy link
Contributor

GitMate.io thinks possibly related issues are #332 (Logging of fixture setup), #1049 (the cache fixture needs a docstring), #2836 (Wrong fixture/conftest being used), #1342 (pytest always creates the .cache directory even when the cache functionality is not used), and #2833 (Per-use fixture scope).

@pytestbot pytestbot added the type: bug problem that needs to be addressed label Apr 12, 2018
@nicoddemus nicoddemus added the topic: fixtures anything involving fixtures directly or indirectly label Apr 12, 2018
@nicoddemus
Copy link
Member

Thanks @gavincyi for posting an example and clearly explaining the issue.

I think there is some misuse here, I don't think metafunc.parametrize is meant to parametrize fixtures at all; what's happening is that your metafunc.parametrize is shadowing the fixtures with the parametrized values. That can be verified if you just comment out f1 and f2 from your sample code it will still run with the exact same output.

@nicoddemus nicoddemus added the status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity label Apr 12, 2018
@RonnyPfannschmidt
Copy link
Member

well, to parametrize fixtures, one needs to use indirect

@nicoddemus
Copy link
Member

Oh right.

@gavincyi if you change your example to:

import pytest

def pytest_generate_tests(metafunc):
    metafunc.parametrize('f1', list('abc'), scope="session", indirect=True)
    metafunc.parametrize('f2', [1, 2], indirect=True)


@pytest.fixture(scope="session")
def f1(request):
    return request.param

@pytest.fixture(scope="session")
def f1_another(f1):
    print("Initialize f1 %s" % f1)

@pytest.fixture
def f2(request):
    pass

def test(f1_another, f2):
    pass

You will get the expected output:

foo.py::test[a-1] Initialize f1 a
PASSED
foo.py::test[a-2] PASSED
foo.py::test[b-1] Initialize f1 b
PASSED
foo.py::test[b-2] PASSED
foo.py::test[c-1] Initialize f1 c
PASSED
foo.py::test[c-2] PASSED

I'm closing this for now but feel free to follow up with more questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs information reporter needs to provide more information; can be closed after 2 or more weeks of inactivity topic: fixtures anything involving fixtures directly or indirectly type: bug problem that needs to be addressed
Projects
None yet
Development

No branches or pull requests

4 participants