使用pyunit对“外部”程序进行单元测试扩展

时间:2011-08-31 09:42:28

标签: python unit-testing python-unittest

我很难知道从哪里开始使用unittest,阅读了潜入python教程并查看了http://pyunit.sourceforge.net/

我有一个分析软件(称之为'prog.exe'),它使用python作为输入平台。我已经开始编写一个python模块,我将从该输入板导入,以提供一些有用的功能。因此,运行其中一个分析将如下所示:

prog.exe inputdeck.py

inputdeck.py包含:

from mymodule import mystuff

那么如何在mymodule上设置和运行测试?以上是否应该使用setUp测试方法进行系统调用,或者是什么?


好的 - 解决方案:

不要使用unittest.main()作为命令行工具。而是直接调用适当的unittest方法,如下所示:

从命令行运行:

prog.exe mytests.py

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)

请参阅http://docs.python.org/release/2.6.7/library/unittest.html#unittest.TextTestRunner

上的示例

1 个答案:

答案 0 :(得分:0)

Pyunit有点过时(2001),它现在完全包含在python核心发行版中(http://docs.python.org/library/unittest.html)。您应该开始阅读本文档,尤其是basic example part

要测试你的模块,你必须创建一个文件,让我们称之为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()

并使用python mymodule_test.py

运行它
相关问题