瓶测试 - 为什么覆盖率排除导入语句和装饰器? [英] Flask Testing - why does coverage exclude import statements and decorators?

查看:181
本文介绍了瓶测试 - 为什么覆盖率排除导入语句和装饰器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的测试清楚地执行每个功能,也没有未使用的进口。然而,根据覆盖率报告,62%的代码从未在以下文件中执行过:


有人可以指出我可能做错了什么?



以下是我如何初始化测试套件和覆盖范围:

pre $ co $ = coverage(branch = True ,omit = ['website / *','run_test_suite.py'])
cov.start()

try:
unittest.main(argv = [sys.argv [0]])
除外:
通过

cov.stop()
cov.save()

print\ n \ nCoverage Report:\\\

cov.report()

printHTML version:+ os.path.join(BASEDIR,tmp / coverage / index.html )
cov.html_report(directory ='tmp / coverage')
cov.erase()


解决方案

这是 coverage.py FAQ


Q :为什么函数(或类)的主体显示为执行,但是
的def行不行?

这是因为覆盖是在函数之后开始的是
定义。定义行在没有覆盖
测量的情况下执行,然后开始覆盖,然后调用该函数。
这意味着身体被测量,但是函数
本身的定义不是。

为了解决这个问题,请尽早开始覆盖。如果您使用命令行
来运行覆盖范围的程序,那么您的整个程序将被
监视。如果您正在使用API​​,则需要在导入定义函数的模块之前调用coverage.start()



<
$ b

  $ coverage运行-m unittest发现
<运行测试覆盖范围内的最简单的事情: / code>

您的自定义测试脚本没有超出覆盖命令行的范围,只是使用命令行。


My tests clearly execute each function, and there are no unused imports either. Yet, according to the coverage report, 62% of the code was never executed in the following file:

Can someone please point out what I might be doing wrong?

Here's how I initialise the test suite and the coverage:

    cov = coverage(branch=True, omit=['website/*', 'run_test_suite.py'])
    cov.start()

    try:
        unittest.main(argv=[sys.argv[0]])
    except:
        pass

    cov.stop()
    cov.save()

    print "\n\nCoverage Report:\n"
    cov.report()

    print "HTML version: " + os.path.join(BASEDIR, "tmp/coverage/index.html")
    cov.html_report(directory='tmp/coverage')
    cov.erase()

解决方案

This is the third question in the coverage.py FAQ:

Q: Why do the bodies of functions (or classes) show as executed, but the def lines do not?

This happens because coverage is started after the functions are defined. The definition lines are executed without coverage measurement, then coverage is started, then the function is called. This means the body is measured, but the definition of the function itself is not.

To fix this, start coverage earlier. If you use the command line to run your program with coverage, then your entire program will be monitored. If you are using the API, you need to call coverage.start() before importing the modules that define your functions.

The simplest thing to do is run you tests under coverage:

$ coverage run -m unittest discover

Your custom test script isn't doing much beyond what the coverage command line would do, it will be simpler just to use the command line.

这篇关于瓶测试 - 为什么覆盖率排除导入语句和装饰器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