我正在运行我的单元测试用例,并希望在html文件中生成覆盖率报告。
我知道我们有一个命令
coverage html --include=myproject/*.*
它在我的项目目录中生成了一个'htmlcov‘文件夹,我们可以在这个文件夹中找到所有应用程序/文件覆盖范围的html格式的报告。但是我只想要一个html文件(或者你可以说index.html文件,它在htmlcov文件夹中),它给我的项目的覆盖范围。我不想生成所有的文件。如何使用覆盖率来做到这一点?或者,有没有其他好的方法可以让我在单个html文件中生成单元测试覆盖率报告?
发布于 2015-06-19 22:29:20
我对此有一个基本完整的实现。在这个阶段,它与coverage html
输出联系得相当紧密。它是非破坏性的,尽管如果你碰巧有一个htmlcov/coverage.html
文件,那么它会覆盖它。
import codecs
import os
from bs4 import BeautifulSoup
scripts = []
styles = []
bodies = []
processed = []
def explode(filename):
# We want to turn the body element into a div so we can use it
# in one page with all of the other body elements.
processed.append(filename)
soup = BeautifulSoup(codecs.open(filename, encoding='utf-8'))
body = soup.body
body.name = 'div'
body['id'] = filename
body['class'] = 'body'
bodies.append(body)
# Ensure that we grab all of the scripts that are required by
# this page.
for script in soup.find_all('script'):
if 'src' in script.attrs:
content = codecs.open(script['src'], encoding='utf-8').read()
else:
content = script.string
if content not in scripts:
scripts.append(content)
# Likewise for the link[stylesheet] elements.
for link in soup.find_all('link'):
content = codecs.open(link['href'], encoding='utf-8').read()
if content not in styles:
styles.append(content)
for a in soup.find_all('a'):
# Make sure all local-links are rewritten to point to a more
# fully-qualified name (these are line number links).
if a['href'][0] == '#':
anchor = u'{}-{}'.format(filename, a['href'][1:])
a['href'] = u'#{}'.format(anchor)
a.parent['id'] = anchor
elif a['href'] in processed:
# Pages we have already seen, we want to have a local link.
a['href'] = u'#{}'.format(a['href'])
else:
# Otherwise, we want to see if we have a file that matches
# the href, and embed that. Yay recursion.
try:
explode(a['href'])
except IOError:
pass
else:
a['href'] = u'#{}'.format(a['href'])
TEMPLATE = u'''<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
<title>Coverage report</title>
<style>{styles}</style>
<script>{scripts}</script>
</head>
<body>
{body}
</body>
</html>
'''
# Fire off the tree walking with htmlcov/index.html
os.chdir('htmlcov')
explode('index.html')
# Override the toggle_lines function to limit it to just our code block.
scripts.append('''
coverage.toggle_lines = function (btn, cls) {
btn = $(btn);
var hide = "hide_"+cls;
if (btn.hasClass(hide)) {
btn.closest('.body').find("#source ."+cls).removeClass(hide);
btn.removeClass(hide);
}
else {
$("#source ."+cls).addClass(hide);
btn.addClass(hide);
}
};
''')
result = TEMPLATE.format(
styles=u'\n'.join(styles),
scripts=u'\n'.join(scripts),
body=u'\n'.join([unicode(x) for x in bodies])
)
codecs.open('coverage.html', 'w', encoding='utf-8').write(result)
os.chdir('..')
https://stackoverflow.com/questions/23799914
复制相似问题