Python:使用跳过测试进行覆盖

时间:2018-06-14 13:19:54

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

这是一个测试:

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

我的问题很简单:

如果我执行我的承保范围,my_function是否会被承保(考虑我的测试被跳过)?

谢谢!

1 个答案:

答案 0 :(得分: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() 中的第5行未执行:

coverage_demo

始终执行顶级$ 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 语句,但第5行是def函数中的唯一一行。