一个好用的python测试框架(pytest)及其CI设置

###背景
我们有部分正式代码是采用python编写,这部分代码也是需要TDD的,因此,需要找到一个好用的测试框架。

###框架选择
python的测试工具大全:
https://wiki.python.org/moin/PythonTestingToolsTaxonomy

python主流的测试工具横向比较
http://docs.python-guide.org/en/latest/writing/tests/
http://pythontesting.net/test-podcast/

根据下面这篇文章,我们决定使用pytest
http://mathieu.agopian.info/presentations/2015_06_djangocon_europe/?full#cover

pytest的官方资料
https://docs.pytest.org/en/latest/

###CI的集成
普通的pytest安装是没有包含覆盖率的

pip install pytest

pytest覆盖率需要额外安装

pip install pytest-cov

这时,新增了py.test命令,这是原来pytest命令基础上封装的,新增了覆盖率功能。

例子示范:

py.test test_conf2bin.py --junit-xml=test_conf2bin_report.xml --cov-report=html --cov-config=.coveragerc --cov=./
  • test_conf2bin.py是目标测试文件
  • –junit-xml=test_conf2bin_report.xml是生成junit格式的test_conf2bin_report.xml
  • –cov-report=html 输出html的报告
  • –cov-config=.coveragerc 使用.coveragerc配置文件(py.test是基于coverage.py实现的,因此支持coverage.py的配置文件)
  • –cov=./ 这个是必要的,如果没有这个,将无法输出html的报告

.coveragerc这个文件可以让py.test实现更多高级的功能,实现举例如下:

[run]
branch = True

[report]
include = conf2bin.py

[html]
directory = python_coverage_report

[report]
include = conf2bin.py
由于py.test会计算一个文件夹里的所有文件,如果只需要统计conf2bin.py这个文件的覆盖率,则可以这么写
[html]
directory = python_coverage_report
指的是html的输出文件夹

###最后
pytest个人试用,确实很好用,比自带的测试框架强多了

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值