1

我有一个测试套件,我正试图让它与我创建的测试一起工作。如果我单独运行它们,但我想在一个测试套件中运行它们,测试就可以工作。下面的代码显示了创建的测试套件:

import unittest

def suite():
    modules_to_test = ('TestAbsoluteMove', 'TestContinuousMove') # and so on
    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 AbsoluteMoveTestSuite(unittest.TestSuite):

def makeAbsoluteMoveTestSuite():
    suite = unittest.TestSuite()
    suite.addTest(TestAbsoluteMove("BasicAbsolutePan"))
    suite.addTest(TestAbsoluteMove("VerifyAbsolutePan"))
    suite.addTest(TestAbsoluteMove("VerifyAbsoluteTilt"))
    suite.addTest(TestAbsoluteMove("VerifyAbsolutePanSpeed"))
    suite.addTest(TestAbsoluteMove("VerifyAbsoluteTiltSpeed"))
    return suite

def suite():
    return unittest.makeSuite(TestAbsoluteMove)

产生的错误声称没有名为“TestAbsoluteMove”和“TestContinuousMove”的模块。有谁知道如何让这段代码工作?

谢谢

4

4 回答 4

2

鼻子使这种事情变得轻而易举。它将自动检测您的测试并将它们作为一个套件运行。(你也可以通过传递一个标志来运行特定的测试。)

于 2010-01-21T16:29:28.627 回答
2

TestAbsoluteMove 是一个类,它需要来自某个地方。无论在何处定义 AbsoluteMoveTestSuite 类,都需要导入 TestAbsoluteMove。

于 2010-01-21T16:31:25.667 回答
2

这就是我创建测试套件的方式(loadTestFromTestCase 会自动检测您的测试)

def suite():
    """ returns all the testcases in this module """
    return TestLoader().loadTestsFromTestCase(AbsoluteMoveTestSuite)

并一次运行它们我有一个包含所有子套件的套件(注意所有导入,您需要先导入它们,然后才能在新模块中使用它们)

import sys
import unittest

import test.framework.asyncprocess as a
import test.framework.easyconfig as e
import test.framework.modulegenerator as mg
import test.framework.modules as m
import test.framework.filetools as f
import test.framework.repository as r
import test.framework.robot as robot
import test.framework.easyblock as b
import test.framework.variables as v
import test.framework.github as g
import test.framework.toolchainvariables as tcv
import test.framework.toolchain as tc
import test.framework.options as o
import test.framework.config as c


# call suite() for each module and then run them all
SUITE = unittest.TestSuite([x.suite() for x in [r, e, mg, m, f, a, robot, b, v, g, tcv, tc, o, c]])

# uses XMLTestRunner if possible, so we can output an XML file that can be supplied to Jenkins
xml_msg = ""
try:
    import xmlrunner  # requires unittest-xml-reporting package
    xml_dir = 'test-reports'
    res = xmlrunner.XMLTestRunner(output=xml_dir, verbosity=1).run(SUITE)
    xml_msg = ", XML output of tests available in %s directory" % xml_dir
except ImportError, err:
    sys.stderr.write("WARNING: xmlrunner module not available, falling back to using unittest...\n\n")
    res = unittest.TextTestRunner().run(SUITE)
于 2013-08-01T12:51:02.097 回答
0

unittest像这样使用有点痛苦。我非常建议您按照 Alison 的建议查看nose或使用我个人最喜欢的 Python 测试工具py.test。只需按照特定的命名约定创建函数并让它撕裂!整个 xUnit 并不真正适合 Python 领域。

于 2010-01-21T16:37:06.830 回答