Skip to content

Instantly share code, notes, and snippets.

@cjerdonek
Created May 16, 2012 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjerdonek/2707352 to your computer and use it in GitHub Desktop.
Save cjerdonek/2707352 to your computer and use it in GitHub Desktop.
In Python, add extra tests to a unittest.main() test run (without depending on global state).
import unittest
def run_tests(data):
extra_tests = create_extra_tests(data)
test_program_class = make_test_program_class(extra_tests)
# In this constructor call you can include any of the arguments supported
# by unittest.main().
test_program_class() # runs tests.
def make_test_program_class(extra_tests):
"""
Return a unittest.TestProgram subclass that adds a list of custom tests.
Arguments:
extra_tests: an iterable of TestCase and TestSuite instances to add in
addition to the usual tests loaded when calling createTests().
cf. http://docs.python.org/py3k/library/unittest.html#unittest.TestSuite.addTests
"""
class MyTestProgram(unittest.TestProgram):
def createTests(self):
super(MyTestProgram, self).createTests()
self.test.addTests(extra_tests)
return MyTestProgram
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment