当前位置:网站首页>Test framework unittest skip test
Test framework unittest skip test
2022-07-25 16:27:00 【wangmcn】
Skip the test
Catalog
- 1、 Use decorator to skip method
- 2、 Skip class with decorator
- 3、 Use skipTest() Skip method
When executing tests that you want to skip , We can go through skip、skipIf、skipUnless Decorator skips a test method / Test class , Or use TestCase.skipTest() Method to skip a test method .
@unittest.skip(reason):
skip(reason) Decorator , Skip the decoration test unconditionally , And explain why the test was skipped .
@unittest.skipIf(reason):
skipIf(condition,reason) Decorator , When the condition is true , Skip the decoration test , And explain why the test was skipped .
@unittest.skipUnless(reason):
skipUnless(condition,reason) Decorator , When the condition is false , Skip the decoration test , And explain why the test was skipped .
@unittest.expectedFailure: The test is marked as failed .
1、 Use decorator to skip method
1、 establish IgnoreDemo.py file , Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
Skip the test ( Skip method )
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo(unittest.TestCase):
def test_case1(self):
print('test_case1')
# skip Jump unconditionally
@unittest.skip(' Skip use cases test_case2')
def test_case2(self):
print('test_case2')
# skipIf When condition by True Skip on
@unittest.skipIf(0>1, ' Don't skip use cases test_case3')
def test_case3(self):
print('test_case3')
# skipIf When condition by True Skip on
@unittest.skipIf(0<1, ' Skip use cases test_case4')
def test_case4(self):
print('test_case4')
# skipUnless When condition by False Skip on
@unittest.skipUnless(0>1, ' Skip use cases test_case5')
def test_case5(self):
print('test_case5')
# skipUnless When condition by False Skip on
@unittest.skipUnless(0<1, ' Don't skip use cases test_case6')
def test_case6(self):
print('test_case6')
# The test is marked as failed
@unittest.expectedFailure
def test_case7(self):
print('test_case7')
if __name__ == '__main__':
unittest.main(verbosity=2)2、 perform IgnoreDemo.py file , Running results :
test_case1 Method execution ;
test_case2 Methods use skip Decorator , Skip don't execute ;
test_case3 Methods use skipIf Decorator , The condition is false , perform ;
test_case4 Methods use skipIf Decorator , Condition is true , Skip don't execute ;
test_case5 Methods use skipUnless Decorator , The condition is false , Skip don't execute ;
test_case6 Methods use skipUnless Decorator , Condition is true , perform ;
test_case7 Methods use expectedFailure Decorator , The test is marked as failed .
2、 Skip class with decorator
1、 establish IgnoreDemo2.py file , Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
Skip the test ( Skip class )
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo1(unittest.TestCase):
def test_case1(self):
print('test_case1')
def test_case2(self):
print('test_case2')
@unittest.skip(' Skip class 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、 perform IgnoreDemo2.py file , Running results :
TestDemo1 Class execution ( Execution method test_case1、test_case2);
TestDemo2 Class uses skip Decorator , Skip don't execute ( Class method test_case3、test_case4 And don't execute ).
3、 Use skipTest() Skip method
1、 establish IgnoreDemo3.py file , Script code :
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Import unittest modular
import unittest
"""
Skip the test :TestCase.skipTest() Method
"""
# Define test classes , The parent class is unittest.TestCase
class TestDemo(unittest.TestCase):
def test_case1(self):
print('test_case1')
# TestCase.skipTest() Method
def test_case2(self):
self.skipTest(' Skip use cases test_case2')
print('test_case2')
if __name__ == '__main__':
unittest.main(verbosity=2)2、 perform IgnoreDemo3.py file , Running results :
test_case1 Method execution ;
test_case2 Methods use TestCase.skipTest() Method , Skip don't execute .
边栏推荐
猜你喜欢

MyBaits
![[JS advanced] JS regular correlation functions and regular objects_ 02](/img/a0/27bf3f5146a5774eb3167a69d1e3cf.png)
[JS advanced] JS regular correlation functions and regular objects_ 02
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]

leetcode:6127. 优质数对的数目【位运算找规律 + 两数之和大于等于k + 二分】

Quickly deploy mqtt clusters on AWS using terraform

使用Huggingface在矩池云快速加载预训练模型和数据集

Communication between processes (pipeline details)

解决Win10磁盘占用100%

Various useful forms of London Silver K-line chart

终极套娃 2.0 | 云原生交付的封装
随机推荐
Attachment handling of SAP Fiori
MySQL显式锁
【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码
Quickly deploy mqtt clusters on AWS using terraform
Exploration of 6-wire SPI transmission mode
Gap locks
Typescript learning 1 - data types
链游开发现成版 链游系统开发详细原理 链游源码交付
测试驱动开发(TDD)在线练功房 | 9月17日开课
How does win11's own drawing software display the ruler?
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
MySQL页锁
Win11动态磁贴没了?Win11中恢复动态磁贴的方法
MySQL-自增锁
mysql 表写锁
SAP Fiori 的附件处理(Attachment handling)
Promise date
mysql 查看是否锁表
Analysis and solution of data and clock mismatch delay in SPI transmission
Recursive menu query (recursion: check yourself)