一、什麼是pytestallure
pytestallure是一個基於pytest測試框架的擴展,它可以為測試結果生成漂亮的HTML報告,讓測試結果更加直觀、易讀。pytestallure的主要功能包括:測試結果展示、歷史記錄比較、執行時間計算、測試結果按模塊分組等。
二、pytestallure的安裝和配置
1、安裝pytestallure支持包:
$ pip install pytest-allure-adaptor
2、安裝allure-cli:
$ brew install allure
3、在項目中加入pytest.ini配置文件,配置pytestallure插件:
[pytest]
addopts = --alluredir ./allure_report
testpaths = ./tests
# 配置allure插件
pytest_allure_adaptor_host = localhost
pytest_allure_adaptor_port = 8080
pytest_allure_adaptor_reporter = allure
pytest_allure_adaptor_reporter_junit_path = ./allure_report/junit.xml
pytest_allure_adaptor_reporter_allure_path = ./allure_report/allure-report
pytest_allure_adaptor_reporter_others_path = ./allure_report/others
三、pytestallure的使用
1、pytestallure的基本用法是通過pytest的命令行參數來調用插件:
$ pytest --alluredir ./allure_report
2、使用pytestmark標記來標記測試用例的信息,例如優先順序、故障原因等:
@pytest.mark.smoke
def test_demo():
'''這是一個示例測試函數'''
assert True
3、使用pytest-allure-adaptor提供的裝飾器來分類測試用例的結果:
from allure_commons.types import Severity, Status
import allure
@allure.feature('購物車功能模塊')
@allure.story('加入商品到購物車')
@allure.severity(Severity.CRITICAL)
def test_add_to_cart():
'''測試加入商品到購物車功能'''
assert True
@allure.feature('購物車功能模塊')
@allure.story('從購物車刪除商品')
def test_remove_from_cart():
'''測試從購物車刪除商品功能'''
assert True
4、使用pytest-allure-adaptor提供的裝飾器來生成測試步驟和截圖:
@allure.step('打開瀏覽器')
def open_browser():
pass
@allure.step('輸入用戶名和密碼')
def input_username_and_password():
pass
@allure.step('登錄')
def click_login():
pass
def test_login():
'''測試登錄功能'''
open_browser()
input_username_and_password()
click_login()
allure.attach('登錄成功截圖', '***')
@allure.step('添加商品到購物車')
def add_to_cart():
pass
@allure.step('檢查購物車是否有商品')
def check_cart():
pass
def test_shopping_cart():
'''測試購物車功能'''
add_to_cart()
check_cart()
allure.attach('購物車內容截圖', '***')
5、pytestallure還支持將測試結果按模塊分組、比較歷史記錄等功能,可以通過pytest-allure-adaptor的命令行參數來進行配置。
四、pytestallure實踐
下面是一個利用pytestallure進行web自動化測試的示例代碼:
import allure
from selenium import webdriver
@allure.feature('百度搜索功能')
class TestBaiduSearch:
def setup_class(self):
self.driver = webdriver.Chrome()
self.driver.maximize_window()
def teardown_class(self):
self.driver.quit()
@allure.story('基本搜索')
def test_basic_search(self):
self.driver.get('https://www.baidu.com')
self.driver.find_element_by_id('kw').send_keys('pytest-allure')
self.driver.find_element_by_id('su').click()
assert 'pytest-allure' in self.driver.title
allure.attach(self.driver.get_screenshot_as_png(), '基本搜索截圖', allure.attachment_type.PNG)
@allure.story('高級搜索')
def test_advanced_search(self):
self.driver.get('https://www.baidu.com')
self.driver.find_element_by_link_text('高級搜索').click()
self.driver.find_element_by_name('q1').send_keys('pytest-allure')
self.driver.find_element_by_id('domain').send_keys('github.com')
self.driver.find_element_by_id('su').click()
assert '百度搜索-更詳細的搜索結果' in self.driver.title
allure.attach(self.driver.get_screenshot_as_png(), '高級搜索截圖', allure.attachment_type.PNG)
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/190618.html