ernestoarbitrio / pytest-runner

vscode extension allows you to run test (in a very quick way) in your 🐍 python codebase using pytest
https://marketplace.visualstudio.com/items?itemName=pamaron.pytest-runner
MIT License
12 stars 0 forks source link
docker pytest python testing visual-studio-code vscode vscode-extension

logo

ci vs vs-d dow rating

This vscode extension allows you to run test (in a very quick way) in your 🐍 python codebase using pytest. It can be configured to run test using a local and a docker 🐳 based interpreter (see Extension Settings).

NOTE: this is an early stage project 🐣. Please open an issue or, even better, a PR if you find it interesting and you wanna improve pytest-runner.

Motivation

In my current job the test ecosystem lives in a complex architecture of docker containers. The 95% of tests need to be ran through the remote python interpreter within the docker container. Moreover the articulated tree of the source code required every time a long node_ids e.g. test_mod.py::test_func notation. Given that I decided to start my own extension in order to run the test using any pytest exectutable, local or docker based, directly from the document your working on. I was inspired by https://vimawesome.com/plugin/pytest-vim and then I decided to write this plugin to facilitate and speed up the test invocation on any python codebase I work on 😊.

Look at the usage section for more details.

Features 😎

If pytest_runner.check_config is true, before the test execution, pytest-runner checks the configuration file of your project, for example for a toml file like this:

[tool.pytest.ini_options]
python_classes = ["Test", "Describe"]
python_functions = ["test_", "it_", "and_", "but_", "they_"]
python_files = ["test_*.py",]
testpaths = ["tests",]

or a setup.cfg:

[tool:pytest]
python_classes = Test Describe
python_files = test_*.py
python_functions = test_ it_ they_ but_ and_it_
testpaths =
    tests

If your test function doesn't start with any of the names above, pytest-runner won't start and it shows you an error ❌ message.

error

Requirements πŸ•Ή

The only requirement is pytest. NOTE: this extension, so far, has been tested with MacOsx and Linux.

Extension Settings βš™οΈ

This extension contributes the following settings:

These options can be set in each settings.json within .vscode in the project root directory or in the settings.json of VSCode (not recommended).

Usage πŸ“š

There are 4 commands available:

You can run them using the VSCode command palette (β‡§βŒ˜P or ctr⇧P)

cmd-palette

or through the 2 custom buttons on the status bar.

cmd-palette

To run a single test you have 2 options:

  1. Position your cursor on the line where the test function or a class is defined and run the desired command.
  2. Select the test name or portion of it and run the desired command.

    According to the command the test will be executed in the local environment Run Test or in the docker container Run Test Docker

    run-test-demo

If you wanna run all the tests in your current python module you can just use Run Test Module or Run Test Module Docker independently of the cursor location.

πŸ’‘ This way of usage (through the command palette) could be not really productive. I would suggest to create a keyboard shortuct for every command you are going to use.

Keybindings and shortcuts ⌨️

The 4 commands in the section above are actually an alias for these:

To increase the speed I would suggest new entries in the keybindigs in order to map the 2 commands to a keyboard key combination:

  1. Open keybinding.json -> command palette (β‡§βŒ˜P or ctr⇧P) Open Keyboard Shortcut
  2. Add your key combination:
      {
        "key": "ctrl+alt+1",
        "command": "pytest-runner.run-test"
      },
      ...
      {
        "key": "ctrl+alt+2",
        "command": "pytest-runner.run-module-test"
      }

    Now, as well as the command palette, the tests can be exectued using your custom shortcut. In the example above a single test will run with the ctrl+alt+1 key combination and the whole module can be tested using the ctrl+alt+2.

Known Issues 🧐

Only when pytest_runner.pytest_exec is set to pytest without specifying the complete path of the exectuable, the first time the Run Test will fail due to async problem between terminal venv activation and the send command time.

The second run should work properly.


Do you like pytest-runner?

Buy Me A Coffee


Release Notes πŸ“‹

0.0.7

0.0.6

0.0.5

0.0.4

0.0.3

0.0.2

0.0.1