Skip to content

Commit

Permalink
testing: add CapturingTestCase to test stdout/stderr/click CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
kba committed Jun 7, 2020
1 parent eee9483 commit fba15a6
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,30 @@ def setUpClass(cls):
def tearDown(self):
initLogging()

class CapturingTestCase(TestCase):
"""
A TestCase that needs to capture stderr/stdout and invoke click CLI.
"""

@pytest.fixture(autouse=True)
def _setup_pytest_capfd(self, capfd):
self.capfd = capfd

def invoke_cli(self, cli, args):
"""
Substitution for click.CliRunner.invooke that works together nicely
with unittests/pytest capturing stdout/stderr.
"""
self.capture_out_err() # XXX snapshot just before executing the CLI
try:
cli.main(args=args)
except SystemExit as e:
out, err = self.capture_out_err()
return e.code, out, err

def capture_out_err(self):
return self.capfd.readouterr()

# import traceback
# import warnings
# def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
Expand Down

0 comments on commit fba15a6

Please sign in to comment.