Installation and Getting Started
Install pytest
Create your first test
Run multiple tests
Assert that a certain exception is raised
Group multiple tests in a class
Request a unique temporary directory for functional tests
Continue reading
Usage and Invocations
Calling pytest through python -m pytest
Possible exit codes
Getting help on version, option names, environment variables
Stopping after the first (or N) failures
Specifying tests / selecting tests
Modifying Python traceback printing
Detailed summary report
Dropping to PDB (Python Debugger) on failures
Dropping to PDB (Python Debugger) at the start of a test
Setting breakpoints
Using the builtin breakpoint function
Profiling test execution duration
Creating JUnitXML format files
Creating resultlog format files
Sending test report to online pastebin service
Early loading plugins
Disabling plugins
Calling pytest from Python code
Using pytest with an existing test suite
Running an existing test suite with pytest
The writing and reporting of assertions in tests
Asserting with the assert statement
Assertions about expected exceptions
Assertions about expected warnings
Making use of context-sensitive comparisons
Defining your own explanation for failed assertions
Assertion introspection details
pytest fixtures: explicit, modular, scalable
Fixtures as Function arguments
Fixtures: a prime example of dependency injection
conftest.py: sharing fixture functions
Sharing test data
Scope: sharing a fixture instance across tests in a class, module or session
Higher-scoped fixtures are instantiated first
Fixture finalization / executing teardown code
Fixtures can introspect the requesting test context
Factories as fixtures
Parametrizing fixtures
Using marks with parametrized fixtures
Modularity: using fixtures from a fixture function
Automatic grouping of tests by fixture instances
Using fixtures from classes, modules or projects
Autouse fixtures (xUnit setup on steroids)
Overriding fixtures on various levels
Marking test functions with attributes
Raising errors on unknown marks: –strict
Marker revamp and iteration
Monkeypatching/mocking modules and environments
Simple example: monkeypatching functions
example: preventing “requests” from remote operations
API Reference
Temporary directories and files
The tmp_path fixture
The tmp_path_factory fixture
The ‘tmpdir’ fixture
The ‘tmpdir_factory’ fixture
The default base temporary directory
Capturing of the stdout/stderr output
Default stdout/stderr/stdin capturing behaviour
Setting capturing methods or disabling capturing
Using print statements for debugging
Accessing captured output from a test function
Warnings Capture
@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
Custom failure messages
Internal pytest warnings
Doctest integration for modules and test files
The ‘doctest_namespace’ fixture
Output format
Skip and xfail: dealing with tests that cannot succeed
Skipping test functions
XFail: mark test functions as expected to fail
Skip/xfail with parametrize
Parametrizing fixtures and test functions
@pytest.mark.parametrize: parametrizing test functions
Basic pytest_generate_tests example
More examples
Cache: working with cross-testrun state
Usage
Rerunning only failures or failures first
Behavior when no tests failed in the last run
The new config.cache object
Inspecting Cache content
Clearing Cache content
Stepwise
unittest.TestCase Support
Benefits out of the box
pytest features in unittest.TestCase subclasses
Mixing pytest fixtures into unittest.TestCase subclasses using marks
Using autouse fixtures and accessing other fixtures
Running tests written for nose
Usage
Supported nose Idioms
Unsupported idioms / known issues
classic xunit-style setup
Module level setup/teardown
Class level setup/teardown
Method and function level setup/teardown
Installing and Using plugins
Requiring/Loading plugins in a test module or conftest file
Finding out which plugins are active
Deactivating / unregistering a plugin by name
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
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
Optionally using hooks from 3rd party plugins
Logging
caplog fixture
Live Logs
Release notes
Incompatible changes in pytest 3.4
Reference
Functions
Marks
Fixtures
Hooks
Objects
Special Variables
Environment Variables
Configuration Options
Good Integration Practices
Install package with pip
Conventions for Python test discovery
Choosing a test layout / import rules
tox
Integrating with setuptools / python setup.py test / pytest-runner
Flaky tests
Why flaky tests are a problem
Potential root causes
Pytest features
Other general strategies
Research
Resources
pytest import mechanisms and sys.path/PYTHONPATH
Test modules / conftest.py files inside packages
Standalone test modules / conftest.py files
Invoking pytest versus python -m pytest
Configuration
Command line options and configuration file settings
Initialization: determining rootdir and inifile
How to change command line options defaults
Builtin configuration file options
Examples and customization tricks
Demo of Python failure reports with pytest
Basic patterns and examples
Parametrizing tests
Working with custom markers
A session-fixture which can look at all collected tests
Changing standard (Python) test discovery
Working with non-python tests
Setting up bash completion
Backwards Compatibility Policy
Deprecation Roadmap
Deprecations and Removals
Deprecated Features
Removed Features
Python 2.7 and 3.4 support plan
Historical Notes
cache plugin integrated into the core
funcargs and pytest_funcarg__
@pytest.yield_fixture decorator
[pytest] header in setup.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
License
Contribution getting started
Feature requests and feedback
Report bugs
Fix bugs
Implement features
Write documentation
Submitting Plugins to pytest-dev
Preparing Pull Requests
Writing Tests
Joining the Development Team
Development Guide
Code Style
Branches
Issues
Release Procedure
Talks and Tutorials
Books
Talks and blog postings
Project examples
Some organisations using pytest
Some Issues and Questions
On naming, nosetests, licensing and magic
pytest fixtures, parametrized tests
pytest interaction with other packages
Contact channels