测试套件单元测试

发布于 2024-08-18 15:40:32 字数 1086 浏览 3 评论 0原文

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

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”和 TestContinouslyMove“的模块。有谁知道如何让这段代码工作?

谢谢

I have a test suite and I am trying to get it to work with the tests I have created. The test work if I run them individually but I want to run them all in a test suite. The code below show the test suite created:

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

I have placed this code into my test code to link up with the 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)

The error that is produced claim that there is no modules named 'TestAbsoluteMove' and TestContinuousMove'. Does anyone know how to get this code working?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云裳 2024-08-25 15:40:32

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

Nose makes this sort of thing a no-brainer. It will auto-detect your tests and run them as a suite. (You can also run specific tests by passing it a flag.)

沧桑㈠ 2024-08-25 15:40:32

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

TestAbsoluteMove is a class, and it needs to come from somewhere. Wherever your AbsoluteMoveTestSuite class is defined, you need to import TestAbsoluteMove.

久夏青 2024-08-25 15:40:32

这就是我创建测试套件的方式(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)

this is how I create my testsuite (loadTestFromTestCase detects your tests automatically)

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

and to run them all at once I have a suite that incorporates all subsuites (notice all the imports, you will need to import them before they are available in your new module)

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)
盗琴音 2024-08-25 15:40:32

unittest 这样使用有点痛苦。我强烈建议您遵循 Alison 的建议,看看 nose 或使用我个人最喜欢的 Python 测试工具 py.test。只需按照特定的命名约定创建函数并让它发挥作用!整个 xUnit 事物并不真正适合 Python 领域。

unittest is a bit of a pain to use like this. I would very much reommend that you follow Alison's advice a look at nose or use my personal favourite Python testing tool py.test. Just create functions following a specific naming convention and let it rip! The whole xUnit thing doesn't really fit into Python territory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文