pytest測驗框架也是主流的一個測驗框架,推薦使用該框架,對比unnitest框架來說,其效率更高
1.Pytest介紹
pytest測驗框架也是主流的一個測驗框架,推薦使用該框架,對比unnitest框架來說,其不需要像unittest那樣單獨創建類繼承unittest.TestCase,它使用起來非常簡單,只需要創建測驗類或者測驗檔案,然后以test開頭即可,pytest可以按照這個規則找到測驗用例并執行,
pytest測驗框架,主要有以下幾個特點:
- 簡單靈活,容易上手,檔案豐富;
- 支持用簡單的assert陳述句實作豐富的斷言,無需復雜的self.assert*函式
- 支持引數化;
- 兼容unittest和 nose測驗集
- 能夠支持簡單的單元測驗和復雜的功能測驗,還可應用到介面自動化測驗(pytest+requests)中;
- 豐富的插件生態,具有很多第三方插件,并且可以自定義擴展,如pytest-Selenium(集成Selenium)等;
- 支持重復執行失敗的case等;
- 方便和持續集成工具jenkins集成;
- 可以很好的和jenkins集成,
2.安裝Pytest
pytest不是Python默認的package,需要手動安裝,其同時可以在windows、unix系統上安裝,最新的pytest版本為5.4.1,pytest5.4.1支持Python 3.5、3.6、3.7版本,本書采取(書籍資料包中)的Python版本為Python3.7,因此可以與pytest結合使用,
打開windows系統的cmd命令視窗,在命令列中運行以下命令:
pip install -U pytest
注:
通過cmd安裝pytest,可能下載程序較長或安裝程序報錯中斷,多嘗試幾次,
安裝進度,如圖所示,

