Refactor base test classes inheritance for reuse

Refactor base test class inheritance to allow for BaseTest class to be
reused without needing to also import the TestCase class everywhere in
order to avoid having the common test function executed on base classes.

This makes it easier to build base testing classes and then simplifies
the subclassing of these for actual tests.

Change-Id: I89809e8082469f814f245db4a9ab7658aac8a405
This commit is contained in:
Darragh Bailey 2016-06-29 12:20:42 +01:00
parent b62e2ea0b6
commit 4573b3a25d
23 changed files with 84 additions and 157 deletions

View File

@ -30,6 +30,7 @@ import fixtures
from six.moves import StringIO from six.moves import StringIO
import testtools import testtools
from testtools.content import text_content from testtools.content import text_content
import testscenarios
from yaml import safe_dump from yaml import safe_dump
from jenkins_jobs.config import JJBConfig from jenkins_jobs.config import JJBConfig
@ -100,22 +101,17 @@ def get_scenarios(fixtures_path, in_ext='yaml', out_ext='xml',
return scenarios return scenarios
class LoggingFixture(object): class BaseTestCase(testtools.TestCase):
def setUp(self):
super(LoggingFixture, self).setUp()
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
class BaseTestCase(LoggingFixture):
scenarios = []
fixtures_path = None
# TestCase settings: # TestCase settings:
maxDiff = None # always dump text difference maxDiff = None # always dump text difference
longMessage = True # keep normal error message when providing our longMessage = True # keep normal error message when providing our
def setUp(self):
super(BaseTestCase, self).setUp()
self.useFixture(fixtures.FakeLogger(level=logging.DEBUG))
def _read_utf8_content(self): def _read_utf8_content(self):
# if None assume empty file # if None assume empty file
if self.out_filename is None: if self.out_filename is None:
@ -137,6 +133,12 @@ class BaseTestCase(LoggingFixture):
return jjb_config return jjb_config
class BaseScenariosTestCase(testscenarios.TestWithScenarios, BaseTestCase):
scenarios = []
fixtures_path = None
def test_yaml_snippet(self): def test_yaml_snippet(self):
if not self.in_filename: if not self.in_filename:
return return
@ -192,7 +194,8 @@ class BaseTestCase(LoggingFixture):
) )
class SingleJobTestCase(BaseTestCase): class SingleJobTestCase(BaseScenariosTestCase):
def test_yaml_snippet(self): def test_yaml_snippet(self):
config = self._get_config() config = self._get_config()
@ -223,7 +226,7 @@ class SingleJobTestCase(BaseTestCase):
) )
class JsonTestCase(BaseTestCase): class JsonTestCase(BaseScenariosTestCase):
def test_yaml_snippet(self): def test_yaml_snippet(self):
expected_json = self._read_utf8_content() expected_json = self._read_utf8_content()
@ -240,7 +243,7 @@ class JsonTestCase(BaseTestCase):
) )
class YamlTestCase(BaseTestCase): class YamlTestCase(BaseScenariosTestCase):
def test_yaml_snippet(self): def test_yaml_snippet(self):
expected_yaml = self._read_utf8_content() expected_yaml = self._read_utf8_content()

View File

