【问题标题】:Python click sample code test case does not give 100% coverage [duplicate]Python单击示例代码测试用例未提供100%的覆盖率[重复]
【发布时间】:2022-01-05 08:57:18
【问题描述】:

我写了以下代码。

https://gitlab.com/ksaito11/click-test

$ cat commands/cmd.py 
import click

from commands.hello import hello

def print_version(ctx, param, value):
    if not value or ctx.resilient_parsing:
        return
    click.echo('Version 1.0')
    ctx.exit()

@click.group()
@click.option('--opt1')
@click.option('--version', is_flag=True, callback=print_version,
              expose_value=False, is_eager=True)
@click.pass_context
def cmd(ctx, **kwargs):
    ctx.obj = kwargs

def main():
    cmd.add_command(hello)
    cmd(auto_envvar_prefix='HELLOCLI')

if __name__ == '__main__':
    main()
$ cat commands/hello.py 
import click

@click.command()
def hello():
    click.echo('Hello World!')

代码运行正常。

$ export PYTHONPATH=.
$ python commands/cmd.py 
Usage: cmd.py [OPTIONS] COMMAND [ARGS]...

Options:
  --opt1 TEXT
  --version
  --help       Show this message and exit.

Commands:
  hello
$ python commands/cmd.py --version
Version 1.0
$ python commands/cmd.py hello
Hello World!

我写了下面的测试用例。

$ cat tests/test_cmd.py 
from click.testing import CliRunner
import click
import pytest

from commands.cmd import cmd, main
from commands.hello import hello

def test_version():
    runner = CliRunner()
    result = runner.invoke(cmd, ["--version"])
    assert result.exit_code == 0

def test_help():
    runner = CliRunner()
    result = runner.invoke(cmd)
    assert result.exit_code == 0

def test_hello():
    runner = CliRunner()
    result = runner.invoke(hello)
    assert result.exit_code == 0

我使用以下命令测量了覆盖率。

$ pytest --cov-branch --cov=commands
================================================================ test session starts ================================================================
platform linux -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /home/ksaito/ghq/gitlab.com/ksaito11/click-test
plugins: cov-3.0.0
collected 3 items                                                                                                                                   

tests/test_cmd.py ...                                                                                                                         [100%]

----------- coverage: platform linux, python 3.9.9-final-0 -----------
Name                   Stmts   Miss Branch BrPart  Cover
--------------------------------------------------------
commands/__init__.py       0      0      0      0   100%
commands/cmd.py           18      5      4      2    68%
commands/hello.py          4      0      0      0   100%
--------------------------------------------------------
TOTAL                     22      5      4      2    73%


================================================================= 3 passed in 0.15s =================================================================

我不知道如何编写代码来测试下面的部分并且无法获得 100% 的覆盖率。

def cmd(ctx, **kwargs):
    ctx.obj = kwargs

def main():
    cmd.add_command(hello)
    cmd(auto_envvar_prefix='HELLOCLI')

使用“@click.group”时可能不需要下面的代码,但我无法确定。

def print_version(ctx, param, value):
    if not value or ctx.resilient_parsing:
        return

请给我建议。

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    通过添加以下设置,将不需要包含在覆盖范围内的代码排除在外。

    $ cat .coveragerc 
    [run]
    branch = True
    
    [report]
    exclude_lines =
        # Don't complain if non-runnable code isn't run:
        if 0:
        if __name__ == .__main__.:
        def main
        ctx.obj = kwargs
    

    我认为没有必要删除了下面的代码。

    if not value or ctx.resilient_parsing:
        return
    

    现在覆盖率为 100%。

    $ pytest --cov-branch --cov=commands
    ================================================================ test session starts ================================================================
    platform linux -- Python 3.9.9, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
    rootdir: /home/ksaito/ghq/gitlab.com/ksaito11/click-test
    plugins: cov-3.0.0
    collected 3 items                                                                                                                                   
    
    tests/test_cmd.py ...                                                                                                                         [100%]
    
    ----------- coverage: platform linux, python 3.9.9-final-0 -----------
    Name                   Stmts   Miss Branch BrPart  Cover
    --------------------------------------------------------
    commands/__init__.py       0      0      0      0   100%
    commands/cmd.py           10      0      0      0   100%
    commands/hello.py          4      0      0      0   100%
    --------------------------------------------------------
    TOTAL                     14      0      0      0   100%
    
    
    ================================================================= 3 passed in 0.22s =================================================================
    

    【讨论】:

      猜你喜欢
      • 2023-03-21
      • 2012-01-18
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      相关资源
      最近更新 更多