diff --git a/pylint_pytest/checkers/fixture.py b/pylint_pytest/checkers/fixture.py index c2a00a0..51c1a94 100644 --- a/pylint_pytest/checkers/fixture.py +++ b/pylint_pytest/checkers/fixture.py @@ -110,10 +110,10 @@ def visit_module(self, node): is_test_module = True break + stdout, stderr = sys.stdout, sys.stderr try: with open(os.devnull, 'w') as devnull: # suppress any future output from pytest - stdout, stderr = sys.stdout, sys.stderr sys.stderr = sys.stdout = devnull # run pytest session with customized plugin to collect fixtures @@ -238,11 +238,16 @@ def patch_add_message(self, msgid, line=None, node=None, args=None, return # check W0613 unused-argument - if msgid == 'unused-argument' and \ - _can_use_fixture(node.parent.parent) and \ - isinstance(node.parent, astroid.Arguments) and \ - node.name in FixtureChecker._pytest_fixtures: - return + if msgid == 'unused-argument': + if _can_use_fixture(node.parent.parent): + if isinstance(node.parent, astroid.Arguments): + if node.name in FixtureChecker._pytest_fixtures: + return # argument is used as a fixture + else: + fixnames = (arg.name for arg in node.parent.args if arg.name in FixtureChecker._pytest_fixtures) + for fixname in fixnames: + if node.name in FixtureChecker._pytest_fixtures[fixname][0].argnames: + return # argument is used by a fixture # check W0621 redefined-outer-name if msgid == 'redefined-outer-name' and \ diff --git a/tests/input/unused-argument/func_param_as_fixture_arg.py b/tests/input/unused-argument/func_param_as_fixture_arg.py new file mode 100644 index 0000000..2ef5f10 --- /dev/null +++ b/tests/input/unused-argument/func_param_as_fixture_arg.py @@ -0,0 +1,24 @@ +""" +This module illustrates a situation in which unused-argument should be +suppressed, but is not. +""" + +import pytest + + +@pytest.fixture +def myfix(arg): + """A fixture that requests a function param""" + print("arg is ", arg) + return True + + +@pytest.mark.parametrize("arg", [1, 2, 3]) +def test_myfix(myfix, arg): + """A test function that uses the param through a fixture""" + assert myfix + +@pytest.mark.parametrize("narg", [4, 5, 6]) +def test_nyfix(narg): # unused-argument + """A test function that does not use its param""" + assert True diff --git a/tests/test_unused_argument.py b/tests/test_unused_argument.py index 7ab2747..529e764 100644 --- a/tests/test_unused_argument.py +++ b/tests/test_unused_argument.py @@ -28,3 +28,8 @@ def test_caller_not_a_test_func(self, enable_plugin): def test_args_and_kwargs(self, enable_plugin): self.run_linter(enable_plugin) self.verify_messages(2) + + @pytest.mark.parametrize('enable_plugin', [True, False]) + def test_func_param_as_fixture_arg(self, enable_plugin): + self.run_linter(enable_plugin) + self.verify_messages(1 if enable_plugin else 2)