Skip to main content
 首页 » 编程设计

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

2025年05月04日8leader

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

我知道我们有命令

coverage html --include=myproject/*.* 

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

请您参考如下方法:

我有一个基本完整的实现。在此阶段,它与 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('..')