Created
September 16, 2012 07:12
-
-
Save erikzaadi/3731389 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The magic attribute
__test__
isn't working for me :(