俄罗斯网站建设,公司网站备案后在百度上多长时间可以搜索到,在哪找专业做淘宝网站,中文绿色环保网站模板前言我们在做测试的时候#xff0c;经常遇到领导的灵魂拷问#xff1a;你的测试用例覆盖率是多少#xff0c;达到100%了么#xff1f;你如何保证你的测试质量#xff1f;测试用例的覆盖率如何统计呢#xff0c;如何知道开发的代码#xff0c;我们都测到了#xff0c;不… 前言我们在做测试的时候经常遇到领导的灵魂拷问你的测试用例覆盖率是多少达到100%了么你如何保证你的测试质量测试用例的覆盖率如何统计呢如何知道开发的代码我们都测到了不会存在漏测的情况。pytest-cov先命令行安装 pytest-cov 2.10.1版本pip install pytest-cov2.10.1环境要求1.python3.6.6 版本备注其它版本没试过python3.6.0版本会遇到以下问题INTERNALERRORraise CoverageException(Couldnt use data file {!r}:{}.format(self.filename, msg))
INTERNALERROR coverage.misc.CoverageException: Couldnt use data fileC:\\Users\\Desktop\\Pytest\\.coverage:
Safety level may not be changed inside a transaction解决办法安装3.6.1以上版本实现功能在做单元测试时代码覆盖率常常被拿来作为衡量测试好坏的指标甚至用代码覆盖率来考核测试任务完成情况比如代码覆盖率必须达到80或 90。于是乎测试人员费尽心思设计案例覆盖代码。单元测试的方法有语句覆盖/判定覆盖/条件覆盖/路径覆盖先看一个简单的案例前端实现一个功能根据接口返回的不同code值判断支付的结果给用户返回提示友好的信息前端实现功能根据接口返回的不同code值判断支付的结果给用户返回提示友好的信息
接口返回格式{code: 0,msg: success!,data: []
}错误码参照
0 - 成功
30000 - 参数错误
30001 - 余额不足
30002 - 达到当天最大额度
201102 - 银行卡被冻结实现代码# pay.py# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
接口返回格式
{code: 0,msg: success!,data: []
}错误码参照
0 - 成功
30000 - 参数错误
30001 - 余额不足
30002 - 达到当天最大额度
201102 - 银行卡被冻结
def pay_status(result):根据接口返回code状态给用户提示对应的结果if result.get(code) 0:return 支付成功elif result.get(code) 30000:return 支付失败: %s % result.get(msg)elif result.get(code) 30001:return 支付失败: %s % result.get(msg)elif result.get(code) 30002:return 支付失败: %s % result.get(msg)elif result.get(code) 201102:return 支付失败: %s % result.get(msg)else:return 支付失败: 系统异常未知错误整个项目目录结构如下src 是项目的源码tests 是我们写的单元测试用例src和tests放同一个项目的根目录下用例设计在tests/test_pay.py下写测试用例,先只写一个支付成功的案例from src.pay import pay_status
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/def test_pay_success():result {code: 0,msg: success!,data: []}assert pay_status(result) 支付成功运行用例运行用例的时候加上 —cov 参数pytest —cov运行结果pytest --covtest session starts
platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\soft\pytest-demo-cov
plugins: change-report-1.0, cov-2.10.1, html-1.19.0, metadata-1.8.0
collected 1 itemtests\test_pay.py . [100%]----------- coverage: platform win32, python 3.6.6-final-0 -----------
Name Stmts Miss Cover
---------------------------------------
src\__init__.py 0 0 100%
src\pay.py 13 9 31%
tests\__init__.py 0 0 100%
tests\test_pay.py 4 0 100%
---------------------------------------
TOTAL 17 9 47% 1 passed in 0.10s 从报告可以看出src\pay.py 的代码测试覆盖率是31%其它文件都是100%覆盖这就说明我们单元测试代码测试覆盖率是31%还有一个指标是测试用例的执行率测试用例在test_pay.py文件执行率是100%说明用例全部执行了。coverage生成html报告coverage 相关参数查看使用pytest -h pytest -h
coverage reporting with distributed testing support:--cov[SOURCE] Path or package name to measure during execution (multi-allowed). Use --cov to not do anysource filtering and record everything.--cov-reportTYPE Type of report to generate: term, term-missing, annotate, html, xml (multi-allowed). term, term-missing may be followed by :skip-covered. annotate, html and xml may be followed by :DESTwhere DEST specifies the output location. Use --cov-report to not generate any output.--cov-configPATH Config file for coverage. Default: .coveragerc--no-cov-on-fail Do not report coverage if test run fails. Default: False--no-cov Disable coverage report completely (useful for debuggers). Default: False--cov-fail-underMIN Fail if the total coverage is less than MIN.--cov-append Do not delete coverage but append to current. Default: False--cov-branch Enable branch coverage.--cov-contextCONTEXTDynamic contexts to use. test for now.生成html的报告pytest —cov —cov-reporthtml执行完成在项目根目录会生成 htmlcov 目录运行 index.html 文件查看代码覆盖率点开src\pay.py想覆盖率达到100%那得再继续写用例让每个if分支情况都覆盖到指定被测代码如果我们想指定执行项目里面的某个模块可以通过—cov模块 来运行pytest —covsrcpytest --covsrctest session starts
platform win32 -- Python 3.6.6, pytest-6.0.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\soft\pytest-demo-cov
plugins: change-report-1.0, cov-2.10.1, html-1.19.0, metadata-1.8.0
collected 1 itemtests\test_pay.py . [100%]----------- coverage: platform win32, python 3.6.6-final-0 -----------
Name Stmts Miss Cover
-------------------------------------
src\__init__.py 0 0 100%
src\pay.py 13 9 31%
-------------------------------------
TOTAL 13 9 31% 1 passed in 0.07s 也可以指定具体的py模块名称pytest —covsrc.pay但不能写成pytest --covsrc/pay.py2020年第五期《python接口自动化测试开发》课程10月11号开学(火热报名中)本期上课时间10月11号-1月3号每周六、周日晚上20:30-22:30联系微信/QQ283340479