Pytest Installation & Configuration
Install
Running pytest
# run testfile
pytest test_file_name.py
# run testfile in some nested dir
pytest test_dir1/test_dir2/test_file.py
# running a specific test
pytest test_dir1/test_file.py::my_test_name
# Specifying a specific test method
pytest tests/test_mod.py::TestClass::test_method
# Specifying a specific parametrization of a test
pytest tests/test_mod.py::test_func[x1,y2]
# marker expressions: run all tests decorated with `@pytest.mark.slow`
pytest -m slow
# marker expression: `@pytest.mark.slow(phase=1)`
pytest -m "slow(phase=1)"
pytest options
-q: Run in quiet mode (minimal output).-v: Run in verbose mode (detailed output).-s(stdout): Disable output capturing (printsprint()statements).--tb=short: Show a shortened traceback for test failures.-k "expression" (keyword): Run tests matching the given substring or expression.
Test discovery rule
standard test discovery
- If no arguments are specified then collection starts from
testpaths(if configured inpyproject.tomlorpytest.ini), or the current directory. - Recurse into directories, unless they match
norecursedirs. - In those directories, search for
test_*.pyor*_test.pyfiles. - From those files, collect test items:
- functions with
testprefixed name. e.g.,test_is_valid, etc. - Methods with
testprefixed name inside Classes with nameTest{ClassName}without__init__method. - Methods decorated with
@staticmethodand@classmethodsare also considered in classes withTestprefixed name and without__init__.
- functions with
Pytest Configuration
To get help on command line options and values in INI-style configurations files by using the general help option:
This will display command line and configuration file settings which were registered by installed plugins.
- We can specify pytest configurations in either
pytest.ini, or.pytest.ini(hidden file), orpyproject.tomlfile. -
pytest.inifiles take precedence over other files, even when empty. -
sample
pytest.inifile
# pytest.ini or .pytest.ini
[pytest]
minversion = 6.0
addopts = -ra -q
testpaths =
tests
integration
- sample
pyproject.tomlpytest config
# pyproject.toml
[tool.pytest.ini_options]
minversion = "6.0"
testpaths = [
"tests",
]
norecursedirs = [
".git",
".github",
"dist",
"build",
"docs",
]
addopts = [
"--strict-markers",
"--doctest-modules",
"--color=yes",
"--disable-pytest-warnings",
"--ignore=legacy/checkpoints",
]
markers = [
"cloud: Run the cloud tests for example",
]
filterwarnings = [
"error::FutureWarning",
]
xfail_strict = true
junit_duration_report = "call"
Explanation of some pytest config options
minversion = "6.0"→ Requires pytest version 6.0 or higher.testpaths→ Specifies directories or files where pytest should look for tests, improving test discovery performance.norecursedirs→ Prevents pytest from searching for tests in specified directories.addopts→ Additional command-line options for pytest execution.--strict-markers→ Enforces strict marker usage; unregistered markers cause errors.--doctest-modules→ Runs doctests in all modules.--color=yes→ Enables colored output in pytest results.--disable-pytest-warnings→ Suppresses pytest-specific warnings.--ignore=legacy/checkpoints→ Ignores the specified directory when discovering tests.
markers→ Defines custom markers likecloudfor categorizing tests.filterwarnings→ ConvertsFutureWarningto an error, ensuring deprecated features are addressed.xfail_strict = true→ Treats allxfailtests as failures if they unexpectedly pass.junit_duration_report = "call"→ Includes test call durations in JUnit XML reports.
Recommended project structure
To run tests:
- First install project in
editable mode.
- then, run tests