Pytest - 使用介绍
1. 概述
pytest是一個(gè)非常成熟的全功能的Python測試框架,主要特點(diǎn)有以下幾點(diǎn):
- 1、簡單靈活,容易上手,文檔豐富;
- 2、支持參數(shù)化,可以細(xì)粒度地控制要測試的測試用例;
- 3、能夠支持簡單的單元測試和復(fù)雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests);
- 4、pytest具有很多第三方插件,并且可以自定義擴(kuò)展,比較好用的如pytest-selenium(集成selenium)、pytest-html(完美html測試報(bào)告生成)、pytest-rerunfailures(失敗case重復(fù)執(zhí)行)、pytest-xdist(多CPU分發(fā))等;
- 5、測試用例的skip和xfail處理;
- 6、可以很好的和CI工具結(jié)合,例如jenkins
2. 使用介紹
2.1. 安裝
pip install pytest
2.2. 示例代碼
編寫規(guī)則
編寫pytest測試樣例非常簡單,只需要按照下面的規(guī)則:
- 測試文件以test_開頭(以_test結(jié)尾也可以)
- 測試類以Test開頭,并且不能帶有 init 方法
- 測試函數(shù)以test_開頭
- 斷言使用基本的assert即可
pytest1.py
# -*- coding:utf-8 -*-
import pytest@pytest.fixture(scope='function')
def setup_function(request): def teardown_function(): print("teardown_function called.") request.addfinalizer(teardown_function) # 此內(nèi)嵌函數(shù)做teardown工作 print('setup_function called.') @pytest.fixture(scope='module') def setup_module(request): def teardown_module(): print("teardown_module called.") request.addfinalizer(teardown_module) print('setup_module called.') @pytest.mark.website def test_1(setup_function): print('Test_1 called.') def test_2(setup_module): print('Test_2 called.') def test_3(setup_module): print('Test_3 called.') assert 2==1+1 # 通過assert斷言確認(rèn)測試結(jié)果是否符合預(yù)期 fixture的scope參數(shù)
scope參數(shù)有四種,分別是'function','module','class','session',默認(rèn)為function。
- function:每個(gè)test都運(yùn)行,默認(rèn)是function的scope
- class:每個(gè)class的所有test只運(yùn)行一次
- module:每個(gè)module的所有test只運(yùn)行一次
- session:每個(gè)session只運(yùn)行一次
setup和teardown操作
- setup,在測試函數(shù)或類之前執(zhí)行,完成準(zhǔn)備工作,例如數(shù)據(jù)庫鏈接、測試數(shù)據(jù)、打開文件等
- teardown,在測試函數(shù)或類之后執(zhí)行,完成收尾工作,例如斷開數(shù)據(jù)庫鏈接、回收內(nèi)存資源等
- 備注:也可以通過在fixture函數(shù)中通過yield實(shí)現(xiàn)setup和teardown功能
2.3. 測試結(jié)果
如何執(zhí)行
- pytest # run all tests below current dir
- pytest test_mod.py # run tests in module file test_mod.py
- pytest somepath # run all tests below somepath like ./tests/
- pytest -k stringexpr # only run tests with names that match the
# the "string expression", e.g. "MyClass and not method"
# will select TestMyClass.test_something
# but not TestMyClass.test_method_simple - pytest test_mod.py::test_func # only run tests that match the "node ID",
# e.g "test_mod.py::test_func" will be selected
# only run test_func in test_mod.py
通過pytest.mark對test方法分類執(zhí)行
通過@pytest.mark控制需要執(zhí)行哪些feature的test,例如在執(zhí)行test前增加修飾@pytest.mark.website
- 通過 -m "website" 執(zhí)行有website標(biāo)記的test方法
$ pytest -v -m "website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python cachedir: .cache Using --randomly-seed=1522925202 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py::test_1 PASSED ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. =============================================================================== 2 tests deselected =============================================================================== =========================================================== 1 passed, 2 deselected, 1 pytest-warnings in 0.00 seconds ============================================================ - 通過 -m "not website" 執(zhí)行沒有website標(biāo)記的test方法
$ pytest -v -m "not website" pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python cachedir: .cache Using --randomly-seed=1522925192 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py::test_3 PASSED pytest1.py::test_2 PASSED ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. =============================================================================== 1 tests deselected =============================================================================== =========================================================== 2 passed, 1 deselected, 1 pytest-warnings in 0.00 seconds ============================================================ Console參數(shù)介紹
- -v 用于顯示每個(gè)測試函數(shù)的執(zhí)行結(jié)果
- -q 只顯示整體測試結(jié)果
- -s 用于顯示測試函數(shù)中print()函數(shù)輸出
- -x, --exitfirst, exit instantly on first error or failed test
- -h 幫助
Case 1
$ pytest -v pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 -- /home/kevin/soft/anaconda2/bin/python cachedir: .cache Using --randomly-seed=1522920341 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py::test_1 PASSED pytest1.py::test_3 PASSED pytest1.py::test_2 PASSED ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. ================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds =================================================================== Case 2
$ pytest -s pytest1.py
============================================================================== test session starts ===============================================================================
platform linux2 -- Python 2.7.14, pytest-3.0.0, py-1.5.2, pluggy-0.3.1 Using --randomly-seed=1522920508 rootdir: /home/kevin/learn/python-web/tox/case2, inifile: plugins: randomly-1.0.0, mock-1.2, cov-2.0.0 collected 3 items pytest1.py setup_function called. Test_1 called. .teardown_function called. setup_module called. Test_2 called. .Test_3 called. .teardown_module called. ============================================================================= pytest-warning summary ============================================================================= WC1 None pytest_funcarg__cov: declaring fixtures using "pytest_funcarg__" prefix is deprecated and scheduled to be removed in pytest 4.0. Please remove the prefix and use the @pytest.fixture decorator instead. ================================================================== 3 passed, 1 pytest-warnings in 0.01 seconds =================================================================== 3. 擴(kuò)展插件
3.1. 測試報(bào)告
安裝與樣例
pip install pytest-cov # 計(jì)算pytest覆蓋率,支持輸出多種格式的測試報(bào)告
pytest --cov-report=html --cov=./ test_code_target_dir
Console參數(shù)介紹
- --cov=[path], measure coverage for filesystem path (multi-allowed), 指定被測試對象,用于計(jì)算測試覆蓋率
- --cov-report=type, type of report to generate: term, term-missing, annotate, html, xml (multi-allowed), 測試報(bào)告的類型
- --cov-config=path, config file for coverage, default: .coveragerc, coverage配置文件
- --no-cov-on-fail, do not report coverage if test run fails, default: False,如果測試失敗,不生成測試報(bào)告
- --cov-fail-under=MIN, Fail if the total coverage is less than MIN. 如果測試覆蓋率低于MIN,則認(rèn)為失敗
Console Result
---------------------------------------------------------------- coverage: platform linux2, python 2.7.14-final-0 ---------------------------------------------------------------- Name Stmts Miss Cover -------------------------------- pytest1.py 18 0 100% Html Result
image.png3.2. 測試順序隨機(jī)
pip install pytest-randomly
3.3. 分布式測試
pip install pytest-xdist
3.4. 出錯(cuò)立即返回
pip install pytest-instafail
4. 參考
-
python的測試工具大全
https://wiki.python.org/moin/PythonTestingToolsTaxonomy -
python主流的測試工具橫向比較
http://docs.python-guide.org/en/latest/writing/tests/
http://pythontesting.net/test-podcast/ -
python單元測試框架pytest簡介
https://blog.csdn.net/liuchunming033/article/details/46501653
作者:紅薯愛帥
鏈接:https://www.jianshu.com/p/a754e3d47671
來源:簡書
簡書著作權(quán)歸作者所有,任何形式的轉(zhuǎn)載都請聯(lián)系作者獲得授權(quán)并注明出處。
總結(jié)
以上是生活随笔為你收集整理的Pytest - 使用介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pip 将 某包指定到某目录 批量安装
- 下一篇: 我爱自然语言处理bert ner chi