接口自动化-unittest&unittestreport&ddt
被测试的函数
def login_check(username=None,password=None):
"""
登录校验
:param username:
:param password:
:return:
"""
if username !=None and password !=None:
if username =="gxj" and password =='123':
return {"code":0,"msg":"登录成功"}
else:
return {"code":1,"msg":"用户名或密码不正确"}
else:
return {"code":1,"msg":"用户名和密码不可为空"}
测试数据准备excel,文件扩展名:xlsx:
case_id | title | expected | param | result |
---|---|---|---|---|
1 | 登录成功 | 成功 | ||
2 | 登录失败 | 成功 |
import unittest
from day11.login_func import login_check
from unittestreport import ddt,list_data
from day12.HandleExcel import Handle_excle
handle = Handle_excle(r"E:\pythoncode\Python基础\day12\apicase.xlsx", "Sheet1")
cases = handle.read()
@ddt
class Test_login(unittest.TestCase):
# 准备用例数据
# 调用被测试的函数,获取实际结果
# 断言
print(cases)
@list_data(cases)
def test_login(self,items):
data =eval(items["param"])
print(data)
expected =eval(items["expected"])
print(expected)
res = login_check(**data)
try:
self.assertEqual(expected,res)
except AssertionError as e:
print(e)
handle.write(int(items["case_id"])+1,5,"失败")
else:
handle.write(int(items["case_id"])+1,5,"成功")
用例收集器+执行
import unittest
from unittestreport import TestRunner
suit = unittest.defaultTestLoader.discover("E:\pythoncode\Python基础\day12")
runner = TestRunner(suit,filename="python自动化",
title='鸿源系统',
tester='高小杰',
desc="鸿源自营API项目测试生成的报告",
templates=1)
runner.run()
生成的测试报告