bazel-contrib / bazel-contrib/rules_python

[gazelle] Support resolving pytest fixtures

Offen
#2,162 2 Kommentare 3 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
gazelle help wanted
Vorherrschende Sprache
Starlark
Sterne
688
Forks
721
Ø Merge
15 Std. 7 Min.
Gemergte PRs (30 T.)
76

Beschreibung

# 🚀 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,
```
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:

```
# gazelle:python_resolve_pytest_fixture mocker @pip//pytest_mock
```

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Rechercherichtung

Start with the Gazelle plugin's existing special handling for conftest files and trace how module dependencies are tracked. Define how python_resolve_pytest_fixture directives map fixture names to targets, including fixtures that depend on other fixtures. Done means test targets receive the configured fixture dependencies, with resolution behavior covered for the mocker example and nested fixtures.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
build-system, testing-qa
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.