2014-12-15 13:04:08 +00:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
# not use this file except in compliance with the License. You may obtain
|
|
|
|
# a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
# License for the specific language governing permissions and limitations
|
|
|
|
# under the License.
|
|
|
|
"""Generate HTTP tests from YAML files
|
|
|
|
|
|
|
|
Each HTTP request is its own TestCase and can be requested to be run in
|
2015-04-08 00:00:03 +01:00
|
|
|
isolation from other tests. If it is a member of a sequence of requests,
|
2014-12-15 13:04:08 +00:00
|
|
|
prior requests will be run.
|
|
|
|
|
|
|
|
A sequence is represented by an ordered list in a single YAML file.
|
|
|
|
|
|
|
|
Each sequence becomes a TestSuite.
|
|
|
|
|
|
|
|
An entire directory of YAML files is a TestSuite of TestSuites.
|
|
|
|
"""
|
2014-10-29 16:05:26 +00:00
|
|
|
|
|
|
|
import glob
|
2014-12-27 18:50:05 +00:00
|
|
|
import inspect
|
2014-10-29 16:05:26 +00:00
|
|
|
import os
|
2016-04-05 15:20:52 +01:00
|
|
|
import unittest
|
2014-12-15 13:04:08 +00:00
|
|
|
from unittest import suite
|
2014-12-15 21:55:14 +00:00
|
|
|
import uuid
|
2016-06-16 13:41:56 +01:00
|
|
|
import warnings
|
2014-10-29 16:05:26 +00:00
|
|
|
|
2016-06-16 13:41:56 +01:00
|
|
|
from gabbi import exception
|
2015-03-27 16:33:53 +00:00
|
|
|
from gabbi import handlers
|
2016-04-16 02:32:36 +01:00
|
|
|
from gabbi import reporter
|
Extract suitemaking out of driver
This creates a new module, suitemaker.py, which contains the
code used to create a suite.GabbiSuite, which contains
case.HTTPTestCase tests.
This limits driver.py to being mostly code that is necessary to
assemble the necessary information to create tests that are runnable
by python test harnesses. This is distinguished from runner.py
which assembles the necessary information to both create and _run_
tests itself.
The idea here is that if the interface presented by test_suite_from_dict
(both signature and that it returns a GabbiSuite) is preserved, then
anything in suitemaker.py is welcome to change. The signature on
test_suite_from_dict is the definition of what a caller (driver or runner
or whatever else) needs to have learned:
loader: the unittest.TestLoader which we might be able to default
out of existence
test_base_name: names help with test grouping so useful
suite_dict: the main piece of the pie, the dict
test_directory: the place where `data` can load files from
host: the host any unqualified test goes to
port: the port any unqualified test goes to
fixture_module: the sole module from which fixtures come
intercept: the wsgi app to intercept if any
prefix: the url prefix
host and intercept are mutually exclusive
One can image it might be possible to replace host, port, prefix
with a URL instead. If that were done than host and url would be mutually
exclusive.
If the signature of test_suite_from_dict changes then stuff in both driver
and runner, in how they assemble the required info, would need to change.
Note: For reasons unclear these changes have exposed some state management
issues in test_runner, noted in a NOTE.
2016-06-03 15:07:57 +01:00
|
|
|
from gabbi import suitemaker
|
2016-06-06 11:42:05 +01:00
|
|
|
from gabbi import utils
|
2014-12-15 13:33:00 +00:00
|
|
|
|
|
|
|
|
2014-12-27 18:50:05 +00:00
|
|
|
def build_tests(path, loader, host=None, port=8001, intercept=None,
|
2015-03-28 18:07:12 +00:00
|
|
|
test_loader_name=None, fixture_module=None,
|
2016-04-02 15:07:02 +01:00
|
|
|
response_handlers=None, content_handlers=None,
|
2016-09-26 21:02:18 +01:00
|
|
|
prefix='', require_ssl=False, url=None,
|
|
|
|
inner_fixtures=None):
|
2014-12-15 13:04:08 +00:00
|
|
|
"""Read YAML files from a directory to create tests.
|
|
|
|
|
|
|
|
Each YAML file represents an ordered sequence of HTTP requests.
|
2015-06-17 12:04:25 +01:00
|
|
|
|
|
|
|
:param path: The directory where yaml files are located.
|
|
|
|
:param loader: The TestLoader.
|
2015-06-17 12:16:24 +01:00
|
|
|
:param host: The host to test against. Do not use with ``intercept``.
|
2015-06-17 12:04:25 +01:00
|
|
|
:param port: The port to test against. Used with ``host``.
|
|
|
|
:param intercept: WSGI app factory for wsgi-intercept.
|
2016-09-20 19:28:48 +01:00
|
|
|
:param test_loader_name: Base name for test classes. Use this to align the
|
|
|
|
naming of the tests with other tests in a system.
|
2015-06-17 12:04:25 +01:00
|
|
|
:param fixture_module: Python module containing fixture classes.
|
2016-06-15 18:19:16 +01:00
|
|
|
:param response_handers: :class:`~gabbi.handlers.ResponseHandler` classes.
|
2015-06-17 12:04:25 +01:00
|
|
|
:type response_handlers: List of ResponseHandler classes.
|
2016-04-02 15:07:02 +01:00
|
|
|
:param content_handlers: ContentHandler classes.
|
|
|
|
:type content_handlers: List of ContentHandler classes.
|
2015-06-17 12:04:25 +01:00
|
|
|
:param prefix: A URL prefix for all URLs that are not fully qualified.
|
2016-06-13 20:10:24 +01:00
|
|
|
:param url: A full URL to test against. Replaces host, port and prefix.
|
2016-06-02 09:15:57 +01:00
|
|
|
:param require_ssl: If ``True``, make all tests default to using SSL.
|
2016-09-28 16:30:07 +01:00
|
|
|
:param inner_fixtures: A list of ``Fixtures`` to use with each
|
|
|
|
individual test request.
|
|
|
|
:type inner_fixtures: List of fixtures.Fixture classes.
|
2015-06-17 12:16:24 +01:00
|
|
|
:rtype: TestSuite containing multiple TestSuites (one for each YAML file).
|
2014-12-15 13:04:08 +00:00
|
|
|
"""
|
2015-03-10 16:04:54 +00:00
|
|
|
|
2015-10-11 12:03:07 +01:00
|
|
|
# Exit immediately if we have no host to access, either via a real host
|
|
|
|
# or an intercept.
|
2016-05-07 14:00:37 +01:00
|
|
|
if not bool(host) ^ bool(intercept):
|
2015-05-26 23:04:37 +01:00
|
|
|
raise AssertionError('must specify exactly one of host or intercept')
|
2015-03-10 16:04:54 +00:00
|
|
|
|
2016-06-13 20:10:24 +01:00
|
|
|
# If url is being used, reset host, port and prefix.
|
|
|
|
if url:
|
|
|
|
host, port, prefix, force_ssl = utils.host_info_from_target(url)
|
|
|
|
if force_ssl and not require_ssl:
|
|
|
|
require_ssl = force_ssl
|
|
|
|
|
2016-09-20 19:28:48 +01:00
|
|
|
# If the client has not provided a name to use as our base,
|
|
|
|
# create one so that tests are effectively namespaced.
|
2015-01-14 14:31:10 +00:00
|
|
|
if test_loader_name is None:
|
2016-09-20 19:28:48 +01:00
|
|
|
all_test_base_name = inspect.stack()[1]
|
|
|
|
all_test_base_name = os.path.splitext(
|
|
|
|
os.path.basename(all_test_base_name[1]))[0]
|
|
|
|
else:
|
|
|
|
all_test_base_name = None
|
2014-12-15 21:55:14 +00:00
|
|
|
|
2016-04-02 15:07:02 +01:00
|
|
|
# Initialize response and content handlers. This is effectively
|
|
|
|
# duplication of effort but not results. This allows for
|
|
|
|
# backwards compatibility for existing callers.
|
2015-10-18 13:59:51 +02:00
|
|
|
response_handlers = response_handlers or []
|
2016-04-02 15:07:02 +01:00
|
|
|
content_handlers = content_handlers or []
|
2016-04-14 17:01:04 +01:00
|
|
|
handler_objects = []
|
2016-06-08 13:16:43 +01:00
|
|
|
for handler in (content_handlers + response_handlers
|
|
|
|
+ handlers.RESPONSE_HANDLERS):
|
2016-04-14 17:01:04 +01:00
|
|
|
handler_objects.append(handler())
|
2015-03-27 16:33:53 +00:00
|
|
|
|
2015-10-18 13:59:51 +02:00
|
|
|
top_suite = suite.TestSuite()
|
|
|
|
for test_file in glob.iglob('%s/*.yaml' % path):
|
2016-06-16 13:41:56 +01:00
|
|
|
if '_' in os.path.basename(test_file):
|
|
|
|
warnings.warn(exception.GabbiSyntaxWarning(
|
|
|
|
"'_' in test filename %s. This can break suite grouping."
|
|
|
|
% test_file))
|
2015-03-10 16:04:54 +00:00
|
|
|
if intercept:
|
|
|
|
host = str(uuid.uuid4())
|
2016-06-06 11:42:05 +01:00
|
|
|
suite_dict = utils.load_yaml(yaml_file=test_file)
|
2016-09-20 19:28:48 +01:00
|
|
|
test_base_name = os.path.splitext(os.path.basename(test_file))[0]
|
|
|
|
if all_test_base_name:
|
|
|
|
test_base_name = '%s_%s' % (all_test_base_name, test_base_name)
|
2016-06-02 09:15:57 +01:00
|
|
|
|
|
|
|
if require_ssl:
|
|
|
|
if 'defaults' in suite_dict:
|
|
|
|
suite_dict['defaults']['ssl'] = True
|
|
|
|
else:
|
|
|
|
suite_dict['defaults'] = {'ssl': True}
|
|
|
|
|
Extract suitemaking out of driver
This creates a new module, suitemaker.py, which contains the
code used to create a suite.GabbiSuite, which contains
case.HTTPTestCase tests.
This limits driver.py to being mostly code that is necessary to
assemble the necessary information to create tests that are runnable
by python test harnesses. This is distinguished from runner.py
which assembles the necessary information to both create and _run_
tests itself.
The idea here is that if the interface presented by test_suite_from_dict
(both signature and that it returns a GabbiSuite) is preserved, then
anything in suitemaker.py is welcome to change. The signature on
test_suite_from_dict is the definition of what a caller (driver or runner
or whatever else) needs to have learned:
loader: the unittest.TestLoader which we might be able to default
out of existence
test_base_name: names help with test grouping so useful
suite_dict: the main piece of the pie, the dict
test_directory: the place where `data` can load files from
host: the host any unqualified test goes to
port: the port any unqualified test goes to
fixture_module: the sole module from which fixtures come
intercept: the wsgi app to intercept if any
prefix: the url prefix
host and intercept are mutually exclusive
One can image it might be possible to replace host, port, prefix
with a URL instead. If that were done than host and url would be mutually
exclusive.
If the signature of test_suite_from_dict changes then stuff in both driver
and runner, in how they assemble the required info, would need to change.
Note: For reasons unclear these changes have exposed some state management
issues in test_runner, noted in a NOTE.
2016-06-03 15:07:57 +01:00
|
|
|
file_suite = suitemaker.test_suite_from_dict(
|
|
|
|
loader, test_base_name, suite_dict, path, host, port,
|
2016-09-26 20:34:47 +01:00
|
|
|
fixture_module, intercept, prefix=prefix,
|
2016-09-26 21:02:18 +01:00
|
|
|
test_loader_name=test_loader_name, handlers=handler_objects,
|
|
|
|
inner_fixtures=inner_fixtures)
|
2015-03-10 16:04:54 +00:00
|
|
|
top_suite.addTest(file_suite)
|
2014-12-12 14:44:33 +00:00
|
|
|
return top_suite
|
2014-12-15 13:33:00 +00:00
|
|
|
|
|
|
|
|
2016-04-05 15:20:52 +01:00
|
|
|
def py_test_generator(test_dir, host=None, port=8001, intercept=None,
|
|
|
|
prefix=None, test_loader_name=None,
|
2016-06-02 09:15:57 +01:00
|
|
|
fixture_module=None, response_handlers=None,
|
2016-11-01 16:16:35 +00:00
|
|
|
content_handlers=None, require_ssl=False, url=None,
|
|
|
|
metafunc=None):
|
2016-04-05 15:20:52 +01:00
|
|
|
"""Generate tests cases for py.test
|
|
|
|
|
|
|
|
This uses build_tests to create TestCases and then yields them in
|
|
|
|
a way that pytest can handle.
|
|
|
|
"""
|
2016-06-16 13:42:21 +01:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
pluginmanager = pytest.config.pluginmanager
|
|
|
|
pluginmanager.import_plugin('gabbi.pytester')
|
|
|
|
|
2016-04-05 15:20:52 +01:00
|
|
|
loader = unittest.TestLoader()
|
2016-04-16 02:32:36 +01:00
|
|
|
result = reporter.PyTestResult()
|
2016-04-05 15:20:52 +01:00
|
|
|
tests = build_tests(test_dir, loader, host=host, port=port,
|
|
|
|
intercept=intercept,
|
|
|
|
test_loader_name=test_loader_name,
|
|
|
|
fixture_module=fixture_module,
|
|
|
|
response_handlers=response_handlers,
|
2016-05-06 18:23:13 +01:00
|
|
|
content_handlers=content_handlers,
|
2016-06-13 20:10:24 +01:00
|
|
|
prefix=prefix, require_ssl=require_ssl,
|
|
|
|
url=url)
|
2016-04-05 15:20:52 +01:00
|
|
|
|
2016-10-28 19:11:08 +02:00
|
|
|
test_list = []
|
2016-04-05 15:20:52 +01:00
|
|
|
for test in tests:
|
|
|
|
if hasattr(test, '_tests'):
|
2016-06-16 13:42:21 +01:00
|
|
|
# Establish fixtures as if they were tests. These will
|
|
|
|
# be cleaned up by the pytester plugin.
|
2016-11-01 16:16:35 +00:00
|
|
|
test_list.append(('start_%s' % test._tests[0].__class__.__name__,
|
|
|
|
test.start, result))
|
2016-04-16 02:32:36 +01:00
|
|
|
for subtest in test:
|
2016-11-01 16:16:35 +00:00
|
|
|
test_list.append(('%s' % subtest.__class__.__name__,
|
|
|
|
subtest, result))
|
|
|
|
test_list.append(('stop_%s' % test._tests[0].__class__.__name__,
|
|
|
|
test.stop))
|
|
|
|
|
|
|
|
if metafunc:
|
|
|
|
if metafunc.function == test_pytest:
|
|
|
|
ids = []
|
|
|
|
args = []
|
|
|
|
for test in test_list:
|
2016-11-01 20:24:04 +00:00
|
|
|
if len(test) >= 3:
|
2016-11-01 16:16:35 +00:00
|
|
|
name, method, arg = test
|
|
|
|
else:
|
|
|
|
name, method = test
|
|
|
|
arg = None
|
|
|
|
ids.append(name)
|
|
|
|
args.append((method, arg))
|
|
|
|
|
|
|
|
metafunc.parametrize("test, result", argvalues=args, ids=ids)
|
|
|
|
else:
|
|
|
|
# preserve backwards compatibility with old calling style
|
|
|
|
return test_list
|
2016-10-28 19:11:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_pytest(test, result):
|
2016-11-01 20:24:04 +00:00
|
|
|
if result:
|
2016-10-28 19:11:08 +02:00
|
|
|
test(result)
|
2016-11-01 20:24:04 +00:00
|
|
|
else:
|
2016-10-28 19:11:08 +02:00
|
|
|
test()
|
2016-04-05 15:20:52 +01:00
|
|
|
|
|
|
|
|
2015-10-15 14:18:56 +02:00
|
|
|
def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory,
|
|
|
|
host, port, fixture_module, intercept, prefix=''):
|
|
|
|
"""Legacy wrapper retained for backwards compatibility."""
|
|
|
|
|
|
|
|
with warnings.catch_warnings(): # ensures warnings filter is restored
|
|
|
|
warnings.simplefilter('default', DeprecationWarning)
|
|
|
|
warnings.warn('test_suite_from_yaml has been renamed to '
|
|
|
|
'test_suite_from_dict', DeprecationWarning, stacklevel=2)
|
Extract suitemaking out of driver
This creates a new module, suitemaker.py, which contains the
code used to create a suite.GabbiSuite, which contains
case.HTTPTestCase tests.
This limits driver.py to being mostly code that is necessary to
assemble the necessary information to create tests that are runnable
by python test harnesses. This is distinguished from runner.py
which assembles the necessary information to both create and _run_
tests itself.
The idea here is that if the interface presented by test_suite_from_dict
(both signature and that it returns a GabbiSuite) is preserved, then
anything in suitemaker.py is welcome to change. The signature on
test_suite_from_dict is the definition of what a caller (driver or runner
or whatever else) needs to have learned:
loader: the unittest.TestLoader which we might be able to default
out of existence
test_base_name: names help with test grouping so useful
suite_dict: the main piece of the pie, the dict
test_directory: the place where `data` can load files from
host: the host any unqualified test goes to
port: the port any unqualified test goes to
fixture_module: the sole module from which fixtures come
intercept: the wsgi app to intercept if any
prefix: the url prefix
host and intercept are mutually exclusive
One can image it might be possible to replace host, port, prefix
with a URL instead. If that were done than host and url would be mutually
exclusive.
If the signature of test_suite_from_dict changes then stuff in both driver
and runner, in how they assemble the required info, would need to change.
Note: For reasons unclear these changes have exposed some state management
issues in test_runner, noted in a NOTE.
2016-06-03 15:07:57 +01:00
|
|
|
return suitemaker.test_suite_from_dict(
|
|
|
|
loader, test_base_name, test_yaml, test_directory, host, port,
|
|
|
|
fixture_module, intercept, prefix)
|