Python 2.6:正确使用 unittest.TestSuite [英] Python 2.6: proper usage of unittest.TestSuite
问题描述
使用 Python 2.6,我在一个目录中的 python 文件中进行了一个非常简单的测试:
Using Python 2.6, I have a very simple test in a python file in a directory:
#mytest.py
import unittest
class BasicTests(unittest.TestCase):
def test_ok(self):
self.assertTrue(True)
suite = unittest.TestLoader().loadTestsFromTestCase(BasicTests)
我切换到目录并运行 python -m unittest mytest.suite
并且我收到以下错误:
I change into the directory and run python -m unittest mytest.suite
and I get the following error:
Traceback (most recent call last):
File "/usr/lib/python2.6/runpy.py", line 122, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.6/runpy.py", line 34, in _run_code
exec code in run_globals
File "/usr/lib/python2.6/unittest.py", line 875, in <module>
main(module=None)
File "/usr/lib/python2.6/unittest.py", line 816, in __init__
self.parseArgs(argv)
File "/usr/lib/python2.6/unittest.py", line 843, in parseArgs
self.createTests()
File "/usr/lib/python2.6/unittest.py", line 849, in createTests
self.module)
File "/usr/lib/python2.6/unittest.py", line 613, in loadTestsFromNames
suites = [self.loadTestsFromName(name, module) for name in names]
File "/usr/lib/python2.6/unittest.py", line 598, in loadTestsFromName
test = obj()
File "/usr/lib/python2.6/unittest.py", line 464, in __call__
return self.run(*args, **kwds)
TypeError: run() takes exactly 2 arguments (1 given)
我尝试了几种变体(例如 unittest.makeSuite
和 unittest.LoadTestFromNames
),但它们都给我相同的基本错误.我错过了什么?我一直阅读文档,我似乎正在关注规格
I've tried several variations (such as unittest.makeSuite
and unittest.LoadTestFromNames
) but they all give me the same basic error. What am I missing? I keep reading the documentation and I seem to be following the spec.
推荐答案
我通常不会在命令行上使用 unittest
,但有自己的测试运行脚本.
I don't usually work with unittest
on the command line, but have my own test running scripts.
你需要在模块中添加一个函数suite
You need to add a function suite
to the module
def suite():
return unittest.TestLoader().loadTestsFromTestCase(BasicTests)
然后像python -m unittest mytest.suite
一样调用它.但后来我遇到了以下问题:
and then call it like python -m unittest mytest.suite
. But then I run into the following problem:
TypeError: calling <function suite at 0x00C1FB70> returned <unittest.TestSuite tests=[<mysite.BasicTests testMethod=test_ok>]>, not a test
发生这种情况是因为 unittest
使用了类似 isinstance(mytest.suite(), TestSuite)
但通过使用 -m
执行,你得到TestSuite
类的两个不同版本(一个是 __main__.TestSuite
,另一个是 unittest.TestSuite
),所以 isinstance代码> 返回假.
对我来说,这看起来像是一个错误.通过在loadTestsFromName
的开头插入from unittest import TestSuite, TestCase
来修补unittest.py
解决了isinstance
问题.抱歉,我无法为您提供正确"解决方案(如果有).
which happens because unittest
uses something like isinstance(mytest.suite(), TestSuite)
but through executing with -m
, you get two different versions of the TestSuite
class (one is __main__.TestSuite
, the other is unittest.TestSuite
), so isinstance
returns false.
To me, this looks like a bug. Patching unittest.py
by inserting from unittest import TestSuite, TestCase
at the beginning of loadTestsFromName
solves the isinstance
problem. Sorry I can't give you the "correct" solution (if there is one).
这篇关于Python 2.6:正确使用 unittest.TestSuite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!