click.testing.CliRunner.invoke swallows programming errors

Created on 9 Jun 2014  路  4Comments  路  Source: pallets/click

CliRunner.invoke captures _all_ exceptions:

...which makes it difficult to see programming errors.

Perhaps it should only catch click specific exceptions.

Here's an example.

import unittest

import click
from click.testing import CliRunner

@click.command()
@click.argument('bar')
def foo():
    """
    This command will raise a TypeError('foo() takes no arguments (1 given)',)
    when called via runner.invoke, but you won't see that when testing with
    CliRunner.invoke.
    """
    click.echo('hello world\n')



class VolumeTests(unittest.TestCase):
    def test_noarguments(self):
        runner = CliRunner()
        result = runner.invoke(foo, ['baz'])
        self.assertEqual(b'hello world\n', result.output)
        # Only way to see the error is by examining the exception attribute.
        # self.assertEqual(b'', result.exception)



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

Most helpful comment

runner.invoke(loader.loader, ["-t", "rdr", fname], catch_exceptions=False)

All 4 comments

Click 3.0 has a flag to control this.

runner.invoke(loader.loader, ["-t", "rdr", fname], catch_exceptions=False)

A note about this in the testing docs would be great

Looks like this is covered by the documentation here: https://click.palletsprojects.com/en/7.x/api/#testing

Was this page helpful?
0 / 5 - 0 ratings