規范
1、首先創建 py 文件命名以 test_ 開始或者以 _test 結尾
2、若是新建類,測試類需要以 Test_開頭
3、測試用例(方法)需要以 test_開頭
assert 斷言
assert xx:判斷 xx 為真
assert not xx:判斷 xx 不為真
assert a in b:判斷 b 包含 a
assert a == b:判斷 a 等于 b
assert a !=b:判斷 a 不等于 b
from calculator import add, subtractdef test_add(): #passassert add(2, 3) == 5
def test_subtract(): #passassert subtract(5, 3) != 1def test_zhen():#failassert 0
def test_zhen2():#passassert 1def test_jia():#passassert not 0
def test_jia1():#failassert not 1def test_include():#passassert 1 in [1,2,3]
def test_include2():#failassert 4 in [1,2,3]
執行
命令行執行:
pytest .\test_calculator.py 指定執行某文件
pytest 文件夾下所有test_* 或者*_test的文件,全部執行
pytest test_tt.py::Test::test_case1 pytest路徑/文件名::類名::方法名 執行指定文件指定方法
pytest test_tt.py::test_case1 pytest路徑/文件名::方法名
ide執行:
if __name__ == '__main__':
- pytest.main(['test_tt.py']) 指定文件, 指定執行某文件
- pytest.mian() 沒有指定文件,文件夾下所有test_* 或者*_test的文件,全部執行
- pytest.main(['test_tt.py::Test::test_case1']) 指定文件指定類指定方法
- pytest.main(['test_tt.py::test_case1']) 指定文件指定方法
命令行參數執行:
pytest -q 簡化控制臺的輸出
????????pytest -q test_calculator.py
pytest -v 輸出用例更加詳細的執行信息,比如用例所在文件和用例名稱
????????pytest -v test_calculator.py
pytest -k 執行用例中包含‘關鍵字’的用例 pytest -v -k "include" 方法名包含include的會被執行
????????pytest -k "include" test_calculator.py
pytest -s 輸出用例中的調試信息,比如 print 打印信息,如果不加參數則不輸出打印信息
????????pytest -s test_calculator.py
pytest -m 執行‘標記’的內容,執行特定的測試用例,執行有相同標記的測試用例
????????標記方法:方法前 @pytest.mark.run_these 打標run_these
????????執行 pytest -m run_these test_calculator.py test_calculator文件中的所有打標run_these的都會被執行
pytest -x執行失敗則停止執行,后面的用例不會被執行
????????pytest -x test_calculator.py 執行出fail就會break
pytest --maxfail=n執行失敗 n 次之后停止執行,n 是執行失敗的次數
????????pytest --maxfail=3 test_calculator.py 執行,當fail3次的時候,break
pytest --count=n 執行用例 n 次,n=2 就是執行兩次
????????pytest --count=2 test_calculator.py 執行2次
pytest --lf (last failed)重新運行上次失敗的用例,若沒有失敗的會全部跑
????????pytest --lf test_calculator.py 將上次失敗的again一遍,沒有失敗的就all again
pytest --ff (failed first)重新運行所有用例,但首先運行上次失敗的用例
????????pytest --ff test_calculator.py 所有全部執行一遍,上次失敗的優先執行
標記跳過執行
skipif(condition, reason=None) 參數:
condition:跳過的條件,必傳參數reason:標注原因,必傳參數
使用方法:
@pytest.mark.skipif(condition, reason="xxx") condition 條件為真時跳過
@pytest.mark.skip()
標記預期失敗
xfail(condition=None, reason=None, raises=None, run=True, strict=False)
常用參數:
condition:預期失敗的條件,必傳參數reason:失敗的原因,必傳參數
使用方法:
@pytest.mark.xfail(condition, reason="xx")condition 為真則標記失敗
在某種條件不滿足的時候, 預期它是失敗的, 就將它標記為預期失敗, 若condition 條件不滿足則正常執行
預期失敗:
沒有條件,失敗就是xfailed
有條件,條件滿足為真,失敗就是xfailed
有條件,條件不滿足為假,失敗就是failed
參數化
方法:
parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
常用參數:
argnames:參數名
argvalues:參數對應值,類型必須為 list 當參數為一個時格式:[value]
當 參 數 個 數 大 于 一 個 時 , 格 式 為 : [(param_value1,param_value2.....),(param_value1,param_value2 )]
使用方法:
@pytest.mark.parametrize(argnames,argvalues) 參數名,參數值
@pytest.mark.parametrize("a",[3,6])單參數
@pytest.mark.parametrize("a,b",[(1,2),(0,3)])多參數
參數值為 N 個,測試方法就會運行 N 次
標記用例多次執行
首先安裝 repeat: pip install pytest-repeat
@pytest.mark.repeat(n)執行當前用例 n 次 然后再往下執行其他用例
標記用例執行順序
使用:
安 裝 pip install pytest-ordering
@pytest.mark.run(order=1)---第1個執行
@pytest.mark.run(order=2)---第2個執行
@pytest.mark.last---最后執行
fixtrue
自定義測試用例預置條件--pytest 精髓fixture
@pytest.fixture()(scope="function",params=None,autouse=False, ids=None, name=None)
調用時被優先執行 預處理或者重復操作scope:被標記方法的作用域
function(default):作用于每個測試方法,每個 test 都運行一次
class:每個 class類執行開始時執行一次 @pytest.fixture()(scope="class",autouse=True)
module:每個 module 的所有 test開始前只執行一次 @pytest.fixture()(scope="module",autouse=True)
session:多個.py 文件的用例的時候, 如果多個用例只需調用一次fixture,那就可以設置為 scope="session"。
params:(list 類型)提供參數數據,供調用標記方法的函數使用
autouse:是否自動運行,默認為 False 不運行,設置為 True 自動運行
若不為 True 則需要調用才會優先執行。
@pytest.fixture()
定義函數,命名不要以 test 開頭與用例區分開,fixture 有返回值, 沒有返回值默認為 None。用例調用 fixture 返回值,直接就是把 fixture 的函數名稱當做變量名稱。
生成測試報告
想要生成測試報告,需要先安裝 pytest-html
安裝命令: pip install pytest-html
命令行生成:pytest --html==路徑/文件名.html 執行用例文件.py
pytest --html==./report_sample.html test_sample.py html報告
pytest --junit-xml==./report_sample.xml test_sample.py xml報告
使用 PyCharm 生成報告
if name == "__main__": pytest.main('-s','-v','--html==./report_sample.html','test_samle.py')