【问题标题】:Python : is coverage using skipped testPython:是使用跳过测试的覆盖率
【发布时间】:2018-06-14 13:19:54
【问题描述】:

这是一个测试:

@skip("my test is skipped")
def test_coverage():
    my_function()

我的问题很简单:

如果我进行覆盖,my_function 是否会被覆盖(考虑到我的测试被跳过)?

谢谢!

【问题讨论】:

  • 试试看就知道了。

标签: python unit-testing testing code-coverage skip


【解决方案1】:

不执行跳过的测试。根据定义,未执行的代码不包括在内。

演示;给定一个coverage_demo 模块:

def foo():
    var = "This function has is covered"

def bar():
    var = "This function is not"

还有一个coverage_demo_tests.py 文件:

from unittest import TestCase, skip
import coverage_demo

class DemoTests(TestCase):
    def test_foo(self):
        coverage_demo.foo()

    @skip("No bar for us today")
    def test_bar(self):
        import coverage_demo
        coverage_demo.bar()

if __name__ == '__main__':
    import unittest
    unittest.main()

在覆盖范围内运行表明coverage_demo 中的第 5 行未执行:

$ coverage run coverage_demo_tests.py
s.
----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK (skipped=1)
$ coverage report --include=coverage_demo\.py -m
Name               Stmts   Miss  Cover   Missing
------------------------------------------------
coverage_demo.py       4      1    75%   5

顶层的 def 语句总是被执行,但第 5 行是 bar() 函数中的唯一行。

【讨论】:

  • 好的!我真的不知道覆盖范围是如何工作的!
  • @AntoineLB:项目文档的How coverage works section 对此进行了解释。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2018-06-11
  • 2015-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多