11import fnmatch
2- import os
2+ import io
33import sys
44from pathlib import Path
55from typing import Set , Tuple
@@ -68,8 +68,8 @@ class FixtureChecker(BasePytestChecker):
6868 "F6401" : (
6969 (
7070 "pylint-pytest plugin cannot enumerate and collect pytest fixtures. "
71- "Please run `pytest --fixtures --collect-only path/to/current/module.py` "
72- " and resolve any potential syntax error or package dependency issues"
71+ "Please run `pytest --fixtures --collect-only %s` and resolve "
72+ "any potential syntax error or package dependency issues. stdout: %s. stderr: %s. "
7373 ),
7474 "cannot-enumerate-pytest-fixtures" ,
7575 "Used when pylint-pytest has been unable to enumerate and collect pytest fixtures." ,
@@ -118,9 +118,10 @@ def visit_module(self, node):
118118
119119 stdout , stderr = sys .stdout , sys .stderr
120120 try :
121- with open ( os . devnull , "w" ) as devnull :
121+ with io . StringIO () as captured_stdout , io . StringIO ( ) as captured_stderr :
122122 # suppress any future output from pytest
123- sys .stderr = sys .stdout = devnull
123+ sys .stderr = captured_stderr
124+ sys .stdout = captured_stdout
124125
125126 # run pytest session with customized plugin to collect fixtures
126127 fixture_collector = FixtureCollector ()
@@ -143,8 +144,32 @@ def visit_module(self, node):
143144
144145 FixtureChecker ._pytest_fixtures = fixture_collector .fixtures
145146
146- if (ret != pytest .ExitCode .OK or fixture_collector .errors ) and is_test_module :
147- self .add_message ("cannot-enumerate-pytest-fixtures" , node = node )
147+ legitimate_failure_paths = set (
148+ collection_report .nodeid
149+ for collection_report in fixture_collector .errors
150+ if any (
151+ fnmatch .fnmatch (
152+ Path (collection_report .nodeid ).name ,
153+ pattern ,
154+ )
155+ for pattern in FILE_NAME_PATTERNS
156+ )
157+ )
158+ if (ret != pytest .ExitCode .OK or legitimate_failure_paths ) and is_test_module :
159+ files_to_report = {
160+ str (Path (x ).absolute ().relative_to (Path .cwd ()))
161+ for x in legitimate_failure_paths | {node .file }
162+ }
163+
164+ self .add_message (
165+ "cannot-enumerate-pytest-fixtures" ,
166+ args = (
167+ " " .join (files_to_report ),
168+ captured_stdout .getvalue (),
169+ captured_stderr .getvalue (),
170+ ),
171+ node = node ,
172+ )
148173 finally :
149174 # restore output devices
150175 sys .stdout , sys .stderr = stdout , stderr
0 commit comments