Skip to content

Commit

Permalink
Merge pull request #221 from dkt26111/use_prior_test
Browse files Browse the repository at this point in the history
use_prior_test directive and flag
  • Loading branch information
cdent committed Oct 12, 2017
2 parents 9c218ae + d7e32ac commit d7617dd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 6 deletions.
4 changes: 3 additions & 1 deletion gabbi/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
'xfail': False,
'skip': '',
'poll': {},
'use_prior_test': True,
}


Expand Down Expand Up @@ -134,7 +135,8 @@ def test_request(self):
if self.test_data['skip']:
self.skipTest(self.test_data['skip'])

if self.prior and not self.prior.has_run:
if (self.prior and not self.prior.has_run and
self.test_data['use_prior_test']):
# Use a different result so we don't count this test
# in the results.
self.prior.run(result.TestResult())
Expand Down
13 changes: 11 additions & 2 deletions gabbi/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
test_loader_name=None, fixture_module=None,
response_handlers=None, content_handlers=None,
prefix='', require_ssl=False, url=None,
inner_fixtures=None, verbose=False):
inner_fixtures=None, verbose=False,
use_prior_test=True):
"""Read YAML files from a directory to create tests.
Each YAML file represents an ordered sequence of HTTP requests.
Each YAML file represents a list of HTTP requests.
:param path: The directory where yaml files are located.
:param loader: The TestLoader.
Expand All @@ -66,6 +67,8 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
individual test request.
:param verbose: If ``True`` or ``'all'``, make tests verbose by default
``'headers'`` and ``'body'`` are also accepted.
:param use_prior_test: If ``True``, uses prior test to create ordered
sequence of tests
:type inner_fixtures: List of fixtures.Fixture classes.
:rtype: TestSuite containing multiple TestSuites (one for each YAML file).
"""
Expand Down Expand Up @@ -126,6 +129,12 @@ def build_tests(path, loader, host=None, port=8001, intercept=None,
else:
suite_dict['defaults'] = {'verbose': verbose}

if not use_prior_test:
if 'defaults' in suite_dict:
suite_dict['defaults']['use_prior_test'] = use_prior_test
else:
suite_dict['defaults'] = {'use_prior_test': use_prior_test}

file_suite = suitemaker.test_suite_from_dict(
loader, test_base_name, suite_dict, path, host, port,
fixture_module, intercept, prefix=prefix,
Expand Down
25 changes: 22 additions & 3 deletions gabbi/tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ def setUp(self):
self.loader = unittest.defaultTestLoader
self.test_dir = os.path.join(os.path.dirname(__file__), TESTS_DIR)

def test_driver_loads_two_tests(self):
def test_driver_loads_three_tests(self):
suite = driver.build_tests(self.test_dir, self.loader,
host='localhost', port=8001)
self.assertEqual(1, len(suite._tests),
'top level suite contains one suite')
self.assertEqual(2, len(suite._tests[0]._tests),
'contained suite contains two tests')
self.assertEqual(3, len(suite._tests[0]._tests),
'contained suite contains three tests')
the_one_test = suite._tests[0]._tests[0]
self.assertEqual('test_driver_sample_one',
the_one_test.__class__.__name__,
Expand Down Expand Up @@ -97,3 +97,22 @@ def test_build_url_target_forced_ssl(self):
first_test = suite._tests[0]._tests[0]
full_url = first_test._parse_url(first_test.test_data['url'])
self.assertEqual('https://example.com:1024/theend/', full_url)

def test_build_url_use_prior_test(self):
suite = driver.build_tests(self.test_dir, self.loader,
host='localhost',
use_prior_test=True)
for test in suite._tests[0]._tests:
if test.test_data['name'] != 'use_prior_false':
expected_use_prior = True
else:
expected_use_prior = False

self.assertEqual(expected_use_prior,
test.test_data['use_prior_test'])

suite = driver.build_tests(self.test_dir, self.loader,
host='localhost',
use_prior_test=False)
for test in suite._tests[0]._tests:
self.assertEqual(False, test.test_data['use_prior_test'])
5 changes: 5 additions & 0 deletions gabbi/tests/test_gabbits/sample.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
defaults:
use_prior_test: True

tests:
- name: one
url: /
- name: two
url: http://example.com/moo
- name: use_prior_false
url: http://example.com/foo
use_prior_test: False
64 changes: 64 additions & 0 deletions gabbi/tests/test_use_prior_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# 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.
"""Test use_prior_test directive.
"""

import copy
from six.moves import mock
import unittest

from gabbi import case


class UsePriorTest(unittest.TestCase):

@staticmethod
def make_test_case(use_prior_test=None):
http_case = case.HTTPTestCase('test_request')
http_case.test_data = copy.copy(case.BASE_TEST)
if use_prior_test is not None:
http_case.test_data['use_prior_test'] = use_prior_test
return http_case

@mock.patch('gabbi.case.HTTPTestCase._run_test')
def test_use_prior_true(self, m_run_test):
http_case = self.make_test_case(True)
http_case.has_run = False
http_case.prior = self.make_test_case(True)
http_case.prior.run = mock.MagicMock(unsafe=True)
http_case.prior.has_run = False

http_case.test_request()
http_case.prior.run.assert_called_once()

@mock.patch('gabbi.case.HTTPTestCase._run_test')
def test_use_prior_false(self, m_run_test):
http_case = self.make_test_case(False)
http_case.has_run = False
http_case.prior = self.make_test_case(True)
http_case.prior.run = mock.MagicMock(unsafe=True)
http_case.prior.has_run = False

http_case.test_request()
http_case.prior.run.assert_not_called()

@mock.patch('gabbi.case.HTTPTestCase._run_test')
def test_use_prior_default_true(self, m_run_test):
http_case = self.make_test_case()
http_case.has_run = False
http_case.prior = self.make_test_case(True)
http_case.prior.run = mock.MagicMock(unsafe=True)
http_case.prior.has_run = False

http_case.test_request()
http_case.prior.run.assert_called_once()

0 comments on commit d7617dd

Please sign in to comment.