Skip to content

Commit e72bcf8

Browse files
authored
Merge pull request #9 from reverbc/feature/#2-import-fixture-to-conftest
#2 suppressing FP when importing fixtures into conftest
2 parents 8d8dc05 + 6c11a57 commit e72bcf8

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## [Unreleased]
44

5+
### Added
6+
- Suppressing FP `unused-import` when fixtures defined elsewhere are imported into `conftest.py` but not directly used (#2)
7+
58
## [1.0.0] - 2021-03-02
69
### Added
710
- Python 3.9 support

pylint_pytest/checkers/fixture.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ def visit_module(self, node):
7171

7272
# run pytest session with customized plugin to collect fixtures
7373
fixture_collector = FixtureCollector()
74+
75+
# save and restore sys.path to prevent pytest.main from altering it
76+
sys_path = sys.path.copy()
7477
pytest.main(
7578
[node.file, '--fixtures', '--collect-only'],
7679
plugins=[fixture_collector],
7780
)
81+
sys.path = sys_path
7882
FixtureChecker._pytest_fixtures = fixture_collector.fixtures
7983
finally:
8084
# restore output devices
@@ -118,6 +122,13 @@ def patch_add_message(self, msgid, line=None, node=None, args=None,
118122
if message_tokens[0] == 'import' and len(message_tokens) == 2:
119123
pass
120124

125+
# fixture is defined in other modules and being imported to
126+
# conftest for pytest magic
127+
elif isinstance(node.parent, astroid.Module) \
128+
and node.parent.name.split('.')[-1] == 'conftest' \
129+
and fixture_name in FixtureChecker._pytest_fixtures:
130+
return
131+
121132
# imported fixture is referenced in test/fixture func
122133
elif fixture_name in FixtureChecker._invoked_with_func_args \
123134
and fixture_name in FixtureChecker._pytest_fixtures:

tests/base_tester.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,22 @@ def run_linter(self, enable_plugin, file_path=None):
3030

3131
with open(file_path) as fin:
3232
content = fin.read()
33-
module = astroid.parse(content)
33+
module = astroid.parse(content, module_name=module)
3434
module.file = fin.name
3535

3636
self.walk(module) # run all checkers
37-
BasePytestTester.MESSAGES = self.linter.release_messages()
37+
self.MESSAGES = self.linter.release_messages()
3838

3939
def verify_messages(self, msg_count, msg_id=None):
4040
msg_id = msg_id or self.MSG_ID
4141

4242
matched_count = 0
4343
for message in self.MESSAGES:
44-
print(message)
4544
# only care about ID and count, not the content
4645
if message.msg_id == msg_id:
4746
matched_count += 1
4847

49-
assert matched_count == msg_count
48+
assert matched_count == msg_count, f'expecting {msg_count}, actual {matched_count}'
5049

5150
def setup_method(self):
5251
self.linter = UnittestLinter()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pytest
2+
3+
4+
@pytest.fixture
5+
def conftest_fixture_attr():
6+
return True

tests/input/unused-import/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from _fixture_for_conftest import conftest_fixture_attr

tests/test_unused_import.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,10 @@ def test_same_name_decorator(self, enable_plugin):
3232
name as fixture - should still raise unused-import warning'''
3333
self.run_linter(enable_plugin)
3434
self.verify_messages(1)
35+
36+
@pytest.mark.parametrize('enable_plugin', [True, False])
37+
def test_conftest(self, enable_plugin):
38+
'''fixtures are defined in different modules and imported to conftest
39+
for pytest to do its magic'''
40+
self.run_linter(enable_plugin)
41+
self.verify_messages(0 if enable_plugin else 1)

0 commit comments

Comments
 (0)