Are you tired of jumping through hoops to run Python tests, especially in complex environments like Docker? Meet the Pytest Runner, a Visual Studio Code extension designed to make running Python tests a breeze. Whether you’re executing single tests, modules, or working with local and containerized setups, Pytest Runner streamlines your workflow for a faster, more intuitive testing experience.
Install Pytest Runner Plugin
Getting started with Pytest Runner is easy! Install the plugin directly from the Visual Studio Code Marketplace. Click the link to install it instantly. Once installed, you’ll be ready to run Python tests effortlessly with just a few clicks or commands. Wanna know more? Continue reading 👇
🚀 Why Pytest Runner?
Modern testing can feel cumbersome. Take my job as an example: most tests run in a Docker-based architecture, requiring me to manage remote interpreters and lengthy pytest identifiers like test_module.py::test_function
. This process wasn’t just slow—it was frustrating. I created Pytest Runner to simplify testing, inspired by tools like pytest-vim.
This extension focuses on one goal: reducing friction in Python testing. With Pytest Runner, you can focus on writing great code while the extension handles the grunt work.
🌟 Features
- Run tests locally or in Docker: Execute individual tests or entire test modules effortlessly, using either local environments or Dockerized setups.
- Configuration checks: Optional inspection of
setup.cfg
orpyproject.toml
files ensures your test files follow your project’s conventions. - Customizable settings: Tailor pytest commands, options, and configurations to fit your unique workflow.
📋 Configuration Check: An Extra Layer of Validation
Pytest Runner checks your project’s configuration files when enabled. For example, given a pyproject.toml
:
|
|
or a setup.cfg
file like:
|
|
If a test function doesn’t follow these naming conventions, Pytest Runner will halt and notify you with an error message.
🛠️ Requirements
- Pytest: The only hard requirement.
- Currently tested on macOS and Linux.
⚙️ Extension Settings
This extension offers several customizable settings:
pytest_runner.pytest_exec
: Specifies the local pytest executable. Defaults to your active virtual environment or /usr/bin/pytest.pytest_runner.pytest_exec_docker
: Command to run pytest in a Docker container (e.g., docker-compose run –rm test-container pytest).pytest_runner.check_config
: Enables/disables configuration checks (default: false).pytest_runner.pytest_options
: Additional pytest options (e.g., -sv, -x).
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).
File example:
|
|
🎯 Usage
Commands
- Run Test: Execute a single test locally.
- Run Test Docker: Execute a single test in Docker.
- Run Test Module: Run all tests in the current file locally.
- Run Test Module Docker: Run all tests in the current file via Docker.
- Access these commands via the VSCode command palette (⇧⌘P or Ctrl+⇧P) or use the custom buttons in the status bar.
Access these commands via the VSCode command palette (⇧⌘P or Ctrl+⇧P)
or use the custom buttons in the status bar.
▶️ Running Tests
Place your cursor within a test function or class to execute it directly. Alternatively, select the test name or a portion of it and run the desired command. For module-level tests, use the appropriate command regardless of cursor location.
💡 Pro Tip: Create keyboard shortcuts for frequently used commands to save time!
⌨️ Keybindings
Define shortcuts for a more productive workflow:
|
|
Run single tests with Ctrl+Alt+1 and module tests with Ctrl+Alt+2 (or any combination you prefer).