Run pytest tests by line number instead of exact test name
Project description
Run Pytest Tests by Line Number
A pytest plugin that lets you run tests by specifying any line number within the test function or class, not just the exact definition line. This makes it easier to run specific tests from your editor or IDE without needing to navigate to the precise test definition.
The Problem
Pytest natively supports running specific tests using the file.py::test_name syntax, but if you try to use file.py:123 where line 123 is inside a test function (not the exact line where def test_* starts), pytest won't find the test.
This plugin solves that by automatically resolving line numbers to the nearest test function or class.
Installation
uv add pytest-line-runner
Usage
Simply pass a file path and line number to pytest:
pytest tests/test_example.py:42
The plugin will find the test function or class that contains line 42 and run it.
Examples
# tests/test_example.py
def test_addition():
result = 1 + 1 # Line 3
assert result == 2 # Line 4
class TestCalculator: # Line 6
def test_multiply(self): # Line 7
result = 2 * 3 # Line 8
assert result == 6 # Line 9
All of these commands work:
pytest tests/test_example.py:2 # Runs test_addition (definition line)
pytest tests/test_example.py:3 # Runs test_addition (inside function)
pytest tests/test_example.py:4 # Runs test_addition (inside function)
pytest tests/test_example.py:6 # Runs TestCalculator (class line)
pytest tests/test_example.py:8 # Runs TestCalculator::test_multiply
pytest tests/test_example.py:9 # Runs TestCalculator::test_multiply
Features
- Works with test functions (both sync and async)
- Works with test classes
- Handles decorators correctly (finds tests even when you specify a decorator line)
- Uses AST parsing for accurate test resolution
- Automatically falls back to pytest's default behavior if the file doesn't exist or no test is found
License
This project was created from iloveitaly/python-package-template
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytest_line_runner-0.1.0.tar.gz.
File metadata
- Download URL: pytest_line_runner-0.1.0.tar.gz
- Upload date:
- Size: 2.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4510d4223436b006376e51c2418c452c52ec8f2570556d67f9df5e514d912077
|
|
| MD5 |
789c5ce7a9262c467d2463ab5fea471d
|
|
| BLAKE2b-256 |
4fa160109a15e73a36a3049c95e57eba32025c7ed76c2160b8c0eb15f1c14ce6
|
File details
Details for the file pytest_line_runner-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pytest_line_runner-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af0ff8c0edae89fda8a542da031949d888eff11b8ff0610a1bdf4133cfc5a087
|
|
| MD5 |
01953922a6c36d9119a476ace512666c
|
|
| BLAKE2b-256 |
328a7821b9bb8d00735261c34e2a03156a9c1b152fe8142bed3494611c81d9e9
|