没有合适的资源?快使用搜索试试~ 我知道了~
首页pytest.pdf 官方文档
pytest.pdf 官方文档
需积分: 4 10 下载量 173 浏览量
更新于2023-03-03
1
收藏 1.01MB PDF 举报
python 自动化测试 pytest 官方文档,测试开发必备python技能,pytest使用从入门到精通
资源详情
资源推荐
pytest Documentation
Release 5.4
holger krekel, trainer and consultant, http://merlinux.eu
Apr 03, 2020
Contents
1 Installation and Getting Started 3
1.1 Install pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Create your first test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Run multiple tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4 Assert that a certain exception is raised . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.5 Group multiple tests in a class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.6 Request a unique temporary directory for functional tests . . . . . . . . . . . . . . . . . . . . . . . . 6
1.7 Continue reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
2 Usage and Invocations 7
2.1 Calling pytest through python -m pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Possible exit codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Getting help on version, option names, environment variables . . . . . . . . . . . . . . . . . . . . . 8
2.4 Stopping after the first (or N) failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.5 Specifying tests / selecting tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.6 Modifying Python traceback printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.7 Detailed summary report . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.8 Dropping to PDB (Python Debugger) on failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.9 Dropping to PDB (Python Debugger) at the start of a test . . . . . . . . . . . . . . . . . . . . . . . . 12
2.10 Setting breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.11 Using the builtin breakpoint function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.12 Profiling test execution duration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.13 Fault Handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.14 Creating JUnitXML format files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.15 Creating resultlog format files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.16 Sending test report to online pastebin service . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.17 Early loading plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.18 Disabling plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.19 Calling pytest from Python code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3 Using pytest with an existing test suite 21
3.1 Running an existing test suite with pytest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4 The writing and reporting of assertions in tests 23
4.1 Asserting with the assert statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 Assertions about expected exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.3 Assertions about expected warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
i
4.4 Making use of context-sensitive comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.5 Defining your own explanation for failed assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.6 Assertion introspection details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
5 pytest fixtures: explicit, modular, scalable 29
5.1 Fixtures as Function arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.2 Fixtures: a prime example of dependency injection . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.3 conftest.py: sharing fixture functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.4 Sharing test data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.5 Scope: sharing a fixture instance across tests in a class, module or session . . . . . . . . . . . . . . . 32
5.6 Order: Higher-scoped fixtures are instantiated first . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.7 Fixture finalization / executing teardown code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
5.8 Fixtures can introspect the requesting test context . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.9 Factories as fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.10 Parametrizing fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.11 Using marks with parametrized fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
5.12 Modularity: using fixtures from a fixture function . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
5.13 Automatic grouping of tests by fixture instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
5.14 Using fixtures from classes, modules or projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
5.15 Autouse fixtures (xUnit setup on steroids) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
5.16 Overriding fixtures on various levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6 Marking test functions with attributes 53
6.1 Registering marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
6.2 Raising errors on unknown marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7 Monkeypatching/mocking modules and environments 55
7.1 Simple example: monkeypatching functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
7.2 Monkeypatching returned objects: building mock classes . . . . . . . . . . . . . . . . . . . . . . . . 56
7.3 Global patch example: preventing “requests” from remote operations . . . . . . . . . . . . . . . . . 58
7.4 Monkeypatching environment variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
7.5 Monkeypatching dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
7.6 API Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
8 Temporary directories and files 63
8.1 The tmp_path fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
8.2 The tmp_path_factory fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.3 The ‘tmpdir’ fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
8.4 The ‘tmpdir_factory’ fixture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
8.5 The default base temporary directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
9 Capturing of the stdout/stderr output 67
9.1 Default stdout/stderr/stdin capturing behaviour . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.2 Setting capturing methods or disabling capturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
9.3 Using print statements for debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
9.4 Accessing captured output from a test function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
10 Warnings Capture 71
10.1 @pytest.mark.filterwarnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
10.2 Disabling warnings summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.3 Disabling warning capture entirely . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.4 DeprecationWarning and PendingDeprecationWarning . . . . . . . . . . . . . . . . . . . . . . . . . 73
10.5 Ensuring code triggers a deprecation warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.6 Asserting warnings with the warns function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
10.7 Recording warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
ii
10.8 Custom failure messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
10.9 Internal pytest warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
11 Doctest integration for modules and test files 79
11.1 Encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
11.2 Using ‘doctest’ options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
11.3 Continue on failure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.4 Output format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
11.5 pytest-specific features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
12 Skip and xfail: dealing with tests that cannot succeed 85
12.1 Skipping test functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
12.2 XFail: mark test functions as expected to fail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
12.3 Skip/xfail with parametrize . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
13 Parametrizing fixtures and test functions 93
13.1 @pytest.mark.parametrize: parametrizing test functions . . . . . . . . . . . . . . . . . . . 93
13.2 Basic pytest_generate_tests example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
13.3 More examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
14 Cache: working with cross-testrun state 97
14.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.2 Rerunning only failures or failures first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
14.3 Behavior when no tests failed in the last run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.4 The new config.cache object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
14.5 Inspecting Cache content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
14.6 Clearing Cache content . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
14.7 Stepwise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
15 unittest.TestCase Support 105
15.1 Benefits out of the box . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
15.2 pytest features in unittest.TestCase subclasses . . . . . . . . . . . . . . . . . . . . . . . . . 106
15.3 Mixing pytest fixtures into unittest.TestCase subclasses using marks . . . . . . . . . . . . . 106
15.4 Using autouse fixtures and accessing other fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
16 Running tests written for nose 109
16.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
16.2 Supported nose Idioms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
16.3 Unsupported idioms / known issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
17 classic xunit-style setup 111
17.1 Module level setup/teardown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
17.2 Class level setup/teardown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
17.3 Method and function level setup/teardown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
18 Installing and Using plugins 113
18.1 Requiring/Loading plugins in a test module or conftest file . . . . . . . . . . . . . . . . . . . . . . . 114
18.2 Finding out which plugins are active . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
18.3 Deactivating / unregistering a plugin by name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
19 Writing plugins 115
19.1 Plugin discovery order at tool startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
19.2 conftest.py: local per-directory plugins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
19.3 Writing your own plugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
19.4 Making your plugin installable by others . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
iii
剩余336页未读,继续阅读
yeashine88
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 收起
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
会员权益专享
最新资源
- 保险服务门店新年工作计划PPT.pptx
- 车辆安全工作计划PPT.pptx
- ipqc工作总结PPT.pptx
- 车间员工上半年工作总结PPT.pptx
- 保险公司员工的工作总结PPT.pptx
- 报价工作总结PPT.pptx
- 冲压车间实习工作总结PPT.pptx
- ktv周工作总结PPT.pptx
- 保育院总务工作计划PPT.pptx
- xx年度现代教育技术工作总结PPT.pptx
- 出纳的年终总结PPT.pptx
- 贝贝班班级工作计划PPT.pptx
- 变电值班员技术个人工作总结PPT.pptx
- 大学生读书活动策划书PPT.pptx
- 财务出纳月工作总结PPT.pptx
- 大学生“三支一扶”服务期满工作总结(2)PPT.pptx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功