我已经为我的Python应用程序编写了一堆测试,但是他们突然看起来不再正常工作了!
我在tests
模块中创建了一堆测试:
在tests.__main__.py
内部,我已经包含以下代码来加载我的测试套件:
import unittest
if __name__ == "__main__":
loader = unittest.TestLoader()
start_dir = "."
suite = loader.discover(start_dir=start_dir, pattern='*_test.py')
runner = unittest.TextTestRunner()
runner.run(suite)
我知道我听起来像一个完整的菜鸟,但是大约一个小时前这些测试对我来说非常合适。我会通过从我的基目录中键入python3 -m tests
来发出它们。但是,我现在收到一个非常奇怪的TypeError
:
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/yuchen/Desktop/myproj/myapp/tests/__main__.py", line 6, in <module>
suite = loader.discover(start_dir=start_dir, pattern='*_test.py')
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 341, in discover
tests = list(self._find_tests(start_dir, pattern))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 406, in _find_tests
yield from self._find_tests(full_path, pattern, namespace)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 406, in _find_tests
yield from self._find_tests(full_path, pattern, namespace)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 398, in _find_tests
full_path, pattern, namespace)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 452, in _find_test_path
return self.loadTestsFromModule(module, pattern=pattern), False
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 123, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 92, in loadTestsFromTestCase
loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/suite.py", line 24, in __init__
self.addTests(tests)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/suite.py", line 57, in addTests
for test in tests:
TypeError: __init__() takes 0 positional arguments but 2 were given
我已经跟踪堆栈跟踪到我认为导致问题的相应文件,suite.py
unittest
文件:
"""TestSuite"""
import sys
from . import case
from . import util
__unittest = True
def _call_if_exists(parent, attr):
func = getattr(parent, attr, lambda: None)
func()
class BaseTestSuite(object):
"""A simple test suite that doesn't provide class or module shared fixtures.
"""
_cleanup = True
def __init__(self, tests=()):
self._tests = []
self._removed_tests = 0
print("Tests", tests) # <--- this was added by me
self.addTests(tests)
但是,__init__()
签名似乎不需要0位置参数。此外,它显然正在执行,因为在我在上面的代码中添加print("Tests", tests)
之后,我在抛出错误之前看到了以下输出:
Tests: []
Tests: <map object at 0x11033c0b8>
Tests: <map object at 0x11033c0b8>
Tests: <map object at 0x11033c0b8>
Tests: [<unittest.suite.TestSuite tests=[... a list of my tests]]
Tests: <map object at 0x110483978>
Tests: <map object at 0x110483978>
Traceback (most recent call last):
我对发生的事情感到很茫然。堆栈跟踪中打印的错误似乎与我在源代码中看到的内容完全一致。
编辑(决议):
接受的答案是现场。我编写了另一个测试类,并决定只为__init__
方法添加一个存根:
class MyUnfinishedTest(BaseTest):
def __init__():
pass
然后我忘记了它并研究了其他的东西,并进行了测试。那是我开始看到我的错误的时候。从我的测试模块中删除此类后,测试顺利进行。
答案 0 :(得分:1)
回溯看起来有点混乱的原因之一是:
for test in tests
实际上正在点击一个生成器,它正在动态发现和加载测试。
所以我猜这个:
TypeError: __init__() takes 0 positional arguments but 2 were given
实际上意味着发现过程找到了一个无法加载的测试类,可能是因为该类有自己的__init__
方法,其中的参数数量与发现期望值不同。
我会仔细检查__init__
方法的测试类,也可能更改此行:
start_dir = "."
为:
start_dir = os.path.dirname(__file__)
这意味着发现过程将从tests
文件夹开始查看,这比“。”更安全。 (当前工作目录)因为当前工作目录可能会根据您从哪里开始测试而改变(并且最终可能会获得您不期望的测试)。