我有一个测试套件来执行冒烟测试。我将所有脚本都存储在各种类中,但是当我尝试运行测试套件时,如果它在一个类中,我似乎无法让它工作。代码如下:(调用测试的类)
from alltests import SmokeTests
class CallTests(SmokeTests):
def integration(self):
self.suite()
if __name__ == '__main__':
run = CallTests()
run.integration()
和测试套件:
class SmokeTests():
def suite(self): #Function stores all the modules to be tested
modules_to_test = ('external_sanity', 'internal_sanity')
alltests = unittest.TestSuite()
for module in map(__import__, modules_to_test):
alltests.addTest(unittest.findTestCases(module))
return alltests
if __name__ == '__main__':
unittest.main(defaultTest='suite')
这个输出是一个错误: 属性错误:“模块”对象没有属性“套件”
所以我可以看到如何调用已定义的普通函数,但我发现很难在套件中调用。在其中一项测试中,套件设置如下:
class InternalSanityTestSuite(unittest.TestSuite):
# Tests to be tested by test suite
def makeInternalSanityTestSuite():
suite = unittest.TestSuite()
suite.addTest(TestInternalSanity("BasicInternalSanity"))
suite.addTest(TestInternalSanity("VerifyInternalSanityTestFail"))
return suite
def suite():
return unittest.makeSuite(TestInternalSanity)
如果我在类 SmokeTests 中有 someSuite() python 找不到属性套件,但如果我删除它工作的类。我将其作为脚本运行并将变量调用到测试中。我不想通过 os.system('python tests.py') 运行测试。我希望像任何其他函数一样通过我拥有的类调用测试
谁能帮我让它运行起来?
提前感谢您的帮助。
最佳答案
我知道这不是答案,但我建议使用可以使用测试发现的库,例如 Python 2.7+ 的 nose 或单元测试功能。
做的可能性
nosetests module.submodule
或
nosetests module.submodule:TestCase.test_method
是无价的:)
关于python - 问题的运行 unittest 测试套件 OO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2606515/