原文地址:https://blog.csdn.net/weixin_44006041/article/details/107934174
本文介紹pytest.main運行測試用例的方法
pytest.main():main中傳入不同的指令用以執行指定測試用例
-s: 顯示程序中的print/logging輸出
-v: 豐富信息模式, 輸出更詳細的用例執行信息
-q: 安靜模式, 不輸出環境信息
-k:關鍵字匹配,用and區分:匹配范圍(文件名、類名、函數名)
test_module1.py
1 def test_m1_1(): 2 print('這是 subpath1/test_module1.py::test_m1_1') 3 assert 1==1 4 5 def test_m1_2(): 6 print('這是 subpath1/test_module1.py::test_m1_2') 7 assert 2==2 8 9 def test_spec_1(): 10 print ('這是 subpath1/test_module1.py::test_spec_1') 11 assert 2 == 2
test_module2.py
1 def test_m2_01(): 2 print('這是 subpath1/test_module1.py::test_m1_1') 3 assert 1==1 4 5 class TestM2(): 6 def test_m2_02(self): 7 print ('這是 subpath2/test_module2.py::TestM2::test_m2_02') 8 assert 1==1 9 10 def test_pp(self): 11 print ('這是 subpath2/test_module2.py::TestM2::test_pp') 12 assert 1 == 1
maintest.py
1 import pytest 2 3 if __name__ == '__main__': 4 # 運行當前目錄下所有(test_*.py 和 *_test.py) 5 pytest.main()
運行結果:
1 ============================= test session starts ============================= 2 platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1 3 rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f001_pytest_用例運行 4 collected 6 items 5 6 subpath1\test_module1.py ... [ 50%] 7 subpath2\test_module2.py ... [100%] 8 9 ============================== 6 passed in 0.15s ==============================
本文介紹pytest.main運行測試用例的方法pytest.main():main中傳入不同的指令用以執行指定測試用例-s: 顯示程序中的print/logging輸出-v: 豐富信息模式, 輸出更詳細的用例執行信息-q: 安靜模式, 不輸出環境信息-k:關鍵字匹配,用and區分:匹配范圍(文件名、類名、函數名)
示例
test_module1.py
def test_m1_1(): print('這是 subpath1/test_module1.py::test_m1_1') assert 1==1
def test_m1_2(): print('這是 subpath1/test_module1.py::test_m1_2') assert 2==2
def test_spec_1(): print ('這是 subpath1/test_module1.py::test_spec_1') assert 2 == 21234567891011test_module2.py
def test_m2_01(): print('這是 subpath1/test_module1.py::test_m1_1') assert 1==1
class TestM2(): def test_m2_02(self): print ('這是 subpath2/test_module2.py::TestM2::test_m2_02') assert 1==1
def test_pp(self): print ('這是 subpath2/test_module2.py::TestM2::test_pp') assert 1 == 1123456789101112maintest.py
import pytest
if __name__ == '__main__': # 運行當前目錄下所有(test_*.py 和 *_test.py) pytest.main()12345運行結果:
============================= test session starts =============================platform win32 -- Python 3.6.6rc1, pytest-5.4.1, py-1.8.1, pluggy-0.13.1rootdir: E:\study\python\study\BasicKnowledgePoints\s5_frame\f001_pytest_用例運行collected 6 items
subpath1\test_module1.py ... [ 50%]subpath2\test_module2.py ... [100%]
============================== 6 passed in 0.15s ==============================1234567891、運行指定路徑下的用例
pytest.main(['./']) # 運行./目錄下所有(test_*.py 和 *_test.py)1pytest.main (['./subpath1']) # 運行./subpath1 目錄下用例1pytest.main (['./subpath1/test_module1.py']) # 運行指定模塊1pytest.main (['./subpath1/test_module1.py::test_m1_1']) # 運行模塊中的指定用例1pytest.main (['./subpath2/test_module2.py::TestM2::test_m2_02']) # 運行類中的指定用例1pytest.main (['-k','pp']) # 匹配包含pp的用例(匹配目錄名、模塊名、類名、用例名)1pytest.main(['-k','spec','./subpath1/test_module1.py']) # 匹配test_module1.py模塊下包含spec的用例1pytest.main(['-k','pp','./subpath2/test_module2.py::TestM2']) # 匹配TestM2類中包含pp的用例12、運行參數
pytest.main(['-s','./subpath1/test_module1.py']) # -s: 顯示程序中的print/logging輸出1運行結果:
subpath1\test_module1.py 這是 subpath1/test_module1.py::test_m1_1.這是 subpath1/test_module1.py::test_m1_2.這是 subpath1/test_module1.py::test_spec_1.1234pytest.main(['-v','./subpath1/test_module1.py']) # -v: 豐富信息模式, 輸出更詳細的用例執行信息1運行結果:
subpath1/test_module1.py::test_m1_1 PASSED [ 33%]subpath1/test_module1.py::test_m1_2 PASSED [ 66%]subpath1/test_module1.py::test_spec_1 PASSED [100%]
============================== 3 passed in 0.06s ==============================12345pytest.main(['-q','./subpath1/test_module1.py']) # -q: 安靜模式, 不輸出環境信息1運行結果:
... [100%]3 passed in 0.06s12pytest.main(['-v','-s','./subpath1/test_module1.py']) # 多個參數組合1運行結果:
subpath1/test_module1.py::test_m1_1 這是 subpath1/test_module1.py::test_m1_1PASSEDsubpath1/test_module1.py::test_m1_2 這是 subpath1/test_module1.py::test_m1_2PASSEDsubpath1/test_module1.py::test_spec_1 這是 subpath1/test_module1.py::test_spec_1PASSED
============================== 3 passed in 0.10s ==============================————————————————版權聲明:本文為CSDN博主「小白愛吃飯」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。原文鏈接:https://blog.csdn.net/weixin_44006041/article/details/107934174