Examples and customization tricks¶
Here is a (growing) list of examples. Contact us if you need more examples or have questions. Also take a look at the comprehensive documentation which contains many example snippets as well. Also, pytest on stackoverflow.com often comes with example answers.
For basic examples, see
Get Started for basic introductory examples
How to write and report assertions in tests for basic assertion examples
Fixtures for basic fixture/setup examples
How to parametrize fixtures and test functions for basic test function parametrization
How to use unittest-based tests with pytest for basic unittest integration
How to run tests written for nose for basic nosetests integration
The following examples aim at various use cases you might encounter.
- Demo of Python failure reports with pytest
- Basic patterns and examples
- How to change command line options defaults
- Pass different values to a test function, depending on command line options
- Dynamically adding command line options
- Control skipping of tests according to command line option
- Writing well integrated assertion helpers
- Detect if running from within a pytest run
- Adding info to test report header
- Profiling test duration
- Incremental testing - test steps
- Package/Directory-level fixtures (setups)
- Post-process test reports / failures
- Making test result information available in fixtures
PYTEST_CURRENT_TEST
environment variable- Freezing pytest
- Parametrizing tests
- Generating parameters combinations, depending on command line
- Different options for test IDs
- A quick port of “testscenarios”
- Deferring the setup of parametrized resources
- Indirect parametrization
- Apply indirect on particular arguments
- Parametrizing test methods through per-class configuration
- Indirect parametrization with multiple fixtures
- Indirect parametrization of optional implementations/imports
- Set marks or test ID for individual parametrized test
- Parametrizing conditional raising
- Working with custom markers
- Marking test functions and selecting them for a run
- Selecting tests based on their node ID
- Using
-k expr
to select tests based on their name - Registering markers
- Marking whole classes or modules
- Marking individual tests when using parametrize
- Custom marker and command line option to control test runs
- Passing a callable to custom markers
- Reading markers which were set from multiple places
- Marking platform specific tests with pytest
- Automatically adding markers based on test names
- A session-fixture which can look at all collected tests
- Changing standard (Python) test discovery
- Working with non-python tests