Python unittest基本使用方法代码实例
作者:拉努斯石 时间:2023-06-28 06:42:56
基本使用
import unittest
class Testcase(unittest.TestCase):
@classmethod
def setUpClass(cls):
pass
@classmethod
def tearDownClass(cls):
pass
def setUp(self):
pass
def tearDown(self):
pass
def test01(self):
print("01")
def test02(self):
print("02")
if __name__ == "__main__":
unittest.main()
断言的使用
unittest的执行顺序:
并不是按照代码顺序,而是按照函数名称顺序;
通过添加执行的顺序
if __name__ == "__main__":
#unittest.main()
suite = unittest.TestSuite()
'''
suite.addTest(TestCase01('test_06'))
suite.addTest(TestCase01('test_04'))
suite.addTest(TestCase01('test_02'))
suite.addTest(TestCase01('test_05'))
suite.addTest(TestCase01('test_01'))
suite.addTest(TestCase01('test_07'))
'''
tests =[TestCase01('test_06'),TestCase01('test_02'),TestCase01('test_03'),TestCase01('test_05'),TestCase01('test_01')]
suite.addTests(tests)
runner = unittest.TextTestRunner()
runner.run(suite)
unittest中套件的运用
有多个测试脚本py文件时,需要一次性执行测试脚本
import unittest
import sys
sys.path.append("dir")添加当前工程目录
from Unittest.test_case01 import Testcase01
from Unittest.test_case01 import Testcase02
testcase01 = unittest.TestLoader().loadTestsFromTestCase(Testcase01)
testcase02 = unittest.TestLoader().loadTestsFromTestCase(Testcase02)
suote = unittest.TestCase([testcase01,testcase02])
unittest.TextTestRunner().run(suote)
unittest 中条件控制跳过case
@unittest.skip("这个case不执行")
def test_07(self):
print("执行case07")
flag = "adfadfadfadfadsfaqeewr"
s = "fads"
self.assertIn(s, flag, msg="不包含")
@unittest.skipIf(4==5,"执行")
def test_01(self):
print("执行case01")
# res = requests.get(url=url,params=data).json()
data1 = {
"user": "11111"
}
self.assertDictEqual(data1, data)
unittest中case的批量管理
import unittest
import sys
import os
sys.path.append("dir")添加当前工程目录
from Unittest.test_case01 import Testcase01
from Unittest.test_case01 import Testcase02
case_path = os.getcwd() + '/Unittestcase'
discover = unittest.defaultTestLoader.discover(case_path)
unittest.TextTestRunner().run(discover)
来源:https://www.cnblogs.com/LinxiHuang/p/13191088.html
标签:python,unittest
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
asp如何制作一个安全的页面?
2010-06-29 21:22:00
SQL语句实现查询当前数据库IO等待状况
2024-01-17 02:04:32
SQL2005 四个排名函数(row_number、rank、dense_rank和ntile)的比较
2024-01-18 02:47:08
![](https://img.aspxhome.com/file/2023/3/108543_0s.jpg)
Python列表推导式,元组推导式,字典推导式,集合推导式
2022-08-01 23:26:36
mySQL中replace的用法
2024-01-27 13:42:07
python3获取控制台输入的数据的具体实例
2022-05-08 13:49:37
![](https://img.aspxhome.com/file/2023/6/118726_0s.png)
如何在页面错误时向数据库中添加记录?
2010-06-26 12:26:00
在Pandas中导入CSV数据时去除默认索引的方法汇总
2023-03-16 02:35:53
win10下安装Anaconda的教程(python环境+jupyter_notebook)
2023-11-27 13:27:08
![](https://img.aspxhome.com/file/2023/2/85232_0s.jpg)
什么是Python中的顺序表
2021-07-12 22:17:47
![](https://img.aspxhome.com/file/2023/8/115138_0s.png)
mac下如何将python2.7改为python3
2023-12-07 17:14:43
![](https://img.aspxhome.com/file/2023/2/112822_0s.jpg)
python list转置和前后反转的例子
2022-04-26 10:39:55
![](https://img.aspxhome.com/file/2023/9/81719_0s.jpg)
Python通过文本和图片生成词云图
2023-08-03 09:39:35
![](https://img.aspxhome.com/file/2023/3/82963_0s.jpg)
基于python分析你的上网行为 看看你平时上网都在干嘛
2021-09-02 15:04:39
![](https://img.aspxhome.com/file/2023/6/103146_0s.gif)
浅谈JavaScript中的parseInt()的妙用
2024-04-19 10:47:52
Python 使用SFTP和FTP实现对服务器的文件下载功能
2023-10-29 09:39:40
python使用paramiko执行服务器脚本并拿到实时结果
2021-06-02 17:54:29
如何恢复MySQL主从数据一致性
2024-01-26 23:34:33
![](https://img.aspxhome.com/file/2023/7/95617_0s.png)
MySQL数据库的一次死锁实例分析
2024-01-27 18:21:58
详解使用vue脚手架工具搭建vue-webpack项目
2024-05-21 10:29:19
![](https://img.aspxhome.com/file/2023/3/123933_0s.png)