python自動化學習筆記之pytest

2021-08-19 07:14:00 字數 2177 閱讀 7039

特點:

1,**風格比較自由,可單獨定義方法,也可將方法封裝到class裡

2,引數化執行,可靈活配置測試計畫

3,可生成xml報告,方便結果分析及jenkins整合

安裝可以直接pip install pytest

檔名定義:test_*.py or *_test.py

類定義:test開頭

方法定義:test_開頭(官網要求,但是自己試著不帶_也可以)

**示例:

import pytest

# 基礎方法呼叫也可以放到專用目錄中

class basefunc:

@staticmethod

def base_func(x):

return x+1

@pytest.mark.mymark1

def test_myfunc1():

print('im in test_myfunc1')

assert basefunc.base_func(1) == 2

def test_myfunc2():

print('im in test_myfunc2')

assert basefunc.base_func(2) == 2

class testclass_cls1:

# 測試類不能有__init__建構函式

# 同樣支援setup和teardown,每個case均單獨執行一次

def setup(self):

print('has been setup!!! cls1')

def teardown(self):

print('has been teardown!!! cls1')

# 靜態方法也可以執行,但是不能再用mark註解

@staticmethod

def test_func1():

print('im in func1 cls1')

assert basefunc.base_func(1) == 2

@pytest.mark.mymark2

def test_func2(self):

print('im in func2 cls1')

assert basefunc.base_func(2) < 2

def test_func3(self):

print('im in func3 cls1')

assert basefunc.base_func(0) > 2

if __name__ == "__main__":

pytest.main(['-v', 'study2_test.py', '-m', 'mymark1'])

# 以下方式會執行study2_test.py所在目錄下包含但不限於study2_test.py的所有case檔案

# pytest.main()

常用引數:

pytest -q 靜默模式,只輸出異常case

pytest -v 詳細,顯示明細及case結果標誌燈

pytest casefile.py

指定case檔案執行

pytest casedir

指定路徑執行

pytest casedir/casefile::caseclass::casefunc

執行具體的case方法、類等

pytest --pyargs pkgname

指定包執行,根據系統檔案路徑定位case,後期可以用pip安裝包的方法部署執行case

pytest -k "key_words_1 and not key_words_1"

執行符合key_words_1命名規則的檔案、類及方法,忽略key_words_2命名規則的檔案、類及方法

pytest -m "mark_name"

需要在指定case方法上新增@pytest.mark.mark_name來指定方法屬於哪個mark

pytest --junitxml=file.xml 生成xml報告,方便對執行結果進一步分析,後期可以通過xmltodict庫轉成json格式分析入庫及自定義報告

pytest --html=./report.html 生成html格式報告,需要提前安裝pytest-html模組,個人測試時發現生成的html報告沒有teardown裡面的內容,控制台卻是有teardown裡的內容輸出的,這算是個小bug?

python自動化學習筆記之DAY21

cookie 1 cookie與session cookie出現原因 由於http協議無法保持狀態,但是在認證的環境裡面,需要保持狀態,因此產生了cookie cookie工作原理 由伺服器產生內容,瀏覽器收到請求後儲存在本地,當瀏覽器再次訪問時,瀏覽器會自動帶上cookie,伺服器會通過cooki...

python自動化學習筆記之DAY17

django基礎篇 mtv url url的路徑與檢視函式的對映關係 caidian foo car bar login login template 模板 與html檔案相關的操作 建立專案命令 django admin startprject 專案名 url系統 url配置就像django所支撐...

python介面自動化學習之函式三

可復用 可讀性強 def add a,b return a b print add 2,3 a指向2,b指向3 print add a 3,b 2 a指向3,b指向2 print add b 3,a 2 a指向2,b指向3函式中可以定義預設引數,但是預設引數要放在後面,非預設引數要放到前面。使用函式...