我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unittest2.TestSuite()。
def _run_suite(suite): """Run tests from a unittest.TestSuite-derived class.""" if verbose: runner = unittest.TextTestRunner(sys.stdout, verbosity=2, failfast=failfast) else: runner = BasicTestRunner() result = runner.run(suite) if not result.wasSuccessful(): if len(result.errors) == 1 and not result.failures: err = result.errors[0][1] elif len(result.failures) == 1 and not result.errors: err = result.failures[0][1] else: err = "multiple errors occurred" if not verbose: err += "; run in verbose mode for details" raise TestFailed(err)
def test_skip_doesnt_run_setup(self): class Foo(unittest2.TestCase): wasSetUp = False wasTornDown = False def setUp(self): Foo.wasSetUp = True def tornDown(self): Foo.wasTornDown = True @unittest2.skip('testing') def test_1(self): pass result = unittest2.TestResult() test = Foo("test_1") suite = unittest2.TestSuite([test]) suite.run(result) self.assertEqual(result.skipped, [(test, "testing")]) self.assertFalse(Foo.wasSetUp) self.assertFalse(Foo.wasTornDown)
def test_decorated_skip(self): def decorator(func): def inner(*a): return func(*a) return inner class Foo(unittest2.TestCase): @decorator @unittest2.skip('testing') def test_1(self): pass result = unittest2.TestResult() test = Foo("test_1") suite = unittest2.TestSuite([test]) suite.run(result) self.assertEqual(result.skipped, [(test, "testing")])
def test_startTestRun_stopTestRun_called(self): class LoggingTextResult(LoggingResult): separator2 = '' def printErrors(self): pass class LoggingRunner(unittest2.TextTestRunner): def __init__(self, events): super(LoggingRunner, self).__init__(StringIO()) self._events = events def _makeResult(self): return LoggingTextResult(self._events) events = [] runner = LoggingRunner(events) runner.run(unittest2.TestSuite()) expected = ['startTestRun', 'stopTestRun'] self.assertEqual(events, expected)
def test_init__TestSuite_instances_in_tests(self): def tests(): ftc = unittest2.FunctionTestCase(lambda: None) yield unittest2.TestSuite([ftc]) yield unittest2.FunctionTestCase(lambda: None) suite = unittest2.TestSuite(tests()) self.assertEqual(suite.countTestCases(), 2) # countTestCases() still works after tests are run suite.run(unittest.TestResult()) self.assertEqual(suite.countTestCases(), 2) ################################################################ ### /Tests for TestSuite.__init__ # Container types should support the iter protocol
def test_countTestCases_nested(self): class Test1(unittest2.TestCase): def test1(self): pass def test2(self): pass test2 = unittest2.FunctionTestCase(lambda: None) test3 = unittest2.FunctionTestCase(lambda: None) child = unittest2.TestSuite((Test1('test2'), test2)) parent = unittest2.TestSuite((test3, child, Test1('test1'))) self.assertEqual(parent.countTestCases(), 4) # countTestCases() still works after tests are run parent.run(unittest.TestResult()) self.assertEqual(parent.countTestCases(), 4) self.assertEqual(child.countTestCases(), 2) # "Run the tests associated with this suite, collecting the result into # the test result object passed as result." # # And if there are no tests? What then?
def test_run(self): events = [] result = LoggingResult(events) class LoggingCase(unittest2.TestCase): def run(self, result): events.append('run %s' % self._testMethodName) def test1(self): pass def test2(self): pass tests = [LoggingCase('test1'), LoggingCase('test2')] unittest2.TestSuite(tests).run(result) self.assertEqual(events, ['run test1', 'run test2']) # "Add a TestCase ... to the suite"
def test_addTest__TestSuite(self): class Foo(unittest2.TestCase): def test(self): pass suite_2 = unittest2.TestSuite([Foo('test')]) suite = unittest2.TestSuite() suite.addTest(suite_2) self.assertEqual(suite.countTestCases(), 1) self.assertEqual(list(suite), [suite_2]) # countTestCases() still works after tests are run suite.run(unittest.TestResult()) self.assertEqual(suite.countTestCases(), 1) # "Add all the tests from an iterable of TestCase and TestSuite # instances to this test suite." # # "This is equivalent to iterating over tests, calling addTest() for # each element"
def test_loadTestsFromTestCase__no_matches(self): class Foo(unittest2.TestCase): def foo_bar(self): pass empty_suite = unittest2.TestSuite() loader = unittest2.TestLoader() self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite) # "Return a suite of all tests cases contained in the TestCase-derived # class testCaseClass" # # What happens if loadTestsFromTestCase() is given an object # that isn't a subclass of TestCase? Specifically, what happens # if testCaseClass is a subclass of TestSuite? # # This is checked for specifically in the code, so we better add a # test for it.
def test_loadTestsFromTestCase__TestSuite_subclass(self): class NotATestCase(unittest2.TestSuite): pass loader = unittest2.TestLoader() try: loader.loadTestsFromTestCase(NotATestCase) except TypeError: pass else: self.fail('Should raise TypeError') # "Return a suite of all tests cases contained in the TestCase-derived # class testCaseClass" # # Make sure loadTestsFromTestCase() picks up the default test method # name (as specified by TestCase), even though the method name does # not match the default TestLoader.testMethodPrefix string
def test_loadTestsFromModule__not_a_module(self): class MyTestCase(unittest2.TestCase): def test(self): pass class NotAModule(object): test_2 = MyTestCase loader = unittest2.TestLoader() suite = loader.loadTestsFromModule(NotAModule) reference = [unittest2.TestSuite([MyTestCase('test')])] self.assertEqual(list(suite), reference) # Check that loadTestsFromModule honors (or not) a module # with a load_tests function.
def test_loadTestsFromModule__load_tests(self): m = types.ModuleType('m') class MyTestCase(unittest2.TestCase): def test(self): pass m.testcase_1 = MyTestCase load_tests_args = [] def load_tests(loader, tests, pattern): self.assertIsInstance(tests, unittest2.TestSuite) load_tests_args.extend((loader, tests, pattern)) return tests m.load_tests = load_tests loader = unittest2.TestLoader() suite = loader.loadTestsFromModule(m) self.assertIsInstance(suite, unittest2.TestSuite) self.assertEqual(load_tests_args, [loader, suite, None]) # With Python 3.5, the undocumented and unofficial use_load_tests is # ignored (and deprecated). load_tests_args = [] with warnings.catch_warnings(record=False): warnings.simplefilter('never') suite = loader.loadTestsFromModule(m, use_load_tests=False) self.assertEqual(load_tests_args, [loader, suite, None])
def test_loadTestsFromModule__use_load_tests_other_bad_keyword(self): m = types.ModuleType('m') class MyTestCase(unittest.TestCase): def test(self): pass m.testcase_1 = MyTestCase load_tests_args = [] def load_tests(loader, tests, pattern): self.assertIsInstance(tests, unittest.TestSuite) load_tests_args.extend((loader, tests, pattern)) return tests m.load_tests = load_tests loader = unittest.TestLoader() with warnings.catch_warnings(): warnings.simplefilter('never') with self.assertRaises(TypeError) as cm: loader.loadTestsFromModule( m, use_load_tests=False, very_bad=True, worse=False) self.assertEqual(type(cm.exception), TypeError) # The error message names the first bad argument alphabetically, # however use_load_tests (which sorts first) is ignored. self.assertEqual( str(cm.exception), "loadTestsFromModule() got an unexpected keyword argument 'very_bad'")
def test_loadTestsFromModule__pattern(self): m = types.ModuleType('m') class MyTestCase(unittest.TestCase): def test(self): pass m.testcase_1 = MyTestCase load_tests_args = [] def load_tests(loader, tests, pattern): self.assertIsInstance(tests, unittest.TestSuite) load_tests_args.extend((loader, tests, pattern)) return tests m.load_tests = load_tests loader = unittest.TestLoader() suite = loader.loadTestsFromModule(m, pattern='testme.*') self.assertIsInstance(suite, unittest.TestSuite) self.assertEqual(load_tests_args, [loader, suite, 'testme.*'])
def test_loadTestsFromName__empty_name(self): loader = unittest2.TestLoader() try: loader.loadTestsFromName('') except ValueError: e = sys.exc_info()[1] self.assertEqual(str(e), "Empty module name") else: self.fail("TestLoader.loadTestsFromName failed to raise ValueError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # What happens when the name contains invalid characters?
def test_loadTestsFromName__relative_unknown_name(self): loader = unittest2.TestLoader() suite = loader.loadTestsFromName('sdasfasfasdf', unittest) error, test = self.check_deferred_error(loader, suite) self.check_module_lookup_error(error, test, 'unittest2') # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # Does loadTestsFromName raise ValueError when passed an empty # name relative to a provided module? # # XXX Should probably raise a ValueError instead of an AttributeError
def test_loadTestsFromName__relative_empty_name(self): loader = unittest.TestLoader() suite = loader.loadTestsFromName('', unittest) error, test = self.check_deferred_error(loader, suite) expected = "has no attribute ''" self.assertIn( expected, error, 'missing error string in %r' % error) self.assertRaisesRegex(AttributeError, expected, getattr(test, '')) # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # What happens when an impossible name is given, relative to the provided # `module`?
def test_loadTestsFromName__relative_not_a_module(self): class MyTestCase(unittest2.TestCase): def test(self): pass class NotAModule(object): test_2 = MyTestCase loader = unittest2.TestLoader() suite = loader.loadTestsFromName('test_2', NotAModule) reference = [MyTestCase('test')] self.assertEqual(list(suite), reference) # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # Does it raise an exception if the name resolves to an invalid # object?
def test_loadTestsFromName__relative_testmethod(self): m = types.ModuleType('m') class MyTestCase(unittest2.TestCase): def test(self): pass m.testcase_1 = MyTestCase loader = unittest2.TestLoader() suite = loader.loadTestsFromName('testcase_1.test', m) self.assertIsInstance(suite, loader.suiteClass) self.assertEqual(list(suite), [MyTestCase('test')]) # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # Does loadTestsFromName() raise the proper exception when trying to # resolve "a test method within a test case class" that doesn't exist # for the given name (relative to a provided module)?
def test_loadTestsFromName__relative_invalid_testmethod(self): m = types.ModuleType('m') class MyTestCase(unittest2.TestCase): def test(self): pass m.testcase_1 = MyTestCase loader = unittest.TestLoader() suite = loader.loadTestsFromName('testcase_1.testfoo', m) expected = "type object 'MyTestCase' has no attribute 'testfoo'" error, test = self.check_deferred_error(loader, suite) self.assertIn( expected, error, 'missing error string in %r' % error) self.assertRaisesRegex(AttributeError, expected, test.testfoo) # "The specifier name is a ``dotted name'' that may resolve ... to # ... a callable object which returns a ... TestSuite instance"
def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self): class SubTestSuite(unittest2.TestSuite): pass m = types.ModuleType('m') testcase_1 = unittest2.FunctionTestCase(lambda: None) def return_TestCase(): return testcase_1 m.return_TestCase = return_TestCase loader = unittest2.TestLoader() loader.suiteClass = SubTestSuite suite = loader.loadTestsFromName('return_TestCase', m) self.assertIsInstance(suite, loader.suiteClass) self.assertEqual(list(suite), [testcase_1]) # "The specifier name is a ``dotted name'' that may resolve ... to # ... a test method within a test case class" #***************************************************************** #Override the suiteClass attribute to ensure that the suiteClass #attribute is used
def check_deferred_error(self, loader, suite): """Helper function for checking that errors in loading are reported. :param loader: A loader with some errors. :param suite: A suite that should have a late bound error. :return: The first error message from the loader and the test object from the suite. """ self.assertIsInstance(suite, unittest.TestSuite) self.assertEqual(suite.countTestCases(), 1) # Errors loading the suite are also captured for introspection. self.assertNotEqual([], loader.errors) self.assertEqual(1, len(loader.errors)) error = loader.errors[0] test = list(suite)[0] return error, test # "Similar to loadTestsFromName(), but takes a sequence of names rather # than a single name." # # What happens if that sequence of names is empty?
def test_loadTestsFromNames__empty_name(self): loader = unittest2.TestLoader() try: loader.loadTestsFromNames(['']) except ValueError: e = sys.exc_info()[1] self.assertEqual(str(e), "Empty module name") else: self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # What happens when presented with an impossible module name?
def test_loadTestsFromNames__malformed_name(self): loader = unittest2.TestLoader() # XXX Should this raise ValueError or ImportError? suite = loader.loadTestsFromNames(['abc () //']) error, test = self.check_deferred_error(loader, list(suite)[0]) expected = "Failed to import test module: abc () //" expected_regex = "Failed to import test module: abc \(\) //" self.assertIn( expected, error, 'missing error string in %r' % error) self.assertRaisesRegex( ImportError, expected_regex, getattr(test, 'abc () //')) # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # What happens when no module can be found for the given name?
def test_loadTestsFromNames__unknown_attr_name(self): loader = unittest.TestLoader() suite = loader.loadTestsFromNames( ['unittest2.loader.sdasfasfasdf', 'unittest2.test.dummy']) error, test = self.check_deferred_error(loader, list(suite)[0]) self.check_module_lookup_error(error, test, 'unittest2.loader') # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # What happens when given an unknown attribute on a specified `module` # argument?
def test_loadTestsFromNames__unknown_name_relative_1(self): loader = unittest.TestLoader() suite = loader.loadTestsFromNames(['sdasfasfasdf'], unittest) error, test = self.check_deferred_error(loader, list(suite)[0]) self.check_module_lookup_error(error, test, 'unittest2') # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # Do unknown attributes (relative to a provided module) still raise an # exception even in the presence of valid attribute names?
def test_loadTestsFromNames__unknown_name_relative_2(self): loader = unittest.TestLoader() suite = loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) error, test = self.check_deferred_error(loader, list(suite)[1]) self.check_module_lookup_error(error, test, 'unittest2') # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # What happens when faced with the empty string? # # XXX This currently raises AttributeError, though ValueError is probably # more appropriate
def test_loadTestsFromNames__relative_empty_name(self): loader = unittest2.TestLoader() suite = loader.loadTestsFromNames([''], unittest) error, test = self.check_deferred_error(loader, list(suite)[0]) expected = "has no attribute ''" self.assertIn( expected, error, 'missing error string in %r' % error) self.assertRaisesRegex(AttributeError, expected, getattr(test, '')) # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # ... # "The method optionally resolves name relative to the given module" # # What happens when presented with an impossible attribute name?
def test_loadTestsFromNames__relative_not_a_module(self): class MyTestCase(unittest2.TestCase): def test(self): pass class NotAModule(object): test_2 = MyTestCase loader = unittest2.TestLoader() suite = loader.loadTestsFromNames(['test_2'], NotAModule) reference = [unittest2.TestSuite([MyTestCase('test')])] self.assertEqual(list(suite), reference) # "The specifier name is a ``dotted name'' that may resolve either to # a module, a test case class, a TestSuite instance, a test method # within a test case class, or a callable object which returns a # TestCase or TestSuite instance." # # Does it raise an exception if the name resolves to an invalid # object?
def test_loadTestsFromNames__relative_testmethod(self): m = types.ModuleType('m') class MyTestCase(unittest2.TestCase): def test(self): pass m.testcase_1 = MyTestCase loader = unittest2.TestLoader() suite = loader.loadTestsFromNames(['testcase_1.test'], m) self.assertIsInstance(suite, loader.suiteClass) ref_suite = unittest2.TestSuite([MyTestCase('test')]) self.assertEqual(list(suite), [ref_suite]) # #14971: Make sure the dotted name resolution works even if the actual # function doesn't have the same name as is used to find it.
def test_loadTestsFromName__function_with_different_name_than_method(self): # lambdas have the name '<lambda>'. m = types.ModuleType('m') class MyTestCase(unittest.TestCase): test = lambda: 1 m.testcase_1 = MyTestCase loader = unittest.TestLoader() suite = loader.loadTestsFromNames(['testcase_1.test'], m) self.assertIsInstance(suite, loader.suiteClass) ref_suite = unittest.TestSuite([MyTestCase('test')]) self.assertEqual(list(suite), [ref_suite]) # "The specifier name is a ``dotted name'' that may resolve ... to ... a # test method within a test case class" # # Does the method gracefully handle names that initially look like they # resolve to "a test method within a test case class" but don't?
def test_loadTestsFromNames__relative_invalid_testmethod(self): m = types.ModuleType('m') class MyTestCase(unittest2.TestCase): def test(self): pass m.testcase_1 = MyTestCase loader = unittest.TestLoader() suite = loader.loadTestsFromNames(['testcase_1.testfoo'], m) error, test = self.check_deferred_error(loader, list(suite)[0]) expected = "type object 'MyTestCase' has no attribute 'testfoo'" self.assertIn( expected, error, 'missing error string in %r' % error) self.assertRaisesRegex(AttributeError, expected, test.testfoo) # "The specifier name is a ``dotted name'' that may resolve ... to # ... a callable object which returns a ... TestSuite instance"
def test_loadTestsFromNames__callable__TestSuite(self): m = types.ModuleType('m') testcase_1 = unittest2.FunctionTestCase(lambda: None) testcase_2 = unittest2.FunctionTestCase(lambda: None) def return_TestSuite(): return unittest2.TestSuite([testcase_1, testcase_2]) m.return_TestSuite = return_TestSuite loader = unittest2.TestLoader() suite = loader.loadTestsFromNames(['return_TestSuite'], m) self.assertIsInstance(suite, loader.suiteClass) expected = unittest2.TestSuite([testcase_1, testcase_2]) self.assertEqual(list(suite), [expected]) # "The specifier name is a ``dotted name'' that may resolve ... to # ... a callable object which returns a TestCase ... instance"
def test_loadTestsFromNames__callable__call_staticmethod(self): m = types.ModuleType('m') class Test1(unittest2.TestCase): def test(self): pass testcase_1 = Test1('test') class Foo(unittest2.TestCase): @staticmethod def foo(): return testcase_1 m.Foo = Foo loader = unittest2.TestLoader() suite = loader.loadTestsFromNames(['Foo.foo'], m) self.assertIsInstance(suite, loader.suiteClass) ref_suite = unittest2.TestSuite([testcase_1]) self.assertEqual(list(suite), [ref_suite]) # "The specifier name is a ``dotted name'' that may resolve ... to # ... a callable object which returns a TestCase or TestSuite instance" # # What happens when the callable returns something else?
def test_testMethodPrefix__loadTestsFromTestCase(self): class Foo(unittest2.TestCase): def test_1(self): pass def test_2(self): pass def foo_bar(self): pass tests_1 = unittest2.TestSuite([Foo('foo_bar')]) tests_2 = unittest2.TestSuite([Foo('test_1'), Foo('test_2')]) loader = unittest2.TestLoader() loader.testMethodPrefix = 'foo' self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1) loader.testMethodPrefix = 'test' self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2) # "String giving the prefix of method names which will be interpreted as # test methods" # # Implicit in the documentation is that testMethodPrefix is respected by # all loadTestsFrom* methods.
def test_testMethodPrefix__loadTestsFromModule(self): m = types.ModuleType('m') class Foo(unittest2.TestCase): def test_1(self): pass def test_2(self): pass def foo_bar(self): pass m.Foo = Foo tests_1 = [unittest2.TestSuite([Foo('foo_bar')])] tests_2 = [unittest2.TestSuite([Foo('test_1'), Foo('test_2')])] loader = unittest2.TestLoader() loader.testMethodPrefix = 'foo' self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1) loader.testMethodPrefix = 'test' self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2) # "String giving the prefix of method names which will be interpreted as # test methods" # # Implicit in the documentation is that testMethodPrefix is respected by # all loadTestsFrom* methods.
def test_testMethodPrefix__loadTestsFromName(self): m = types.ModuleType('m') class Foo(unittest2.TestCase): def test_1(self): pass def test_2(self): pass def foo_bar(self): pass m.Foo = Foo tests_1 = unittest2.TestSuite([Foo('foo_bar')]) tests_2 = unittest2.TestSuite([Foo('test_1'), Foo('test_2')]) loader = unittest2.TestLoader() loader.testMethodPrefix = 'foo' self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1) loader.testMethodPrefix = 'test' self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2) # "String giving the prefix of method names which will be interpreted as # test methods" # # Implicit in the documentation is that testMethodPrefix is respected by # all loadTestsFrom* methods.
def loadTests(self, namespace): """ Load L{csb.test.Case}s from the given CSB C{namespace}. If the namespace ends with a wildcard, tests from sub-packages will be loaded as well. If the namespace is '__main__' or '.', tests are loaded from __main__. @param namespace: test module namespace, e.g. 'csb.test.cases.bio' will load tests from '/csb/test/cases/bio/__init__.py' @type namespace: str @return: a C{unittest.TestSuite} ready for the test runner @rtype: C{unittest.TestSuite} """ if namespace.strip() == '.*': namespace = '__main__.*' elif namespace.strip() == '.': namespace = '__main__' if namespace.endswith('.*'): return self.loadAllTests(namespace[:-2]) else: loader = unittest.TestLoader() tests = loader.loadTestsFromName(namespace) return unittest.TestSuite(self._filter(tests))
def loadMultipleTests(self, namespaces): """ Load L{csb.test.Case}s from a list of given CSB C{namespaces}. @param namespaces: a list of test module namespaces, e.g. ('csb.test.cases.bio', 'csb.test.cases.bio.io') will load tests from '/csb/test/cases/bio.py' and '/csb/test/cases/bio/io.py' @type namespaces: tuple of str @return: a C{unittest.TestSuite} ready for the test runner @rtype: C{unittest.TestSuite} """ if not csb.core.iterable(namespaces): raise TypeError(namespaces) return unittest.TestSuite(self.loadTests(n) for n in namespaces)
def loadTests(self, namespace): if namespace.strip() == '.*': namespace = '__main__.*' elif namespace.strip() == '.': namespace = '__main__' if namespace.endswith('.*'): return self.loadAllTests(namespace[:-2]) else: try: mod = __import__(namespace, fromlist=['']) except ImportError: raise InvalidNamespaceError('Namespace {0} is not importable'.format(namespace)) suites = self._inspect(mod) return unittest.TestSuite(suites)
def _filter(self, factories): """ Filter a list of objects using C{self.labels}. """ filtered = [] for obj in factories: for label in self.labels: if hasattr(obj, label) and getattr(obj, label) is True: suite = obj() if not isinstance(suite, unittest.TestSuite): raise ValueError('Custom test function {0} must return a ' 'unittest.TestSuite, not {1}'.format(obj.__name__, type(suite))) filtered.append(suite) return filtered
def custom(function): """ A function decorator, used to mark functions which build custom (dynamic) test suites when called. @param function: a callable object, which returns a dynamically compiled C{unittest.TestSuite} @type function: callable """ if isinstance(function, type): raise TypeError("Can't apply function decorator on a class") elif not hasattr(function, '__call__'): raise TypeError("Can't apply function decorator on non-callable {0}".format(type(function))) setattr(function, Attributes.CUSTOM, True) return function
def exclude_tests(suite, blacklist): """ Example: blacklist = [ 'test_some_test_that_should_be_skipped', 'test_another_test_that_should_be_skipped' ] """ new_suite = unittest.TestSuite() for test_group in suite._tests: for test in test_group: if not hasattr(test, '_tests'): # e.g. ModuleImportFailure new_suite.addTest(test) continue for subtest in test._tests: method = subtest._testMethodName if method in blacklist: setattr(test, method, getattr(SkipCase(), 'skeleton_run_test')) new_suite.addTest(test) return new_suite
def suite(): """The global test suite. """ logging.basicConfig(level=logging.DEBUG) mysuite = unittest.TestSuite() mysuite.addTests(test_scene.suite()) mysuite.addTests(test_dataset.suite()) mysuite.addTests(test_writers.suite()) mysuite.addTests(test_readers.suite()) mysuite.addTests(test_resample.suite()) mysuite.addTests(test_yaml_reader.suite()) mysuite.addTests(test_helper_functions.suite()) mysuite.addTests(reader_tests.suite()) mysuite.addTests(writer_tests.suite()) mysuite.addTests(test_file_handlers.suite()) mysuite.addTests(test_utils.suite()) mysuite.addTests(test_enhancements.suite()) return mysuite