如何获得识别退出错误的覆盖率?

2024-04-16 08:22:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标是在我正在编写的单元测试中达到100%的覆盖率(Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32

我正在测试的代码有如下形式的退出消息:

class _ExitStatus(enum.Enum):
    """The exit statuses."""
    success = _ExitStatusPair(
        0, "success")

我的单元测试代码正确地触发了_exit_abnormally函数:

def _exit_abnormally(name, message):
    """Exit the program with the provided status and message."""
    try:
        print(message, file=sys.stderr)
    finally:
        sys.exit(getattr(_ExitStatus, name).value.status)

正确返回错误代码18。但是coverage没有注意到_exit_abnormally被调用

class TestThis(unittest.TestCase):
  def test_(self):
    with self.subTest(args=args):
        args = (
        'python',
        _EXECUTABLE,
        'foo',
        'bar'
        )
        subprocess.run([x for x in args], check = True)
        try:
             print(subprocess.check_output([x for x in args]))
        except: subprocess.CalledProcessError as e:
             print(" stdout output:\n", e.output)
             print(" stdout output:\n", e.returncode                
             self.assertEqual(e.returncode,18)

断言返回True,但是在运行coverage报告时,_exit_abnormally函数仍然标记为缺少一行。你知道吗

如何让coverage重新确认_exit_abnormally被调用?你知道吗


Tags: 函数代码selfmessageoutputexitcoverageargs