当前位置:网站首页>Unittest测试框架
Unittest测试框架
2022-06-12 08:54:00 【马踏飞燕&lin_li】
Unittest测试框架
1、UintTest是python内置的单元测试框架,具备编写用例、组织用例、执行用例、输出报告等自动化框架的条件
2、UintTest单元测试框架不仅适用于单元测试,还可以适用WEB自动化测试、接口自动化测试的开发与执行。用处还是比较广的
四大组件
testcase testfixture textsuite testrunner
测试套件:TestSuite
常用的方法如下:
⑴unittest.TestSuite()
①addTest():添加单个测试用例方法
②addTests([…]):添加多个测试用例方法,方法名存在一个列表
⑵unittest.TestLoader()
①loadTestsFromTestCase(测试类名):添加一个测试类
②loadTestsFromModule(模块名):添加一个模块
③discover(测试用例的所在目录):指定目录去加载,会自动寻找这个目录下所有符合命名规则的测试用例
import unittest
class Test(unittest.TestCase):
def test_1(self):
print(1)
def test_3(self):
print(3)
def test_2(self):
print(2)
def test_4(self):
print(4)
def test_5(self):
print(5)
def add(self):
print("这条样例不会被执行")
if __name__ =='__main__':
#执行全部用例
# unittest.main()
# 创建套件
suit = unittest.TestSuite()
# 添加用例
# suit.addTest(Test("test_3"))
suit.addTests([Test("test_3"),Test("test_2")])
# 执行用例
run = unittest.TextTestRunner()
run.run(suit)
测试固件:TestFixture
1、用于测试用例环境的搭建和销毁。即用例执行前的环境搭建,用例执行后环境的还原。通过覆盖TestCase的setUP()和tearDown()方法来实现
2、setUP()方法:
⑴初始化。在执行测试之前执行,用于准备本次测试所需环境
⑵比如测试前需要登录获取token等就是测试用例需要的环境
⑶比如在测试用例中需要访问数据库,那么可以在setUp()中建立数据库连接来进行初始化
⑷setUP()方法中还可以用来定义一些公共的变量(数据)、代码等。这样在整个测试类中都可以调用这些公共的变量、数据
3、tearDown()方法:
⑴在执行测试之后执行,用于在下一次执行测试前还原环境(跟setUP方法差不多)
⑵tearDown()方法的过程很重要,要为下一次测试留下一个干净的环境,以免影响下一次测试
⑶比如在tearDown()中清除数据库产生的数据,然后关闭连接
4、UintTest框架中的测试固件有两种使用方式
⑴一种是以测试方法(类方法)为维度的setUp()和tearDown():在执行每个测试方法的前后都会执行一次
⑵一种是以测试类(TestCase)为维度的setUpClass()和tearDownClass():在执行每个测试类的前后执行一次(测试方法前后就不会执行了)
第一种使用方式:
import unittest
class Test(unittest.TestCase):
def setUp(self) -> None:
print("打开浏览器")
def test_1(self):
print(1)
def test_3(self):
print(3)
def test_2(self):
print(2)
def tearDown(self) -> None:
print("关闭浏览器")
if __name__ =='__main__':
# 执行全部用例
unittest.main()

第二种使用方式:
import unittest
class Test(unittest.TestCase):
@classmethod # 指明这是个类方法以类为维度去执行的
def setUpClass(cls):
print("===开始执行测试===")
def test_1(self):
print(1)
def test_3(self):
print(3)
def test_2(self):
print(2)
@classmethod # 指明这是个类方法以类为维度去执行的
def tearDownClass(cls):
print("===结束执行测试===")
if __name__ =='__main__':
# 执行全部用例
unittest.main()

断言
import unittest
from selenium import webdriver
class Test(unittest.TestCase):
def setUp(self) -> None:
self.driver = webdriver.Firefox()
self.driver.get("https://www/baidu.com")
self.title = self.driver.title
print(self.title)
return self.title
def test1(self):
self.assertEqual(self.setUp(),"百度一下,你就知道")
if __name__ == '__main__':
unittest.main()
跳过测试
unittest 提供了几个装饰器用于跳过测试:
import unittest
class MyTestCase(unittest.TestCase):
# 跳过测试,参数为测试被跳过的原因
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
# 条件为真时,跳过测试
@unittest.skipIf(1>3,"if true")
def test_format(self):
print("test_format")
pass
# 除非条件为真,否则跳过测试
@unittest.skipUnless(1>3, "if not true")
def test_windows_support(self):
print("test_windows_support")
pass
if __name__ == "__main__":
unittest.main()
子测试
在一个测试中,传入不同的参数测试同一个方法,subTest 子测试可以满足这个需求,而且单个子测试的失败不影响后续子测试的执行。
import unittest
class NumbersTest(unittest.TestCase):
def test_even(self):
""" Test that numbers between 0 and 5 are all even. """
for i in range(0, 6):
with self.subTest(i==i):
self.assertEqual(i % 2, 0)
if __name__ == "__main__":
unittest.main()

边栏推荐
- 【新规划】
- MFS详解(四)——MFS管理服务器安装与配置
- ip、DNS、域名、URL、hosts
- Jump to an interface at a specified time. (Web Development)
- Make a simple page with the websql database of HTML5:
- Gets the number of occurrences of a character in a string
- Difference between binary GB and gib
- 调整svg宽高
- Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low
- 第五章-[bx]和Loop指令
猜你喜欢

《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测

【动态内存管理】malloc&calloc和realloc和笔试题和柔性数组

Adjust SVG width and height
![[advanced pointer I] character array & array pointer & pointer array](/img/ea/150b2162e4e1641eee7e852935d101.png)
[advanced pointer I] character array & array pointer & pointer array

Analysis of 43 cases of MATLAB neural network: Chapter 7 regression of RBF Network -- Realization of nonlinear function regression

Priority issues

利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
![[character set 9] will GBK be garbled when copied to unicode?](/img/dc/c9ec4a90355d30479f23fdead4b349.png)
[character set 9] will GBK be garbled when copied to unicode?

Composition of box model

【无标题】Task3 多路召回
随机推荐
Configuration and principle of MSTP
JVM learning notes: garbage collection mechanism
【sklearn学习】LightGBM
Construction of memcached cache service under Linux:
Shell basic syntax -- arithmetic operation
Centos8 installing MySQL 8.0 (upper)
判断对象是否为空
[GUI development] browsing function implementation model of image processing software
[character set 8] char8_ t、char16_ t、char32_ t、wchar、char
Binlog in mysql:
Knee joint
《MATLAB 神經網絡43個案例分析》:第7章 RBF網絡的回歸--非線性函數回歸的實現
Background location case II
Audio and video related links
数据库不知道哪里出问题
Flink传入自定义的参数或配置文件
You have an error in your SQL syntax; use near ‘and title=‘xxx‘‘ at line 5
Redis installation test
Installation of Shengxin R package
Audio and video engineer (Preliminary) (I) basic concepts of audio and video