/pytest-fixtures

Pytest fixtures autocomplete for vscode

Primary LanguageTypeScriptMIT LicenseMIT

pytest-fixtures README

VSCode extension to provide intellisense for Pytest Fixtures. Inspired by pytest-vscode. Uses pytest --fixtures to get fixture information.

NOTE: As of Visual Studio Code version 1.76, Pylance natively supports pytest fixture detection. Consider using that instead if it works for you.

Demo

demo

Features

  • Autocomplete suggestions for parameters of test functions where fixtures may be used. Includes docstrings. Context aware of different fixtures available for each file.
  • Go To Definition support. Uses pytest output to determine location of fixture.
  • When type ':' character after a parameter and press autocomplete keyboard shortcut, the return type of the fixture is showed if the fixture is annotated with return type.

Will first check "python.testing.pytestPath" for pytest, then will fallback on python -m pytest with the selected interpreter.

Optional extra command line arguments for pytest to discover fixtures

In the .vscode/settings.json you can add the extra arguments and the plugin uses this args if exist.

"pytest-fixtures.extraArguments" is an array of string.

{
    "pytest-fixtures.extraArguments": [
        "-p",
        "abc.pytest.common.bootstrap",
    ]
}

You can also add additional decorators used to identify fixtures

"pytest-fixtures.additionalDecorators" is an array of string.

{
    "pytest-fixtures.additionalDecorators": [
        "pytest_asyncio.fixture",
    ]
}

Snippet suggestion setting

The autocomplete items generated by this plugin are marked as snippet and they will show first if you set the 'Snippet Suggestions' setting to 'top'. See the picture below.

snippet_suggestion_setting

Requirements

  • Python
  • pytest
  • (Optionally) pytest-asyncio

Known Issues

  • Python extension provides a self-referential definition of function parameters so there will be two definitions, see here
  • Fixtures are provided on a per-file basis, so in-file scope is not respected for suggestions.
  • Scanning for fixtures can take a toll on the CPU for large projects. Consider disabling scanForFixturesOnFileChange and scan manually if performance is impacted.

Release Notes

Change Log