目錄
前言:
pytest 中 yield 和 return 的區(qū)別和相同點(diǎn)
共同點(diǎn)
區(qū)別
usefixtures 與傳 fixture 區(qū)別
Pytest 常用的插件
一鍵安裝多個(gè)模塊
前言:
在軟件測試中,生成清晰、易讀的測試報(bào)告是非常重要的。pytest-allure是一個(gè)流行的測試框架和報(bào)告生成工具,它可以幫助測試團(tuán)隊(duì)生成美觀、詳細(xì)的測試報(bào)告。
- 安裝模塊:pip install allure-pytest
# 第一步:生成xml數(shù)據(jù)
pytest --alluredir=./report/xml testcase.py
# 第二步:生成html文件
allure generate --clean ./report/xml -o ./result/html
將截圖加入到報(bào)告里
- allure.attach(f, '圖片名', allure.attachment_type.JPG)
# -*- coding: utf-8 -*-
from selenium import webdriver
import allure
browser=webdriver.Chrome()
browser.get("https://www.baidu.com")
try:
browser.find_element_by_id("zhiyi").send_keys('test123456') # 輸入密碼,
except Exception as e:
file_name = './test.jpg'
browser.save_screenshot(file_name) # 截圖函數(shù)
'''allure添加截圖附件'''
with open(file_name, mode='rb') as file:
# 讀取文件,將讀取的結(jié)果作為參數(shù)傳給allure
f = file.read()
# 把圖片添加到allure操作步驟里
allure.attach(f, 'login', allure.attachment_type.JPG)
raise e
pytest 中 yield 和 return 的區(qū)別和相同點(diǎn)
共同點(diǎn)
- return 和 yield 都可以返回值
區(qū)別
- yield 返回值后,后面的代碼還會(huì)繼續(xù)運(yùn)行
- return 返回值后,后面的代碼不會(huì)繼續(xù)運(yùn)行
# -*- coding: utf-8 -*-
import pytest
@pytest.fixture()
def openbrower():
print("打開瀏覽器")
yield "返回瀏覽器"
print("關(guān)閉瀏覽器")
def test01(openbrower):
print(openbrower)
運(yùn)行結(jié)果
- 證明 yield 后面的代碼仍執(zhí)行了
testcase.py::test01 打開瀏覽器
# 返回值
返回瀏覽器
PASSED關(guān)閉瀏覽器
usefixtures 與傳 fixture 區(qū)別
-
fixture 有返回值,那么 usefixture 就無法獲取到返回值,這個(gè)是裝飾器 usefixture 與用例直接傳 fixture 參數(shù)的區(qū)別。
-
當(dāng) fixture 需要用到 return 出來的參數(shù)時(shí),只能講參數(shù)名稱直接當(dāng)參數(shù)傳入,不需要用到 return 出來的參數(shù)時(shí),兩種方式都可以
-
@pytest.mark.usefixtures("裝飾器名")
Pytest 常用的插件
-
pytest-selenium 集成 selenium
-
pip install allure-pytest 生成漂亮的 allure 測試報(bào)告
-
pip install pytest-sugar 優(yōu)化運(yùn)行效果
-
pip install pytest-rerunfailures 執(zhí)行用例失敗后重新運(yùn)行
-
pip install pytest-xdist 多線程并行與分布式執(zhí)行
-
pip install pytest-assume 多條斷言前面報(bào)錯(cuò)后面依然執(zhí)行
-
pip install pytest-cover 測試覆蓋率
一鍵安裝多個(gè)模塊
- 創(chuàng)建 requirement.txt 文件
selenium==3.0
requests
- pip install -r requirement.txt
??作為一位過來人也是希望大家少走一些彎路
在這里我給大家分享一些自動(dòng)化測試前進(jìn)之路的必須品,希望能對你帶來幫助。
(軟件測試相關(guān)資料,自動(dòng)化測試相關(guān)資料,技術(shù)問題答疑等等)
相信能使你更好的進(jìn)步!
點(diǎn)擊下方小卡片文章來源:http://www.zghlxwxcb.cn/news/detail-600650.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-600650.html
到了這里,關(guān)于pytest-allure 生成測試報(bào)告的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!