Best Python code snippet using gabbi_python
runner.py
Source:runner.py
1#2# Licensed under the Apache License, Version 2.0 (the "License"); you may3# not use this file except in compliance with the License. You may obtain4# a copy of the License at5#6# http://www.apache.org/licenses/LICENSE-2.07#8# Unless required by applicable law or agreed to in writing, software9# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT10# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the11# License for the specific language governing permissions and limitations12# under the License.13"""Implementation of a command-line runner of single gabbi files."""14import argparse15import sys16import unittest17import yaml18from importlib import import_module19from six.moves.urllib import parse as urlparse20from gabbi import case21from gabbi import driver22from gabbi.reporter import ConciseTestRunner23def run():24 """Run simple tests from STDIN.25 This command provides a way to run a set of tests encoded in YAML that26 is provided on STDIN. No fixtures are supported, so this is primarily27 designed for use with real running services.28 Host and port information may be provided in three different ways:29 * In the URL value of the tests.30 * In a `host` or `host:port` argument on the command line.31 * In a URL on the command line.32 An example run might looks like this::33 gabbi-run example.com:9999 < mytest.yaml34 or::35 gabbi-run http://example.com:999 < mytest.yaml36 It is also possible to provide a URL prefix which can be useful if the37 target application might be mounted in different locations. An example::38 gabbi-run example.com:9999 /mountpoint < mytest.yaml39 or::40 gabbi-run http://example.com:9999/mountpoint < mytest.yaml41 Use `-x` or `--failfast` to abort after the first error or failure:42 gabbi-run -x example.com:9999 /mountpoint < mytest.yaml43 Output is formatted as unittest summary information.44 """45 parser = argparse.ArgumentParser(description='Run gabbi tests from STDIN')46 parser.add_argument(47 'target',48 nargs='?', default='stub',49 help='A fully qualified URL (with optional path as prefix) '50 'to the primary target or a host and port, : separated'51 )52 parser.add_argument(53 'prefix',54 nargs='?', default=None,55 help='Path prefix where target app is mounted. Only used when '56 'target is of the form host[:port]'57 )58 parser.add_argument(59 '-x', '--failfast',60 action='store_true',61 help='Exit on first failure'62 )63 parser.add_argument(64 '-r', '--response-handler',65 nargs='?', default=None,66 dest='response_handlers',67 action='append',68 help='Custom response handler. Should be an import path of the '69 'form package.module or package.module:class.'70 )71 args = parser.parse_args()72 split_url = urlparse.urlsplit(args.target)73 if split_url.scheme:74 target = split_url.netloc75 prefix = split_url.path76 else:77 target = args.target78 prefix = args.prefix79 if ':' in target:80 host, port = target.split(':')81 else:82 host = target83 port = None84 # Initialize response handlers.85 custom_response_handlers = []86 for import_path in (args.response_handlers or []):87 for handler in load_response_handlers(import_path):88 custom_response_handlers.append(handler)89 for handler in driver.RESPONSE_HANDLERS + custom_response_handlers:90 handler(case.HTTPTestCase)91 data = yaml.safe_load(sys.stdin.read())92 loader = unittest.defaultTestLoader93 suite = driver.test_suite_from_dict(loader, 'input', data, '.',94 host, port, None, None,95 prefix=prefix)96 result = ConciseTestRunner(verbosity=2, failfast=args.failfast).run(suite)97 sys.exit(not result.wasSuccessful())98def load_response_handlers(import_path):99 """Load and return custom response handlers from the given Python package100 or module.101 The import path references either a specific response handler class102 ("package.module:class") or a module that contains one or more response103 handler classes ("package.module").104 For the latter, the module is expected to contain a105 ``gabbi_response_handlers`` object, which is either a list of response106 handler classes or a function returning such a list.107 """108 if ":" in import_path: # package.module:class109 module_name, handler_name = import_path.rsplit(":", 1)110 module = import_module(module_name)111 handler = getattr(module, handler_name)112 handlers = [handler]113 else: # package.module shorthand, expecting gabbi_response_handlers114 module = import_module(import_path)115 handlers = module.gabbi_response_handlers116 if callable(handlers):117 handlers = handlers()118 return handlers119if __name__ == '__main__':...
custom_response_handler.py
Source:custom_response_handler.py
...10# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the11# License for the specific language governing permissions and limitations12# under the License.13from gabbi import handlers14def gabbi_response_handlers():15 return [CustomResponseHandler]16class CustomResponseHandler(handlers.ResponseHandler):17 test_key_suffix = 'custom'18 test_key_value = []19 def action(self, test, item, value=None):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!