Pytest04-用例失敗控制、標記表達式
1.生成測試報告
生成junit xml文件測試報告
2.用例失敗控制
在第N個很用例失敗之后,結束測試執行
pytest.main(['--maxfail=2'])
3.通過標記表達式執行
pytest.main(['-m','smoke'])
這條命令會執行被
裝飾器 @pytest.mark.smoke 裝飾的所有測試用例
@pytest.mark.smoke
def test03():
print("用例3執行")
assert 1 == 1
先在pytest.ini 文件中注冊自定義標記
[pytest]
markers =
smoke: marks tests as smoke
slow
happy
serial
練習代碼如下:
代碼目錄

pytest.ini文件
[pytest]
markers =
lever1: very impotant case
lever2
lever3
serial
test_assert.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
pytest的斷言機制,用一句話概括借助python語言的
運算符號和assert關鍵字來實現的
"""
import pytest
def test_fuyao_01():
print("用例1")
a = True
b = False
# 測試相等
# assert 1 == 2
# 測試不相等
assert 1 != 2
# 測試小于
assert 1 < 2
# 測試大于
assert 1 > 2
assert 'a' in 'abc'
assert 'd' not in 'abc'
assert a == True
assert b == False
assert b is not True
@pytest.mark.lever1
def test02():
print("用例2")
assert 1 == 2, "測試失敗"
@pytest.mark.lever1
def test03():
print("用例3")
assert 1 == 1, "測試成功"
def test04():
print("用例4")
assert 1 == 1, "測試成功"
if __name__ == '__main__':
# 生成簡單的測試報告
# pytest.main(['--junit-xml=./report/report01.xml'])
# 用例失敗次數控制
# pytest.main(['-vs','--maxfail=2'])
# 通過標記表達式來運行
pytest.main(['-vs','-m','lever1'])
測試技術交流請聯系我

備注博客園扶搖
【學習軟件測試/Python自動化測試技術/領取Python自動化測試學習路線圖/簡歷優化】
視頻鏈接:
課程服務介紹
加微信(備注博客園扶搖)即可免費領取下面的自動化測試資料和一份軟件測試面試寶典


本文來自博客園,作者:測試老宅男扶搖,轉載請注明原文鏈接:http://www.rzrgm.cn/cekailsf/p/17943500
浙公網安備 33010602011771號