You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Gazelle should allow for the configuration and resolution of pytest fixtures. In pytest, fixtures are method parameters that are automatically discovered and injected into test functions by pytest's plugin machinery.
For example,
def test_foo(mocker):
mocker.patch(...)
...
The mocker keyword is a pytest fixture that is found in the pytest_mock pip package. Fixtures can also be provided by other targets in the workspace.
Describe the solution you'd like
Update the gazelle plugin to find pytest fixtures and track them in addition to module dependencies.
Support resolution config for pytest fixtures through # gazelle:python_resolve_pytest_fixture FIXTURE_NAME TARGET_LABEL directives.
For the above example mocker, a directive line like below could be added to the root build file:
Doing this would have the rules_python gazelle plugin add @pip//pytest_mock to any test target that has a test_* function that accepts an argument named mocker. Some additional handling will need to be done to track fixture functions that depend on other fixtures.
Describe alternatives you've considered
I considered using # gazelle:resolve pytest_fixture FIXTURE_NAME TARGET_LABEL, but it would require implementing a CrossResolver in the gazelle extension to resolve the pytest_fixture language. It also feels like it goes a bit against gazelle design to have a language name in a resolve directive for which there isn't actually a plugin for. Keeping it all in rules_python's directive configuration seems more appropriate given that rules_python already has special handling for conftest files. There could be a whole pytest_fixture indexing process that would allow for automatic resolution of pytest fixtures, but having a manual override seems good enough.
A second alternative is to use # gazelle:include_dep lines in every test file that needs a specific fixture dependency, but this is a bit of a pain to maintain.
The text was updated successfully, but these errors were encountered:
Thinking with a python ruleset maintainer hat, how does making gazelle understand pytest fixture model benefit other frameworks? Asking to support something like this is basically asking to special case pytest.
The alternative for now is to add pytest_mocker to the deps and use the # keep directive. It's not great, because it duplicates the list of dependencies for a test - one in build.bazel and another one elsewhere, but it at least makes things easy to understand.
Not saying yet if gazelle should have or not have the functionality, but special casing pytest in this ruleset does not have precedence yet.
Thinking with a python ruleset maintainer hat, how does making gazelle understand pytest fixture model benefit other frameworks? Asking to support something like this is basically asking to special case pytest.
It would be a special case just for pytest, which has a unique plugin registration and dependency injection/fixture system.
special casing pytest in this ruleset does not have precedence yet.
In the case of the gazelle extension, I don't think this is true. The gazelle extension already has handling for conftest.py files (originally added in #879). conftest.py files are how pytest provides fixtures across multiple files in the same directory. See pytest conftest docs. The gazelle extension was made to understand this and generate a conftest library target that gets added as a dep to all py_test targets in the same directory.
It does this unconditionally, and without the ability to disable it. Under my above proposal, the behavior would only be changed if the gazelle:python_resolve_pytest_fixture directive is used.
🚀 feature request
Description
Gazelle should allow for the configuration and resolution of pytest fixtures. In pytest, fixtures are method parameters that are automatically discovered and injected into test functions by pytest's plugin machinery.
For example,
The
mocker
keyword is a pytest fixture that is found in thepytest_mock
pip package. Fixtures can also be provided by other targets in the workspace.Describe the solution you'd like
Update the gazelle plugin to find pytest fixtures and track them in addition to module dependencies.
Support resolution config for pytest fixtures through
# gazelle:python_resolve_pytest_fixture FIXTURE_NAME TARGET_LABEL
directives.For the above example
mocker
, a directive line like below could be added to the root build file:Doing this would have the rules_python gazelle plugin add
@pip//pytest_mock
to any test target that has atest_*
function that accepts an argument namedmocker
. Some additional handling will need to be done to track fixture functions that depend on other fixtures.Describe alternatives you've considered
I considered using
# gazelle:resolve pytest_fixture FIXTURE_NAME TARGET_LABEL
, but it would require implementing aCrossResolver
in the gazelle extension to resolve thepytest_fixture
language. It also feels like it goes a bit against gazelle design to have a language name in aresolve
directive for which there isn't actually a plugin for. Keeping it all in rules_python's directive configuration seems more appropriate given that rules_python already has special handling forconftest
files. There could be a whole pytest_fixture indexing process that would allow for automatic resolution of pytest fixtures, but having a manual override seems good enough.A second alternative is to use
# gazelle:include_dep
lines in every test file that needs a specific fixture dependency, but this is a bit of a pain to maintain.The text was updated successfully, but these errors were encountered: