前言
上篇博客說到命令行執行測試用例的部分參數如何使用?今天將繼續更新其他一些命令選項的使用,和pytest收集測試用例的規則!
Pytest執行用例命令行參數
--collect-only:羅列出所有當前目錄下所有的測試模塊,測試類及測試函數
--tb=style:屏蔽測試用例執行輸出的回溯信息,可以簡化用例失敗時的輸出信息。style可以是 on,line,short,具體區別請自行體驗
--lf:當一次用例執行完成后,如果其中存在失敗的測試用例,那么我們可以使用此命令重新運行失敗的測試用例
我們第一次執行用例,會看到有2個用例失敗
我們使用--lf參數再次運行用例,可以發現只有之前失敗的兩個用例重新執行了一次
--ff:如果上次測試用例出現失敗的用例,當使用--ff后,失敗的測試用例會首先執行,剩余的用例也會再次執行一次
小結
以上就是命令行運行測試用例時經常使用到的參數,這些參數不僅可以單獨使用,也可以組合一起使用,后期還會涉及到使用fixture時的一些命令,現在不需要了解。你可以使用--help來查看一些命令幫助信息!
Pytest收集測試用例的規則
1)從一個或者多個目錄開始查找,你可以在命令行指定文件或者目錄,如果未指定那么從當前目錄開始收集用例
2)在該目錄和所有子目錄下遞歸查找測試模塊
3)測試模塊是指文件名為test_*.py或者*_test.py的文件
4)在測試模塊中查找以test_開頭的函數
5)查找名字以Test開頭的類。其中首先篩選掉包含__init__()函數的類,再查找類中以Test_開頭的類方法
規則驗證
現在我們就依次演示pytest搜索測試用例的過程
首先我們按照以下目錄結構新建一個項目
每個文件編寫如下代碼(我們只是為了驗證規則,所以用例些的很簡單,實際項目不會存在這樣簡單的用例)
test_測試模塊1.py
1 # 測試函數 2 3 def test_2(): 4 assert 1 == 1 5 6 7 # 普通函數 8 def func_2(): 9 print('普通函數') 10 11 # 測試類 12 class TestClass_2(object): 13 14 # 測試函數 15 def test_class_3(self): 16 assert 1 == 1 17 # 普通函數 18 def func_class_3(self): 19 assert 1 == 1 20 21 # 普通類 22 class NoTestClass_2(object): 23 # 測試函數 24 def test_class_4(self): 25 assert 1 == 1 26 27 # 普通函數 28 def func_class_4(self): 29 assert 1 == 1
test_測試模塊2.py
1 # 測試函數 2 3 def test_1(): 4 5 assert 1==1 6 # 普通函數 7 def func_1(): 8 print('普通函數') 9 10 # 測試類 11 class TestClass_1(object): 12 # 測試函數 13 def test_class_1(self): 14 assert 1==1 15 16 # 普通函數 17 def func_class_1(self): 18 assert 1==1 19 # 普通類 20 class NoTestClass_1(object): 21 22 # 測試函數 23 def test_class_2(self): 24 assert 1 == 1 25 26 # 普通函數 27 def func_class_2(self): 28 assert 1 == 1
測試用例.py
1 # 測試函數 2 def test_one(): 3 assert 1==1 4 5 # 普通函數 6 def func(): 7 assert 1==1
代碼分析
我們現在根據理論分析並結合代碼,可以大致計算出,從項目根目錄執行用例,應該會執行4條有效測試用例!
我們在項目根目錄下執行pytest --collect-only看下情況,可以發現 搜索了test_測試模塊1和test_測試模塊2文件,並包括TestClass_2和TestClass_1類及內部test_class_3和test_class_1 和外部的測試函數test_2,test_1.
D:\pytest搜索測試用例規則>pytest --collect-only
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1
rootdir: D:\pytest搜索測試用例規則, inifile:
collected 4 items
<Package 'D:\\pytest搜索測試用例規則\\測試用例目錄1'>
<Module 'test_測試模塊1.py'>
<Function 'test_2'>
<Class 'TestClass_2'>
<Instance '()'>
<Function 'test_class_3'>
<Module 'test_測試模塊2.py'>
<Function 'test_1'>
<Class 'TestClass_1'>
<Instance '()'>
<Function 'test_class_1'>
======================== no tests ran in 0.14 seconds =========================
我們大致已經能看出pytest的搜索規則,現在們再執行一下所有用例看下 使用命令 pytest -v。可以看出確實只有4個用例被執行了,也就是只識別到了4個用例,根據下面的輸出信息可以看出每個用例所在的位置
D:\pytest搜索測試用例規則>pytest -v
============================= test session starts =============================
platform win32 -- Python 3.6.4, pytest-3.8.0, py-1.6.0, pluggy-0.7.1 -- c:\python36\python.exe
cachedir: .pytest_cache
rootdir: D:\pytest搜索測試用例規則, inifile:
collected 4 items
測試用例目錄1/test_測試模塊1.py::test_2 PASSED [ 25%]
測試用例目錄1/test_測試模塊1.py::TestClass_2::test_class_3 PASSED [ 50%]
測試用例目錄1/test_測試模塊2.py::test_1 PASSED [ 75%]
測試用例目錄1/test_測試模塊2.py::TestClass_1::test_class_1 PASSED [100%]
========================== 4 passed in 0.07 seconds ===========================
大家可以自己試試修改文件,函數,類名稱,然后執行用例,查看是否會按照自己的想法執行用例!
總結
ok,通過這兩篇文章,我們大概已經知道如何命名測試模塊,測試類,測試函數,並且如何使用簡單的命令行執行測試用例。那么就趕緊動手試試吧!