文章目錄
- 一、常見的第三方庫結合 unittest 生產 html 格式測試報告
- 1、HtmlTestRunner
- 2、BeatifulReport
- 二、裝飾器 @ unittest.skip 強制跳過&條件跳過
- 三、unittest的常用斷言方法
一、常見的第三方庫結合 unittest 生產 html 格式測試報告
1、HtmlTestRunner
官網下載 HtmlTestRunner.py 只能支持 python2 版本,支持 Python3 ,需要做修改
路徑:python安裝路徑/Lib
import unittest
from TestReport.testcase_01 import TestCase01
import HTMLTestRunner# 加載用例
testcases = unittest.TestLoader().loadTestsFromTestCase(TestCase01)
# HTMLTestRunner 生成的測試報告
with open('F:/Pycharm/TestShop/TestReport/html_report.html', 'wb+') as hf:HTMLTestRunner.HTMLTestRunner(stream=hf,title="Html 測試報告",description="測試用例執行詳情").run(testcases)
HTMLTestRunner 文件內容
2、BeatifulReport
安裝三方庫:BeatifulReport
import unittest
from TestReport.testcase_01 import TestCase01
from BeautifulReport import BeautifulReport# 加載用例
testcases = unittest.TestLoader().loadTestsFromTestCase(TestCase01)
# BeautifulReport 生成的測試報告
BeautifulReport(testcases).report(description="Html 測試報告",filename="report_bf",report_dir="reports")
企業測試報告的優化及定制 優化測試報告模板 通過 js+html/html5
pytest+allure 生成更加美觀的測試報告+優化定制(裝飾器)
二、裝飾器 @ unittest.skip 強制跳過&條件跳過
import unittest
"""
@unittest.skip 強制跳過執行
@unittest.skipIf 符合條件,則跳過執行
@unittest.skipUnless 條件不成立,則跳過執行
"""
# @unittest.skipUnless(False,"裝飾器也可以作用于類,整個模塊下的用例強制跳過執行")
class TestCase01(unittest.TestCase):@unittest.skip("此用例暫時不啟用")def test_login(self):print('test_01')@unittest.skipIf(True,"符合條件,則跳過執行")def test_select_goods(self):print('test_02')@unittest.skipUnless(2>3, "不符合條件,則跳過執行")def test_put_to_cart(self):print('test_03')def test_pay_goods(self):print('test_04')
if __name__ == '__main__':unittest.main()
三、unittest的常用斷言方法
import unittest
from selenium import webdriver
import time
from selenium.webdriver.common.by import By'''
二、常用斷言方法
1、assertIn(字符1,字符2) 字符1是否包含在字符2
2、self.assertNotIn(字符1,字符2) 字符1不包含包含在字符2
self.assertEqual(參數1,參數2,"斷言失敗的描述") 參數1等于參數2
self.assertNotEqual(參數1,參數2,"斷言失敗的描述")參數不等于參數2
self.assertTrue(True)
self.assertFalse(False)
'''
class TestCase02(unittest.TestCase):def setUp(self) -> None:# 打開瀏覽器self.driver = webdriver.Edge()# 加載地址self.driver.get("http://116.62.63.211/shop/")def test_login(self):"""登錄用例"""username = "hc_test"password = "hctest123"el_login_link = self.driver.find_element(By.LINK_TEXT, "登錄")# 點擊登錄el_login_link.click()time.sleep(1)el_login = self.driver.find_element(By.XPATH, '//button[text()="登錄"]')el_username = self.driver.find_element(By.NAME, "accounts")el_password = self.driver.find_element(By.XPATH, '//input[@type="password"]')# 輸入用戶名el_username.send_keys(username)# 輸入密碼el_password.send_keys(password)# 點擊登錄time.sleep(1)el_login.click()time.sleep(2)# 斷言 獲取賬號的名稱=usernameel_username = self.driver.find_element(By.XPATH, "//div[@class='menu-hd']/em[2]")self.assertIn(username, el_username.text)def test_select_goods(self):"""檢索商品"""self.driver.get("http://116.62.63.211/shop/")select_goods = "手機"# 定位搜索輸入el_select = self.driver.find_element(By.ID, "search-input")el_select.send_keys(select_goods)el_button = self.driver.find_element(By.ID, "ai-topsearch")el_button.click()time.sleep(2)# 斷言:驗證測試結果與預期結果是否一致# 獲取商品列表的標題el_goods = self.driver.find_elements(By.XPATH, "//div[@class='items am-padding-bottom-xs']")# 判斷content是否包含手機字符?# 標題是否包含手機for el in el_goods:self.assertIn(select_goods, el.text, "商品標題中未包含檢索內容")def tearDown(self) -> None:self.driver.close()if __name__ == '__main__':unittest.main()