#!/usr/bin/env/python3
# -*- coding:utf-8 -*-
'''
Author:leo
Date&Time:2022/3/15 12:32
Project:python3
Comment:
1.转载页面:https://www.cnblogs.com/fnng/p/16010170.html
2.执行方式:python .\lib_xtestrunner.py
'''
# 单元测试
import unittest
from XTestRunner import HTMLTestRunner
# UI测试
import unittest
from XTestRunner import HTMLTestRunner
from selenium import webdriver
from selenium.webdriver.common.by import By
# 接口测试
import requests
import unittest
from XTestRunner import HTMLTestRunner
class Unit_test(unittest.TestCase):
# 单元测试
@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)
def test_success(self):
"""执行成功"""
self.assertEqual(2 + 3, 5)
class UiTest(unittest.TestCase):
# UI测试
"""测试用例说明"""
@classmethod
def setUpClass(cls) -> None:
cls.driver = webdriver.Chrome()
cls.base_url = "https://cn.bing.com/"
@classmethod
def tearDownClass(cls) -> None:
cls.driver.quit()
def test_success(self):
"""测试bing搜索:XTestRunner """
self.driver.get(self.base_url)
search = self.driver.find_element(By.ID, "sb_form_q")
search.send_keys("XTestRunner")
search.submit()
def test_error(self):
"""测试bing搜索,定位失败 """
self.driver.get(self.base_url)
self.driver.find_element(By.ID, "sb_form_qxxx").send_keys("python")
def test_fail(self):
"""测试bing搜索,断言失败 """
self.driver.get(self.base_url)
self.driver.find_element(By.ID, "sb_form_q").send_keys("unittest")
self.assertEqual(self.driver.title, "unittest")
def test_screenshots(self):
"""测试截图"""
self.driver.get(self.base_url)
# 元素截图
elem = self.driver.find_element(By.ID, "sb_form_q")
self.images.append(elem.screenshot_as_base64)
# 竖屏截图
self.images.append(self.driver.get_screenshot_as_base64())
# 最大化截图
self.driver.maximize_window()
self.images.append(self.driver.get_screenshot_as_base64())
class BiTest(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__':
test_flag = 1
if test_flag == 1: # 全部单元测试
suit = unittest.TestSuite()
suit.addTests([
Unit_test("test_success"),
Unit_test("test_skip"),
Unit_test("test_fail"),
Unit_test("test_error"),
UiTest("test_success"),
UiTest("test_error"),
UiTest("test_fail"),
UiTest("test_screenshots"),
BiTest("test_get"),
BiTest("test_post"),
BiTest("test_put"),
BiTest("test_delete")
])
with(open('./unit_result.html', 'wb')) as fp:
runner = HTMLTestRunner(
stream=fp,
title='<project name>Test 自动化测试报告',
description=['类型:selenium', '操作系统:Windows', '浏览器:Chrome', '执行人:测试打工人'],
language='en',
)
runner.run(
testlist=suit,
rerun=2,
save_last_run=False
)
elif test_flag == 2: # UI测试
report = "./ui_result.html"
suit = unittest.TestSuite()
suit.addTests([
UiTest("test_success"),
UiTest("test_error"),
UiTest("test_fail"),
UiTest("test_screenshots")
])
with(open(report, 'wb')) as fp:
unittest.main(testRunner=HTMLTestRunner(
stream=fp,
title='Selenium自动化测试报告',
description=['类型:selenium', '操作系统:Windows', '浏览器:Chrome', '执行人:测试打工人']
))
elif test_flag == 3: # API测试
suit = unittest.TestSuite()
suit.addTests([
BiTest("test_get"),
BiTest("test_post"),
BiTest("test_put"),
BiTest("test_delete")
])
report = "./bi_result.html"
with(open(report, 'wb')) as fp:
unittest.main(testRunner=HTMLTestRunner(
stream=fp,
title='Seldom自动化测试报告',
description=['类型:API', '地址:https://httpbin.org/', '执行人:测试打工人']
))
【Web_接口测试_Python3_Unittest_XTestRunner_测试报告】Unittest测试框架,支持UI/API/UNIT测试,扩展功能支持邮件/黑白名单/截图功能,定制化测试报告
于 2022-03-16 11:15:52 首次发布