当前位置:网站首页>测试框架-unittest-命令行操作、断言方法
测试框架-unittest-命令行操作、断言方法
2022-07-25 16:19:00 【wangmcn】
命令行操作、断言方法
目录
- 1、命令行操作
- 1.1、执行测试模块
- 1.2、执行测试类
- 1.3、执行测试方法
- 1.4、打印详细信息
- 1.5、自动搜索执行
- 2、断言方法
1、命令行操作
命令行操作示例使用已经创建好的test_Demo.py文件做为演示。
脚本代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入unittest模块
import unittest
"""
基本示例
"""
# 定义测试类,父类为unittest.TestCase
class TestDemo(unittest.TestCase):
# 必须使用@classmethod装饰器,所有test运行前执行一次
@classmethod
def setUpClass(cls):
print('setUpClass')
# 必须使用@classmethod装饰器,所有test运行完后执行一次
@classmethod
def tearDownClass(cls):
print('tearDownClass')
# 每个测试方法运行前执行
def setUp(self):
print('setUp')
# 每个测试方法运行完后执行
def tearDown(self):
print('tearDown')
# 所有的测试方法都要以test为开头
def test_case1(self):
print('test_case1')
def test_case2(self):
print('test_case2')
def test_case3(self):
print('test_case3')
if __name__ == '__main__':
unittest.main()1.1、执行测试模块
python -m unittest test_module1 test_module2
例如:python -m unittest test_Demo
运行结果:执行测试模块test_Demo。
1.2、执行测试类
python -m unittest test_module.TestClass
例如:python -m unittest test_Demo.TestDemo
运行结果:执行测试模块test_Demo里的TestDemo测试类。
1.3、执行测试方法
python -m unittest test_module.TestClass.test_method
例如:python -m unittest test_Demo.TestDemo.test_case2
运行结果:执行测试模块test_Demo里的TestDemo测试类里的test_case2测试方法。
1.4、打印详细信息
在执行测试模块/类/方法时,可加-v参数打印详细信息。
python -m unittest -v test_module
例如:python -m unittest -v test_Demo
运行结果:执行测试模块test_Demo,打印详细信息。
1.5、自动搜索执行
unittest支持简单的测试搜索。命令进入项目后,框架会自动在当前目录搜索要测试的用例并执行。搜索目录必须是包或者模块,测试用例文件的默认匹配规则为test*.py。
python -m unittest discover
运行结果:当前项目目录只有一个模块(test_Demo.py)。
参数如下:
-v, –verbose 输出信息的详细级别
-s, –start-directory directory 开始搜索目录(默认为当前目录)
-p, –pattern pattern 匹配的文件名(默认为test*.py)
-t, –top-level-directory directory 搜索的顶层目录(默认为start directory)
例如:python -m unittest discover –v
运行结果:执行输出详细信息。
2、断言方法
在执行测试用例的过程中,最终用例是否执行通过,是通过判断测试得到的实际结果和预期结果是否相等决定的,这时会用到断言方法。
常用的断言方法:
assertEqual(a,b,[msg='测试失败时打印的信息']) 断言a和b是否相等,相等则测试用例通过。
assertNotEqual(a,b,[msg='测试失败时打印的信息']) 断言a和b是否相等,不相等则测试用例通过。
assertTrue(x,[msg='测试失败时打印的信息']) 断言x是否True,是True则测试用例通过。
assertFalse(x,[msg='测试失败时打印的信息']) 断言x是否False,是False则测试用例通过。
assertIs(a,b,[msg='测试失败时打印的信息']) 断言a是否是b,是则测试用例通过。
assertNotIs(a,b,[msg='测试失败时打印的信息']) 断言a是否是b,不是则测试用例通过。
assertIsNone(x,[msg='测试失败时打印的信息']) 断言x是否None,是None则测试用例通过。
assertIsNotNone(x,[msg='测试失败时打印的信息']) 断言x是否None,不是None则测试用例通过。
assertIn(a,b,[msg='测试失败时打印的信息']) 断言a是否在b中,在b中则测试用例通过。
assertNotIn(a,b,[msg='测试失败时打印的信息']) 断言a是否在b中,不在b中则测试用例通过。
assertIsInstance(a,b,[msg='测试失败时打印的信息']) 断言a是否是b的一个实例,是则测试用例通过。
assertNotIsInstance(a,b,[msg='测试失败时打印的信息']) 断言a是否是b的一个实例,不是则测试用例通过。1、创建AssertDemo.py文件,脚本代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 导入unittest模块
import unittest
"""
断言方法
"""
# 定义测试类,父类为unittest.TestCase
class TestDemo(unittest.TestCase):
def test_case1(self):
self.assertEqual(1+1,2,'用例失败')
def test_case2(self):
self.assertEqual(1+1,3,'用例失败')
if __name__ == '__main__':
unittest.main(verbosity=2)2、执行AssertDemo.py文件,运行结果:
test_case1方法断言成功;
test_case2方法断言失败。
边栏推荐
- [image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
- Pagehelper.startpage is not effective
- [Shakespeare: keep the fun of being a man]
- doGet与doPost
- MyBaits
- 今天睡眠质量记录84分
- 百度富文本编辑器UEditor单张图片上传跨域
- MySQL metadata lock (MDL)
- MySQL check whether the table is locked
- 哪个led显示屏厂家更好
猜你喜欢

JWT diagram

Visual studio 2022 view class diagram

优必选大型仿人服务机器人Walker X的核心技术突破

【图像去噪】基于双立方插值和稀疏表示实现图像去噪matlab源码

Introduction to redis

Ice 100g network card fragment message hash problem

Sum arrays with recursion

Cookie、cookie与session区别

Waterfall flow layout

How does win11's own drawing software display the ruler?
随机推荐
Recommended collection, which is probably the most comprehensive coding method summary of category type features
2W word detailed data Lake: concept, characteristics, architecture and cases
MySQL self incrementing lock
Shared lock
MySQL metadata lock (MDL)
Breakthrough in core technology of the large humanoid Service Robot Walker x
使用Huggingface在矩池云快速加载预训练模型和数据集
共享锁(Shared Lock)
国债年化利率太低了,有比国债逆回购年化利率还要高的理财产品吗?
墨天轮高分技术文档分享——数据库安全篇(共48个)
JWT diagram
Win11动态磁贴没了?Win11中恢复动态磁贴的方法
MySQL全局锁
mysql意向锁
MySQL global lock
[image denoising] image denoising based on bicube interpolation and sparse representation matlab source code
Quickly deploy mqtt clusters on AWS using terraform
Leetcode:6127. Number of high-quality number pairs [bit operation finding rules + the sum of two numbers is greater than or equal to K + dichotomy]
今天睡眠质量记录84分
0x80131500打不开微软商店的解决办法