Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 5804967

Browse files
committed
Fix pytest fixture collection error on non-test modules
1 parent 6efd8bc commit 5804967

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changelog
22

33
## [Unreleased]
4+
## Fixed
5+
- Fix pytest fixture collection error on non-test modules
46

57
## [1.1.0] - 2021-04-11
68
## Added

pylint_pytest/checkers/fixture.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
22
import sys
3+
from pathlib import Path
4+
import fnmatch
5+
36
import astroid
47
import pylint
58
from pylint.checkers.variables import VariablesChecker
@@ -14,6 +17,9 @@
1417
)
1518
from . import BasePytestChecker
1619

20+
# TODO: support pytest python_files configuration
21+
FILE_NAME_PATTERNS = ('test_*.py', '*_test.py')
22+
1723

1824
class FixtureCollector:
1925
fixtures = {}
@@ -55,7 +61,7 @@ class FixtureChecker(BasePytestChecker):
5561
'F6401': (
5662
(
5763
'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'
5965
),
6066
'cannot-enumerate-pytest-fixtures',
6167
'Used when pylint-pytest has been unable to enumerate and collect pytest fixtures.',
@@ -98,6 +104,12 @@ def visit_module(self, node):
98104
# storing all invoked fixtures through @pytest.mark.usefixture(...)
99105
FixtureChecker._invoked_with_usefixtures = set() # Set[str]
100106

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+
101113
try:
102114
with open(os.devnull, 'w') as devnull:
103115
# suppress any future output from pytest
@@ -123,7 +135,7 @@ def visit_module(self, node):
123135

124136
FixtureChecker._pytest_fixtures = fixture_collector.fixtures
125137

126-
if ret != pytest.ExitCode.OK or fixture_collector.errors:
138+
if (ret != pytest.ExitCode.OK or fixture_collector.errors) and is_test_module:
127139
self.add_message('cannot-enumerate-pytest-fixtures', node=node)
128140
finally:
129141
# restore output devices

tests/base_tester.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
from pylint.utils import PyLintASTWalker as ASTWalker
1212
from pylint.checkers import BaseChecker
1313

14+
import pylint_pytest.checkers.fixture
15+
16+
# XXX: allow all file name
17+
pylint_pytest.checkers.fixture.FILE_NAME_PATTERNS = ('*', )
18+
1419

1520
class BasePytestTester(object):
1621
CHECKER_CLASS = BaseChecker

0 commit comments

Comments
 (0)