How can I pass a ctx (Context) to CliRunner?
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'