首先如果你沒有安裝 pytest 庫的話,先使用 pip 安裝一下:
pip install pytest
另外還需要安裝 pytest 支持 allure 報告的插件庫:
pip install allure-pytest
接下來在運行測試時,使用 pytest 命令運行:
pytest <測試目錄> --alluredir <測試結果存放目錄>
例如:
pytest tests --alluredir report/allure_raw
tests 為測試用例存放目錄,告訴 pytest 應該去哪里找用例;
report 是存放測試報告的,allure 收集 pytest 運行后產出的結果放在 allure_raw 文件夾中。
通過allure生產測試報告:
allure generate <allure測試結果目錄> -o <存放報告的目錄> --clean
allure測試結果目錄,是上面運行 pytest 命令后存放結果的地方,我們這里的目錄是 report 下的 allure_raw 文件夾;
存放報告的目錄,是最終生成的測試報告存放的目錄,我打算把生成出的報告放在 report 下的 allure_report文件夾中;
--clean參數用來清空已有的報告,避免覆蓋時出錯。
例如:allure generate report/allure_raw -o report/allure_report --clean,
然后在allure_report下面,通過瀏覽器打開
或者通過啟動服務打開,兩種方式:
1.allure open ./report/allure_report
2.allure serve ./report/allure_raw
定制報告
- Feature: 標注主要功能模塊
- Story: 標注Features功能模塊下的分支功能
- Severity: 標注測試用例的重要級別
- Step: 標注測試用例的重要步驟
- Issue和TestCase: 標注Issue、Case,可加入URL
pytest-repeat
pytest-repeat是pytest的一個插件,用於重復執行單個用例,或多個測試用例,並指定重復次數
使用pip安裝pytest-repeat
pip install pytest-repeat
使用—count命令行選項指定要運行測試用例和測試次數
pytest -v test_foure.py --count=5
如果要在代碼中標記要重復多次的測試,可以使用@pytest.mark.repeat(count)裝飾器
import allure
import pytest
@allure.feature('出口業務')
@allure.story('創建出庫單')
def test_case_01():
"""
出口流程可以正常創建
"""
assert 0
@allure.feature('入庫業務')
@allure.story('創建入庫單')
@pytest.mark.repeat(7)
def test_case_02():
"""
入庫流程可以正常創建
"""
assert 0 == 0
if __name__ == '__main__':
pytest.main(['-s', '-q', '--alluredir', './report/xml'])
centos7 安裝allure
下載allure:https://github.com/allure-framework/allure2/releases/tag/2.7.0
百度網盤:鏈接:https://pan.baidu.com/s/1-QLm6dl6z6sByasbrYMd1A
提取碼:ld7b
下載下來進行解壓到你指定的目錄:
例如:tar -zxvf allure-2.7.0.tgz -C /usr/local
添加環境變量:
vim /etc/profile
添加:PATH="$PATH:/usr/local/allure-2.7.0/bin"
export PATH
然后:wq 報存退出
使用:source /etc/profile 使之生效
最后,在終端輸入:allure,不報錯就可以了