def test_inner_list_required(self): test_yaml = {'tests': {'name': 'house', 'url': '/'}} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_yaml(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_yaml(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('malformed test file, invalid format', str(failure.exception))
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_yaml(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn('"METHOD: url" pairs not allowed in defaults', str(failure.exception))
def test_name_key_required(self): test_yaml = {'tests': [{'url': '/'}]} with self.assertRaises(AssertionError) as failure: driver.test_suite_from_yaml(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('Test name missing in a test in foo.', str(failure.exception))
def test_url_key_required(self): test_yaml = {'tests': [{'name': 'missing url'}]} with self.assertRaises(AssertionError) as failure: driver.test_suite_from_yaml(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('Test url missing in test foo_missing_url.', str(failure.exception))
def test_tests_key_required(self): test_yaml = {'name': 'house', 'url': '/'} with self.assertRaises(driver.GabbiFormatError) as failure: driver.test_suite_from_yaml(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertEqual('malformed test file, "tests" key required', str(failure.exception))
def test_unsupported_key_errors(self): test_yaml = {'tests': [{ 'url': '/', 'name': 'simple', 'bad_key': 'wow', }]} with self.assertRaises(AssertionError) as failure: driver.test_suite_from_yaml(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_yaml(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(AssertionError) as failure: driver.test_suite_from_yaml(self.loader, 'foo', test_yaml, '.', 'localhost', 80, None, None) self.assertIn("Invalid test keys used in test foo_simple:", str(failure.exception))
def run_gabi(self, gabbi_declaration): # initialise the gabbi handlers for handler in RESPONSE_HANDLERS: handler(case.HTTPTestCase) # take only the host name and port from the live server _, host = self.live_server_url.split('://') # use gabbi to create the test suite from our declaration suite = test_suite_from_yaml( unittest.defaultTestLoader, self.id(), gabbi_declaration, '.', host, None, None, None, ) # run the test (we store the the output into a custom stream so that hypothesis can display only the simple # case test result on failure rather than every failing case) s = StringIO() result = ConciseTestRunner(stream=s, verbosity=0).run(suite) # if we weren't successfull we need to fail the test case with the error string from gabbi if not result.wasSuccessful(): self.fail(s.getvalue())
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 two different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. An example run might looks like this:: gabbi-run example.com:9999 < mytest.yaml Output is formatted as unittest summary information. """ try: hostport = sys.argv[1] if ':' in hostport: host, port = hostport.split(':') else: host = hostport port = None except IndexError: host, port = 'stub', None loader = unittest.defaultTestLoader # Initialize the extensions for response handling. for handler in driver.RESPONSE_HANDLERS: handler(case.HTTPTestCase) data = yaml.safe_load(sys.stdin.read()) suite = driver.test_suite_from_yaml(loader, 'input', data, '.', host, port, None, None) result = unittest.TextTestRunner(verbosity=2).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 two different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. An example run might looks like this:: gabbi-run example.com:9999 < mytest.yaml Output is formatted as unittest summary information. """ try: hostport = sys.argv[1] if ":" in hostport: host, port = hostport.split(":") else: host = hostport port = None except IndexError: host, port = "stub", None loader = unittest.defaultTestLoader # Initialize the extensions for response handling. for handler in driver.RESPONSE_HANDLERS: handler(case.HTTPTestCase) data = yaml.safe_load(sys.stdin.read()) suite = driver.test_suite_from_yaml(loader, "input", data, ".", host, port, None, None) result = unittest.TextTestRunner(verbosity=2).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 two different ways: * In the URL value of the tests. * In a `host` or `host:port` argument on the command line. An example run might looks like this:: gabbi-run example.com:9999 < 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 Use `-x` to abort after the first error or failure: gabbi-run -x example.com:9999 /mountpoint < mytest.yaml Output is formatted as unittest summary information. """ args = sys.argv[1:] try: args.remove("-x") failfast = True except ValueError: failfast = False try: hostport = args[0] if ':' in hostport: host, port = hostport.split(':') else: host = hostport port = None except IndexError: host, port = 'stub', None try: prefix = args[1] except IndexError: prefix = None loader = unittest.defaultTestLoader # Initialize the extensions for response handling. for handler in driver.RESPONSE_HANDLERS: handler(case.HTTPTestCase) data = yaml.safe_load(sys.stdin.read()) suite = driver.test_suite_from_yaml(loader, 'input', data, '.', host, port, None, None, prefix=prefix) result = ConciseTestRunner(verbosity=2, failfast=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', help='Exit on first failure', action='store_true' ) 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 loader = unittest.defaultTestLoader # Initialize the extensions for response handling. for handler in driver.RESPONSE_HANDLERS: handler(case.HTTPTestCase) data = yaml.safe_load(sys.stdin.read()) suite = driver.test_suite_from_yaml(loader, 'input', data, '.', host, port, None, None, prefix=prefix) result = ConciseTestRunner(verbosity=2, failfast=args.failfast).run(suite) sys.exit(not result.wasSuccessful())