metafunc.parametrize(..., scope="session") fails when mixed with other parametrizations
See original GitHub issueBoth, python 2.7.13 and 3.5.3, pytest 3.6.1, no additional pytest plugins.
# conftest.py
import pytest
KNOWN_BRANCHES = {
# The number means how many times given branch has been set-up
"branch_1": 0,
"branch_2": 0,
}
def pytest_generate_tests(metafunc):
if 'tested_branch' in metafunc.fixturenames:
branches = KNOWN_BRANCHES.keys()
metafunc.parametrize("tested_branch", branches, scope="session")
@pytest.fixture(scope="session")
def repo_preparation(tested_branch):
""" If it would have a "session" scopoe then the value should be always 1 for each branch."""
KNOWN_BRANCHES[tested_branch] += 1
return KNOWN_BRANCHES[tested_branch]
And the simplest reproduction:
# test_parametrization.py
import pytest
def test_nothing(repo_preparation):
assert repo_preparation == 1 # that passes
@pytest.mark.parametrize("_", ["param_1", "param_2"])
def test_basic_parametrization(repo_preparation, _):
# that causes parametrization confisuon
assert repo_preparation == 1, "Repo prepared more than once."
Gives such an result:
$ pytest -v test_parametrization.py
================================== test session starts ==================================
platform linux -- Python 3.5.3, pytest-3.6.1, py-1.5.3, pluggy-0.6.0 -- /usr/bin/python3
cachedir: .pytest_cache
rootdir: /home/kamichal/ecws/pyplayground, inifile:
collected 6 items
test_parametrization.py::test_nothing[branch_1] PASSED [ 16%]
test_parametrization.py::test_basic_parametrization[branch_1-param_1] PASSED [ 33%]
test_parametrization.py::test_nothing[branch_2] PASSED [ 50%]
test_parametrization.py::test_basic_parametrization[branch_1-param_2] PASSED [ 66%]
test_parametrization.py::test_basic_parametrization[branch_2-param_1] FAILED [ 83%]
test_parametrization.py::test_basic_parametrization[branch_2-param_2] FAILED [100%]
======================================= FAILURES ========================================
_____________________ test_basic_parametrization[branch_2-param_1] ______________________
repo_preparation = 2, _ = 'param_1'
@pytest.mark.parametrize("_", ["param_1", "param_2"])
def test_basic_parametrization(repo_preparation, _):
# that causes parametrization confisuon
> assert repo_preparation == 1, "Repo prepared more than once."
E AssertionError: Repo prepared more than once.
E assert 2 == 1
test_parametrization.py:12: AssertionError
_____________________ test_basic_parametrization[branch_2-param_2] ______________________
repo_preparation = 3, _ = 'param_2'
@pytest.mark.parametrize("_", ["param_1", "param_2"])
def test_basic_parametrization(repo_preparation, _):
# that causes parametrization confisuon
> assert repo_preparation == 1, "Repo prepared more than once."
E AssertionError: Repo prepared more than once.
E assert 3 == 1
test_parametrization.py:12: AssertionError
========================== 2 failed, 4 passed in 0.03 seconds ===========================
Setting indirect=True
in the metafunc.parametrize
call fixes that problem, but there is no information that it’s required for larger scopes to work properly. There is only an advice to do so, but…
Issue Analytics
- State:
- Created 5 years ago
- Reactions:1
- Comments:9 (5 by maintainers)
Top Results From Across the Web
Fixture scope doesn't work when parametrized tests use ...
From the way pytest prints the test parametesr, it seems like it doesn't distinguish between the different types of parameters used for each...
Read more >Parametrizing tests — pytest documentation
Let's say we want to execute a test with different computation parameters and the parameter range shall be determined by a command line...
Read more >Deep dive into Pytest parametrization | by George Shuklin
Each of those tests can fail independently of each other (if in this example the test with value 0 fails, and four others...
Read more >How To Do Parameterization In Pytest With Selenium?
Since Python 3.5, fixtures of scope session have a higher scope than fixtures ... @pytest.mark.parametrize can take different parameters as ...
Read more >pytest Documentation - Read the Docs
How to re-run failed tests and maintain state between test runs . ... names, function names and parameters from parametrization, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I came across the same bug, I think… When using
metafunc.parametrize(scope=session)
in combination with parametrized tests, all of my session scoped fixtures teared down after each test.This behavior can be demonstrated very vividly with
--setup-show
:Workaround
@kamichal For me, the workaround with
indirect=True
also works in combination with parameters received frommetafunc.config.getoption
.Just define a real fixture with the same name (in the example below
my_fixture
) and use therequest
fixture to return the indirect parametrized value:Then you can call it with
pytest --my_parser_option hello
, which will result in"hello" != "world"
I hope this helps someone.Regards Winklerrr
As usual with open source projects, it will be addressed once someone feels like working on it (or someone pays someone to work on it).
@RonnyPfannschmidt (a pytest core maintainer) said “that looks like a bug” above.