Skip to content
Snippets Groups Projects
Commit 7802ee67 authored by Yannick DAYER's avatar Yannick DAYER
Browse files

Add a click command result handler for tests.

parent 74771fea
No related branches found
No related tags found
1 merge request!44Add click result utility
import sys
import click
from click.testing import CliRunner
from bob.io.base import test_utils
@click.command("dummy")
def dummy_command_0():
sys.exit(0)
@click.command("dummy_exit_1")
def dummy_command_1():
sys.exit(1)
@click.command("dummy_exit_raise")
def dummy_command_raise():
raise RuntimeError("Expected exception")
def test_assert_dummy():
result = CliRunner().invoke(dummy_command_0)
assert result.exit_code == 0
test_utils.assert_click_runner_result(result)
result = CliRunner().invoke(dummy_command_1)
assert result.exit_code == 1
test_utils.assert_click_runner_result(result, exit_code=1)
result = CliRunner().invoke(dummy_command_raise)
assert result.exit_code == 1
test_utils.assert_click_runner_result(
result, exit_code=1, exception_type=RuntimeError
)
...@@ -10,8 +10,13 @@ ...@@ -10,8 +10,13 @@
import functools import functools
import os import os
import traceback
import unittest import unittest
from typing import Optional
import click.testing
def datafile(f, module=None, path="data"): def datafile(f, module=None, path="data"):
"""datafile(f, [module], [data]) -> filename """datafile(f, [module], [data]) -> filename
...@@ -117,3 +122,38 @@ def extension_available(extension): ...@@ -117,3 +122,38 @@ def extension_available(extension):
return wrapper return wrapper
return test_wrapper return test_wrapper
def assert_click_runner_result(
result: click.testing.Result,
exit_code: int = 0,
exception_type: Optional[Exception] = None,
):
"""Helper for asserting click runner results.
Parameters
----------
result
The return value on ``click.testing.CLIRunner.invoke()``.
exit_code
The expected command exit code (defaults to 0).
exception_type
If given, will ensure that the raised exception is of that type.
"""
m = (
"Click command exited with code '{}', instead of '{}'.\n"
"Exception:\n{}\n"
"Output:\n{}"
)
exception = (
"None"
if result.exc_info is None
else "".join(traceback.format_exception(*result.exc_info))
)
m = m.format(result.exit_code, exit_code, exception, result.output)
assert result.exit_code == exit_code, m
if exit_code == 0:
assert not result.exception, m
if exception_type is not None:
assert isinstance(result.exception, exception_type), m
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment