Skip to content

Commit 2903cf3

Browse files
committed
#10 unload modules imported by pytest
1 parent 717981f commit 2903cf3

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
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 **Module already imported so cannot be rewritten** warning when the package being linted was used by pytest/conftest already (#10)
46

57
## [1.0.1] - 2021-03-03
68
### Added

pylint_pytest/checkers/fixture.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,25 @@ def visit_module(self, node):
7272
# run pytest session with customized plugin to collect fixtures
7373
fixture_collector = FixtureCollector()
7474

75+
# save and remove modules imported by pytest to prevent pytest warning during fixture collection:
76+
# <PytestAssertRewriteWarning: Module already imported so cannot be rewritten>
77+
sys_mods = set(sys.modules.keys())
78+
7579
# save and restore sys.path to prevent pytest.main from altering it
7680
sys_path = sys.path.copy()
81+
7782
pytest.main(
7883
[node.file, '--fixtures', '--collect-only'],
7984
plugins=[fixture_collector],
8085
)
86+
87+
# restore sys.path
8188
sys.path = sys_path
89+
90+
# unload modules imported by pytest.main
91+
for module in set(sys.modules.keys()) - sys_mods:
92+
del sys.modules[module]
93+
8294
FixtureChecker._pytest_fixtures = fixture_collector.fixtures
8395
finally:
8496
# restore output devices

0 commit comments

Comments
 (0)