文章目錄
- 一、環境準備
-
- 1、Python
- 2、Pytest
- 3、Allure
- 二、demo實作
-
- 1、建立工程
- 2、填寫代碼
- 3、運作工程
前言
建議大家學習pytest及allure架構的時候參照着官網,雖然說官網上都是英文,但Google 有個翻譯網頁的功能,雖然不咋滴 但大緻可以看懂,當然了英文好的小夥伴直接原文學習
雖然網上的資料多,但還是官網全,本文就權當抛磚引玉了
Pytest官方文檔.
Allure官方文檔.
一、環境準備
1、Python
官網下載下傳最新版本,目前為3.8,選擇exe下載下傳安裝,友善加入環境變量
官網連結: 點我.
安裝完畢後打開cmd,校驗是否安裝成功,輸入python指令,如下截圖證明安裝成功環境變量已添加
2、Pytest
這個安裝就簡單了,進入python的安裝目錄,打開Scripts檔案夾。利用快捷方式shift+滑鼠右鍵打開powershell(win10)視窗。win7則是cmd視窗
pip install pytest
順便安裝一下
pip install allure-pytest
安裝成功後打開cmd視窗檢視
其他安裝,如UI自動化需要selenium子產品,接口自動化需要requests子產品,可以順手都安裝完畢
pip install selenium
pip install requests
3、Allure
官方文檔說明有兩種安裝方法,指令行安裝和手動下載下傳安裝,如下截圖,allure依賴java環境,確定已安裝,具體如何安裝可自行百度
本文以手動安裝為例
目前,allure最新版本為2.13.0。allure下載下傳連結
下載下傳後解壓到指定目錄,并添加到環境變量。如本文解壓到G盤下
則環境變量如下添加
添加完畢後重新打開一個cmd視窗,驗證是否成功
至此,環境則準備完畢了,相對還比較簡單吧。下面則開始愉快的自動化測試及報告生成之旅了,demo隻是說明怎麼如何進行自動化測試及測試報告的生成,關于測試架構的慢慢學習積累吧,先會走路再學跑吧
二、demo實作
1、建立工程
demo的gitee位址
打開PyCharm,建立一個工程,如工程名稱PytestDemo,建立工程結構,建立results目錄放置測試結果,當然了,如果不指定目錄則運作項目時預設建立allure-results目錄。
建立test_cases目錄放置測試用例,建立run.py檔案,運作整個測試腳本,建立兩個測試用例,用例内容我直接從官網拷貝的,test_case1.py、test_case2.py。如圖:
2、填寫代碼
至于代碼中某些字段的含義,可以結合官方文檔來
run.py
import pytest
import os
if __name__ == '__main__':
pytest.main(["-sq",
"--alluredir", 'results'])
os.system("allure generate -c results -o report")
test_case1.py
import pytest
class TestCase1():
def test_success(self):
"""this test succeeds"""
assert True
def test_failure(self):
"""this test fails"""
assert False
def test_skip(self):
"""this test is skipped"""
pytest.skip('for a reason!')
def test_broken(self):
raise Exception('oops')
test_case2.py
import pytest
class TestCase2():
@pytest.mark.xfail(condition=lambda: True, reason='this test is expecting failure')
def test_xfail_expected_failure(self):
"""this test is an xfail that will be marked as expected failure"""
assert False
3、運作工程
在run.py頁面,右鍵運作,運作結果:
此刻,檢視工程目錄,生成了report目錄,右鍵index.html,以浏覽器打開,選擇火狐
振奮人心的時刻來臨了,也可以切換成中文語言
這時候發現測試報告中,環境一欄是空白的,不怕,官網給出了
在結果目錄下,建立environment.properties檔案或者xml,本文結果目錄為results
重新運作run.py,然後檢視最新的測試報告
具體的測試結果