0
点赞
收藏
分享

微信扫一扫

Pytest单元测试系列[v1.0.0][coverage测试测试代码覆盖率]


coverage.py 判断测试覆盖了多少代码

测试覆盖率是指项目代码被测试用例覆盖的百分比,覆盖率工具可以告诉你,系统哪些部分完全没有被测试覆盖,​​coverage.py​​就是Python的一款覆盖率工具。

在使用​​coverage.py​​之前必须先安装​​pytest-cov​​插件,它允许你在pytest中使用​​coverage.py​​,并且提供了更多的pytest选项,而因为​​coverage​​是​​pytest-cov​​的一个依赖包,因此安装​​pytest-cov​​就够了。

(venv) E:\Programs\Python\Python_Pytest\TestScripts>pip install pytest-cov
Collecting pytest-cov
Using cached https://files.pythonhosted.org/packages/84/7b/73f8522619d1cbb22b9a36f9c54bc5ce5e24648e53cc1bf566477d2d1f2b/pytest_cov-2.7.1-py2.py3-none-any.whl
Collecting coverage>=4.4 (from pytest-cov)
Downloading https://files.pythonhosted.org/packages/38/eb/252f991555d408528ad927d0e8ab9e9f14c85daefa81bc73c9ad1e0ed706/coverage-4.5.4-cp37-cp37m-win_amd64.whl (183kB)
100% |████████████████████████████████| 184kB 13kB/s
Requirement already satisfied: pytest>=3.6 in c:\python37\lib\site-packages (from pytest-cov) (4.5.0)
Requirement already satisfied: colorama; sys_platform == "win32" in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (0.4.1)
Requirement already satisfied: pluggy!=0.10,<1.0,>=0.9 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (0.11.0)
Requirement already satisfied: wcwidth in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (0.1.7)
Requirement already satisfied: attrs>=17.4.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (19.1.0)
Requirement already satisfied: setuptools in c:\users\davieyang\appdata\roaming\python\python37\site-packages (from pytest>=3.6->pytest-cov) (41.0.1)
Requirement already satisfied: more-itertools>=4.0.0; python_version > "2.7" in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (7.0.0)
Requirement already satisfied: py>=1.5.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (1.8.0)
Requirement already satisfied: atomicwrites>=1.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (1.3.0)
Requirement already satisfied: six>=1.10.0 in c:\python37\lib\site-packages (from pytest>=3.6->pytest-cov) (1.12.0)
Installing collected packages: coverage, pytest-cov
Successfully installed coverage-4.5.4 pytest-cov-2.7.1

假设项目有如下结构:

Pytest单元测试系列[v1.0.0][coverage测试测试代码覆盖率]_测试代码覆盖率

执行命令看测试覆盖率

(venv) E:\Programs\Python\Python_Pytest\SourceCode\tasks_proj>pytest --cov=src
======================================== test session starts =====================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest, inifile: pytest.ini
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, nice-0.1.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, cov-2.7.1, allure-pytest-2.6.3
collected 1 item

tests\func\test_add.py . [100%]C
overage.py warning: No data was collected. (no-data-collected)


----------- coverage: platform win32, python 3.7.3-final-0 -----------
Name Stmts Miss Cover
--------------------------------------------------
src\tasks\__init__.py 2 2 0%
src\tasks\api.py 77 77 0%
src\tasks\cli.py 44 44 0%
src\tasks\config.py 18 18 0%
src\tasks\tasksdb_pymongo.py 56 56 0%
src\tasks\tasksdb_tinydb.py 32 32 0%
--------------------------------------------------
TOTAL 229 229 0%
================================ 1 passed in 0.32 seconds ========================================

还可以生成html格式的覆盖率报告,执行命令:

(venv) E:\Programs\Python\Python_Pytest\SourceCode\tasks_proj>pytest --cov=src --cov-report=html
================================= test session starts ===============================================
platform win32 -- Python 3.7.3, pytest-4.5.0, py-1.8.0, pluggy-0.11.0
rootdir: E:\Programs\Python\Python_Pytest, inifile: pytest.ini
plugins: xdist-1.29.0, timeout-1.3.3, repeat-0.8.0, nice-0.1.0, instafail-0.4.1, forked-1.0.2, emoji-0.2.0, cov-2.7.1, allure-pytest-2.6.3
collected 1 item

tests\func\test_add.py . [100%]C
overage.py warning: No data was collected. (no-data-collected)


----------- coverage: platform win32, python 3.7.3-final-0 -----------
Coverage HTML written to dir htmlcov


============================= 1 passed in 0.31 seconds ============================================

会在src被测目录同级别生成一个htmlcov的报告路径,用浏览器打开其中的​​index.html​​,如下样式:

Pytest单元测试系列[v1.0.0][coverage测试测试代码覆盖率]_Pytest_02

点击其中的Module名字可以查看具体覆盖情况,如下图:

Pytest单元测试系列[v1.0.0][coverage测试测试代码覆盖率]_测试代码覆盖率_03

coverage.py和pytest-cov还有很多可用的选项,可以通过​​coverage​​和​​pytest-cov​​查看更多的用法。



举报

相关推荐

0 条评论