Skip to content

Improve error message when TestCase functions use a parametrized fixture #4076

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2535.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve error message when test functions of ``unittest.TestCase`` subclasses use a parametrized fixture.
21 changes: 18 additions & 3 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,20 @@ def _compute_fixture_value(self, fixturedef):
except (AttributeError, ValueError):
param = NOTSET
param_index = 0
if fixturedef.params is not None:
has_params = fixturedef.params is not None
fixtures_not_supported = getattr(funcitem, "nofuncargs", False)
if has_params and fixtures_not_supported:
msg = (
"{name} does not support fixtures, maybe unittest.TestCase subclass?\n"
"Node id: {nodeid}\n"
"Function type: {typename}"
).format(
name=funcitem.name,
nodeid=funcitem.nodeid,
typename=type(funcitem).__name__,
)
fail(msg)
if has_params:
frame = inspect.stack()[3]
frameinfo = inspect.getframeinfo(frame[0])
source_path = frameinfo.filename
Expand All @@ -571,9 +584,11 @@ def _compute_fixture_value(self, fixturedef):
if source_path.relto(funcitem.config.rootdir):
source_path = source_path.relto(funcitem.config.rootdir)
msg = (
"The requested fixture has no parameter defined for the "
"current test.\n\nRequested fixture '{}' defined in:\n{}"
"The requested fixture has no parameter defined for test:\n"
" {}\n\n"
"Requested fixture '{}' defined in:\n{}"
"\n\nRequested here:\n{}:{}".format(
funcitem.nodeid,
fixturedef.argname,
getlocation(fixturedef.func, funcitem.config.rootdir),
source_path,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
import unittest


@pytest.fixture(params=[1, 2])
def two(request):
return request.param


@pytest.mark.usefixtures("two")
class TestSomethingElse(unittest.TestCase):
def test_two(self):
pass
12 changes: 8 additions & 4 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -3600,7 +3600,8 @@ def test_foo(request, get_named_fixture):
result = testdir.runpytest()
result.stdout.fnmatch_lines(
"""
E*Failed: The requested fixture has no parameter defined for the current test.
E*Failed: The requested fixture has no parameter defined for test:
E* test_call_from_fixture.py::test_foo
E*
E*Requested fixture 'fix_with_param' defined in:
E*test_call_from_fixture.py:4
Expand All @@ -3626,7 +3627,8 @@ def test_foo(request):
result = testdir.runpytest()
result.stdout.fnmatch_lines(
"""
E*Failed: The requested fixture has no parameter defined for the current test.
E*Failed: The requested fixture has no parameter defined for test:
E* test_call_from_test.py::test_foo
E*
E*Requested fixture 'fix_with_param' defined in:
E*test_call_from_test.py:4
Expand Down Expand Up @@ -3656,7 +3658,8 @@ def test_foo(request):
result = testdir.runpytest()
result.stdout.fnmatch_lines(
"""
E*Failed: The requested fixture has no parameter defined for the current test.
E*Failed: The requested fixture has no parameter defined for test:
E* test_external_fixture.py::test_foo
E*
E*Requested fixture 'fix_with_param' defined in:
E*conftest.py:4
Expand Down Expand Up @@ -3699,7 +3702,8 @@ def test_foo(request):
result = testdir.runpytest()
result.stdout.fnmatch_lines(
"""
E*Failed: The requested fixture has no parameter defined for the current test.
E*Failed: The requested fixture has no parameter defined for test:
E* test_foos.py::test_foo
E*
E*Requested fixture 'fix_with_param' defined in:
E*fix.py:4
Expand Down
12 changes: 12 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,15 @@ def test_hello(self):
result = testdir.runpytest()
assert "should raise this exception" in result.stdout.str()
assert "ERROR at teardown of MyTestCase.test_hello" not in result.stdout.str()


def test_error_message_with_parametrized_fixtures(testdir):
testdir.copy_example("unittest/test_parametrized_fixture_error_message.py")
result = testdir.runpytest()
result.stdout.fnmatch_lines(
[
"*test_two does not support fixtures*",
"*TestSomethingElse::test_two",
"*Function type: TestCaseFunction",
]
)