高颜值测试报告-xTestRunner
pip install XTestRunner
# test_unit.py
import unittest
from XTestRunner import HTMLTestRunner
class TestDemo(unittest.TestCase):
"""测试用例说明"""
def test_success(self):
"""执行成功"""
self.assertEqual(2 + 3, 5)
@unittest.skip("skip case")
def test_skip(self):
"""跳过用例"""
pass
def test_fail(self):
"""失败用例"""
self.assertEqual(5, 6)
def test_error(self):
"""错误用例"""
self.assertEqual(a, 6)
if __name__ == '__main__':
suit = unittest.TestSuite()
suit.addTests([
TestDemo("test_success"),
TestDemo("test_skip"),
TestDemo("test_fail"),
TestDemo("test_error")
])
with(open('./result.html', 'wb')) as fp:
runner = HTMLTestRunner(
stream=fp,
title='<project name>test report',
description='describe: ... ',
language='en',
)
runner.run(
testlist=suit,
rerun=2,
save_last_run=False
)
HtmlTestRunner 类说明
- stream : 指定报告的路径
- title : 报告的标题
- description : 报告的描述,支持 str , list 俩种类型
- language : 支持中文 zh-CN,默认 en
run() 方法说明
- testlost : 运行的测试套件
- return :重跑次数
- save_last_run : 是否保存最后一个重跑结果
运行测试
python test_unit.py
接口测试
import requests
import unittest
from XTestRunner import HTMLTestRunner
class YouTest(unittest.TestCase):
def test_get(self):
"""测试get接口 """
r = requests.get("https://httpbin.org/get", params={"key":"value"})
print(r.json())
def test_post(self):
"""测试post接口 """
r = requests.post("https://httpbin.org/post", data={"key":"value"})
print(r.json())
def test_put(self):
"""测试put接口 """
r = requests.put("https://httpbin.org/put", data={"key":"value"})
print(r.json())
def test_delete(self):
"""测试delete接口 """
r = requests.delete("https://httpbin.org/delete", data={"key":"value"})
print(r.json())
if __name__ == '__main__':
report = "./reports/api_result.html"
with(open(report, 'wb')) as fp:
unittest.main(testRunner=HTMLTestRunner(
stream=fp,
title='Seldom自动化测试报告',
description=['类型:API', '地址:https://httpbin.org/', '执行人:xx']
))
支持黑白名单
可以通过黑白名单选择要执行(或跳过)的用例
支持黑白名单
- 白名单:whitelist=["base"]只有使用@label('base')装饰的用例执行
- 黑名单:blacklist=['slow']只有使用@label('slow')装饰的用例不被执行
测试用例
import unittest
from XTestRunner import label
from XTestRunner import HTMLTestRunner
class LabelTest(unittest.TestCase):
@label("base")
def test_label_base(self):
self.assertEqual(1+1, 2)
@label("slow")
def test_label_slow(self):
self.assertEqual(1, 2)
def test_no_label(self):
self.assertEqual(2+3, 5)
if __name__ == '__main__':
report = './reports/label_result.html'
with(open(report, 'wb')) as fp:
unittest.main(testRunner=HTMLTestRunner(
stream=fp,
title='<project name>test report',
description='describe: ... ',
whitelist=["base"], # 设置白名单
# blacklist=["slow"], # 设置黑名单
))
标签:__,self,label,test,xTestRunnwe,unittest,def
From: https://www.cnblogs.com/djl-0628/p/16744847.html