【发布时间】:2010-04-12 08:58:26
【问题描述】:
我有一个测试套件来执行冒烟测试。我的所有脚本都存储在不同的类中,但是当我尝试运行测试套件时,如果它在一个类中,我似乎无法让它工作。代码如下:(调用测试的类)
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') # This is the name of the file
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 TestInternalSanity(unittest.TestCase):
def setUp(self):
setUp script ....
def tearDown(self):
script ....
class BasicInternalSanity(TestInternalSanity):
def runTest(self):
test script ....
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)
如果我在class SmokeTests 中有def suite(),则脚本会执行,但测试不会运行,但如果我删除类,测试就会运行。我将其作为脚本运行并将变量调用到测试中。我不想通过 os.system('python tests.py') 运行测试。我希望通过我拥有的任何其他功能的类来调用测试。这需要从一个类中调用,因为我调用它的脚本是面向对象的。如果有人可以使用 Call Tests 运行代码,我将不胜感激。
这项工作的:
def suite(): #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 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')
我似乎无法让它在课堂上运行,任何人都可以看到解决方案。
谢谢
【问题讨论】:
-
请提供工作源代码。这显然不是正确复制粘贴的(注意错误的缩进)。如果你删除 SmokeTests.suite() 它根本不会运行。
-
你为什么又问这个问题? stackoverflow.com/questions/2606515/…
-
@S.Lott -- 我重写了它以使问题清晰易懂
标签: python class oop automated-tests unit-testing