Navigation
next
pytest-3.0
»
Full pytest documentation
ΒΆ
Download latest version as PDF
Installation and Getting Started
Installation
Our first test run
Running multiple tests
Asserting that a certain exception is raised
Grouping multiple tests in a class
Going functional: requesting a unique temporary directory
Where to go next
Usage and Invocations
Calling pytest through
python
-m
pytest
Getting help on version, option names, environment variables
Stopping after the first (or N) failures
Specifying tests / selecting tests
Modifying Python traceback printing
Dropping to PDB (Python Debugger) on failures
Setting a breakpoint / aka
set_trace()
Profiling test execution duration
Creating JUnitXML format files
Creating resultlog format files
Sending test report to online pastebin service
Disabling plugins
Calling pytest from Python code
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 assertion comparison
Advanced assertion introspection
Pytest API and builtin fixtures
Invoking pytest interactively
Helpers for assertions about Exceptions/Warnings
Comparing floating point numbers
Raising a specific test outcome
Fixtures and requests
Builtin fixtures/function arguments
pytest fixtures: explicit, modular, scalable
Fixtures as Function arguments
“Funcargs” a prime example of dependency injection
Sharing a fixture across tests in a module (or class/session)
Fixture finalization / executing teardown code
Fixtures can introspect the requesting test context
Parametrizing 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)
Shifting (visibility of) fixture functions
Overriding fixtures on various levels
Monkeypatching/mocking modules and environments
Simple example: monkeypatching functions
example: preventing “requests” from remote operations
Method reference of the monkeypatch fixture
Temporary directories and files
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
Asserting Warnings
Asserting warnings with the warns function
Recording warnings
Ensuring a function triggers a deprecation warning
Doctest integration for modules and test files
The ‘doctest_namespace’ fixture
Output format
Marking test functions with attributes
API reference for mark related objects
Skip and xfail: dealing with tests that cannot succeed
Marking a test function to be skipped
Skip all test functions of a class or module
Mark a test function as expected to fail
Skip/xfail with parametrize
Imperative xfail from within a test or setup function
Skipping on a missing import dependency
specifying conditions as strings versus booleans
Summary
Parametrizing fixtures and test functions
@pytest.mark.parametrize
: parametrizing test functions
Basic
pytest_generate_tests
example
The
metafunc
object
Cache: working with cross-testrun state
Usage
Rerunning only failures or failures first
The new config.cache object
Inspecting Cache content
Clearing Cache content
config.cache API
Support for unittest.TestCase / Integration of fixtures
Usage
Mixing pytest fixtures into unittest.TestCase style tests
autouse fixtures and accessing other fixtures
Converting from unittest to pytest
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
Pytest default plugin reference
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
pytest hook reference
Initialization, command line and configuration hooks
Generic “runtest” hooks
Collection hooks
Reporting hooks
Debugging/Interaction hooks
Reference of objects involved in hooks
Usages and Examples
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
Good Integration Practices
Conventions for Python test discovery
Choosing a test layout / import rules
Tox
Integrating with setuptools /
python
setup.py
test
/
pytest-runner
Basic test configuration
Command line options and configuration file settings
initialization: determining rootdir and inifile
How to change command line options defaults
Builtin configuration file options
Setting up bash completion
Backwards Compatibility Policy
License
Contribution getting started
Feature requests and feedback
Report bugs
Fix bugs
Implement features
Write documentation
Submitting Plugins to pytest-dev
Preparing Pull Requests on GitHub
Talks and Tutorials
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
Release announcements
Table Of Contents
Home
Contents
Install
Examples
Customize
Contact
Talks/Posts
Changelog
License
Related Topics
Documentation overview
Next:
Installation and Getting Started
Useful Links
The pytest Website
Contribution Guide
pytest @ PyPI
pytest @ GitHub
3rd party plugins
Issue Tracker
PDF Documentation
Quick search
Enter search terms or a module, class or function name.