前言
pytest測驗框架也是主流的一個測驗框架,推薦使用該框架,對比unnitest框架來說,其效率更高,前面的文章講解了,如何使用pytest,這篇文章一起來看下如何針對pytest的測驗用例進行控制,
本篇文章是《Selenium3自動化測驗【38】單元測驗Pytest》的后續,建議連續閱讀,效果更佳,
1. 測驗用例的運行控制
Pytest執行用例的方式,不單單是Pytest這一種方式,Pytest提供了3種運行方式執行測驗用例,
- pytest(一般采用該種方式);
- pytest test.py或pytest test.py;
- python –m pytest,
在pytestDemo目錄下,新創建一個Demo_test.py檔案,代碼如下:
# 乘法,回傳a*b的值
def multi(a,b):
return a*b
# 除法,回傳a/b的值
def divide(a,b):
return a/b
class TestClass:
def test_multi(self):
assert multi(3, 3) == 6
def test_divide(self):
assert divide(3, 2) == 4
1.1 方式1:pytest
格式為:【pytest 檔案名/】或進入pytestDemo目錄下,運行pytest,執行某個目錄下所有的用例,結果如下,可觀察到Demo_test.py與test_demo.py兩個測驗檔案均被運行,
E:\pytestDemo>pytest
=================== test session starts =================================
platform win32 -- Python 3.7.5, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\pytestDemo
collected 4 items
Demo_test.py FF [ 50%]
test_demo.py .. [100%]
======================= FAILURES ================================
_______________________ TestClass.test_multi _________________________
self = <pytestDemo.Demo_test.TestClass object at 0x00000190540EAA48>
def test_multi(self):
> assert multi(3, 3) == 6
E assert 9 == 6
E + where 9 = multi(3, 3)
Demo_test.py:25: AssertionError
_________________ TestClass.test_divide _________________________
self = <pytestDemo.Demo_test.TestClass object at 0x00000190540DF208>
def test_divide(self):
> assert divide(3, 2) == 4
E assert 1.5 == 4
E + where 1.5 = divide(3, 2)
Demo_test.py:28: AssertionError
==================== short test summary info ==========================
FAILED Demo_test.py::TestClass::test_multi - assert 9 == 6
FAILED Demo_test.py::TestClass::test_divide - assert 1.5 == 4
=================== 2 failed, 2 passed in 0.07s =======================
1.2 方式2:pytest test_*.py
執行某一個py檔案下用例【pytest 腳本名稱.py】,操作步驟及運行結果如下,可觀察到僅僅運行了Demo_test.py檔案,而test_demo.py并未被運行,
E:\pytestDemo>pytest Demo_test.py
========= test session starts ==================================
platform win32 -- Python 3.7.5, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\pytestDemo
collected 2 items
Demo_test.py FF [100%]
========================= FAILURES ================================
_____ TestClass.test_multi _________________________
self = <pytestDemo.Demo_test.TestClass object at 0x000001B64767ED48>
def test_multi(self):
> assert multi(3, 3) == 6
E assert 9 == 6
E + where 9 = multi(3, 3)
Demo_test.py:25: AssertionError
________________________TestClass.test_divide ____________________________
self = <pytestDemo.Demo_test.TestClass object at 0x000001B64767EF48>
def test_divide(self):
> assert divide(3, 2) == 4
E assert 1.5 == 4
E + where 1.5 = divide(3, 2)
Demo_test.py:28: AssertionError
===================== short test summary info =========================
FAILED Demo_test.py::TestClass::test_multi - assert 9 == 6
FAILED Demo_test.py::TestClass::test_divide - assert 1.5 == 4
================ 2 failed in 0.10s ===================
1.3 方式3:python -m pytest
通過python -m pytest運行當前目錄下的所有測驗用例檔案,
E:\pytestDemo>python -m pytest
================test session starts ====================================
platform win32 -- Python 3.7.5, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\pytestDemo
collected 4 items
Demo_test.py FF [ 50%]
test_demo.py .. [100%]
=====================FAILURES ==========================
__________ TestClass.test_multi ____________________________________
self = <pytestDemo.Demo_test.TestClass object at 0x0000019554B8AE48>
def test_multi(self):
> assert multi(3, 3) == 6
E assert 9 == 6
E + where 9 = multi(3, 3)
Demo_test.py:25: AssertionError
__________________TestClass.test_divide ____________________
self = <pytestDemo.Demo_test.TestClass object at 0x0000019554B4A708>
def test_divide(self):
> assert divide(3, 2) == 4
E assert 1.5 == 4
E + where 1.5 = divide(3, 2)
Demo_test.py:28: AssertionError
===================== short test summary info ====================
FAILED Demo_test.py::TestClass::test_multi - assert 9 == 6
FAILED Demo_test.py::TestClass::test_divide - assert 1.5 == 4
=================== 2 failed, 2 passed in 0.14s ====================
1.4 節點運行
通過節點運行方式可運行某個.py檔案(模塊)里的某個函式或某個方法,如僅僅運行Demo_test.py檔案中的test_multi方法,而Demo_test.py中的test_divide不要運行,
通過“pytest Demo_test.py::TestClass::test_multi”運行后,在結果中可觀察到僅僅運行了test_multi方法,
E:\pytestDemo>pytest Demo_test.py::TestClass::test_multi
=================== test session starts ===================
platform win32 -- Python 3.7.5, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\pytestDemo
collected 1 item
Demo_test.py F [100%]
======================== FAILURES ======================
_____________________TestClass.test_multi _________________________
self = <pytestDemo.Demo_test.TestClass object at 0x0000015DDF3E64C8>
def test_multi(self):
> assert multi(3, 3) == 6
E assert 9 == 6
E + where 9 = multi(3, 3)
Demo_test.py:25: AssertionError
=================== short test summary info =============================
FAILED Demo_test.py::TestClass::test_multi - assert 9 == 6
==================== 1 failed in 0.05s ===================
1.5 遇到錯誤停止測驗
Demo_test.py檔案中的test_multi與 test_divide兩個方法運行斷言,均會執行失敗,通過pytest -x Demo_test.py執行程序中,當遇到第一個方法或函式執行無法通過,則將停止運行,后面的方法或函式將不被執行,如下所示:
E:\pytestDemo>pytest -x Demo_test.py
====================== test session starts ====================
platform win32 -- Python 3.7.5, pytest-5.4.1, py-1.8.1, pluggy-0.13.1
rootdir: E:\pytestDemo
collected 2 items
Demo_test.py F
=================================== FAILURES =====================
_______________ TestClass.test_multi _____________________________
self = <pytestDemo.Demo_test.TestClass object at 0x00000208030AFD48>
def test_multi(self):
> assert multi(3, 3) == 6
E assert 9 == 6
E + where 9 = multi(3, 3)
Demo_test.py:25: AssertionError
============================== short test summary info ==============
FAILED Demo_test.py::TestClass::test_multi - assert 9 == 6
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
============================= 1 failed in 0.06s =========================
pytest測驗用例撰寫非常簡單,pytest可以在不同的函式、包中發現用例,但pytest有如下約束,
2. 編譯器中配置pytest
Python默認自帶的單元測驗框架是unittest,因此在PyCharm編譯器中默認的單元測驗框架一般是unittest,如果想修改當前工程的單元測驗框架,可通過修改PyCharm默認的【test runner】來指定為單元測驗框架pytest,
修改路徑file->Setting->Tools->python Integrated Tools>Default test runner-> pytest,如圖所示,

圖 PyTest的設定
【測驗測驗課程】....
《全堆疊測驗系列視頻》課程






圖書京東、當當有售
京東:https://item.jd.com/12784287.html
當當:http://product.dangdang.com/29177828.html)!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/500114.html
標籤:其他
下一篇:程式猿學習抖音短視頻制作
