python - 在类中运行单元测试

标签 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) 

如果我在 class SmokeTests 中有 def suite() ,脚本就会执行,但测试不会运行,但如果我删除该类,测试就会运行。我将其作为脚本运行并将变量调用到测试中。我不想通过 os.system('python tests.py') 运行测试。我希望像任何其他函数一样通过我拥有的类来调用测试。这需要从类中调用,因为我调用它的脚本是面向对象的。如果任何人都可以使用调用测试来运行代码,我将非常感激。

这项工作:

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

我似乎无法让它在类里面运行,任何人都可以看到解决方案。

谢谢

最佳答案

成功了,抱歉浪费了大家的时间,答案是更改默认的测试名称。

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

感谢您的帮助。

关于python - 在类中运行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2620837/

相关文章:

python - Pip 指向哪个 PyPI 索引?

python - 将 ColumnTransformer() 结果附加到管道中的原始数据?

python - 如何从 Python 调用 Go 函数

java - 为什么 Java 对象类在转换后保持不变?

Python:使用正则表达式查找最后一对出现的情况

arrays - 如何循环遍历不同类型的类数组并快速打印它们的属性?

python - 多继承python问题

基于PHP OOP的登录系统

oop - CakePHP 中使用的设计模式

javascript - 有没有办法通过 "undefine"实例方法来揭示原型(prototype)中定义的方法?