Unittest单元测试框架
TestCase类
执行顺序
运行
断言 self.assert
1、模块名,以test开头
2、类名以Test开头
3、方法 test开头
4、继承unittest.TestCase
5、运行测试用例 使用unittest
6、断言结果 . 表示通过,或者pass F False,表示断言没有通过 E Error 表示程序内部发生错误 setUp: 每次测试用例方法运行之前都会执行的,前置条件 可以把测试数据放到 setUp当中
tearDown: 每次测试用例完成后,都会执行的方法
断言 assert 如果断言失败,抛出异常,如果成功,正常运行
注意:# 注意需要把读取的字符串格式转成字典格式
login.py
"""
1、模块名,以test开头
2、类名以Test开头
3、方法 test开头
4、继承unittest.TestCase
5、运行测试用例 使用unittest
6、断言结果
. 表示通过,或者pass
F False,表示断言没有通过
E Error 表示程序内部发生错误
setUp: 每次测试用例方法运行之前都会执行的,前置条件
可以把测试数据放到 setUp当中
tearDown: 每次测试用例完成后,都会执行的方法
"""
import unittest
from 接口.common.excel_handler import ExcelHandler
from 接口.common.requests_handler import RequestHandler
from 接口.d4_unittest import visit
from 接口.libs import ddt
# url = 'http://localhost:5000/login'
# data = {"mobliephone": '13112341234', 'pwd': '123456'}
# headers = {'X-Media-Type': 'test.v2'}
# test_data = [
# {'url': 'http://localhost:5000/login',
# 'method': 'post',
# 'data': {"mobliephone": '13112341234', 'pwd': '123456'},
# 'headers': {'X-Media-Type': 'test.v2'},
# 'expected': 'hello world'},
#
# {'url': 'http://localhost:5000/login',
# 'method': 'post',
# 'data': {"mobliephone": '123456', 'pwd': '123'},
# 'headers': {'X-Media-Type': 'test.v2'},
# 'expected': 'hello world'}
# ]
# 从excel中读取数据
test_data = ExcelHandler(r'd:\cases.xlsx').read('Sheet1')
print(test_data)
# 继承unittest.TestCase
@ddt.ddt
class TestLogin(unittest.TestCase):
def setUp(self) -> None:
pass
@ddt.data(*test_data)
# 将*test_data当中的一组测试数据,赋值到data_info这个参数,相当for循环
# 注意需要把读取的字符串格式转成字典格式
def test_login_success(self, data_info):
res = RequestHandler().visit(data_info['method'],
data_info['url'],
json=eval(data_info['data']),
headers=eval(data_info['headers']))
self.assertEqual(res, self.data['expected'])
# try:
# self.assertEqual(1, 3-2)
# except AssertionError as result:
# print('断言失败', result)
# raise AssertionError
def tearDown(self) -> None:
pass
if __name__ == '__main__':
unittest.main()
TestSuite(测试集合,测试套件) TestLoader (加载器) 收集测试用例:TestLoader,加载器,加载测试用例 放到测试集合(测试套件)TestSuite 1、初始化testloader 2、suite = testloader.discover(文件夹路径, 'demo_*.py'), 第二个参数如果不写,默认是以test开头的文件 如果第二个参数写了,就是代表以这个名字demo,开头的文件 3、如果要运行用例,就要放到指定的文件夹中
import os
import unittest
# 1、初始化testloader
testloader = unittest.TestLoader()
# 2、 查找测试用例,加载
dir_path = os.path.dirname(os.path.abspath(__file__))
case_path = os.path.join(dir_path, 'test_cases')
suite = testloader.discover(case_path)
print(suite)
TextTestRunner() 运行器
# report
report_path = os.path.join(dir_path, 'report')
if not os.path.exists(report_path):
os.mkdir(report_path)
file_path = os.path.join(report_path, 'test_result.txt')
# text
with open(file_path, 'w', encoding='utf-8') as f:
# 初始化运行期,是以普通文本生成测试报告,TextTestRunner
runner = unittest.TextTestRunner(f, verbosity=2)
runner.run(suite)
如果想只有某几个模块进行运行,可以使用这种方式
suite = testloader.loadTestsFromModule(test_login)
suite2 = testloader.loadTestsFromModule(test_register)
suite_total = unittest.TestSuite()
suite_total.addTests(suite)
suite_total.addTests(suite2)
# 只运行某几个类
suite = testloader.loadTestsFromTestCase(TestLogin)
suite2 = testloader.loadTestsFromTestCase(TestRecharge)
# 还有根据名字的
suite3 = testloader.loadTestsFromName()
几种加载测试用例的方式:
1、最常用的是,整个项目一起加载,使用discover()
2、只想测试某几个模块的时候,使用loadTestsFromTestCase()
HTMLTestRunner,不是unittest 自带的,需要自己去安装
1、安装方式不是通过pip
2、是一个别人写好的模块,可以直接下载下来的.py
3、复制到目录下,然后导入使用
4、或者放到pyhon的公共库中
注意:对于使用HTMLTestRunner,必须使用二进制读写‘wb’
# report
report_path = os.path.join(dir_path, 'report')
if not os.path.exists(report_path):
os.mkdir(report_path)
file_path = os.path.join(report_path, 'test_result.html')
# text ,对于html 一定要用二进制形式打开,wb
with open(file_path, 'wb') as f:
# 初始化运行期,是以HTML生成测试报告,HTMLTestRunner
runner = HTMLTestRunner(f)
runner.run(suite)
加时间戳,让每个报告都不一样
time.time()
ti = str(int(time.time()))
file_name = 'test_result_{}.html'.format(ti)
file_path = os.path.join(report_path, file_name)
测试报告
代码如下
runtest.py
"""
收集测试用例:TestLoader,加载器,加载测试用例
放到测试集合(测试套件)TestSuite
1、初始化testloader
2、suite = testloader.discover(文件夹路径, 'demo_*.py'), 第二个参数如果不写,默认是以test开头的文件
如果第二个参数写了,就是代表以这个名字demo,开头的文件
3、如果要运行用例,就要放到指定的文件夹中
TestRunner
1、运行用例
2、生成测试报告
"""
import os
import unittest
# 1、初始化testloader
from 接口 import test_login
from 接口.HTMLTestRunnerNew import HTMLTestRunner
from 接口.test_cases import test_register
from 接口.test_cases.test_recharge import TestRecharge
from 接口.test_login import TestLogin
import time
testloader = unittest.TestLoader()
# 2、 查找测试用例,加载
dir_path = os.path.dirname(os.path.abspath(__file__))
case_path = os.path.join(dir_path, 'test_cases')
suite = testloader.discover(case_path)
# 只运行某几个模块
# suite = testloader.loadTestsFromModule(test_login)
# suite2 = testloader.loadTestsFromModule(test_register)
# 只运行某几个类
# suite = testloader.loadTestsFromTestCase(TestLogin)
# suite2 = testloader.loadTestsFromTestCase(TestRecharge)
# suite3 = testloader.loadTestsFromName()
# suite_total = unittest.TestSuite()
# suite_total.addTests(suite)
# suite_total.addTests(suite2)
print(suite)
# report
report_path = os.path.join(dir_path, 'report')
if not os.path.exists(report_path):
os.mkdir(report_path)
# file_path = os.path.join(report_path, 'test_result.txt')
ti = str(int(time.time()))
file_name = 'test_result_{}.html'.format(ti)
file_path = os.path.join(report_path, file_name)
# text ,对于html 一定要用二进制形式打开,wb
with open(file_path, 'wb') as f:
# 初始化运行期,是以普通文本生成测试报告,TextTestRunner
# runner = unittest.TextTestRunner(f, verbosity=2)
runner = HTMLTestRunner(f)
runner.run(suite)
总结:
一、unittest 运行方式
1、unittest 右键
2、python 代码 main:unittest.main()
二、断言方式
self.assertEqual(expected, actual) 会提示预期结果和实际结果
self.assertTrue(表达式) 预期结果和实际结果,没有具体的提示
三、条件准备
def setUp(self):
def tearDown(self):
前置条件,测试用例执行之前都会执行的代码
后置条件,测试用例执行之后都会执行的代码
四、 测试用例执行流程
1、初始化加载器:
testloader = unittest.TestLoader()
2、查找测试用例:
suite = testloader.discover(文件夹, ‘test_*.py’), 以test开头的文件
其他的加载方式:
3、打开一个 with open() as f:
4、初始化运行器
runner = 运行器(f)
5、运行器运行用例
runner.run(suite)
测试报告HTMLTestRunner.py 从网上下载这个文件,并加时间戳,来生成不同的报告
测试用例执行
用例数据管理 Excel