python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)
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
python单元测试框架pytest——fixture函数(类似unitest的setup和teardown)的更多相关文章
- 【Pytest】python单元测试框架pytest简介
		
1.Pytest介绍 pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高.根据pytest的官方网站介绍 ...
 - python单元测试框架pytest
		
首先祝大家国庆节日快乐,这个假期因为我老婆要考注会,我也跟着天天去图书馆学了几天,学习的感觉还是非常不错的,这是一篇总结. 这篇博客准备讲解一下pytest测试框架,这个框架是当前最流行的python ...
 - python单元测试框架——pytest
		
官网:https://docs.pytest.org/en/latest/ pytest帮你写出更好的程序 1.An example of a simple test:(一个简单的例子),命名为tes ...
 - Python单元测试框架pytest常用测试报告类型
		
先前博客有介绍pytest测试框架的安装及使用,现在来聊聊pytest可以生成哪些测试报告 1.allure测试报告 关于allure报告参见先前的一篇博文:https://www.cnblogs.c ...
 - Python单元测试框架之pytest 4 -- 断言
		
From: https://www.cnblogs.com/fnng/p/4774676.html Python单元测试框架之pytest -- 断言 2015-08-31 23:57 by 虫师, ...
 - Python单元测试框架之pytest 3 -- fixtures
		
From: https://www.cnblogs.com/fnng/p/4769020.html Python单元测试框架之pytest -- fixtures 2015-08-29 13:05 b ...
 - Python单元测试框架之pytest 2 -- 生成测试报告
		
From: https://www.cnblogs.com/fnng/p/4768239.html Python单元测试框架之pytest -- 生成测试报告 2015-08-29 00:40 by ...
 - python单元测试框架笔记
		
目录 单元测试概述 什么是单元测试 单元测试什么进行? 单元测试由谁负责? 单元测试需要注意 单元测试覆盖类型 python 单元测试框架 unittest pytest 测试框架 单元测试概述 什么 ...
 - Python单元测试框架
		
目录 概况 系统要求 使用PyUnit构建自己的测试 安装 测试用例介绍 创建一个简单测试用例 复用设置代码:创建固件 包含多个测试方法的测试用例类 将测试用例聚合成测试套件 嵌套测试用例 测试代码的 ...
 
随机推荐
- STL map 的 key 元素
			
在做 compiler 语义分析时, 需要用到 map<?,?> 在别人的代码上做扩展, 所以有些代码是不能动的 这时, 需要一个 map<symbol,int> 的数据结构, ...
 - shell脚本学习总结03--别名的使用
			
1.创建别名 $ alias dms='cd Oracle/Middleware/user_projects/domains/7001_costctl/' $ dms $ dms $ pwd /hom ...
 - Struts 入门(一)   搭建Struts环境
			
eclipse中创建项目 搭建步骤: 1.创建web项目 2.下载导入相关jar包 3.创建并完善相关配置文件 4.创建(控制器)Action 并测试启动 1.文件--新建--动态web项目 给项目起 ...
 - R中K-Means、Clara、C-Means三种聚类的评估
			
R中cluster中包含多种聚类算法,下面通过某个数据集,进行三种聚类算法的评估 # ============================ # 评估聚类 # # ================= ...
 - Android 判断当前thread 是否是UI thread
			
在Android 中判断当前的Thread是否是UI Thread 的方法: 1. if (Looper.myLooper() == Looper.getMainLooper()) { // Curr ...
 - /usr/local/nginx/sbin/nginx -s reload  失败原因pid 进程记录和当前不符
			
[root@a ~]# /usr/local/nginx/sbin/nginx -s reload;nginx: [alert] kill(18834, 1) failed (3: No such p ...
 - zipline目录结构
			
下面列出了zipline主要的目录和文件结构和它的说明 ├── ci - 持续集成相关 ├── conda - 生成conda 包相关 ├── docs - 文档 │ ├── notebooks - ...
 - css冲突2 要关闭的css在项目代码以外,但是是通过<link>标签引入的css(例如bootstrap):解决方法,在APP.css中使用全局样式
			
css冲突,导致html字体过小. 通过浏览器检查发现,导致字体过小的css来自bootstrap. 现要关闭bootstrap的css: 直接在APP.css中添加: html{ font-size ...
 - requests+BeautifulSoup详解
			
简介 Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作,甚至包括各种 ...
 - git学习------>如何汉化GitLab?
			
在上一篇博客中,已经正常安装好了GitLab,然而全部界面都是纯英文的,为了照顾整个团队的英文水平,因此这篇博客的目的是将纯英文的GitLab进行汉化. 纯英文界面 第一步: 确认GitLab版本号 ...