Skip to content

Instantly share code, notes, and snippets.

@erikzaadi
Created September 16, 2012 07:12
Show Gist options
  • Save erikzaadi/3731389 to your computer and use it in GitHub Desktop.
Save erikzaadi/3731389 to your computer and use it in GitHub Desktop.
from unittest import TestCase
class BaseTestClass(TestCase):
"""
Base test class for the two components.
"""
__test__ = False #important, makes sure tests are not run on base class
component = None
def shortDescription(self):
"""
Get's the one liner description to be displayed.
"""
doc = self._testMethodDoc
doc = doc and doc.split("\n")[0].strip() or ""
if "%(component)s" in doc:
doc = doc % {'component':self.component.__name__}
doc = "%s : %s" % (self.__class__.__name__, doc)
return doc
#implement setUp, tearDown, setUpClass and tearDownClass as per your needs
def test_something(self):
""" Test %(component)s constructor sets something. """
comp = self.component()
self.assertTrue(hasattr(comp, "something"))
def test_initialized(self):
""" Test that initialized is set. """
comp = self.component()
self.assertEquals(comp.initialized, True)
class AClass:
""" The Amazing A Class. """
def __init__(self):
""" A class constructor. """
self.something = False
self.initialized = True
class BClass:
""" The rebellious B Class. """
def __init__(self):
""" B class constructor. """
self.something = True
self.initialized = True
from base_test_class import BaseTestClass
from components import AClass, BClass
class TestA(BaseTestClass):
""" Test the A class. """
__test__ = True
component = AClass
class TestB(BaseTestClass):
""" Test the B class. """
__test__ = True
component = BClass
from components import AClass, BClass
from unittest import TestCase
class TestA(TestCase):
""" Test the A class. """
def test_something(self):
""" Test A class constructor set something. """
a = AClass()
self.assertTrue(hasattr(a, "something"))
def test_initialized(self):
""" Test that initialized is set. """
a = AClass()
self.assertEquals(a.initialized, True)
class TestB(TestCase):
""" Test the B class. """
def test_something(self):
""" Test B class constructor sets something. """
b = BClass()
self.assertTrue(hasattr(b, "something"))
def test_initialized(self):
""" Test that initialized is set. """
b = BClass()
self.assertEquals(b.initialized, True)
@skeller88
Copy link

Thanks for this. Very helpful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment