Skip to content

Commit

Permalink
Merge branch 'fix-185'
Browse files Browse the repository at this point in the history
  • Loading branch information
cdent committed Nov 28, 2016
2 parents 042f891 + 7f2c19f commit 60ccdd0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/source/runner.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ YAML will default to ``ssl: True``.

If a ``-x`` or ``--failfast`` argument is provided then ``gabbi-run`` will
exit after the first test failure.

Use ``-v`` or ``--verbose`` with a value of ``all``, ``headers`` or ``body``
to turn on :ref:`verbosity <metadata>` for all tests being run.
22 changes: 19 additions & 3 deletions gabbi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def run():
gabbi-run -x example.com:9999 /mountpoint < mytest.yaml
Use `-v` or `--verbose` with a value of `all`, `headers` or `body` to
turn on verbosity for all tests being run.
Multiple files may be named as arguments, separated from other arguments
by a ``--``. Each file will be run as a separate test suite::
Expand All @@ -75,19 +78,21 @@ def run():

handler_objects = initialize_handlers(args.response_handlers)

verbosity = args.verbosity
failfast = args.failfast
failure = False

if not input_files:
success = run_suite(sys.stdin, handler_objects, host, port,
prefix, force_ssl, failfast)
prefix, force_ssl, failfast, verbosity)
failure = not success
else:
for input_file in input_files:
with open(input_file, 'r') as fh:
data_dir = os.path.dirname(input_file)
success = run_suite(fh, handler_objects, host, port,
prefix, force_ssl, failfast, data_dir)
prefix, force_ssl, failfast, data_dir,
verbosity)
if not failure: # once failed, this is considered immutable
failure = not success
if failure and failfast:
Expand All @@ -97,14 +102,19 @@ def run():


def run_suite(handle, handler_objects, host, port, prefix, force_ssl=False,
failfast=False, data_dir='.'):
failfast=False, data_dir='.', verbosity=False):
"""Run the tests from the YAML in handle."""
data = utils.load_yaml(handle)
if force_ssl:
if 'defaults' in data:
data['defaults']['ssl'] = True
else:
data['defaults'] = {'ssl': True}
if verbosity:
if 'defaults' in data:
data['defaults']['verbose'] = verbosity
else:
data['defaults'] = {'verbose': verbosity}

loader = unittest.defaultTestLoader
test_suite = suitemaker.test_suite_from_dict(
Expand Down Expand Up @@ -196,6 +206,12 @@ def _make_argparser():
help='Custom response handler. Should be an import path of the '
'form package.module or package.module:class.'
)
parser.add_argument(
'-v', '--verbose',
dest='verbosity',
choices=['all', 'body', 'headers'],
help='Turn on test verbosity for all tests run in this session.'
)
return parser


Expand Down
8 changes: 8 additions & 0 deletions gabbi/tests/gabbits_runner/verbosity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests:

- name: simple data post
POST: /
request_headers:
content-type: application/json
data:
cat: poppy
49 changes: 49 additions & 0 deletions gabbi/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ def test_data_dir_good(self):

sys.argv.append('--')
sys.argv.append('gabbi/tests/gabbits_runner/test_data.yaml')

with self.server():
try:
runner.run()
Expand All @@ -272,6 +273,54 @@ def test_data_dir_good(self):
output = sys.stdout.read()
self.assertIn(expected_string, output)

def _run_verbosity_arg(self):
sys.argv.append('--')
sys.argv.append('gabbi/tests/gabbits_runner/verbosity.yaml')

with self.server():
try:
runner.run()
except SystemExit as err:
self.assertSuccess(err)

sys.stdout.seek(0)
output = sys.stdout.read()
return output

def test_verbosity_arg_none(self):
"""Confirm --verbose handling."""
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port)]

output = self._run_verbosity_arg()
self.assertEqual('', output)

def test_verbosity_arg_body(self):
"""Confirm --verbose handling."""
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port),
'--verbose=body']

output = self._run_verbosity_arg()
self.assertIn('{\n "cat": "poppy"\n}', output)
self.assertNotIn('application/json', output)

def test_verbosity_arg_headers(self):
"""Confirm --verbose handling."""
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port),
'--verbose=headers']

output = self._run_verbosity_arg()
self.assertNotIn('{\n "cat": "poppy"\n}', output)
self.assertIn('application/json', output)

def test_verbosity_arg_all(self):
"""Confirm --verbose handling."""
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port),
'--verbose=all']

output = self._run_verbosity_arg()
self.assertIn('{\n "cat": "poppy"\n}', output)
self.assertIn('application/json', output)

def assertSuccess(self, exitError):
errors = exitError.args[0]
if errors:
Expand Down

0 comments on commit 60ccdd0

Please sign in to comment.