Skip to content

Commit

Permalink
Merge pull request #1907 from moser/support-fixtures-from-pytest-plugins
Browse files Browse the repository at this point in the history
Add support for pytest fixtures from local pytest plugins.
  • Loading branch information
davidhalter committed Jan 26, 2024
2 parents 11280ef + 950ce70 commit 740b474
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ Code Contributors
- Leo Ryu (@Leo-Ryu)
- Joseph Birkner (@josephbirkner)
- Márcio Mazza (@marciomazza)
- Martin Vielsmaier (@moser) <martin@vielsmaier.net>

And a few more "anonymous" contributors.

Expand Down
21 changes: 20 additions & 1 deletion jedi/plugins/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,13 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
if Path(file_io.path) != module_context.py__file__():
try:
m = load_module_from_path(module_context.inference_state, file_io)
yield m.as_context()
conftest_module = m.as_context()
yield conftest_module

plugins_list = m.tree_node.get_used_names().get("pytest_plugins")
if plugins_list:
name = conftest_module.create_name(plugins_list[0])
yield from _load_pytest_plugins(module_context, name)
except FileNotFoundError:
pass
folder = folder.get_parent_folder()
Expand All @@ -196,6 +202,19 @@ def _iter_pytest_modules(module_context, skip_own_module=False):
yield module_value.as_context()


def _load_pytest_plugins(module_context, name):
from jedi.inference.helpers import get_str_or_none

for inferred in name.infer():
for seq_value in inferred.py__iter__():
for value in seq_value.infer():
fq_name = get_str_or_none(value)
if fq_name:
names = fq_name.split(".")
for module_value in module_context.inference_state.import_module(names):
yield module_value.as_context()


class FixtureFilter(ParserTreeFilter):
def _filter(self, names):
for name in super()._filter(names):
Expand Down
6 changes: 6 additions & 0 deletions test/completion/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,9 @@ def capsysbinary(capsysbinary):
#? ['close']
capsysbinary.clos
return capsysbinary


# used when fixtures are defined in multiple files
pytest_plugins = [
"completion.fixture_module",
]
6 changes: 6 additions & 0 deletions test/completion/fixture_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Exists only for completion/pytest.py
import pytest

@pytest.fixture
def my_module_fixture():
return 1.0
3 changes: 3 additions & 0 deletions test/completion/pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def test_x(my_con
#? 18 ['my_conftest_fixture']
def test_x(my_conftest_fixture):
return
#? ['my_module_fixture']
def test_x(my_modu
return

#? []
def lala(my_con
Expand Down

0 comments on commit 740b474

Please sign in to comment.