Python 单元测试

2023年 10月 13日 52.2k 0

pytest是python的一种单元测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,效率更高。根据pytest的官方网站介绍,它具有如下特点:

  • 非常容易上手,入门简单,文档丰富,文档中有很多实例可以参考

  • 能够支持简单的单元测试和复杂的功能测试

  • 支持参数化

  • 执行测试过程中可以将某些测试跳过,或者对某些预期失败的case标记成失败

  • 支持重复执行失败的case

  • 支持运行由nose, unittest编写的测试case

  • 具有很多第三方插件,并且可以自定义扩展

  • 方便的和持续集成工具集成

安装 pytest 包

pip install pytest

安装 pytest-cov 包 (此插件生成覆盖率报告)

pip install pytest-cov

安装 pytest-html 包 (用于生成HTML报告的pytest插件)

pip install pytest-html

终端查看单元测试覆盖率

py.test --cov=test/

生成 HTML 测试覆盖率报告

pytest --html=report.html

在 test(测试文件) 目录下执行 pytest 查看效果

gyw@gyw:~/Desktop/code/project/xxxxxx/src/backend$ py.test --cov=test/
=========================== test session starts =====================================
platform linux -- Python 3.5.2, pytest-3.0.7, py-1.4.34, pluggy-0.4.0
metadata: {'Platform': 'Linux-4.10.0-37-generic-x86_64-with-Ubuntu-16.04-xenial', 'Packages': {'py': '1.4.34', 'pluggy': '0.4.0', 'pytest': '3.0.7'}, 'Python': '3.5.2', 'Plugins': {'metadata': '1.5.0', 'cov': '2.5.1', 'html': '1.16.0'}}
rootdir: /home/gyw/Desktop/code/project/xxxxxx/src/backend, inifile:
plugins: metadata-1.5.0, html-1.16.0, cov-2.5.1
collected 4 items 

test/test_exampl.py .
test/test_monitor.py .
test/test_run.py .
test/test_workers.py .

----------- coverage: platform linux, python 3.5.2-final-0 -----------
Name                               Stmts   Miss  Cover
------------------------------------------------------
test/__init__.py                       0      0   100%
test/interactive/__init__.py           1      0   100%
test/interactive/backend.py           11     11     0%
test/interactive/fib.py                3      1    67%
test/interactive/frontend.py          17     17     0%
test/interactive/monitor_test.py      10      5    50%
test/test_exampl.py                   38      3    92%
test/test_monitor.py                  62     10    84%
test/test_run.py                       4      0   100%
test/test_workers.py                  90     15    83%
------------------------------------------------------
TOTAL                                236     62    74%


=========================== 4 passed in 20.61 seconds =========================
gyw@gyw:~/Desktop/code/project/xxxxxx/src/backend$ pytest --html=report.html
=========================== test session starts ================================
platform linux -- Python 3.5.2, pytest-3.0.7, py-1.4.34, pluggy-0.4.0
metadata: {'Plugins': {'metadata': '1.5.0', 'cov': '2.5.1', 'html': '1.16.0'}, 'Python': '3.5.2', 'Packages': {'py': '1.4.34', 'pluggy': '0.4.0', 'pytest': '3.0.7'}, 'Platform': 'Linux-4.10.0-38-generic-x86_64-with-Ubuntu-16.04-xenial'}
rootdir: /home/gyw/Desktop/code/project/xxxxxx/src/backend, inifile:
plugins: metadata-1.5.0, html-1.16.0, cov-2.5.1
collected 4 items 

test/test_exampl.py .
test/test_monitor.py .
test/test_run.py .
test/test_workers.py .

------ generated html file: /home/gyw/Desktop/code/project/xxxxxx/src/backend/report.html ------
============================= 4 passed in 31.82 seconds =======================
参考资料

www.cnblogs.com/landhu/p/74…

www.cnblogs.com/landhu/p/74…

相关文章

服务器端口转发,带你了解服务器端口转发
服务器开放端口,服务器开放端口的步骤
产品推荐:7月受欢迎AI容器镜像来了,有Qwen系列大模型镜像
如何使用 WinGet 下载 Microsoft Store 应用
百度搜索:蓝易云 – 熟悉ubuntu apt-get命令详解
百度搜索:蓝易云 – 域名解析成功但ping不通解决方案

发布评论