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

parametrizing metafunc in pytest_generate_tests does not group tests as expected #6979

Open
dbak-invn opened this issue Mar 28, 2020 · 2 comments
Labels
status: help wanted developers would like help from experts on this topic topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: bug problem that needs to be addressed

Comments

@dbak-invn
Copy link

dbak-invn commented Mar 28, 2020

conftest.py:

@pytest.fixture(scope='session')
def fw_config(request):
    logging.info(f'fw_config called with {request.param}')

    return request.param


def pytest_generate_tests(metafunc):
    if 'fw_config' in metafunc.fixturenames:
        configs_to_test = ['ABC', 'DEF']
        pattern = metafunc.definition.get_closest_marker('config_pattern')

        if pattern is None:
            pattern = '.*'
        else:
            assert len(pattern.args) == 1
            pattern = pattern.args[0]

        regex = re.compile(pattern)
        configs_to_test = filter(regex.match, configs_to_test)

        metafunc.parametrize(argnames='fw_config', argvalues=configs_to_test, ids=configs_to_test, scope='session', indirect=True)

test file:

class TestGrouping:

    @pytest.mark.config_pattern(r'.*ABC.*')
    def test_abc(self, fw_config):
        time.sleep(4)
        return True

    @pytest.mark.config_pattern(r'.*DEF.*')
    def test_def(self, fw_config):
        time.sleep(4)
        return True
    def test_all(self, fw_config):
        time.sleep(4)
        return True

Since fw_config is session-scoped, I am hoping that the tests will run in the following order:

test_abc[ABC]
test_all[ABC]
test_def[DEF]
test_all[DEF]

And that fw_config will be called only twice.

However, instead, they are called in the following order:

test_abc[ABC]
test_def[DEF]
test_all[ABC]
test_all[DEF]

and fw_config is called four times.

I have been trying for almost a month to get this behavior using various methods and have not been able to achieve it.

I am aware that I can just have test_abc skip() if fw_config is 'DEF', but then I have a skipped test in my output. I want to not run the test at all.

fw_config here is a stand-in for an expensive configuration with a name. Some tests need to run on all configs, and some tests need to run on only a subset.

Is there any way to do this? Am I doing something incorrectly?

I am guessing that there are actually three instances of the fixture being created, and pytest is treating them as being totally separated.

@Zac-HD Zac-HD added topic: fixtures anything involving fixtures directly or indirectly topic: parametrize related to @pytest.mark.parametrize type: question general question, might be closed after 2 weeks of inactivity labels Mar 28, 2020
@Zac-HD
Copy link
Member

Zac-HD commented May 24, 2020

Closing this question as inactive. Maybe try StackOverflow?

@RonnyPfannschmidt
Copy link
Member

this is possibly a bug in the test reordering

@Zac-HD Zac-HD added status: help wanted developers would like help from experts on this topic type: bug problem that needs to be addressed and removed type: question general question, might be closed after 2 weeks of inactivity labels Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted developers would like help from experts on this topic 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

3 participants