吉林省吉林市簡介武漢seo搜索引擎優(yōu)化
pytest常用Console參數(shù):
- -v 用于顯示每個測試函數(shù)的執(zhí)行結(jié)果
- -q 只顯示整體測試結(jié)果
- -s 用于顯示測試函數(shù)中print()函數(shù)輸出
- -x 在第一個錯誤或失敗的測試中立即退出
- -m 只運行帶有裝飾器配置的測試用例
- -k 通過表達(dá)式運行指定的測試用例
- -h 幫助
首先來看什么參數(shù)都沒加的運行情況
class TestClass():def test_zne(self):print(1)assert 1==2def test_two(self):print(2)assert 1==2def test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main()============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 itemstest_page.py FF. [100%]================================== FAILURES ===================================
-v 用于顯示每個測試函數(shù)的執(zhí)行結(jié)果
用于打印顯示每條用例的執(zhí)行情況
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2def test_two(self):print(2)assert 1==2def test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-v'])============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0 -- D:\Users\72036454\AppData\Local\Programs\Python\Python38\python.exe
cachedir: .pytest_cache
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collecting ... collected 3 itemstest_page.py::TestClass::test_zne FAILED [ 33%]
test_page.py::TestClass::test_two FAILED [ 66%]
test_page.py::TestClass::test_a PASSED [100%]================================== FAILURES ===================================
-q 只顯示整體測試結(jié)果
簡化測試整體結(jié)果。F:代表測試失敗、.:代表測試通過
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2def test_two(self):print(2)assert 1==2def test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-q'])FF. [100%]
================================== FAILURES ===================================
-s 用于顯示測試函數(shù)中print()函數(shù)輸出
顯示測試用例中 print() 中的值
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2def test_two(self):print(2)assert 1==2def test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-s'])============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 itemstest_page.py 1
F2
F3
.================================== FAILURES ===================================
-x 在第一個錯誤或失敗的測試中立即退出
第一條用例執(zhí)行失敗,立即退出不在往下執(zhí)行用例
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2def test_two(self):print(2)assert 1==2def test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-x','-s'])============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 itemstest_page.py 1
F================================== FAILURES ===================================
-m 只運行帶有裝飾器配置的測試用例
用例中,第二和第三條用例加上了裝飾器,裝飾器最后一個單詞分別為“slow” 和 “faster” ,-m 拿著兩個單詞去識別帶這個裝飾器的用例,識別到就執(zhí)行,沒有識別到的就不執(zhí)行。
-m后面接的是表達(dá)式:['-s','-m slow or faster'] 、['-s','-m slow and faster']、['-s','-m not slow'] 這些表達(dá)式都支持。
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2@pytest.mark.slowdef test_two(self):print(2)assert 1==2@pytest.mark.fasterdef test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-s','-m slow or faster'])============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items / 1 deselected / 2 selectedtest_page.py 2
F3
.================================== FAILURES ===================================
-k 通過表達(dá)式運行指定的測試用例
通過表達(dá)式匹配用例的函數(shù)名去執(zhí)行用例,not test_zne 意思是不執(zhí)行“test_zne”這條用例,所以就會執(zhí)行第二第三條。同理 ['-s','-k test_zne'] 表示只執(zhí)行第一條。
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2@pytest.mark.slowdef test_two(self):print(2)assert 1==2@pytest.mark.fasterdef test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-s','-k not test_zne'])============================= test session starts =============================
platform win32 -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
rootdir: D:\Users\72036454\Desktop\pythonProject\Base
plugins: allure-pytest-2.9.45
collected 3 items / 1 deselected / 2 selectedtest_page.py 2
F3
.================================== FAILURES ===================================
-h 幫助
這才是重點,學(xué)會使用這個,剩余的都學(xué)會了
import pytest
class TestClass():def test_zne(self):print(1)assert 1==2@pytest.mark.slowdef test_two(self):print(2)assert 1==2@pytest.mark.fasterdef test_a(self):print(3)assert 1==1if __name__ == '__main__':pytest.main(['-h'])
pytest的執(zhí)行順序:
- 默認(rèn)情況下,pytest的執(zhí)行順序是自上往下的。
- 可以通過第三方插件
pytest-ordering
實現(xiàn)自定義用例執(zhí)行順序 - 官方文檔:?https://pytest-ordering.readthedocs.io/en/develop/
安裝插件:
pip install pytest-ordering
pytest-ordering使用:
方式一
- 第一個執(zhí)行:
@pytest.mark.first
- 第二個執(zhí)行:
@pytest.mark.second
- 倒數(shù)第二個執(zhí)行:
@pytest.mark.second_to_last
- 最后一個執(zhí)行:
@pytest.mark.last
方式二
- 第一個執(zhí)行:
@pytest.mark.run('first')
- 第二個執(zhí)行:
@pytest.mark.run('second')
- 倒數(shù)第二個執(zhí)行:
@pytest.mark.run('second_to_last')
- 最后一個執(zhí)行:
@pytest.mark.run('last')
方式三
- 第一個執(zhí)行:
@pytest.mark.run(order=1)
- 第二個執(zhí)行:
@pytest.mark.run(order=2)
- 倒數(shù)第二個執(zhí)行:
@pytest.mark.run(order=-2)
- 最后一個執(zhí)行:
@pytest.mark.run(order=-1)
對于以上三張方法,經(jīng)常使用的不多,第一個執(zhí)行和最后一個執(zhí)行比較常用。
如果對你有幫助的話,點個贊收個藏,給作者一個鼓勵。也方便你下次能夠快速查找。? ? ??