Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass a ctx (Context) to CliRunner?

CliRunner lists no parameter to provide a context in its documentation.

The following should qualify as a minimum working example. The real problem is a bit different. It could be solved by moving the click decorated function into its own function for test coverage. Then the click function would be rendered almost useless.

import click
from 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 do I pass ctx to runner.invoke?
    result = runner.invoke(print_numberinfo)
    assert result.output == str(ctx.obj.value) + '\n'
like image 917
hyllos Avatar asked Nov 05 '16 21:11

hyllos


1 Answers

You would directly pass your Config instance as keyword argument obj to runner.invoke:

import click
from 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 do I pass ctx to runner.invoke?
    result = runner.invoke(print_numberinfo, obj=obj)
    assert result.output == str(obj.value) + '\n'
like image 123
Markus Unterwaditzer Avatar answered Sep 24 '22 02:09

Markus Unterwaditzer