上面的模块很好,但是在尝试输入参数时,NoseTests可能很有趣,而且速度更快,可以很好地适合其他模块。
import os, unittestclass Tests(): def suite(self): #Function stores all the modules to be tested modules_to_test = [] test_dir = os.listdir('.') for test in test_dir: if test.startswith('test') and test.endswith('.py'): modules_to_test.append(test.rstrip('.py')) alltests = unittest.TestSuite() for module in map(__import__, modules_to_test): module.testvars = ["variables you want to pass through"] alltests.addTest(unittest.findTestCases(module)) return alltestsif __name__ == '__main__': MyTests = Tests() unittest.main(defaultTest='MyTests.suite')
如果要将结果添加到日志文件中,请在末尾添加:
if __name__ == '__main__': MyTests = Tests() log_file = 'log_file.txt' f = open(log_file, "w") runner = unittest.TextTestRunner(f) unittest.main(defaultTest='MyTests.suite', testRunner=runner)
同样,在模块的底部,您正在测试放置代码,如下所示:
class SomeTestSuite(unittest.TestSuite): # Tests to be tested by test suite def makeRemoveAudioSource(): suite = unittest.TestSuite() suite.AddTest(TestSomething("TestSomeClass")) return suite def suite(): return unittest.makeSuite(TestSomething)if __name__ == '__main__': unittest.main()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)