1
1
import os
2
2
import sys
3
+ from pathlib import Path
4
+ import fnmatch
5
+
3
6
import astroid
4
7
import pylint
5
8
from pylint .checkers .variables import VariablesChecker
14
17
)
15
18
from . import BasePytestChecker
16
19
20
+ # TODO: support pytest python_files configuration
21
+ FILE_NAME_PATTERNS = ('test_*.py' , '*_test.py' )
22
+
17
23
18
24
class FixtureCollector :
19
25
fixtures = {}
@@ -55,7 +61,7 @@ class FixtureChecker(BasePytestChecker):
55
61
'F6401' : (
56
62
(
57
63
'pylint-pytest plugin cannot enumerate and collect pytest fixtures. '
58
- 'Please run `pytest --fixtures --collect-only` and resolve any potential syntax error or package dependency issues'
64
+ 'Please run `pytest --fixtures --collect-only path/to/current/module.py ` and resolve any potential syntax error or package dependency issues'
59
65
),
60
66
'cannot-enumerate-pytest-fixtures' ,
61
67
'Used when pylint-pytest has been unable to enumerate and collect pytest fixtures.' ,
@@ -98,6 +104,12 @@ def visit_module(self, node):
98
104
# storing all invoked fixtures through @pytest.mark.usefixture(...)
99
105
FixtureChecker ._invoked_with_usefixtures = set () # Set[str]
100
106
107
+ is_test_module = False
108
+ for pattern in FILE_NAME_PATTERNS :
109
+ if fnmatch .fnmatch (Path (node .file ).name , pattern ):
110
+ is_test_module = True
111
+ break
112
+
101
113
try :
102
114
with open (os .devnull , 'w' ) as devnull :
103
115
# suppress any future output from pytest
@@ -123,7 +135,7 @@ def visit_module(self, node):
123
135
124
136
FixtureChecker ._pytest_fixtures = fixture_collector .fixtures
125
137
126
- if ret != pytest .ExitCode .OK or fixture_collector .errors :
138
+ if ( ret != pytest .ExitCode .OK or fixture_collector .errors ) and is_test_module :
127
139
self .add_message ('cannot-enumerate-pytest-fixtures' , node = node )
128
140
finally :
129
141
# restore output devices
0 commit comments