Python click sample code test case does not give 100% coverage

Viewed 112

I wrote the following code.

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!')

The code works correctly.

$ 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!

I wrote the following test case.

$ 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

I measured the coverage with the following command.

$ 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 =================================================================

I didn't know how to write the code to test the part below and couldn't get 100% coverage.

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

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

The code below may not be needed when using "@ click.group", but I couldn't determine.

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

Please give me advice.

1 Answers

By adding the following settings, code that does not need to be included in coverage is excluded.

$ 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

I deleted the code below because I thought it was unnecessary.

if not value or ctx.resilient_parsing:
    return

The coverage is now 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 =================================================================
Related