Skip to content

click.testing.CliRunner.invoke prevents use of pdb #138

@wallrj

Description

@wallrj

Here's another one (related to #136). I wanted to explore the contents of the context object passed to a command decorated with @click.pass_context.

But when I try dropping a pdb.set_trace into the invoked command, it immediately quits...but only when invoked via the CliRunner.

if I actually invoke the command from the command line, the debugger works as expected.

Maybe something to do with the handling of stdin and stdout by CliRunner.invoke?

Here's an example:

"""
(click)(master ? =)[~/projects/click]$ python click_bdb_swallow.py 
F
======================================================================
FAIL: test_noarguments (__main__.FooTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "click_bdb_swallow.py", line 21, in test_noarguments
    result = runner.invoke(foo)
AssertionError: <object object at 0x7fb7d0f9f1b0> != <Result BdbQuit()>

----------------------------------------------------------------------
Ran 1 test in 0.006s

FAILED (failures=1)
"""

import unittest

import click
from click.testing import CliRunner

@click.command()
@click.pass_context
def foo(context):
    """
    I want to drop to pdb in here so that I can explore the context object, but
    pdb just exits, maybe because of the way CliRunner.invoke changes stdin,
    stdout.
    """
    import pdb; pdb.set_trace()



class FooTests(unittest.TestCase):
    def test_noarguments(self):
        runner = CliRunner()
        result = runner.invoke(foo)
        # And again it's confusing that BdbQuit has been swallowed by invoke
        # (see https://github.com/mitsuhiko/click/issues/136)
        self.assertEqual(object(), result)



if __name__ == '__main__':
    unittest.main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions