Pytest+Allure環境的搭建
1. pytest的安裝:
1.1. windows下:
pip install pytest
1.2. linux下:
pip install pytest
2. 安裝pytest-allure-adaptor插件
2.1. windows下:
pip install pytest-allure-adaptor
3. allure的安裝:
3.1. windows下:
前情提示: allure
是基於Java
的一個程序,需要Java1.8
的環境,沒有安裝需要去安裝一下。
下載之后,將壓縮包解壓到一個磁盤中,我這里用的是F
盤

image
3.2. 配置allure
的環境變量

image

image
點擊確定,保存。這樣就可以通過CMD
使用allure
命令
3.3. 編寫測試文件
pycharm
新建一個test_demo.py
文件,代碼如下:
import allure @allure.MASTER_HELPER.feature("測試Dome") class TestDome(object): @allure.MASTER_HELPER.step("定義被測函數") def func(self, x): return x+1 @allure.MASTER_HELPER.story("被測場景") @allure.MASTER_HELPER.severity("blocker") @allure.MASTER_HELPER.step("斷言結果") def test_func(self): # with allure.MASTER_HELPER.step("斷言結果"): allure.MASTER_HELPER.attach("預期結果", "{}".format(self.func(3))) allure.MASTER_HELPER.attach("實際結果", "{}".format(5)) assert self.func(3) == 5
3.4. 生成測試報告
在pycharm
中打開terminal

image
輸入命令pytest -s --alluredir=report
,會遇到以下這個錯誤:

image
進入allure
下面的utils
文件,修改以下代碼:
# utils文件,可以通過from allure import utlis進入 for suitable_name in suitable_names: # markers.append(item.get_marker(suitable_name)) markers.append(item.get_closest_marker(suitable_name))

image
修改之后,再次運行pytest -s --alluredir=report
命令:

image
運行后,無上述錯誤,同時會生成一個report
文件。其中會有一個xml
格式的報告:

image

image
當然xml格式的報告不夠直觀,我們需要通過allure
將它轉成HTML
格式的報告。通過cmd
命令cd
到report
的根目錄下,執行allure generate --clean report

image
回到根目錄下,會生成一個allure-report
的文件夾,在pycharm
中打開文件夾,點擊index.html
運行

image
ok,到此為止。可以看到我們的精美的測試報告了

image

作者:努力學習的小白
鏈接:https://www.jianshu.com/p/9673b2aeb0d3
來源:簡書
簡書著作權歸作者所有,任何形式的轉載都請聯系作者獲得授權並注明出處。