[英]Unit-testing extensions for an 'external' program with pyunit
I'm struggling to know where to start with unittest, having read the dive-into-python tutorial and looked at http://pyunit.sourceforge.net/ . 在阅读了deep-into-python教程并查看了http://pyunit.sourceforge.net/之后 ,我很难知道从unittest开始的地方。
I've got a piece of analysis software (call it 'prog.exe') which uses python for its input decks. 我有一块分析软件(称为“ prog.exe”),它使用python作为输入面板。 I've started writing a python module which I'm going to import from that input deck to provide some useful functionality.
我已经开始编写一个python模块,将从该输入平台导入该模块以提供一些有用的功能。 So, running one of these analyses will go like this:
因此,运行这些分析之一将如下所示:
prog.exe inputdeck.py
where inputdeck.py
contains: 其中
inputdeck.py
包含:
from mymodule import mystuff
So how do I set up and run tests on mymodule
? 那么,如何在
mymodule
上设置和运行测试? Should the above be in a system call in a setUp
method of the test, or what? 以上内容应该在测试的
setUp
方法中的系统调用中进行吗?
Ok - solution: 确定-解决方案:
Don't use unittest.main()
as that's the command line tool. 不要使用
unittest.main()
因为这是命令行工具。 Instead call the appropriate unittest methods directly as follows: 而是直接调用适当的unittest方法,如下所示:
From the command line run: 从命令行运行:
prog.exe mytests.py
where mytests.py
contains: 其中
mytests.py
包含:
import unittest
# ... code to run the analysis which we'll use for the tests ...
# ... test definitions ...
suite = unittest.TestLoader().loadTestsFromTestCase(test_cases)
unittest.TextTestRunner().run(suite)
See example at http://docs.python.org/release/2.6.7/library/unittest.html#unittest.TextTestRunner 请参见http://docs.python.org/release/2.6.7/library/unittest.html#unittest.TextTestRunner中的示例
Pyunit is a little bit outdated (2001), it is now completely included in python core distribution (http://docs.python.org/library/unittest.html). Pyunit有点过时(2001年),现在已完全包含在python核心发行版中(http://docs.python.org/library/unittest.html)。 You should start read this documentation, especially the basic example part .
您应该开始阅读本文档,尤其是基本示例部分 。
To test your module you'll have to create a file, let's call it mymodule_test.py
and put in it something like this : 要测试您的模块,您必须创建一个文件,我们将其命名为
mymodule_test.py
并放入如下所示的文件:
import unittest
from mymodule import mystuff
class MyTestCase(unittest.TestCase):
def test_01a(self):
""" test mystuff"""
self.failUnless(mystuff.do_the_right_stuff())
if __name__ == '__main__':
unittest.main()
and run it with python mymodule_test.py
并使用
python mymodule_test.py
运行它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.