pytest的setup和teardown函数(曾被一家云计算面试官问到过)。

pytest提供了fixture函数用以在测试执行前和执行后进行必要的准备和清理工作。与python自带的unitest测试框架中的setup、teardown类似,但是fixture函数对setup和teardown进行了很大的改进。

  • fixture函数可以使用在测试函数中,测试类中,测试文件中以及整个测试工程中。
  • fixture支持模块化,fixture可以相互嵌套
  • fixture支持参数化
  • fixture支持unittest类型的setup和teardown

1)模块级(setup_module/teardown_module)开始于模块始末:在所有测试用例开始执行setup_module和所有用例执行结束后执行teardown_module

2)类级(setup_class/teardown_class)开始于类的始末:在当前测试类的开始与结束执行

3)类里面的(setup/teardown)(运行在调用函数的前后)

4)功能级(setup_function/teardown_function)开始于功能函数始末(不在类中):用于每个测试用例开始执行时执行setup_function和在每个用例执行结束后执行teardown_function

5)方法级(setup_method/teardown_method)开始于方法始末(在类中):在每个测试方法开始与结束执行

 1 def setup_module(module):
2 print("setup_module module:%s" % module.__name__)
3
4
5 def teardown_module(module):
6 print("teardown_module module:%s" % module.__name__)
7
8
9 def setup_function(function):
10 print("setup_function function:%s" % function.__name__)
11
12
13 def teardown_function(function):
14 print("teardown_function function:%s" % function.__name__)
15
16
17 def test_numbers_3_4():
18 print('test_numbers_3_4 <============================ actual test code')
19 assert 3 * 4 == 12
20
21
22 def test_strings_a_3():
23 print('test_strings_a_3 <============================ actual test code')
24 assert 'a' * 3 == 'aaa'
25
26
27 class TestUM:
28 def setup(self):
29 print("setup class:TestStuff")
30
31 def teardown(self):
32 print("teardown class:TestStuff")
33
34 def setup_class(cls):
35 print("setup_class class:%s" % cls.__name__)
36
37 def teardown_class(cls):
38 print("teardown_class class:%s" % cls.__name__)
39
40 def setup_method(self, method):
41 print("setup_method method:%s" % method.__name__)
42
43 def teardown_method(self, method):
44 print("teardown_method method:%s" % method.__name__)
45
46 def test_numbers_5_6(self):
47 print('test_numbers_5_6 <============================ actual test code')
48 assert 5 * 6 == 30
49
50 def test_strings_b_2(self):
51 print('test_strings_b_2 <============================ actual test code')
52 assert 'b' * 2 == 'bb'

好好观察一下运行结果:

setup_module      module:test_pytest_setup_teardown
setup_function    function:test_numbers_3_4
test_numbers_3_4  <============================ actual test code
.teardown_function function:test_numbers_3_4
setup_function    function:test_strings_a_3
test_strings_a_3  <============================ actual test code
.teardown_function function:test_strings_a_3
setup_class       class:TestUM
setup_method      method:test_numbers_5_6
setup             class:TestStuff
test_numbers_5_6  <============================ actual test code
.teardown          class:TestStuff
teardown_method   method:test_numbers_5_6
setup_method      method:test_strings_b_2
setup             class:TestStuff
test_strings_b_2  <============================ actual test code
.teardown          class:TestStuff
teardown_method   method:test_strings_b_2
teardown_class    class:TestUM
teardown_module   module:test_pytest_setup_teardown

最新文章

  1. java---构造器
  2. ubuntu系统怎么分区
  3. &lt;td valign=&quot;center&quot; align=&quot;left&quot;&gt;
  4. 【原创】新手用外挂来学C语言,外挂入门教程【2013.03.12更新V5.1版
  5. JMeter Http测试计划
  6. linux 学习10 shell 基础
  7. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree
  8. kali linux Python开发环境初始化
  9. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property &#39;xxx&#39;: no matching editors or conversion strategy found
  10. ArcGIS删除部分数据后全图范围不正确
  11. xml给提示
  12. linux系统批量无人值守安装
  13. cocos2dx环境配置和打包
  14. Mac 下卸载 Graphviz
  15. UILabel 的属性设置
  16. 【DDD】领域驱动设计实践 —— Domain层实现
  17. luogu P5305 [GXOI/GZOI2019]旧词
  18. 构建Dubbo-2.0.7源码
  19. ZJOI2019一试游记
  20. ASP .NET MVC HtmlHelper扩展——简化“列表控件”的绑定

热门文章

  1. python学习【第一篇】python介绍
  2. 使用隧道技术进行C&amp;C通信
  3. 【BZOJ4231】回忆树 离线+fail树+KMP
  4. System.getProperty()方法大全 (转载)
  5. nexus配置第三方库文件
  6. MySQL数据库主从同步延迟分析及解决方案
  7. zipline目录结构
  8. 并发编程 - 线程 - 1.互斥锁/2.GIL解释器锁/3.死锁与递归锁/4.信号量/5.Event事件/6.定时器
  9. HDFS基本命令行操作及上传文件的简单API
  10. vue状态管理器(用户登录简单应用)