在Class中运行unittest

时间:2010-04-12 08:58:26

标签: python class oop automated-tests 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') # 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) 

如果我在def suite()内有class SmokeTests脚本执行但测试没有运行,但是如果我删除了类测试运行。我将其作为脚本运行并将变量调用到测试中。我不想通过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') 

我似乎无法让这个在课堂上运行,任何人都可以看到解决方案。

由于

2 个答案:

答案 0 :(得分:2)

让它工作,抱歉浪费每个人的时间,答案是更改默认的测试名称。

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__':
    Smoke = SmokeTests()  
    unittest.main(defaultTest='Smoke.suite') 

感谢您的帮助。

答案 1 :(得分:1)

看起来你正在使单元测试比它们实际上复杂得多。也许您的实现看起来应该更像这样:

import unittest

class MyClass(object):

    def add(self, val, other):
        return val + other

    def subtract(self, val, other):
        return val - other


class TestClass(unittest.TestCase):

    def test_add(self):
        myclass = MyClass()
        self.assert_(myclass.add(1, 2) == 3)

    def test_subtract(self):
        myclass = MyClass()
        self.assert_(myclass.subtract(2, 1) == 1)


if __name__ == '__main__':
    unittest.main()
相关问题