当前位置:网站首页>测试框架-unittest-跳过测试
测试框架-unittest-跳过测试
2022-07-25 16:19:00 【wangmcn】
跳过测试
目录
- 1、使用装饰器跳过方法
- 2、使用装饰器跳过类
- 3、使用skipTest()跳过方法
当执行有想要跳过的测试,我们可以通过skip、skipIf、skipUnless装饰器跳过某个测试方法/测试类,或者用TestCase.skipTest()方法跳过某个测试方法。
@unittest.skip(reason):
skip(reason)装饰器,无条件跳过装饰的测试,并说明跳过测试的原因。
@unittest.skipIf(reason):
skipIf(condition,reason)装饰器,条件为真时,跳过装饰的测试,并说明跳过测试的原因。
@unittest.skipUnless(reason):
skipUnless(condition,reason)装饰器,条件为假时,跳过装饰的测试,并说明跳过测试的原因。
@unittest.expectedFailure:测试标记为失败。
1、使用装饰器跳过方法
1、创建IgnoreDemo.py文件,脚本代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入unittest模块
import unittest
"""
跳过测试(跳过方法)
"""
# 定义测试类,父类为unittest.TestCase
class TestDemo(unittest.TestCase):
def test_case1(self):
print('test_case1')
# skip无条件跳过
@unittest.skip('跳过用例test_case2')
def test_case2(self):
print('test_case2')
# skipIf当condition为True时跳过
@unittest.skipIf(0>1, '不跳过用例test_case3')
def test_case3(self):
print('test_case3')
# skipIf当condition为True时跳过
@unittest.skipIf(0<1, '跳过用例test_case4')
def test_case4(self):
print('test_case4')
# skipUnless当condition为False时跳过
@unittest.skipUnless(0>1, '跳过用例test_case5')
def test_case5(self):
print('test_case5')
# skipUnless当condition为False时跳过
@unittest.skipUnless(0<1, '不跳过用例test_case6')
def test_case6(self):
print('test_case6')
# 测试标记为失败
@unittest.expectedFailure
def test_case7(self):
print('test_case7')
if __name__ == '__main__':
unittest.main(verbosity=2)2、执行IgnoreDemo.py文件,运行结果:
test_case1方法执行;
test_case2方法使用skip装饰器,跳过不执行;
test_case3方法使用skipIf装饰器,条件为假,执行;
test_case4方法使用skipIf装饰器,条件为真,跳过不执行;
test_case5方法使用skipUnless装饰器,条件为假,跳过不执行;
test_case6方法使用skipUnless装饰器,条件为真,执行;
test_case7方法使用expectedFailure装饰器,测试标记为失败。
2、使用装饰器跳过类
1、创建IgnoreDemo2.py文件,脚本代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入unittest模块
import unittest
"""
跳过测试(跳过类)
"""
# 定义测试类,父类为unittest.TestCase
class TestDemo1(unittest.TestCase):
def test_case1(self):
print('test_case1')
def test_case2(self):
print('test_case2')
@unittest.skip('跳过类TestDemo2')
class TestDemo2(unittest.TestCase):
def test_case3(self):
print('test_case3')
def test_case4(self):
print('test_case4')
if __name__ == '__main__':
unittest.main(verbosity=2)2、执行IgnoreDemo2.py文件,运行结果:
TestDemo1类执行(执行方法test_case1、test_case2);
TestDemo2类使用skip装饰器,跳过不执行(类里方法test_case3、test_case4也不执行)。
3、使用skipTest()跳过方法
1、创建IgnoreDemo3.py文件,脚本代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入unittest模块
import unittest
"""
跳过测试:TestCase.skipTest()方法
"""
# 定义测试类,父类为unittest.TestCase
class TestDemo(unittest.TestCase):
def test_case1(self):
print('test_case1')
# TestCase.skipTest()方法
def test_case2(self):
self.skipTest('跳过用例test_case2')
print('test_case2')
if __name__ == '__main__':
unittest.main(verbosity=2)2、执行IgnoreDemo3.py文件,运行结果:
test_case1方法执行;
test_case2方法使用TestCase.skipTest()方法,跳过不执行。
边栏推荐
- 用递归进行数组求和
- R语言使用gt包和gtExtras包漂亮地显示表格数据:gt_bar_plot函数和gt_plt_bar_pct函数可视化百分比条形图、原始数据的百分比条形、缩放后的数据的百分比条形、指定数据对齐宽度
- R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
- tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框
- MySQL 悲观锁
- R语言偏相关性计算(Partial Correlation)、使用ggm包的pcor函数计算偏相关性(Partial Correlations)
- 哪个led显示屏厂家更好
- TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
- 进程之间的通信(管道详解)
- Mysql读写锁
猜你喜欢

Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)

Win11自带画图软件怎么显示标尺?

Exploration of 6-wire SPI transmission mode
![[zeloengine] summary of pit filling of reflection system](/img/7a/c85ba66c5dd05908b2d784fab306a2.png)
[zeloengine] summary of pit filling of reflection system

Use huggingface to quickly load pre training models and datasets in moment pool cloud

General test case writing specification

Differences between cookies, cookies and sessions

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
![[wechat applet] detailed explanation of applet host environment](/img/57/582c07f6e6443f9f139fb1af225ea4.png)
[wechat applet] detailed explanation of applet host environment

TypeError: Unrecognized value type: <class ‘str‘> ParserError: Unknown string format
随机推荐
MySQL页锁
论文笔记:Highly accurate protein structure prediction with AlphaFold (AlphaFold 2 & appendix)
[JS advanced] JS regular correlation functions and regular objects_ 02
I interviewed 8 companies and got 5 offers in a week. Share my experience
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
MySQL隐式锁
权限管理-删除菜单(递归)
Promise期约
伦敦银K线图的各种有用形态
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
面试突击:为什么 TCP 需要 3 次握手?
MySQL global lock
Pagehelper.startpage is not effective
MySQL table read lock
从业务需求出发,开启IDC高效运维之路
Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)
[wechat applet] detailed explanation of applet host environment
Understanding service governance in distributed development
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
Typescript learning 2 - Interface