@ -16,14 +16,12 @@
from jenkins_jobs.config import JJBConfig from jenkins_jobs.config import JJBConfig
import jenkins_jobs.builder import jenkins_jobs.builder
from tests.base import LoggingFixture from tests import base
from tests.base import mock from tests.base import mock
from testtools import TestCase
@mock.patch('jenkins_jobs.builder.JobCache', mock.MagicMock) @mock.patch('jenkins_jobs.builder.JobCache', mock.MagicMock)
class TestCaseTestBuilder(LoggingFixture, TestCase): class TestCaseTestBuilder(base.BaseTestCase):
def setUp(self): def setUp(self):
super(TestCaseTestBuilder, self).setUp() super(TestCaseTestBuilder, self).setUp()
jjb_config = JJBConfig() jjb_config = JJBConfig()

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import builders from jenkins_jobs.modules import builders
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleBuilders(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleBuilders(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = builders.Builders klass = builders.Builders

View File

@ -14,14 +14,13 @@
# under the License. # under the License.
import os import os
import testtools
import jenkins_jobs import jenkins_jobs
from tests.base import LoggingFixture from tests import base
from tests.base import mock from tests.base import mock
class TestCaseJobCache(LoggingFixture, testtools.TestCase): class TestCaseJobCache(base.BaseTestCase):
@mock.patch('jenkins_jobs.builder.JobCache.get_cache_dir', @mock.patch('jenkins_jobs.builder.JobCache.get_cache_dir',
lambda x: '/bad/file') lambda x: '/bad/file')

View File

@ -1,12 +1,11 @@
import os import os
import testtools
from jenkins_jobs.cli import entry from jenkins_jobs.cli import entry
from tests.base import LoggingFixture from tests import base
from tests.base import mock from tests.base import mock
class CmdTestsBase(LoggingFixture, testtools.TestCase): class CmdTestsBase(base.BaseTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')

View File

@ -15,20 +15,16 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import ExpectedException from testtools import ExpectedException
from testtools import TestCase
from jenkins_jobs.errors import JenkinsJobsException from jenkins_jobs.errors import JenkinsJobsException
from tests.base import get_scenarios from tests import base
from tests.base import mock from tests.base import mock
from tests.base import SingleJobTestCase
class TestCaseModuleDuplicates(TestWithScenarios, class TestCaseModuleDuplicates(base.SingleJobTestCase):
SingleJobTestCase, TestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
@mock.patch('jenkins_jobs.builder.logger', autospec=True) @mock.patch('jenkins_jobs.builder.logger', autospec=True)
def test_yaml_snippet(self, mock_logger): def test_yaml_snippet(self, mock_logger):

View File

@ -1,8 +1,7 @@
from testtools import ExpectedException from testtools import ExpectedException
from testtools import TestCase
from jenkins_jobs import errors from jenkins_jobs import errors
from tests.base import LoggingFixture from tests import base
def dispatch(exc, *args): def dispatch(exc, *args):
@ -22,7 +21,7 @@ def gen_xml(exc, *args):
raise exc(*args) raise exc(*args)
class TestInvalidAttributeError(LoggingFixture, TestCase): class TestInvalidAttributeError(base.BaseTestCase):
def test_no_valid_values(self): def test_no_valid_values(self):
# When given no valid values, InvalidAttributeError simply displays a # When given no valid values, InvalidAttributeError simply displays a
@ -49,7 +48,7 @@ class TestInvalidAttributeError(LoggingFixture, TestCase):
valid_values) valid_values)
class TestMissingAttributeError(LoggingFixture, TestCase): class TestMissingAttributeError(base.BaseTestCase):
def test_with_single_missing_attribute(self): def test_with_single_missing_attribute(self):
# When passed a single missing attribute, display a message indicating # When passed a single missing attribute, display a message indicating

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import general from jenkins_jobs.modules import general
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleGeneral(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleGeneral(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = general.General klass = general.General

View File

@ -14,15 +14,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import hipchat_notif from jenkins_jobs.modules import hipchat_notif
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModulePublishers(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModulePublishers(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = hipchat_notif.HipChat klass = hipchat_notif.HipChat

View File

@ -17,14 +17,9 @@
import os import os
from testscenarios.testcase import TestWithScenarios from tests import base
from testtools import TestCase
from tests.base import get_scenarios
from tests.base import SingleJobTestCase
class TestCaseModuleJsonParser(TestWithScenarios, class TestCaseModuleJsonParser(base.SingleJobTestCase):
SingleJobTestCase, TestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path, in_ext='json', out_ext='xml') scenarios = base.get_scenarios(fixtures_path, in_ext='json', out_ext='xml')

View File

@ -16,31 +16,26 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import ExpectedException from testtools import ExpectedException
from testtools import TestCase
from yaml.composer import ComposerError from yaml.composer import ComposerError
from jenkins_jobs.config import JJBConfig from jenkins_jobs.config import JJBConfig
from jenkins_jobs.parser import YamlParser from jenkins_jobs.parser import YamlParser
from tests.base import get_scenarios from tests import base
from tests.base import JsonTestCase
from tests.base import LoggingFixture
from tests.base import YamlTestCase
def _exclude_scenarios(input_filename): def _exclude_scenarios(input_filename):
return os.path.basename(input_filename).startswith("custom_") return os.path.basename(input_filename).startswith("custom_")
class TestCaseLocalYamlInclude(TestWithScenarios, JsonTestCase, TestCase): class TestCaseLocalYamlInclude(base.JsonTestCase):
""" """
Verify application specific tags independently of any changes to Verify application specific tags independently of any changes to
modules XML parsing behaviour modules XML parsing behaviour
""" """
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path, 'yaml', 'json', scenarios = base.get_scenarios(fixtures_path, 'yaml', 'json',
filter_func=_exclude_scenarios) filter_func=_exclude_scenarios)
def test_yaml_snippet(self): def test_yaml_snippet(self):
@ -52,16 +47,16 @@ class TestCaseLocalYamlInclude(TestWithScenarios, JsonTestCase, TestCase):
super(TestCaseLocalYamlInclude, self).test_yaml_snippet() super(TestCaseLocalYamlInclude, self).test_yaml_snippet()
class TestCaseLocalYamlAnchorAlias(TestWithScenarios, YamlTestCase, TestCase): class TestCaseLocalYamlAnchorAlias(base.YamlTestCase):
""" """
Verify yaml input is expanded to the expected yaml output when using yaml Verify yaml input is expanded to the expected yaml output when using yaml
anchors and aliases. anchors and aliases.
""" """
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path, 'iyaml', 'oyaml') scenarios = base.get_scenarios(fixtures_path, 'iyaml', 'oyaml')
class TestCaseLocalYamlIncludeAnchors(LoggingFixture, TestCase): class TestCaseLocalYamlIncludeAnchors(base.BaseTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')

View File

@ -17,13 +17,9 @@
import os import os
from testscenarios.testcase import TestWithScenarios from tests import base
from testtools import TestCase
from tests.base import get_scenarios
from tests.base import SingleJobTestCase
class TestCaseModuleSCMMacro(TestWithScenarios, SingleJobTestCase, TestCase): class TestCaseModuleSCMMacro(base.SingleJobTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)

View File

@ -1,18 +1,15 @@
import pkg_resources import pkg_resources
from testscenarios.testcase import TestWithScenarios
import testtools as tt
from testtools.content import text_content from testtools.content import text_content
import testscenarios
from jenkins_jobs.config import JJBConfig from jenkins_jobs.config import JJBConfig
from jenkins_jobs.registry import ModuleRegistry from jenkins_jobs.registry import ModuleRegistry
from tests.base import LoggingFixture from tests import base
class ModuleRegistryPluginInfoTestsWithScenarios(TestWithScenarios, class ModuleRegistryPluginInfoTestsWithScenarios(
LoggingFixture, testscenarios.TestWithScenarios, base.BaseTestCase):
tt.TestCase):
scenarios = [ scenarios = [
('s1', dict(v1='1.0.0', op='__gt__', v2='0.8.0')), ('s1', dict(v1='1.0.0', op='__gt__', v2='0.8.0')),
('s2', dict(v1='1.0.1alpha', op='__gt__', v2='1.0.0')), ('s2', dict(v1='1.0.1alpha', op='__gt__', v2='1.0.0')),

View File

@ -13,7 +13,6 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import testtools
from testtools.matchers import Equals from testtools.matchers import Equals
import xml.etree.ElementTree as XML import xml.etree.ElementTree as XML
import yaml import yaml
@ -21,10 +20,10 @@ import yaml
from jenkins_jobs.errors import InvalidAttributeError from jenkins_jobs.errors import InvalidAttributeError
from jenkins_jobs.errors import MissingAttributeError from jenkins_jobs.errors import MissingAttributeError
from jenkins_jobs.modules.helpers import convert_mapping_to_xml from jenkins_jobs.modules.helpers import convert_mapping_to_xml
from tests.base import LoggingFixture from tests import base
class TestCaseTestHelpers(LoggingFixture, testtools.TestCase): class TestCaseTestHelpers(base.BaseTestCase):
def test_convert_mapping_to_xml(self): def test_convert_mapping_to_xml(self):
""" """

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import notifications from jenkins_jobs.modules import notifications
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleNotifications(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleNotifications(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = notifications.Notifications klass = notifications.Notifications

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import parameters from jenkins_jobs.modules import parameters
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleParameters(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleParameters(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = parameters.Parameters klass = parameters.Parameters

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import properties from jenkins_jobs.modules import properties
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleProperties(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleProperties(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = properties.Properties klass = properties.Properties

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import publishers from jenkins_jobs.modules import publishers
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModulePublishers(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModulePublishers(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = publishers.Publishers klass = publishers.Publishers

View File

@ -16,15 +16,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import reporters from jenkins_jobs.modules import reporters
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleReporters(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleReporters(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = reporters.Reporters klass = reporters.Reporters

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import scm from jenkins_jobs.modules import scm
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleSCM(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleSCM(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = scm.SCM klass = scm.SCM

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import triggers from jenkins_jobs.modules import triggers
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleTriggers(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleTriggers(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = triggers.Triggers klass = triggers.Triggers

View File

@ -17,15 +17,11 @@
import os import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import wrappers from jenkins_jobs.modules import wrappers
from tests.base import BaseTestCase from tests import base
from tests.base import get_scenarios
class TestCaseModuleWrappers(TestWithScenarios, BaseTestCase, TestCase): class TestCaseModuleWrappers(base.BaseScenariosTestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)
klass = wrappers.Wrappers klass = wrappers.Wrappers

View File

@ -17,14 +17,9 @@
import os import os
from testscenarios.testcase import TestWithScenarios from tests import base
from testtools import TestCase
from tests.base import get_scenarios
from tests.base import SingleJobTestCase
class TestCaseModuleYamlInclude(TestWithScenarios, class TestCaseModuleYamlInclude(base.SingleJobTestCase):
SingleJobTestCase, TestCase):
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures') fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path) scenarios = base.get_scenarios(fixtures_path)