【问题标题】:Write unit test for checking code coverage in python编写单元测试以检查python中的代码覆盖率
【发布时间】:2020-03-23 04:04:45
【问题描述】:

我需要在 python 中使用 unittest 编写测试,当覆盖率低于 50% 时会失败。 如:

class ExampleTest(unittest.TestCase):

def setUp(self):
    cov = coverage.Coverage()
    cov.load()
    with open(os.devnull, "w") as f:
        self.total = cov.report(file=f)

def test_compare_values(self):
    self.assertGreaterEqual(self.total, 20)

但是当测试运行时,文件覆盖率被锁定并且无法打开。

如何解决这个问题?

【问题讨论】:

    标签: python python-unittest coverage.py


    【解决方案1】:

    不要尝试从测试中读取覆盖率数据。而是使用覆盖率报告命令上的--fail-under=50 选项。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-14
      • 2012-01-18
      • 2018-09-01
      • 1970-01-01
      相关资源
      最近更新 更多