def test_method_url_pair_format_error(self): test_yaml = {'defaults': {'GET': '/foo'}, 'tests': []} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn('"METHOD: url" pairs not allowed in defaults', str(failure.exception))
def test_inner_list_required(self): test_yaml = {'tests': {'name': 'house', 'url': '/'}} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn('test chunk is not a dict at', str(failure.exception))
def test_upper_dict_required(self): test_yaml = [{'name': 'house', 'url': '/'}] with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('malformed test file, invalid format', str(failure.exception))
def test_url_key_required(self): test_yaml = {'tests': [{'name': 'missing url'}]} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('Test url missing in test foo_missing_url.', str(failure.exception))
def test_name_key_required(self): test_yaml = {'tests': [{'url': '/'}]} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('Test name missing in a test in foo.', str(failure.exception))
def test_unsupported_key_errors(self): test_yaml = {'tests': [{ 'url': '/', 'name': 'simple', 'bad_key': 'wow', }]} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn("Invalid test keys used in test foo_simple:", str(failure.exception))
def test_method_url_pair_duplication_format_error(self): test_yaml = {'tests': [{ 'GET': '/', 'POST': '/', 'name': 'duplicate methods', }]} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn( 'duplicate method/URL directive in "foo_duplicate_methods"', str(failure.exception) )
def test_method_url_pair_duplication_format_error(self): test_yaml = { 'tests': [{ 'GET': '/', 'POST': '/', 'name': 'duplicate methods', }] } with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn( 'duplicate method/URL directive in "foo_duplicate_methods"', str(failure.exception))
def test_unsupported_key_errors(self): test_yaml = { 'tests': [{ 'url': '/', 'name': 'simple', 'bad_key': 'wow', }] } with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn("Invalid test keys used in test foo_simple:", str(failure.exception))
def test_dict_on_invalid_key(self): test_yaml = { 'tests': [{ 'name': '...', 'GET': '/', 'response_html': { 'foo': 'hello', 'bar': 'world', } }] } with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn("invalid key in test: 'response_html'", str(failure.exception))
def test_dict_on_invalid_key(self): test_yaml = {'tests': [{ 'name': '...', 'GET': '/', 'response_html': { 'foo': 'hello', 'bar': 'world', } }]} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_dict(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn( "invalid key in test: 'response_html'", str(failure.exception) )
def run(): """Run simple tests from STDIN. This command provides a way to run a set of tests encoded in YAML that is provided on STDIN. No fixtures are supported, so this is primarily designed for use with real running services. Host and port information may be provided in three different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. * In a URL on the command line. An example run might looks like this:: gabbi-run example.com:9999 < mytest.yaml or:: gabbi-run http://example.com:999 < mytest.yaml It is also possible to provide a URL prefix which can be useful if the target application might be mounted in different locations. An example:: gabbi-run example.com:9999 /mountpoint < mytest.yaml or:: gabbi-run http://example.com:9999/mountpoint < mytest.yaml Use `-x` or `--failfast` to abort after the first error or failure: gabbi-run -x example.com:9999 /mountpoint < mytest.yaml Output is formatted as unittest summary information. """ parser = argparse.ArgumentParser(description="Run gabbi tests from STDIN") parser.add_argument( "target", nargs="?", default="stub", help="A fully qualified URL (with optional path as prefix) " "to the primary target or a host and port, : separated", ) parser.add_argument( "prefix", nargs="?", default=None, help="Path prefix where target app is mounted. Only used when " "target is of the form host[:port]", ) parser.add_argument("-x", "--failfast", action="store_true", help="Exit on first failure") parser.add_argument( "-r", "--response-handler", nargs="?", default=None, dest="response_handlers", action="append", help="Custom response handler. Should be an import path of the " "form package.module or package.module:class.", ) args = parser.parse_args() split_url = urlparse.urlsplit(args.target) if split_url.scheme: target = split_url.netloc prefix = split_url.path else: target = args.target prefix = args.prefix if ":" in target: host, port = target.split(":") else: host = target port = None # Initialize response handlers. custom_response_handlers = [] for import_path in args.response_handlers or []: for handler in load_response_handlers(import_path): custom_response_handlers.append(handler) for handler in driver.RESPONSE_HANDLERS + custom_response_handlers: handler(case.HTTPTestCase) data = yaml.safe_load(sys.stdin.read()) loader = unittest.defaultTestLoader suite = driver.test_suite_from_dict(loader, "input", data, ".", host, port, None, None, prefix=prefix) result = ConciseTestRunner(verbosity=2, failfast=args.failfast).run(suite) sys.exit(not result.wasSuccessful())
def run(): """Run simple tests from STDIN. This command provides a way to run a set of tests encoded in YAML that is provided on STDIN. No fixtures are supported, so this is primarily designed for use with real running services. Host and port information may be provided in three different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. * In a URL on the command line. An example run might looks like this:: gabbi-run example.com:9999 < mytest.yaml or:: gabbi-run http://example.com:999 < mytest.yaml It is also possible to provide a URL prefix which can be useful if the target application might be mounted in different locations. An example:: gabbi-run example.com:9999 /mountpoint < mytest.yaml or:: gabbi-run http://example.com:9999/mountpoint < mytest.yaml Use `-x` or `--failfast` to abort after the first error or failure: gabbi-run -x example.com:9999 /mountpoint < mytest.yaml Output is formatted as unittest summary information. """ parser = argparse.ArgumentParser(description='Run gabbi tests from STDIN') parser.add_argument( 'target', nargs='?', default='stub', help='A fully qualified URL (with optional path as prefix) ' 'to the primary target or a host and port, : separated') parser.add_argument( 'prefix', nargs='?', default=None, help='Path prefix where target app is mounted. Only used when ' 'target is of the form host[:port]') parser.add_argument('-x', '--failfast', action='store_true', help='Exit on first failure') parser.add_argument( '-r', '--response-handler', nargs='?', default=None, dest='response_handlers', action='append', help='Custom response handler. Should be an import path of the ' 'form package.module or package.module:class.') args = parser.parse_args() split_url = urlparse.urlsplit(args.target) if split_url.scheme: target = split_url.netloc prefix = split_url.path else: target = args.target prefix = args.prefix if ':' in target: host, port = target.split(':') else: host = target port = None # Initialize response handlers. custom_response_handlers = [] for import_path in (args.response_handlers or []): for handler in load_response_handlers(import_path): custom_response_handlers.append(handler) for handler in driver.RESPONSE_HANDLERS + custom_response_handlers: handler(case.HTTPTestCase) data = yaml.safe_load(sys.stdin.read()) loader = unittest.defaultTestLoader suite = driver.test_suite_from_dict(loader, 'input', data, '.', host, port, None, None, prefix=prefix) result = ConciseTestRunner(verbosity=2, failfast=args.failfast).run(suite) sys.exit(not result.wasSuccessful())