问题是运行unittest测试套件OO

时间:2010-04-09 10:08:34

标签: python class unit-testing

我有一个测试套件来执行烟雾测试。我将所有脚本存储在各种类中,但是当我尝试运行测试套件时,如果它在类中,我似乎无法使其工作。代码如下:(一个调用测试的类)

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')

此输出是错误的:     属性错误:'module'对象没有属性'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')运行测试。我希望通过我喜欢任何其他功能的课程来调用测试

任何人都可以帮助我开始运行吗?

感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:3)

我知道这不是答案,但我建议使用可以使用测试发现的库,比如Python 2.7 +中的nose或unittest功能。

可以做到

nosetests module.submodule

nosetests module.submodule:TestCase.test_method

无价之宝:)

答案 1 :(得分:1)

这不起作用:

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')

此输出是错误:属性错误:'module'对象没有属性'suite'。

您的套件SmokeTests().suite()方法的值。注意一个名为suite的变量,因为你没有这样的变量。

为您的套件使用简单的功能会更容易。

def someSuite():
    modules_to_test
    ...
    return alltests

if __name__ == "__main__":
    unittest.main( defaultTest= someSuite() )

这样的事情会更接近正确。

相关问题