← 返回命令列表

Linux command

pytest 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Run all tests

pytest

Run specific test file

pytest [test_file.py]

Run specific test function

pytest [test_file.py]::[test_function]

Run tests matching keyword

pytest -k "[keyword]"

Run with verbose output

pytest -v

Stop on first failure

pytest -x

Show print statements

pytest -s

Run tests in parallel

pytest -n [4]

Run tests matching a marker

pytest -m "[slow]"

说明

pytest is Python's most popular testing framework. It discovers and runs tests automatically, with powerful fixtures, parameterization, and plugin ecosystem. Test discovery finds files matching test_*.py or *_test.py, and functions/methods starting with test_. Classes starting with Test are also collected. This convention-based approach minimizes configuration. Fixtures provide reusable test dependencies. Defined with @pytest.fixture, they can set up databases, create objects, or configure environments. Fixtures can be scoped (function, class, module, session) for efficient resource management. Parameterization runs tests with multiple inputs using @pytest.mark.parametrize. Markers (@pytest.mark.X) tag tests for selective running or custom behavior. The plugin ecosystem extends functionality: pytest-cov for coverage, pytest-xdist for parallel execution, pytest-mock for mocking, and hundreds more. Plugins install via pip and activate automatically. Assertion introspection provides detailed failure messages without special assertion methods. Standard Python assert statements work with rich comparison displays.

参数

-v, --verbose
Increase verbosity.
-q, --quiet
Decrease verbosity.
-x, --exitfirst
Stop on first failure.
-s, --capture=no
Disable output capture (show print).
-k _EXPRESSION_
Run tests matching keyword expression.
-m _MARKER_
Run tests with specific marker.
--collect-only
List tests without running.
--lf, --last-failed
Run only last failed tests.
--ff, --failed-first
Run failed tests first.
-n _NUM_
Parallel workers (pytest-xdist).
--cov _PACKAGE_
Measure coverage (pytest-cov).
--cov-report _TYPE_
Coverage report format.
--pdb
Drop into debugger on failure.
--tb _STYLE_
Traceback style: short, long, line, native, no.
--maxfail _NUM_
Stop after N failures.
--ignore _PATH_
Ignore path during collection.
--durations _NUM_
Show N slowest tests.

FAQ

What is the pytest command used for?

pytest is Python's most popular testing framework. It discovers and runs tests automatically, with powerful fixtures, parameterization, and plugin ecosystem. Test discovery finds files matching test_*.py or *_test.py, and functions/methods starting with test_. Classes starting with Test are also collected. This convention-based approach minimizes configuration. Fixtures provide reusable test dependencies. Defined with @pytest.fixture, they can set up databases, create objects, or configure environments. Fixtures can be scoped (function, class, module, session) for efficient resource management. Parameterization runs tests with multiple inputs using @pytest.mark.parametrize. Markers (@pytest.mark.X) tag tests for selective running or custom behavior. The plugin ecosystem extends functionality: pytest-cov for coverage, pytest-xdist for parallel execution, pytest-mock for mocking, and hundreds more. Plugins install via pip and activate automatically. Assertion introspection provides detailed failure messages without special assertion methods. Standard Python assert statements work with rich comparison displays.

How do I run a basic pytest example?

Run `pytest` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -v, --verbose do in pytest?

Increase verbosity.