Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.