Pytest學習筆記(二) 用例執行規則


在用pytest執行用例時,可以按照如下場景來執行

1、執行目錄及其子目錄下的所有用例

pytest filename\

2、執行某一個py文件下的用例

pytest filename.py

3、-k 按關鍵字匹配

 

pytest  test_class.py -k "TestClass and not two"

運行test_class.py中的TestClass.test_one,不運行TestClass.test_two。

4、按節點運行

每個收集的測試都分配了一個唯一的nodeid,由模塊文件名后跟說明符組成。

運行文件中某個測試用例:

pytest test_sample.py::test_answer #文件名::函數名

 

運行文件中某個測試類中的某個用例:

pytest test_class.py::TestClass::test_one   #文件名::類名::函數名

5、-m執行標記用例

執行通過mark標記的所有測試用例。

在test_class.py文件中編寫如下代碼

import pytest
class TestClass(object):
    @pytest.mark.webtest
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

在cmd窗口中執行如下命令:

pytest test_class.py -m webtest

6、-x 遇到錯誤時,停止測試

在test_class.py文件中編寫如下用例:

class TestClass(object):
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

    def test_three(self):
        assert 3 == 5

執行命令:

pytest -x test_class.py

從結果來看,本來有3個用例,在第二個用例失敗后就停止了后面的用例執行了。

7、maxfail 錯誤個數達到指定的數量時,停止測試

pytest --maxfail=1  test_class.py

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM