Full pytest documentation¶
Download latest version as PDF
Start here¶
How-to guides¶
- How to invoke pytest
- How to write and report assertions in tests
- How to use fixtures
- “Requesting” fixtures
- Autouse fixtures (fixtures you don’t have to request)
- Scope: sharing fixtures across classes, modules, packages or session
- Teardown/Cleanup (AKA Fixture finalization)
- Safe teardowns
- Running multiple
assert
statements safely - Fixtures can introspect the requesting test context
- Using markers to pass data to fixtures
- Factories as fixtures
- Parametrizing fixtures
- Using marks with parametrized fixtures
- Modularity: using fixtures from a fixture function
- Automatic grouping of tests by fixture instances
- Use fixtures in classes and modules with
usefixtures
- Overriding fixtures on various levels
- Using fixtures from other projects
- How to mark test functions with attributes
- How to parametrize fixtures and test functions
- How to use temporary directories and files in tests
- How to monkeypatch/mock modules and environments
- How to run doctests
- How to re-run failed tests and maintain state between test runs
- How to manage logging
- How to capture stdout/stderr output
- How to capture warnings
- Controlling warnings
@pytest.mark.filterwarnings
- Disabling warnings summary
- Disabling warning capture entirely
- DeprecationWarning and PendingDeprecationWarning
- Ensuring code triggers a deprecation warning
- Asserting warnings with the warns function
- Recording warnings
- Additional use cases of warnings in tests
- Custom failure messages
- Internal pytest warnings
- How to use skip and xfail to deal with tests that cannot succeed
- How to install and use plugins
- Writing plugins
- Plugin discovery order at tool startup
- conftest.py: local per-directory plugins
- Writing your own plugin
- Making your plugin installable by others
- Assertion Rewriting
- Requiring/Loading plugins in a test module or conftest file
- Accessing another plugin by name
- Registering custom markers
- Testing plugins
- Writing hook functions
- hook function validation and execution
- firstresult: stop at first non-None result
- hookwrapper: executing around other hooks
- Hook function ordering / call example
- Declaring new hooks
- Using hooks in pytest_addoption
- Optionally using hooks from 3rd party plugins
- Storing data on items across hook functions
- How to use pytest with an existing test suite
- How to use
unittest
-based tests with pytest - How to run tests written for nose
- How to implement xunit-style set-up
- How to set up bash completion
Reference guides¶
Explanation¶
Further topics¶
- Examples and customization tricks
- Backwards Compatibility Policy
- History
- Deprecations and Removals
- Python 2.7 and 3.4 support
- Contribution getting started
- Development Guide
- Sponsor
- pytest for enterprise
- License
- Contact channels
- History
- Historical Notes
- Marker revamp and iteration
- cache plugin integrated into the core
- funcargs and
pytest_funcarg__
@pytest.yield_fixture
decorator[pytest]
header insetup.cfg
- Applying marks to
@pytest.mark.parametrize
parameters @pytest.mark.parametrize
argument names as a tuple- setup: is now an “autouse fixture”
- Conditions as strings instead of booleans
pytest.set_trace()
- “compat” properties
- Talks and Tutorials