网站建设一般多少钱方案,去除wordpress主题版权,怎么给自己网站做搜索框,wordpress主题404coverage代码覆盖率测试介绍
背景知识补充
1、什么是覆盖率
测试过程中提到的覆盖率#xff0c;指的是已测试的内容#xff0c;占待测内容的百分比#xff0c;在一定程度上反应测试的完整程度。
覆盖率有可以根据要衡量的对象细分很多种#xff0c;比如接口覆盖率、分支…coverage代码覆盖率测试介绍
背景知识补充
1、什么是覆盖率
测试过程中提到的覆盖率指的是已测试的内容占待测内容的百分比在一定程度上反应测试的完整程度。
覆盖率有可以根据要衡量的对象细分很多种比如接口覆盖率、分支覆盖率、行覆盖率等等
2、做覆盖率有什么用处
覆盖率的好处是可以将测试的完整性量化可以作为补充测试的手段也可以在一定程度上佐证测试结果的可靠性。 简介
代码覆盖率测量通常用于衡量测试的有效性。它可以显示你的代码的哪些部分正在被测试执行哪些没有被执行。
coverage是一个测量 Python 程序代码覆盖率的工具。它监视你的程序并分析源码生成代码覆盖率报告。 安装
coverage依赖python环境
python环境安装https://computingforgeeks.com/install-latest-python-on-centos-linux/
安装coverage
python3 -m pip install coverage
or
pip3 install coverage安装完成
coverage --version
Coverage.py, version 7.2.5 with C extension
Full documentation is at https://coverage.readthedocs.io/en/7.2.5命令概览
coverage支持命令如下
[yhgaolocalhost ~]$ coverage --help
Coverage.py, version 7.2.5 with C extension
Measure, collect, and report on code coverage in Python programs.usage: coverage command [options] [args]Commands:annotate Annotate source files with execution information.combine Combine a number of data files.debug Display information about the internals of coverage.pyerase Erase previously collected coverage data.help Get help on using coverage.py.html Create an HTML report.json Create a JSON report of coverage results.lcov Create an LCOV report of coverage results.report Report coverage stats on modules.run Run a Python program and measure code execution.xml Create an XML report of coverage results.Use coverage help command for detailed help on any command.
Full documentation is at https://coverage.readthedocs.io/en/7.2.5其中每个小命令后面可以查看更详细的帮助 以run命令举例
[yhgaolocalhost ~]$ coverage run --help
Usage: coverage run [options] pyfile [program options]Run a Python program, measuring code execution.Options:-a, --append Append coverage data to .coverage, otherwise it startsclean each time.--branch Measure branch coverage in addition to statementcoverage.--concurrencyLIBS Properly measure code using a concurrency library.Valid values are: eventlet, gevent, greenlet,multiprocessing, thread, or a comma-list of them.--contextLABEL The context label to record for this coverage run.--data-fileOUTFILE Write the recorded coverage data to this file.Defaults to .coverage. [env: COVERAGE_FILE]--includePAT1,PAT2,...Include only files whose paths match one of thesepatterns. Accepts shell-style wildcards, which must bequoted.-m, --module pyfile is an importable Python module, not a scriptpath, to be run as python -m would run it.--omitPAT1,PAT2,... Omit files whose paths match one of these patterns.Accepts shell-style wildcards, which must be quoted.-L, --pylib Measure coverage even inside the Python installedlibrary, which isnt done by default.-p, --parallel-mode Append the machine name, process id and random numberto the data file name to simplify collecting data frommany processes.--sourceSRC1,SRC2,...A list of directories or importable names of code tomeasure.--timid Use a simpler but slower trace method. Try this if youget seemingly impossible results!--debugOPTS Debug options, separated by commas. [env:COVERAGE_DEBUG]-h, --help Get help on this command.--rcfileRCFILE Specify configuration file. By default .coveragerc,setup.cfg, tox.ini, and pyproject.toml aretried. [env: COVERAGE_RCFILE]Full documentation is at https://coverage.readthedocs.io/en/7.2.5快速使用
如果你的测试运行程序命令以“python”开头只需将初始的“python”替换为“coverage run”即可。
python something.py -- becomes -- coverage run something.pypython -m amodule -- becomes -- coverage run -m amodulepython其他单元测试框架
pytest
pytest arg1 arg2 arg3 -- becomes -- coverage run -m pytest arg1 arg2 arg3unittest
python -m unittest discover -- becomes -- coverage run -m unittest discovernosetest
nosetests arg1 arg2 -- becomes -- coverage run -m nose arg1 arg2测试完成后会在当前目录下默认生成一个.coverage文件这里面保存了代码覆盖率结果也可以通过–data-file生成指定名称的文件
直接查看
coverage report
$ coverage report -m
Name Stmts Miss Cover Missing
-------------------------------------------------------
my_program.py 20 4 80% 33-35, 39
my_other_module.py 56 6 89% 17-23
-------------------------------------------------------
TOTAL 76 10 87%其他查看方式
使用其他格式查看例如html会生成html文件使用浏览器打开index.html即可查看
coverage html[yhgaolocalhost coverage]$ coverage html
Wrote HTML report to htmlcov/index.html
[yhgaolocalhost coverage]$ ll
总用量 4
-rw-rw-r-- 1 yhgao yhgao 679 6月 15 16:49 demo.py
drwxrwxr-x 2 yhgao yhgao 190 6月 15 16:51 htmlcov[yhgaolocalhost coverage]$ cd htmlcov/
[yhgaolocalhost htmlcov]$ ls -al
总用量 92
drwxrwxr-x 2 yhgao yhgao 190 6月 15 16:51 .
drwxrwxr-x 3 yhgao yhgao 53 6月 15 16:51 ..
-rw-rw-r-- 1 yhgao yhgao 21359 6月 15 16:51 coverage_html.js
-rw-rw-r-- 1 yhgao yhgao 11790 6月 15 16:51 demo_py.html
-rw-rw-r-- 1 yhgao yhgao 1732 6月 15 16:51 favicon_32.png
-rw-rw-r-- 1 yhgao yhgao 27 6月 15 16:51 .gitignore
-rw-rw-r-- 1 yhgao yhgao 3816 6月 15 16:51 index.html //浏览器打开该文件
-rw-rw-r-- 1 yhgao yhgao 9004 6月 15 16:51 keybd_closed.png
-rw-rw-r-- 1 yhgao yhgao 9003 6月 15 16:51 keybd_open.png
-rw-rw-r-- 1 yhgao yhgao 236 6月 15 16:51 status.json
-rw-rw-r-- 1 yhgao yhgao 12387 6月 15 16:51 style.cssDemo
下面以一个demo.py举例
测试目标%100
测试case数量个3
//查看demo文件
[yhgaobogon coverage]$ cat demo.py
import sysnum int(sys.argv[1])
if num 1:print(output num: 1)
else:if num 2:print(output num: 2)elif (num 2 or num 1):print(error input num)//python执行结果
[yhgaobogon coverage]$ python demo.py 1
output num: 1
[yhgaobogon coverage]$ python demo.py 2
output num: 2
[yhgaobogon coverage]$ python demo.py 3
error input num//coverage执行代码覆盖率区分单个case情况
[yhgaobogon coverage]$ coverage run --data-filec1 demo.py 1
output num: 1
[yhgaobogon coverage]$ coverage run --data-filec2 demo.py 2
output num: 2
[yhgaobogon coverage]$ coverage run --data-filec3 demo.py 3
error input num//查看每个代码覆盖率文件
[yhgaobogon coverage]$ coverage report --data-filec1
Name Stmts Miss Cover
-----------------------------
demo.py 8 4 50%
-----------------------------
TOTAL 8 4 50%
[yhgaobogon coverage]$ coverage report --data-filec2
Name Stmts Miss Cover
-----------------------------
demo.py 8 3 62%
-----------------------------
TOTAL 8 3 62%
[yhgaobogon coverage]$ coverage report --data-filec3
Name Stmts Miss Cover
-----------------------------
demo.py 8 2 75%
-----------------------------
TOTAL 8 2 75%//合并代码覆盖率文件
[yhgaobogon coverage]$ coverage combine --data-filecc c1 c2 c3
Combined data file c1
Combined data file c2
Combined data file c3//查看合并后的代码覆盖率文件
[yhgaobogon coverage]$ coverage report --data-filecc
Name Stmts Miss Cover
-----------------------------
demo.py 8 0 100%
-----------------------------
TOTAL 8 0 100%可以看到整合之后的代码覆盖率达到100%我们也就可以据此判断我们的3个测试case是符合要求的。//coverage执行代码覆盖率不区分单个case情况
未指定-a的情况
[yhgaobogon coverage]$ coverage run --data-filecc2 demo.py 1
output num: 1
[yhgaobogon coverage]$ coverage run --data-filecc2 demo.py 2
output num: 2
[yhgaobogon coverage]$ coverage run --data-filecc2 demo.py 3
error input num
[yhgaobogon coverage]$ coverage report --data-filecc2
Name Stmts Miss Cover
-----------------------------
demo.py 8 2 75%
-----------------------------
TOTAL 8 2 75%代码覆盖率是会被覆盖的所以只保留最后一个case的代码覆盖率指定-a的情况
[yhgaobogon coverage]$ coverage run -a --data-filecc2 demo.py 1
output num: 1
[yhgaobogon coverage]$ coverage run -a --data-filecc2 demo.py 2
output num: 2
[yhgaobogon coverage]$ coverage run -a --data-filecc2 demo.py 3
error input num
[yhgaobogon coverage]$ coverage report --data-filecc2
Name Stmts Miss Cover
-----------------------------
demo.py 8 0 100%
-----------------------------
TOTAL 8 0 100%指定-a和同一个data-file即可不用执行coverage combine直接得到一份儿完整的代码覆盖率文件
文章转载自: http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.rrpsw.cn.gov.cn.rrpsw.cn http://www.morning.qhmhz.cn.gov.cn.qhmhz.cn http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.przc.cn.gov.cn.przc.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.xnbd.cn.gov.cn.xnbd.cn http://www.morning.sjwws.cn.gov.cn.sjwws.cn http://www.morning.npbkx.cn.gov.cn.npbkx.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.fhddr.cn.gov.cn.fhddr.cn http://www.morning.mfmrg.cn.gov.cn.mfmrg.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.qcztm.cn.gov.cn.qcztm.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.smrkf.cn.gov.cn.smrkf.cn http://www.morning.qrpx.cn.gov.cn.qrpx.cn http://www.morning.tpwrm.cn.gov.cn.tpwrm.cn http://www.morning.lktjj.cn.gov.cn.lktjj.cn http://www.morning.zcncb.cn.gov.cn.zcncb.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.rykw.cn.gov.cn.rykw.cn http://www.morning.jfjqs.cn.gov.cn.jfjqs.cn http://www.morning.nyqm.cn.gov.cn.nyqm.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.byshd.cn.gov.cn.byshd.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.jcfg.cn.gov.cn.jcfg.cn http://www.morning.srnhk.cn.gov.cn.srnhk.cn http://www.morning.xwbwm.cn.gov.cn.xwbwm.cn http://www.morning.hrgxk.cn.gov.cn.hrgxk.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.xxrwp.cn.gov.cn.xxrwp.cn http://www.morning.fmkjx.cn.gov.cn.fmkjx.cn http://www.morning.wjplr.cn.gov.cn.wjplr.cn http://www.morning.znrgq.cn.gov.cn.znrgq.cn http://www.morning.mnbcj.cn.gov.cn.mnbcj.cn http://www.morning.stxg.cn.gov.cn.stxg.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.lxfyn.cn.gov.cn.lxfyn.cn http://www.morning.tkxyx.cn.gov.cn.tkxyx.cn http://www.morning.yjprj.cn.gov.cn.yjprj.cn http://www.morning.clxpp.cn.gov.cn.clxpp.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.ebpz.cn.gov.cn.ebpz.cn http://www.morning.fmqng.cn.gov.cn.fmqng.cn http://www.morning.ypzsk.cn.gov.cn.ypzsk.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.hqsnt.cn.gov.cn.hqsnt.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.nbsbn.cn.gov.cn.nbsbn.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.ngqdp.cn.gov.cn.ngqdp.cn http://www.morning.kjmcq.cn.gov.cn.kjmcq.cn http://www.morning.ngcsh.cn.gov.cn.ngcsh.cn http://www.morning.jqlx.cn.gov.cn.jqlx.cn http://www.morning.fbnsx.cn.gov.cn.fbnsx.cn http://www.morning.tnwwl.cn.gov.cn.tnwwl.cn http://www.morning.mrqwy.cn.gov.cn.mrqwy.cn http://www.morning.sjjtz.cn.gov.cn.sjjtz.cn http://www.morning.tkyxl.cn.gov.cn.tkyxl.cn http://www.morning.mhybs.cn.gov.cn.mhybs.cn http://www.morning.cbnlg.cn.gov.cn.cbnlg.cn http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.mmplj.cn.gov.cn.mmplj.cn http://www.morning.rdzgm.cn.gov.cn.rdzgm.cn http://www.morning.gfrjs.cn.gov.cn.gfrjs.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.mwcqz.cn.gov.cn.mwcqz.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.nzmw.cn.gov.cn.nzmw.cn http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.gqtxz.cn.gov.cn.gqtxz.cn http://www.morning.jxcwn.cn.gov.cn.jxcwn.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.gqnll.cn.gov.cn.gqnll.cn http://www.morning.rtkz.cn.gov.cn.rtkz.cn http://www.morning.kvzvoew.cn.gov.cn.kvzvoew.cn