0

以下超级简单的单元测试在python2.7下运行良好,但在2.6中出现以下错误“no method runTest()”,这是为什么呢?

谢谢!杨

myuser@myhost /tmp $ python26 -m unittest a.AA
Traceback (most recent call last):
  File "/usr/lib64/python2.6/runpy.py", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/usr/lib64/python2.6/runpy.py", line 34, in _run_code
    exec code in run_globals
  File "/usr/lib64/python2.6/unittest.py", line 874, in <module>
    main(module=None)
  File "/usr/lib64/python2.6/unittest.py", line 815, in __init__
    self.parseArgs(argv)
  File "/usr/lib64/python2.6/unittest.py", line 842, in parseArgs
    self.createTests()
  File "/usr/lib64/python2.6/unittest.py", line 848, in createTests
    self.module)
  File "/usr/lib64/python2.6/unittest.py", line 612, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib64/python2.6/unittest.py", line 597, in loadTestsFromName
    test = obj()
  File "/usr/lib64/python2.6/unittest.py", line 215, in __init__
    (self.__class__, methodName)
ValueError: no such test method in <class 'a.AA'>: runTest
myuser@myhost /tmp $ vi a.py
myuser@myhost /tmp $ cat a.py
import unittest

class AA(unittest.TestCase):
    pass
4

2 回答 2

0

它适用于 2.7 而不是 2.6 的原因可以在文档中找到。

它说

在 2.7 版中更改:在早期版本中,只能运行单个测试方法,而不能运行模块或类。

您正在尝试运行整个班级的所有测试AA。你不能在 2.6 中这样做。您可以unittest.main()在代码底部调用 并通过命令行从命令行运行脚本python a.py

$ cat a.py
import unittest

class AA(unittest.TestCase):
    def test_something(self):
        self.assertEqual(1, 1)

unittest.main()

$ python -V
Python 2.6.1
$ python a.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

您可能还需要保护调用以unittest.main()确保仅当您从命令行运行此文件时才调用它。

于 2012-10-05T06:45:24.923 回答
0

You have to overwrite runTest function in your class.

Or functions named test_something, wich unittest understands as test functions. This is helpful when you want to execute a conscequent number of tests, because you can provide setUp and tearDown, which are functions that are called before each test, to create classes or stuff that you need in your test.

于 2012-10-04T19:22:30.870 回答