-
pytest介紹
- pytest介紹 - unittest\nose
- pytest:基於unittest之上的單元測試框架
- 自動發現測試模塊和測試方法
- 斷言使用assert+表達式即可
- 可以設置測試會話級、模塊級、類級、函數級的fixtures 數據准備 + 清理工作
- unittest:setUp、teardown、setUpClass、tearDownClass
- 共享前置后置 -- conftest.py
- 有豐富的插件庫,目前在900個以上 allure
- 安裝命令
- pip install pytest
- 安裝html報告的插件
- pip install pytest-html
- pytest插件地址
- pytest:基於unittest之上的單元測試框架
- pytest介紹 - unittest\nose
-
pytest之mark功能
-
pytest - 收集測試用例
- pytest收集測試用例的規則
- 默認從當前目錄中搜集測試用例,即在哪個目錄下運行pytest命令,則從哪個目錄當中搜索
- 搜索規則
- 符合命名規則 test_*.py 或者 *_test.py的文件
- 以test開頭的函數名
- 以Test開頭的測試類(沒有__init__函數)當中,以test_開頭的函數
- pytest收集測試用例的規則
-
對測試用例打標簽。在運行測試用例的時候,可根據標簽名來過濾要運行的用例
-
使用方法
- 注冊標簽名
- 在測試用例/測試前面加上:@pytest.mark.已注冊的標記名
-
注冊方式
-
創建pytest.ini文件,在文件中按如下形式添加標簽名:
-
[pytest] markers= slow:marks tests as slow(deselect with '-m "not slow"') serial
-
注:冒號之后是可選的描述信息
-
-
在conftest.py文件當中,通過hook注冊
-
def pytest_configure(config): config.addinivalue_line("markers","smoke1:標記只運行冒煙用例") config.addinivalue_line("markers","demo1:示例運行")
-
-
-
給用例打標記
-
方式1
- 打標記范圍:測試用例、測試類、模塊文件
- 在測試用例/測試類前面加上:@pytest.mark.標記名
- @pytest.mark.slow
- 可在一個用例上打多個標簽,多次使用@pytest.mark.標記名即可
- @pytest.mark.slow
- @pytest.mark.serial
-
方式2
-
打標記范圍:測試用例、測試類、模塊文件
-
在測試類里,使用以下申明(測試類下,所有用例都被打上該標簽)
-
class TestClass(object): pytestmark = pytest.mark.已注冊標簽名 pytestmark = [pytest.mark.標簽1, pytest.mark.標簽2] # 多標簽模式
-
-
在模塊文件里,同理(py文件下,所有測試函數和測試類里的測試函數,都有該標簽)
-
import pytest pytestmark = pytest.mark.已注冊標簽名 pytestmark = [pytest.mark.標簽1, pytest.mark.標簽2] # 多標簽模式
-
-
-
-
-
-
pytest之命令行運行用例
-
安裝后,pytest.exe在python安裝目錄的Scripts目錄下,因為配置了環境變量后,可以之間運行pytest
-
腳本里面是,效果通命令行
-
import pytest if __name__ == '__main__': pytest.main()
-
-
只運行某個標記
- pytest -m slow
- pytest -m slow -s -v # 詳細輸出
-
-
pytest之fixture功能
-
pytest之fixture參數化 - 多運行、pytest層級覆蓋。測試用例與其同級或者在其子目錄
-
共享前置后置 -- conftest.py
-
文件名不可更改,不需要引入就可以使用其中的fixture
-
一個函數:前置+后置
-
yield分隔前置后置
-
設置作用域:中間的夾的是什么,默認"function"
-
@pytest.fixture def init_driver(): # 前置 pass # 分隔線 yield 返回值寫在這 # 后置 pass
-
-
-
-
調用fixture的三種方式
-
在測試用例中直接調用它
-
將fixture的函數名作為測試用例的參數
-
如果fixture有返回值,那么測試用例中的fixture函數名字就接收返回值
-
eg
-
def test_xxx(self,myfixture): myfixture.find_element_by_xpath("xxx") # 函數名代表了fixture的返回值,即driver
-
-
-
用fixture裝飾器調用fixture
-
在測試用例/測試類前面加上@pytest.mark.usefixtures("fixture函數名字")
-
ps:定義conftest.py文件,在此文件中可定義多個fixture,pytest會自動搜索此文件
-
@pytest.mark.usefixtures("init_driver") class TestLogin: @pytest.mark.slow def test_login_success(self, init_driver): init_driver[1]. #返回值直接用,這里返回元組
-
-
用autos調用fixture
- 在定義fixture時,有一個參數是autouse,默認設置為False
- 當默認為False,就可以選擇用上面兩種方式來試用fixture
- 當設置為True時,在一個session內的所有test都會自動調用這個fixture(權限大,責任也大,所以用該功能時也要謹慎小心)
-
-
-
pytest之參數化
-
在測試用例的前面加上
- @pytest.mark.parametrize("參數名",列表數據)
- 參數名:用來接收每一項數據,並作為測試用例的參數
- 列表數據:一組測試數據
- @pytest.mark.parametrize("參數1,參數2",[(數據1,數據2),(數據1,數據2)])
-
示例
-
@pytest.mark.parametrize("aa,b,c", [(1, 3, 4), (10, 35, 45), (22.22, 22.22, 44.44)]) def test_add(self, a, b, c): res = a + b assert res == c
-
-
組合參數化:多組參數,依次組合
-
使用多個@pytest.mark.parametrize
-
示例
-
@pytest.mark.parametrize("x",[1,2]) @pytest.mark.parametrize("y",[2,3]) def test_foo(x,y): pass
-
用例有四個1,2/1,3/2,2/2,3 笛卡爾積
-
-
-
-
pytest之重運行
- pytest提供了失敗重試機制
- 插件名稱rerunfailures
- 安裝方法
- pip install pytest-rerunfailures
- 使用方式
- 命令行參數形式
- 命令:pytest --reruns 重試次數
- 比如:pytest --reruns 2 表示:運行失敗的用例可以重新運行2次
- 命令:pytest --reruns 重試次數 --reruns-delay 次數之間的延時設置(單位:秒)
- pytest --reruns 2 --reruns-delay5
- 表示失敗的用例可以重新運行2次,第一次和第二次的間隔時間為5秒鍾
- 命令:pytest --reruns 重試次數
- 命令行參數形式
-
pytest之html測試報告
-
需要安裝pytest-html插件
-
pytest可以生成多種樣式的結果
- 生成JunitXML格式的測試報告,命令
- --junitxml=path
- 生成result log格式的測試報告,命令
- --resultlog=report\log.txt
- 生成html格式的測試報告,命令
- --html=report\test_one_func.html(相對路徑)
- 生成JunitXML格式的測試報告,命令
-
import pytest if __name__ == '__main__': pytest.main(["--reruns", "3", "--reruns-delay", "5", "-m", "fail", "--html=Reports\\report.html", "--junitxml=Reports\\report.xml"])
-
-
pytest之allure測試報告
- 安裝allure
- 下載allure.zip
- 下載地址
- alure-github:https://github.com/allure-framework/allure2
- 解壓到本地目錄,配置allure.bat的環境變量ALLURE_HOME
- 在命令行中運行allure,確認環境變量配置成功
- pytest插件安裝
- pip install allure-pytest
- pytest生成allure測試報告的命令參數
- --alluredir=/XXX/my_allure_results
- 查看allure的測試報告命令
- allure serve allure報告目錄 相對/絕對
- eg:allure serve D:\reports\allure
- 安裝allure
-
pytest之jenkins集成
-
安裝插件Allure Jenkins Plugin
-
配置工具路徑D:\allure-2.13.5
-
配置時構建后操作生成allure報告,選擇allure report並配置路徑(相對)
-
分布式
- master/slave模式
- 分擔jenkins服務器的壓力,任務分配到其它執行機來執行
- master:jenkins服務器
- slave:執行機(奴隸機),執行master分配的任務,並返回任務的進度和結果
- master
- 管理節點
- 分配任務
- slave
- 反饋狀態
- 反饋任務進度
- 反饋任務結果
- master/slave
- slave向master注冊
- slave的狀態,空閑/忙碌
- slave的能力,可並行執行任務
- master/slave模式
-
配置
- 節點管理新建節點
- 全局設置--代理--選擇”隨機選取“
- 節點管理新建節點
- 名字 - 可以唯一指定
- 執行器數量 - 可以同時執行的任務數
- 遠程工作目錄 - 執行機的目錄,會自動在該目錄下創建workspace,並建相應的job目錄
- 標簽 - 可以指定一組中隨機一個執行
- 用法 - 指定
- 啟動方式 - Launch agent by connecting it to the master(利用java web連接)
- 可用性 - 盡可能使用
- 節點屬性
- 可以設置執行機的環境變量和工具
- 連接
- 連接處下載slave-agent.jnlp直接在執行機運行
- 可以安裝為系統服務,這樣的化可以靜默執行
- 連接后就可以運行了
- 或者在命令行中啟動節點
- 連接處下載slave-agent.jnlp直接在執行機運行
- 可以執行了
-