首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在单个html文件中获取django覆盖报告

如何在单个html文件中获取django覆盖报告
EN

Stack Overflow用户
提问于 2014-05-22 15:02:38
回答 1查看 3.4K关注 0票数 2

我正在运行我的单元测试用例,并希望在html文件中生成覆盖率报告。

我知道我们有一个命令

代码语言:javascript
复制
coverage html --include=myproject/*.*

它在我的项目目录中生成了一个'htmlcov‘文件夹,我们可以在这个文件夹中找到所有应用程序/文件覆盖范围的html格式的报告。但是我只想要一个html文件(或者你可以说index.html文件,它在htmlcov文件夹中),它给我的项目的覆盖范围。我不想生成所有的文件。如何使用覆盖率来做到这一点?或者,有没有其他好的方法可以让我在单个html文件中生成单元测试覆盖率报告?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-06-19 22:29:20

我对此有一个基本完整的实现。在这个阶段,它与coverage html输出联系得相当紧密。它是非破坏性的,尽管如果你碰巧有一个htmlcov/coverage.html文件,那么它会覆盖它。

代码语言:javascript
复制
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('..')
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23799914

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档