-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
3.0: Parametrization gets values from previous tests?! #1832
Comments
Trying to bisect this, but it'll probably take me a while... |
That's weird... 😕 |
Bisected to d72afe7 |
So indeed reverting that commit on the newest Looking at that commit, it relies on the order of if self._arg2fixturedefs:
# Takes the most narrow scope from used fixtures
fixtures_scopes = [fixturedef[0].scope for fixturedef in self._arg2fixturedefs.values()]
for scope in reversed(scopes):
if scope in fixtures_scopes:
break
else:
scope = 'function' However, |
Hi, the only thing here is to check if scope exists in defined fixtures. The order doesn't matter |
In case it helps, here is an overly simplified case that reproduces this: https://github.com/vmalloc/pytest-bug-reproduction |
@vmalloc Thanks, that definitely helps a lot! I can confirm that reproduces it. @Stranger6667 Oh, I'm sorry - I misread |
From what I've seen in my case, the bug appears only in the presence of the session-scoped fixture(s) |
If no other fixture is used, the default scope is This is "proven" correct by adding a single diff --git a/tests/test_front_matter_parser.py b/tests/test_front_matter_parser.py
index 96d5917..6a0214b 100644
--- a/tests/test_front_matter_parser.py
+++ b/tests/test_front_matter_parser.py
@@ -1,14 +1,18 @@
import pytest
+@pytest.fixture(scope='function')
+def bugfix():
+ pass
+
@pytest.mark.parametrize('animal', [
"dog",
"cat",
])
-def test_1(animal):
+def test_1(animal, bugfix):
assert animal != 'fish'
@pytest.mark.parametrize('animal', [
'fish',
])
-def test_2(animal):
+def test_2(animal, bugfix):
assert animal == 'fish' -> Now, as I said, I'm not familar with the inner workings of pytest, but I guess that the scope should stay "function" if at least one non-indirect parametrization is given (who wants a single parameter parametrized over a whole session?). A quick and dirty solution might look like diff --git a/_pytest/python.py b/_pytest/python.py
index 7abd962..47da080 100644
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -807,14 +807,26 @@ class Metafunc(fixtures.FuncargnamesCompatAttr):
newmarks[newmark.markname] = newmark
if scope is None:
- if self._arg2fixturedefs:
- # Takes the most narrow scope from used fixtures
- fixtures_scopes = [fixturedef[0].scope for fixturedef in self._arg2fixturedefs.values()]
- for scope in reversed(scopes):
- if scope in fixtures_scopes:
- break
- else:
+ if indirect is False or isinstance(indirect, (tuple, list)) and len(indirect) < len(argnames):
+ # at least one non-indirect fixture, scope should be function
+ # then
scope = 'function'
+ else:
+ fixturedefs = self._arg2fixturedefs
+ if fixturedefs is None:
+ fixturedefs = {}
+ used_scopes = [fixturedef[0].scope
+ for name, fixturedef in fixturedefs.items()
+ if indirect is True
+ or isinstance(indirect, (tuple, list))
+ and name in indirect]
+ if used_scopes:
+ # Takes the most narrow scope from used fixtures
+ for scope in reversed(scopes):
+ if scope in used_scopes:
+ break
+ else:
+ scope = 'function'
scopenum = scopes.index(scope)
valtypes = {}
for arg in argnames: i.e. the scope stays "function" if at least one non-indirect parametrization is given, and only scopes actually parametrized are considered when searching the most narrow scope. Now (after reverting the bugfix in the example), the tests for both the bug example and the example for issue 634 are green:
This might be a starting point for someone who knows more about pytest and how |
I can confirm this behavior also exists with module scoped fixtures as well. As long as there are no explicit function scoped fixtures, parametrized fixtures get weird values from other tests. |
I can confirm what @Kingdread says - I added a autouse function-scoped fixture to my |
This is a workaround for pytest-dev/pytest#1832
Temporary workaround for pytest-dev/pytest#1832.
A simple workaround is also to pass |
PR opened at #1847 |
So I just updated qutebrowser to pytest 3.0, and did run into a weird issue...
The regular build on AppVeyor works just fine.
For the frozen build tests seem to pick up parametrization values of previous tests?!
For example, near the bottom, this is failing:
Now where did that
True
forexpected
come from? It's certainly not in the parametrized list there...Then for the next test, suddenly the
'foo'
we parametrized forkey
shows up again:here,
key
never should be'foo'
andexpected
never should beTrue
...The text was updated successfully, but these errors were encountered: