python - How can I pass a ctx (Context) to CliRunner? -
clirunner
lists no parameter provide context in documentation.
the following should qualify minimum working example. real problem bit different. solved moving click decorated function own function test coverage. click function rendered useless.
import click click.testing import clirunner class config(): def __init__(self): self.value = 651 @click.command() @click.pass_context def print_numberinfo(ctx): if not hasattr(ctx.obj, 'value'): ctx.obj = config() click.echo(ctx.obj.value) def test_print_numberinfo(): ctx = click.context(print_numberinfo, obj = config()) ctx.obj.value = 777 runner = clirunner() # how pass ctx runner.invoke? result = runner.invoke(print_numberinfo) assert result.output == str(ctx.obj.value) + '\n'
you directly pass config
instance keyword argument obj
runner.invoke
:
import click click.testing import clirunner class config(): def __init__(self): self.value = 651 @click.command() @click.pass_obj def print_numberinfo(obj): if not hasattr(obj, 'value'): obj = config() click.echo(obj.value) def test_print_numberinfo(): obj = config() obj.value = 777 runner = clirunner() # how pass ctx runner.invoke? result = runner.invoke(print_numberinfo, obj=obj) assert result.output == str(obj.value) + '\n'
Comments
Post a Comment