Pytest單元測試框架實戰之Pytest用例運行規則


1.Pytest測試用例運行規則


 在pytest單元測試框架下面執行用例,需要滿足以下幾個特點:

  1. 文件名以test_*.py開頭或者*_test.py

  2. 測試類、測試函數以test開頭

  3. 所有的包必須要有 __init__.py文件

一般在cmd命令行下面執行pytest用例有3種方法。大家可以選擇使用,我推薦第一種:

  pytest  文件名

  py.test  文件名

  python -m pytest 文件名

如果運行某個測試類下面的具體函數,可以使用:pytest  文件名::測試函數名

如果在測試過程中,遇到測試停止的方法可以加 -x參數: pytests -x 文件名

E:\untitled1>pytest -x collect.py ============================= test session starts ============================= platform win32 -- Python 3.5.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: E:\untitled1 collected 2 items collect.py .F [100%] ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x00000000036A4A58> def test_two(self): x = 'hello'
>       assert hasattr(x, 'check') E AssertionError: assert False E +  where False = hasattr('hello', 'check') collect.py:102: AssertionError ===================== 1 failed, 1 passed in 0.08 seconds ======================

從結果可以看出第二個測試用例沒有運行成功並且停止了。當錯誤數達到某個量級時,測試停止,參數為:maxfail=num 示例如下:

E:\untitled1>pytest --maxfail=1 collect.py ============================= test session starts ============================= platform win32 -- Python 3.5.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: E:\untitled1 collected 3 items collect.py .F ================================== FAILURES =================================== _____________________________ TestClass.test_two ______________________________ self = <collect.TestClass object at 0x000000000368FCF8> def test_two(self): x = 'hello'
>       assert hasattr(x, 'check') E AssertionError: assert False E +  where False = hasattr('hello', 'check') collect.py:102: AssertionError ===================== 1 failed, 1 passed in 0.08 seconds ======================

2.在Pycharm中編寫測試代碼


 在編寫測試代碼運行之前需要把切換pytest運行環境。file-->setting-->tools-->python intergrated tools--->default test runner 切換為 py.test  然后編寫如下測試代碼:

import pytest class TestClass: def test_one(self): x = 'hello' assert 'h' in x def test_two(self): x = 'hello' assert hasattr(x, 'check') def test_secound(self): assert 'x' in 'ddd'

if __name__ == '__main__': pytest.main('-q test_class.py')

運行結果如下   (其中:     .表示測試結果是通過的 pass  E:表示errror  腳本中可能存在問題  F表示failed 測試結果不通過)

C:\Python35\python.exe "C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\helpers\pycharm\pytestrunner.py" -p pytest_teamcity E:/untitled1/test_class.py "-k TestClass and test_secound" Testing started at 17:28 ... ============================= test session starts ============================= platform win32 -- Python 3.5.1, pytest-5.0.1, py-1.8.0, pluggy-0.12.0 rootdir: C:\Program Files (x86)\JetBrains\PyCharm 5.0.3\jre\jre\bin collected 3 items / 2 deselected / 1 selected . F self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self): >       assert 'x' in 'ddd' E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:106: AssertionError ================================== FAILURES =================================== ___________________________ TestClass.test_secound ____________________________ self = <test_class.TestClass object at 0x000000000365EB38> def test_secound(self): >       assert 'x' in 'ddd' E AssertionError: assert 'x' in 'ddd' E:\untitled1\test_class.py:106: AssertionError =================== 1 failed, 2 deselected in 0.06 seconds ====================


免責聲明!

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



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