我们从Python开源项目中,提取了以下21个代码示例,用于说明如何使用unittest.BaseTestSuite()。
def set_timeout(testsuite, seconds=None): """ add timout to test case if it didn't have one, @param testsuite testsuite form loader() @param seconds: timeout seconds @return: updated testsuite """ def _testset(testsuite): """interate tcs in testsuite""" for each in testsuite: if not isinstance(each, unittest.BaseTestSuite): yield each else: for each2 in _testset(each): yield each2 if seconds: for tc in _testset(testsuite): assert hasattr(tc, "_testMethodName"), \ "%s is not an unittest.TestCase object" testMethod = getattr(tc, tc._testMethodName) test_func = testMethod.im_func if not hastimeout(test_func): tc.run = timeout(seconds)(tc.run) return testsuite
def assert_garbage_collect_test_after_run(self, TestSuiteClass): if not unittest.BaseTestSuite._cleanup: raise unittest.SkipTest("Suite cleanup is disabled") class Foo(unittest.TestCase): def test_nothing(self): pass test = Foo('test_nothing') wref = weakref.ref(test) suite = TestSuiteClass([wref()]) suite.run(unittest.TestResult()) del test # for the benefit of non-reference counting implementations gc.collect() self.assertEqual(suite._tests, [None]) self.assertIsNone(wref())
def injectTestCase(self, test_suites): for test in test_suites: if isinstance(test, unittest.BaseTestSuite): self.injectTestCase(test) else: # inject testclient and logger into each unittest setattr(test, "debug", self.__tcRunLogger.debug) setattr(test, "info", self.__tcRunLogger.info) setattr(test, "warn", self.__tcRunLogger.warning) setattr(test, "error", self.__tcRunLogger.error) setattr(test, "clstestclient", self.__testClient) setattr(test, "testClient", self.__testClient) setattr(test, "config", self.__parsedConfig) if hasattr(test, "user"): # when the class-level attr applied. all test runs as # 'user' self.__testClient.getUserApiClient(test.UserName, test.DomainName, test.AcctType)
def test_basetestsuite(self): class Test(unittest.TestCase): wasSetUp = False wasTornDown = False @classmethod def setUpClass(cls): cls.wasSetUp = True @classmethod def tearDownClass(cls): cls.wasTornDown = True def testPass(self): pass def testFail(self): fail class Module(object): wasSetUp = False wasTornDown = False @staticmethod def setUpModule(): Module.wasSetUp = True @staticmethod def tearDownModule(): Module.wasTornDown = True Test.__module__ = 'Module' sys.modules['Module'] = Module self.addCleanup(sys.modules.pop, 'Module') suite = unittest.BaseTestSuite() suite.addTests([Test('testPass'), Test('testFail')]) self.assertEqual(suite.countTestCases(), 2) result = unittest.TestResult() suite.run(result) self.assertFalse(Module.wasSetUp) self.assertFalse(Module.wasTornDown) self.assertFalse(Test.wasSetUp) self.assertFalse(Test.wasTornDown) self.assertEqual(len(result.errors), 1) self.assertEqual(len(result.failures), 0) self.assertEqual(result.testsRun, 2)
def filter_tagexp(testsuite, tagexp): """filter according to true or flase of tag expression""" if not tagexp: return testsuite caselist = [] for each in testsuite: if not isinstance(each, unittest.BaseTestSuite): if checktags(each, tagexp): caselist.append(each) else: caselist.append(filter_tagexp(each, tagexp)) return testsuite.__class__(caselist)
def test_remove_test_at_index(self): if not unittest.BaseTestSuite._cleanup: raise unittest.SkipTest("Suite cleanup is disabled") suite = unittest.TestSuite() suite._tests = [1, 2, 3] suite._removeTestAtIndex(1) self.assertEqual([1, None, 3], suite._tests)
def test_remove_test_at_index_not_indexable(self): if not unittest.BaseTestSuite._cleanup: raise unittest.SkipTest("Suite cleanup is disabled") suite = unittest.TestSuite() suite._tests = None # if _removeAtIndex raises for noniterables this next line will break suite._removeTestAtIndex(2)
def test_garbage_collect_test_after_run_BaseTestSuite(self): self.assert_garbage_collect_test_after_run(unittest.BaseTestSuite)
def _initializer(self, tests: Tuple[type]=()): # Forces unittest.BaseTestSuite to filter out missing test classes in its initializer. self.unpatched_init(list(filter(lambda x: x is not None, tests)))
def test_basetestsuite(self): class Test(unittest.TestCase): wasSetUp = False wasTornDown = False @classmethod def setUpClass(cls): cls.wasSetUp = True @classmethod def tearDownClass(cls): cls.wasTornDown = True def testPass(self): pass def testFail(self): fail class Module(object): wasSetUp = False wasTornDown = False @staticmethod def setUpModule(): Module.wasSetUp = True @staticmethod def tearDownModule(): Module.wasTornDown = True Test.__module__ = 'Module' sys.modules['Module'] = Module self.addCleanup(sys.modules.pop, 'Module') suite = unittest.BaseTestSuite() suite.addTests([Test('testPass'), Test('testFail')]) self.assertEqual(suite.countTestCases(), 2) result = unittest.TestResult() suite.run(result) self.assertFalse(Module.wasSetUp) self.assertFalse(Module.wasTornDown) self.assertFalse(Test.wasSetUp) self.assertFalse(Test.wasTornDown) self.assertEqual(len(result.errors), 1) self.assertEqual(len(result.failures), 0) self.assertEqual(result.testsRun, 2) self.assertEqual(suite.countTestCases(), 2)