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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,20 +15,16 @@
import os
from testscenarios.testcase import TestWithScenarios
from testtools import ExpectedException
from testtools import TestCase
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 SingleJobTestCase
class TestCaseModuleDuplicates(TestWithScenarios,
SingleJobTestCase, TestCase):
class TestCaseModuleDuplicates(base.SingleJobTestCase):
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)
def test_yaml_snippet(self, mock_logger):

View File

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

View File

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

View File

@ -14,15 +14,11 @@
import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from jenkins_jobs.modules import hipchat_notif
from tests.base import BaseTestCase
from tests.base import get_scenarios
from tests import base
class TestCaseModulePublishers(TestWithScenarios, BaseTestCase, TestCase):
class TestCaseModulePublishers(base.BaseScenariosTestCase):
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

View File

@ -17,14 +17,9 @@
import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from tests.base import get_scenarios
from tests.base import SingleJobTestCase
from tests import base
class TestCaseModuleJsonParser(TestWithScenarios,
SingleJobTestCase, TestCase):
class TestCaseModuleJsonParser(base.SingleJobTestCase):
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
from testscenarios.testcase import TestWithScenarios
from testtools import ExpectedException
from testtools import TestCase
from yaml.composer import ComposerError
from jenkins_jobs.config import JJBConfig
from jenkins_jobs.parser import YamlParser
from tests.base import get_scenarios
from tests.base import JsonTestCase
from tests.base import LoggingFixture
from tests.base import YamlTestCase
from tests import base
def _exclude_scenarios(input_filename):
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
modules XML parsing behaviour
"""
fixtures_path = os.path.join(os.path.dirname(__file__), 'fixtures')
scenarios = get_scenarios(fixtures_path, 'yaml', 'json',
filter_func=_exclude_scenarios)
scenarios = base.get_scenarios(fixtures_path, 'yaml', 'json',
filter_func=_exclude_scenarios)
def test_yaml_snippet(self):
@ -52,16 +47,16 @@ class TestCaseLocalYamlInclude(TestWithScenarios, JsonTestCase, TestCase):
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
anchors and aliases.
"""
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')

View File

@ -17,13 +17,9 @@
import os
from testscenarios.testcase import TestWithScenarios
from testtools import TestCase
from tests.base import get_scenarios
from tests.base import SingleJobTestCase
from tests import base
class TestCaseModuleSCMMacro(TestWithScenarios, SingleJobTestCase, TestCase):
class TestCaseModuleSCMMacro(base.SingleJobTestCase):
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
from testscenarios.testcase import TestWithScenarios
import testtools as tt
from testtools.content import text_content
import testscenarios
from jenkins_jobs.config import JJBConfig
from jenkins_jobs.registry import ModuleRegistry
from tests.base import LoggingFixture
from tests import base
class ModuleRegistryPluginInfoTestsWithScenarios(TestWithScenarios,
LoggingFixture,
tt.TestCase):
class ModuleRegistryPluginInfoTestsWithScenarios(
testscenarios.TestWithScenarios, base.BaseTestCase):
scenarios = [
('s1', dict(v1='1.0.0', op='__gt__', v2='0.8.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
# under the License.
import testtools
from testtools.matchers import Equals
import xml.etree.ElementTree as XML
import yaml
@ -21,10 +20,10 @@ import yaml
from jenkins_jobs.errors import InvalidAttributeError
from jenkins_jobs.errors import MissingAttributeError
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):
"""

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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