测试套件单元测试
我有一个测试套件,我正在尝试让它与我创建的测试一起工作。如果我单独运行它们,测试就可以工作,但我想在测试套件中运行它们。下面的代码显示了创建的测试套件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.)
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.
这就是我创建测试套件的方式(loadTestFromTestCase 自动检测您的测试)
并立即运行它们,我有一个包含所有子套件的套件(注意所有导入,您需要在它们在新模块中可用之前导入它们)
this is how I create my testsuite (loadTestFromTestCase detects your tests automatically)
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)
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 atnose
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.