Skip to content

Commit 3322c1e

Browse files
authored
Casting fixture parameter to list at the beginning of parameter… (#5950)
Casting fixture parameter to list at the beginning of parameter parsing.
2 parents 4f2abd7 + 122cf60 commit 3322c1e

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

changelog/5946.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed issue when parametrizing fixtures with numpy arrays (and possibly other sequence-like types).

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def main():
2828
"mock",
2929
"nose",
3030
"requests",
31+
"numpy",
3132
"xmlschema",
3233
]
3334
},

src/_pytest/fixtures.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,9 @@ def fixture(
11131113
``fixture_<fixturename>`` and then use
11141114
``@pytest.fixture(name='<fixturename>')``.
11151115
"""
1116+
if params is not None:
1117+
params = list(params)
1118+
11161119
fixture_function, arguments = _parse_fixture_args(
11171120
callable_or_scope,
11181121
*args,
@@ -1134,8 +1137,6 @@ def fixture(
11341137
fixture_function
11351138
)
11361139

1137-
if params is not None and not isinstance(params, (list, tuple)):
1138-
params = list(params)
11391140
return FixtureFunctionMarker(scope, params, autouse, ids=ids, name=name)
11401141

11411142

testing/python/fixtures.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4187,3 +4187,21 @@ def test_check_fixture_instantiations():
41874187
)
41884188
result = testdir.runpytest()
41894189
result.assert_outcomes(passed=7)
4190+
4191+
4192+
def test_fixture_parametrization_nparray(testdir):
4193+
testdir.makepyfile(
4194+
"""
4195+
from numpy import linspace
4196+
from pytest import fixture
4197+
4198+
@fixture(params=linspace(1, 10, 10))
4199+
def value(request):
4200+
return request.param
4201+
4202+
def test_bug(value):
4203+
assert value == value
4204+
"""
4205+
)
4206+
result = testdir.runpytest()
4207+
result.assert_outcomes(passed=10)

0 commit comments

Comments
 (0)