圖 pytest安裝
:::
如仍然安裝不成功,可以指定國內鏡像,如國內的清華大學開源軟體鏡像站,地址是:https://mirrors.tuna.tsinghua.edu.cn/,命令如下:
pip install --index https://mirrors.ustc.edu.cn/pypi/web/simple/ pytest
安裝進度,如下所示,
C:\Windows\system32>pip install --index https://mirrors.ustc.edu.cn/pypi/web/simple/ pytest
Looking in indexes: https://mirrors.ustc.edu.cn/pypi/web/simple/
Collecting pytest
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/c7/e2/c19c667f42f72716a7d03e8dd4d6f63f47d39feadd44cc1ee7ca3089862c/pytest-5.4.1-py3-none-any.whl (246kB)
|████████████████████████████████| 256kB 1.7MB/s
Collecting pluggy<1.0,>=0.12 (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/a0/28/85c7aa31b80d150b772fbe4a229487bc6644da9ccb7e427dd8cc60cb8a62/pluggy-0.13.1-py2.py3-none-any.whl
Collecting colorama; sys_platform == "win32" (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/c9/dc/45cdef1b4d119eb96316b3117e6d5708a08029992b2fee2c143c7a0a5cc5/colorama-0.4.3-py2.py3-none-any.whl
Collecting py>=1.5.0 (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/99/8d/21e1767c009211a62a8e3067280bfce76e89c9f876180308515942304d2d/py-1.8.1-py2.py3-none-any.whl (83kB)
|████████████████████████████████| 92kB 2.0MB/s
Collecting more-itertools>=4.0.0 (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/72/96/4297306cc270eef1e3461da034a3bebe7c84eff052326b130824e98fc3fb/more_itertools-8.2.0-py3-none-any.whl (43kB)
|████████████████████████████████| 51kB 3.4MB/s
Collecting packaging (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/62/0a/34641d2bf5c917c96db0ded85ae4da25b6cd922d6b794648d4e7e07c88e5/packaging-20.3-py2.py3-none-any.whl
Collecting attrs>=17.4.0 (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/a2/db/4313ab3be961f7a763066401fb77f7748373b6094076ae2bda2806988af6/attrs-19.3.0-py2.py3-none-any.whl
Collecting atomicwrites>=1.0; sys_platform == "win32" (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/52/90/6155aa926f43f2b2a22b01be7241be3bfd1ceaf7d0b3267213e8127d41f4/atomicwrites-1.3.0-py2.py3-none-any.whl
Collecting importlib-metadata>=0.12; Python_version < "3.8" (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/8b/03/a00d504808808912751e64ccf414be53c29cad620e3de2421135fcae3025/importlib_metadata-1.5.0-py2.py3-none-any.whl
Collecting wcwidth (from pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/58/b4/4850a0ccc6f567cc0ebe7060d20ffd4258b8210efadc259da62dc6ed9c65/wcwidth-0.1.8-py2.py3-none-any.whl
Collecting six (from packaging->pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Collecting pyparsing>=2.0.2 (from packaging->pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/5d/bc/1e58593167fade7b544bfe9502a26dc860940a79ab306e651e7f13be68c2/pyparsing-2.4.6-py2.py3-none-any.whl (67kB)
|████████████████████████████████| 71kB 4.8MB/s
Collecting zipp>=0.5 (from importlib-metadata>=0.12; Python_version < "3.8"->pytest)
Downloading https://mirrors.tuna.tsinghua.edu.cn/pypi/web/packages/b2/34/bfcb43cc0ba81f527bc4f40ef41ba2ff4080e047acb0586b56b3d017ace4/zipp-3.1.0-py3-none-any.whl
Installing collected packages: zipp, importlib-metadata, pluggy, colorama, py, more-itertools, six, pyparsing, packaging, attrs, atomicwrites, wcwidth, pytest
Successfully installed atomicwrites-1.3.0 attrs-19.3.0 colorama-0.4.3 importlib-metadata-1.5.0 more-itertools-8.2.0 packaging-20.3 pluggy-0.13.1 py-1.8.1 pyparsing-2.4.6 pytest-5.4.1 six-1.14.0 wcwidth-0.1.8 zipp-3.1.0
WARNING: You are using pip version 19.2.3, however version 20.0.2 is available.
You should consider upgrading via the 'Python -m pip install --upgrade pip' command.
檢查您是否安裝了正確的版本
pytest --version,
C:\Windows\system32>pytest --version
This is pytest version 5.4.1, imported from d:\program files\python37\lib\site-packages\pytest\__init__.py
Pytest幫助命令
pytest --help或pytest -h,
E:\pytestDemo>pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
general:
-k EXPRESSION only run tests which match the given substring expression. An expression is a Python evaluatable
expression where all names are substring-matched against test names and their parent classes.
Example: -k 'test_method or test_other' matches all test functions and classes whose name
contains 'test_method' or 'test_other', while -k 'not test_method' matches those that don't
contain 'test_method' in their names. -k 'not test_method and not test_other' will eliminate the
matches. Additionally keywords are matched to classes and functions containing extra names in
their 'extra_keyword_matches' set, as well as functions which have names assigned directly to
them. The matching is case-insensitive.
-m MARKEXPR only run tests matching given mark expression. example: -m 'mark1 and not mark2'.
--markers show markers (builtin, plugin and per-project ones).
-x, --exitfirst exit instantly on first error or failed test.
--maxfail=num exit after first num failures or errors.
3.Pytest案例實戰
3.1. pytest基礎案例
通過PyCharm在工程目錄下,創建pytestDemo的Python package,Pytest的案例均存放在pytestDemo下,
pytest測驗用例撰寫非常簡單,pytest可以在不同的函式、包中發現用例,但pytest有如下約束,
- 檔案名以test_(如:test_.py)開頭或以_test(如: _test.py)結尾的py檔案;
- 以test_開頭的函式或test_開頭的方法;
- 以Test開頭的類,并且不能帶有 init 方法;
- 要注意的是所有的包必須要有init.py檔案(在PyCharm中會自動生成),
3.1.1 創建基礎待測方法
在pytestDemo下新建test_demo.py檔案,代碼如下:
def add(a,b):
return a + b
def test_add():
assert add(2,3) == 5
3.1.2 運行測驗方法
通過cmd進入到test_demo.py所在的檔案夾下,然后運行pytest,如下所示,
E:\>cd pytestDemo
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 1 item
test_demo.py . [100%]
================================ 1 passed in 0.02s =============
將test_demo.py將代碼進行變動,將實際值與期望值改成不一致,代碼如下:
def add(a,b):
return a + b
def test_add():
assert add(2,3) == 6
cmd進入到test_demo.py所在的檔案夾下,然后運行pytest,如下所示,
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 1 item
test_demo.py F [100%]
======================= FAILURES =========================
_____________________ test_add __________________________
def test_add():
> assert add(2,3) == 6
E assert 5 == 6
E + where 5 = add(2, 3)
test_demo.py:5: AssertionError
======================== short test summary info ==========================
FAILED test_demo.py::test_add - assert 5 == 6
==================== 1 failed in 0.06s ============================
3.2. 測驗用例存放在類中
可將測驗用例放在測驗類中,通過執行測驗類,而執行類中的測驗用例,
3.2.1 優化測驗代碼
將test_demo.py將代碼進行優化,代碼如下:
def add(a, b):
return a + b
def minus(a, b):
return a - b
class TestClass:
def test_add(self):
assert add(2, 3) == 5
def test_minus(self):
assert minus(3, 2) == 2
3.2.2 運行優化后的測驗方法
cmd進入到test_demo.py所在的檔案夾下,然后運行pytest,結果如下所示,
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 2 items
test_demo.py .F [100%]
=================== FAILURES =========================
___________________ TestClass.test_minus _______________________________
self = <pytestDemo.test_demo.TestClass object at 0x0000023E3C369FC8>
def test_minus(self):
> assert minus(3, 2) == 2
E assert 1 == 2
E + where 1 = minus(3, 2)
test_demo.py:27: AssertionError
===================== short test summary info ===========
FAILED test_demo.py::TestClass::test_minus - assert 1 == 2
=============== 1 failed, 1 passed in 0.06s ==============================
【測驗測驗課程】8折銷售中【6月18日--6月24日】....
《全堆疊測驗系列視頻》課程






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

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/493342.html
標籤:其他
