先介绍一下Python的单元测试常用框架
下面重点介绍pytest
pytest是一个非常成熟的全功能的Python测试框架,主要特点有以下几点:
pip install pytest
编写pytest测试样例非常简单,只需要按照下面的规则:
# -*- coding:utf-8 -*- import pytest @pytest.fixture(scope=‘function‘) def setup_function(request): def teardown_function(): print("teardown_function called.") request.addfinalizer(teardown_function) # 此内嵌函数做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.‘)
scope参数有四种,分别是‘function‘,‘module‘,‘class‘,‘session‘,默认为function。
通过@pytest.mark控制需要执行哪些feature的test,例如在执行test前增加修饰@pytest.mark.website
$ 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" 执行没有website标记的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 ============================================================
$ 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 ===================================================================
$ 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 ===================================================================
pip install pytest-cov # 计算pytest覆盖率,支持输出多种格式的测试报告
pytest --cov-report=html --cov=./ test_code_target_dir
---------------------------------------------------------------- coverage: platform linux2, python 2.7.14-final-0 ---------------------------------------------------------------- Name Stmts Miss Cover -------------------------------- pytest1.py 18 0 100%
pip install pytest-randomly
pip install pytest-xdist
pip install pytest-instafail
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.cnblogs.com/zhaoyingjie/p/12525883.html