Skip to content

Commit

Permalink
Merge pull request #191 from cdent/runner-data-dir
Browse files Browse the repository at this point in the history
Use the dirname of each runner testfile as the test_dir
  • Loading branch information
cdent committed Nov 28, 2016
2 parents e005836 + 398318e commit 042f891
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
8 changes: 5 additions & 3 deletions gabbi/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import argparse
from importlib import import_module
import os
import sys
import unittest

Expand Down Expand Up @@ -84,8 +85,9 @@ def run():
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)
prefix, force_ssl, failfast, data_dir)
if not failure: # once failed, this is considered immutable
failure = not success
if failure and failfast:
Expand All @@ -95,7 +97,7 @@ def run():


def run_suite(handle, handler_objects, host, port, prefix, force_ssl=False,
failfast=False):
failfast=False, data_dir='.'):
"""Run the tests from the YAML in handle."""
data = utils.load_yaml(handle)
if force_ssl:
Expand All @@ -106,7 +108,7 @@ def run_suite(handle, handler_objects, host, port, prefix, force_ssl=False,

loader = unittest.defaultTestLoader
test_suite = suitemaker.test_suite_from_dict(
loader, 'input', data, '.', host, port, None, None, prefix=prefix,
loader, 'input', data, data_dir, host, port, None, None, prefix=prefix,
handlers=handler_objects)

result = ConciseTestRunner(
Expand Down
1 change: 1 addition & 0 deletions gabbi/tests/gabbits_runner/subdir/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"items": {"house": "blue"}}
8 changes: 8 additions & 0 deletions gabbi/tests/gabbits_runner/test_data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tests:

- name: POST data from file
verbose: true
POST: /
request_headers:
content-type: application/json
data: <@subdir/sample.json
23 changes: 23 additions & 0 deletions gabbi/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from gabbi import exception
from gabbi.handlers import base
from gabbi.handlers.jsonhandler import JSONHandler
from gabbi import runner
from gabbi.tests.simple_wsgi import SimpleWsgi

Expand Down Expand Up @@ -249,6 +250,28 @@ def test_verbose_output_formatting(self):
self.assertIn('{\n', output)
self.assertIn('}\n', output)

def test_data_dir_good(self):
"""Confirm that data dir is the test file's dir."""
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port)]

sys.argv.append('--')
sys.argv.append('gabbi/tests/gabbits_runner/test_data.yaml')
with self.server():
try:
runner.run()
except SystemExit as err:
self.assertSuccess(err)

# Compare the verbose output of tests with pretty printed
# data.
with open('gabbi/tests/gabbits_runner/subdir/sample.json') as data:
data = JSONHandler.loads(data.read())
expected_string = JSONHandler.dumps(data, pretty=True)

sys.stdout.seek(0)
output = sys.stdout.read()
self.assertIn(expected_string, output)

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

0 comments on commit 042f891

Please sign in to comment.