diff options
| author | Matt Riedemann <mriedem.os@gmail.com> | 2019-09-18 11:58:12 -0400 |
|---|---|---|
| committer | Matt Riedemann <mriedem.os@gmail.com> | 2019-11-20 13:39:40 +0000 |
| commit | 5b3a827a1ff80e4b51c7ede44b84bf640d5b6380 (patch) | |
| tree | cd22a0cf7689b6e2c4d8981fa89f49d459d53a77 /openstackclient/tests | |
| parent | 874a726f522dae3a08832f32230e2590a787d45c (diff) | |
| download | python-openstackclient-5b3a827a1ff80e4b51c7ede44b84bf640d5b6380.tar.gz | |
Provide stderr in exception when check_parser fails
For negative tests that are asserting an argparse failure
it would be useful to assert the specific reason for the
failure in the test rather than just getting an exception,
especially to avoid false positives in the tests when what
is being tested and failing isn't the actual expected reason
for the failure.
This wraps the check_parser code that parses the args and
mocks sys.stderr so we can trap that output and put it in the
exception message that gets raised to the test.
As a result, we can tighten up a test that was passing before
for the wrong reason [1].
[1] https://review.opendev.org/#/c/673725/12/openstackclient/tests/unit/compute/v2/test_server.py@605
Change-Id: I0f1dc1215bdfb3eba98ccaf66a0041d220b93812
Diffstat (limited to 'openstackclient/tests')
| -rw-r--r-- | openstackclient/tests/unit/compute/v2/test_server.py | 3 | ||||
| -rw-r--r-- | openstackclient/tests/unit/utils.py | 12 |
2 files changed, 10 insertions, 5 deletions
diff --git a/openstackclient/tests/unit/compute/v2/test_server.py b/openstackclient/tests/unit/compute/v2/test_server.py index c70d6d72..c2bac277 100644 --- a/openstackclient/tests/unit/compute/v2/test_server.py +++ b/openstackclient/tests/unit/compute/v2/test_server.py @@ -626,7 +626,8 @@ class TestServerVolumeV279(TestServerVolume): ex = self.assertRaises(utils.ParserException, self.check_parser, self.cmd, arglist, verifylist) - self.assertIn('Argument parse failed', str(ex)) + self.assertIn('argument --disable-delete-on-termination: not allowed ' + 'with argument --enable-delete-on-termination', str(ex)) class TestServerAddNetwork(TestServer): diff --git a/openstackclient/tests/unit/utils.py b/openstackclient/tests/unit/utils.py index c15d8bbf..8df81a50 100644 --- a/openstackclient/tests/unit/utils.py +++ b/openstackclient/tests/unit/utils.py @@ -17,6 +17,7 @@ import os import fixtures +from six.moves import StringIO import testtools from cliff import columns as cliff_columns @@ -72,10 +73,13 @@ class TestCommand(TestCase): def check_parser(self, cmd, args, verify_args): cmd_parser = cmd.get_parser('check_parser') - try: - parsed_args = cmd_parser.parse_args(args) - except SystemExit: - raise ParserException("Argument parse failed") + stderr = StringIO() + with fixtures.MonkeyPatch('sys.stderr', stderr): + try: + parsed_args = cmd_parser.parse_args(args) + except SystemExit: + raise ParserException("Argument parse failed: %s" % + stderr.getvalue()) for av in verify_args: attr, value = av if attr: |
