logo资料库

pytest pdf文档完整版.pdf

第1页 / 共310页
第2页 / 共310页
第3页 / 共310页
第4页 / 共310页
第5页 / 共310页
第6页 / 共310页
第7页 / 共310页
第8页 / 共310页
资料共310页,剩余部分请下载后查看
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
pytest Documentation Release 4.4 holger krekel, trainer and consultant, http://merlinux.eu Mar 31, 2019
Contents . 1 Installation and Getting Started . . . Install pytest . . Create your first test Run multiple tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 1.4 Assert that a certain exception is raised . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.5 Group multiple tests in a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Request a unique temporary directory for functional tests . . . . . . . . . . . . . . . . . . . . . . . . 1.6 1.7 Continue reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Usage and Invocations . . . . . . . . . . . . . . . . . . . Calling pytest through python -m pytest . . . . Possible exit codes . . . . . . . . . . . . . . . . 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.2 . . . . . . . . . . . . . . . . . . . . . 2.3 Getting help on version, option names, environment variables Stopping after the first (or N) failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.4 2.5 Specifying tests / selecting tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.6 Modifying Python traceback printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.7 Detailed summary report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.8 Dropping to PDB (Python Debugger) on failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.9 Dropping to PDB (Python Debugger) at the start of a test . . . . . . . . . . . . . . . . . . . . . . . . 2.10 Setting breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.11 Using the builtin breakpoint function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.12 Profiling test execution duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.13 Creating JUnitXML format files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.14 Creating resultlog format files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.15 Sending test report to online pastebin service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.16 Early loading plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.17 Disabling plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2.18 Calling pytest from Python code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Using pytest with an existing test suite 3.1 Running an existing test suite with pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 The writing and reporting of assertions in tests . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Asserting with the assert statement . . . . . 4.2 Assertions about expected exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Assertions about expected warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.4 Making use of context-sensitive comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3 3 4 4 5 5 6 7 7 7 7 8 8 9 9 12 12 13 13 13 13 16 17 17 17 17 19 19 21 21 22 23 23 i
4.5 Defining your own explanation for failed assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.6 Assertion introspection details . 5 pytest fixtures: explicit, modular, scalable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fixture finalization / executing teardown code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Fixtures can introspect the requesting test context . . . . . . . . . . . . . . . . . . . . . . . . . . . Factories as fixtures Fixtures as Function arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.1 Fixtures: a prime example of dependency injection . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . conftest.py: sharing fixture functions 5.3 Sharing test data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.4 Scope: sharing a fixture instance across tests in a class, module or session . . . . . . . . . . . . . . . 5.5 . 5.6 Higher-scoped fixtures are instantiated first . 5.7 . 5.8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.10 Parametrizing fixtures . 5.11 Using marks with parametrized fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.12 Modularity: using fixtures from a fixture function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.13 Automatic grouping of tests by fixture instances . 5.14 Using fixtures from classes, modules or projects . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.15 Autouse fixtures (xUnit setup on steroids) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5.16 Overriding fixtures on various levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Marking test functions with attributes 6.1 6.2 Marker revamp and iteration . Raising errors on unknown marks: –strict . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Monkeypatching/mocking modules and environments 7.1 7.2 7.3 API Reference . Simple example: monkeypatching functions example: preventing “requests” from remote operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Temporary directories and files . 8.1 8.2 8.3 8.4 8.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The tmp_path fixture . The tmp_path_factory fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ‘tmpdir’ fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The ‘tmpdir_factory’ fixture . The default base temporary directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Capturing of the stdout/stderr output . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting capturing methods or disabling capturing . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1 Default stdout/stderr/stdin capturing behaviour . 9.2 . 9.3 Using print statements for debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.4 Accessing captured output from a test function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Warnings Capture . . . . . . . . . . . . . . . . . . . . . . . . 10.1 @pytest.mark.filterwarnings . . . . . 10.2 Disabling warnings summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3 Disabling warning capture entirely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.4 DeprecationWarning and PendingDeprecationWarning . . . . . . . . . . . . . . . . . . . . . . . . . 10.5 Ensuring code triggers a deprecation warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.6 Asserting warnings with the warns function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.7 Recording warnings . 10.8 Custom failure messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.9 Internal pytest warnings . . . . . . . . . . . . . . . 11 Doctest integration for modules and test files ii 24 25 27 27 29 29 29 29 31 32 33 34 35 38 39 39 41 42 44 49 49 50 53 53 53 54 55 55 56 56 57 58 59 59 59 60 60 63 64 65 65 65 66 66 67 68 68 71
11.1 The ‘doctest_namespace’ fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Output format . . . . . . . . . 12 Skip and xfail: dealing with tests that cannot succeed 12.1 Skipping test functions . 12.2 XFail: mark test functions as expected to fail 12.3 Skip/xfail with parametrize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Parametrizing fixtures and test functions 13.1 @pytest.mark.parametrize: parametrizing test functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.2 Basic pytest_generate_tests example . . . 13.3 More examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Cache: working with cross-testrun state . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.1 Usage . 14.2 Rerunning only failures or failures first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.3 Behavior when no tests failed in the last run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.4 The new config.cache object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.5 Inspecting Cache content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14.6 Clearing Cache content . 14.7 Stepwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 unittest.TestCase Support . 15.1 Benefits out of the box . . . . . . . . . . . . . . . . 15.2 pytest features in unittest.TestCase subclasses . . 15.3 Mixing pytest fixtures into unittest.TestCase subclasses using marks . . . . . . . . . . . 15.4 Using autouse fixtures and accessing other fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 73 75 75 78 80 83 83 85 86 87 87 87 89 90 91 93 93 95 95 96 96 97 101 16 Running tests written for nose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 16.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 16.2 Supported nose Idioms . 16.3 Unsupported idioms / known issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 . . . . . . . . . . . . . . . . 17 classic xunit-style setup 17.1 Module level setup/teardown . 17.2 Class level setup/teardown . . 17.3 Method and function level setup/teardown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 . 104 . . 18 Installing and Using plugins 105 18.1 Requiring/Loading plugins in a test module or conftest file . . . . . . . . . . . . . . . . . . . . . . . 106 18.2 Finding out which plugins are active . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 18.3 Deactivating / unregistering a plugin by name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 19 Writing plugins 107 19.1 Plugin discovery order at tool startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 19.2 conftest.py: local per-directory plugins . 108 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 19.3 Writing your own plugin . 19.4 Making your plugin installable by others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 19.5 Assertion Rewriting . 19.6 Requiring/Loading plugins in a test module or conftest file . . . . . . . . . . . . . . . . . . . . . . . 110 19.7 Accessing another plugin by name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 19.8 Testing plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 Writing hook functions 115 20.1 hook function validation and execution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 iii
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 20.2 firstresult: stop at first non-None result 20.3 hookwrapper: executing around other hooks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 20.4 Hook function ordering / call example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 20.5 Declaring new hooks . 20.6 Optionally using hooks from 3rd party plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 . . . . . 21 Logging 119 21.1 caplog fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 . 21.2 Live Logs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 21.3 Release notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 21.4 Incompatible changes in pytest 3.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Reference . . . . . . . . . . . . . . . . . 22.1 Functions . . . . 22.2 Marks . . . . . 22.3 Fixtures . . . . 22.4 Hooks . . . . 22.5 Objects . . 22.6 Special Variables . . 22.7 Environment Variables . 22.8 Configuration Options . . . . . . . . . . . . . 123 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Good Integration Practices 179 23.1 Install package with pip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 23.2 Conventions for Python test discovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 23.3 Choosing a test layout / import rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 23.4 tox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182 . . . . . . . 182 23.5 Integrating with setuptools / python setup.py test / pytest-runner . . . . . . . . . . . . . . . . . . . . . 24 Flaky tests 24.1 Why flaky tests are a problem . . 24.2 Potential root causes . . 24.3 Pytest features . . 24.4 Other general strategies 24.5 Research . . . . . 24.6 Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 25 pytest import mechanisms and sys.path/PYTHONPATH 25.1 Test modules / conftest.py files inside packages . . . . 25.2 Standalone test modules / conftest.py files . . . . . . 25.3 Invoking pytest versus python -m pytest . . . . . 189 . . . . . . . . . . . . . . . . . . . . . . 189 . . . . . . . . . . . . . . . 190 . . . . . . . . . 190 . . . . . . . . . . . . . . . . . . . . . 26 Configuration 26.1 Command line options and configuration file settings . . . . . . . . . . . . . . . . . . . . . . . . . 26.2 Initialization: determining rootdir and inifile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26.3 How to change command line options defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26.4 Builtin configuration file options . 191 . 191 . 191 . 193 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193 . 27 Examples and customization tricks 195 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 27.1 Demo of Python failure reports with pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207 . . 27.2 Basic patterns and examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 27.3 Parametrizing tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232 27.4 Working with custom markers . 27.5 A session-fixture which can look at all collected tests . . . . . . . . . . . . . . . . . . . . . . . . . . 243 27.6 Changing standard (Python) test discovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 . . . . iv
27.7 Working with non-python tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249 28 Setting up bash completion 29 Backwards Compatibility Policy . . 29.1 Deprecation Roadmap . 30 Deprecations and Removals 30.1 Deprecated Features . 30.2 Removed Features . . . . . . . . . . . . . . 31 Python 2.7 and 3.4 support plan 253 255 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 257 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 267 32 Historical Notes 269 32.1 cache plugin integrated into the core . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 32.2 funcargs and pytest_funcarg__ . . . . . . . . . . . . . . . . . . . . . 269 . . . . . . . . . . . . . . . . . . . . . . . . . 269 32.3 @pytest.yield_fixture decorator . . . . . . . . . . . . . . . . . . . . . 270 32.4 [pytest] header in setup.cfg . . . . . . . . . . 32.5 Applying marks to @pytest.mark.parametrize parameters . . . . . . . . . . 270 32.6 @pytest.mark.parametrize argument names as a tuple . . . . . . . . . . . . . . . . . . . . 270 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 32.7 setup: is now an “autouse fixture” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270 32.8 Conditions as strings instead of booleans . 32.9 pytest.set_trace() . . 271 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272 . 32.10 “compat” properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 License 273 34 Contribution getting started . . . . . . . 275 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 34.1 Feature requests and feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 . . 34.2 Report bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 . 34.3 Fix bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 34.4 Implement features . . . 34.5 Write documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 276 . 34.6 Submitting Plugins to pytest-dev . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277 34.7 Preparing Pull Requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278 34.8 Writing Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 34.9 Joining the Development Team . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Development Guide . 35.1 Code Style . . . 35.2 Branches . 35.3 Issues . . . . 35.4 Release Procedure . . . . . . . . . . . . . . . 36 Talks and Tutorials . 36.1 Books . . 36.2 Talks and blog postings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 . 281 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 . 281 . 282 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285 . 285 . 285 289 . 290 37 Project examples 37.1 Some organisations using pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 Some Issues and Questions 291 38.1 On naming, nosetests, licensing and magic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291 38.2 pytest fixtures, parametrized tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 38.3 pytest interaction with other packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 v
39 Contact channels 295 vi
分享到:
收藏