0

在阅读了dive-into-python 教程并查看了http://pyunit.sourceforge.net/之后,我很难知道从哪里开始使用 unittest 。

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

prog.exe inputdeck.py

其中inputdeck.py包含:

from mymodule import mystuff

那么我该如何设置和运行测试mymodule呢?以上应该在setUp测试方法中的系统调用中,还是什么?


好的 - 解决方案:

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

从命令行运行:

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中的示例

4

1 回答 1

0

Pyunit is a little bit outdated (2001), it is now completely included in python core distribution (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 :

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

于 2011-08-31T09:48:10.580 回答