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

1.Pytest測試用例運行規則


 在pytest單元測試框架下面執行用例,須要知足如下幾個特色:python

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

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

  3. 全部的包必需要有 __init__.py文件函數

通常在cmd命令行下面執行pytest用例有3種方法。你們能夠選擇使用,我推薦第一種:單元測試

  pytest  文件名測試

  py.test  文件名spa

  python -m pytest 文件名命令行

若是運行某個測試類下面的具體函數,可使用:pytest  文件名::測試函數名code

若是在測試過程當中,遇到測試中止的方法能夠加 -x參數: pytests -x 文件名orm

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 ====================
相關文章
相關標籤/搜索