如何在回溯中仅使用 runTest 和 newfunc 来追踪 nosetests error/failure 的来源?
How do I trace the source of nosetests error/failure with only runTest and newfunc in the traceback?
我在这种格式的 nosetests 脚本中遇到了一个神秘的(无用的,可能是错误的)错误(函数名称已匿名化为 "some_function",那是我写的那个,但 nose 没有调用正确):
File "/Users/REDACTED/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/REDACTED/lib/python2.7/site-packages/nose/util.py", line 620, in newfunc
self.test(*self.arg)
TypeError: some_function() takes exactly 1 argument (0 given)
此错误没有用处,因为它没有提供有关问题根源的详细信息。此外,手动 运行 通过测试脚本中的所有测试函数(示例:nosetests tests/test_myprogram.py:test_some_function())不会产生任何错误。
我还手动检查了测试,以便在测试之间共享变量(以验证之前测试的剩余数据更改不会破坏以后的测试)。
尽职调查:关于该主题的所有搜索都没有任何用处:
- https://www.google.com/search?q=nosetests+show+full+context+of+error+instead+of+runTest+and+newfunc&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=nosetests+show+code+location+source+of+all+errors&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=why+doesn%27t+nosetests+show+the+origin+of+an+error&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=nosetests+show+source+of+typeerror&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=nosetests+fails+to+produce+full+stack+trace%2C+returns+only+newFunc+and+runTest&ie=utf-8&oe=utf-8&client=firefox-b-1
找到问题了。
https://github.com/nose-devs/nose/issues/294
nosetests 有一个隐蔽的问题,如果您将名称中带有 "test" 的函数导入测试脚本,它们会被错误地标记为测试函数和 运行。如果他们接受任何参数,nosetests 将 运行 他们与 none 并立即产生无法追踪的错误。
例如:
foo.py:
def prepare_test_run_program(params):
# some programming here, could be anything
print("...")
if (some conditions): # pseudocode, use your imagination
return True
else:
return False
现在匹配测试脚本test_foo.py:
from foo import prepare_test_run_program
from nose.tools import assert_equal
def test_prepare_test_run_program():
params = ... # some parameter settings to test
assert_equal(prepare_test_run_program(params), True)
现在 运行 来自命令行的测试脚本:
nosetests test_foo.py
你将得到一个只能追溯到 运行Test 和 newFunc(如问题中所述)的 TypeError:
TypeError: prepare_test_run_program() takes at least 1 argument (0 given)
解决此问题的最佳方法:将被误认为是测试的函数中的 test 变量设置为 False。由于特定于站点的格式,我提到的 link 无法正确显示双下划线,因此这是我修改后的示例 test_foo.py,已为 nosetests 修复:
from foo import prepare_test_run_program
from nose.tools import assert_equal
prepare_test_run_program.__test__ = False # tell nose that this function isn't a test
def test_prepare_test_run_program():
params = ... # some parameter settings to test
assert_equal(prepare_test_run_program(params), True)
我在这种格式的 nosetests 脚本中遇到了一个神秘的(无用的,可能是错误的)错误(函数名称已匿名化为 "some_function",那是我写的那个,但 nose 没有调用正确):
File "/Users/REDACTED/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg)
File "/Users/REDACTED/lib/python2.7/site-packages/nose/util.py", line 620, in newfunc
self.test(*self.arg)
TypeError: some_function() takes exactly 1 argument (0 given)
此错误没有用处,因为它没有提供有关问题根源的详细信息。此外,手动 运行 通过测试脚本中的所有测试函数(示例:nosetests tests/test_myprogram.py:test_some_function())不会产生任何错误。
我还手动检查了测试,以便在测试之间共享变量(以验证之前测试的剩余数据更改不会破坏以后的测试)。
尽职调查:关于该主题的所有搜索都没有任何用处:
- https://www.google.com/search?q=nosetests+show+full+context+of+error+instead+of+runTest+and+newfunc&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=nosetests+show+code+location+source+of+all+errors&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=why+doesn%27t+nosetests+show+the+origin+of+an+error&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=nosetests+show+source+of+typeerror&ie=utf-8&oe=utf-8&client=firefox-b-1
- https://www.google.com/search?q=nosetests+fails+to+produce+full+stack+trace%2C+returns+only+newFunc+and+runTest&ie=utf-8&oe=utf-8&client=firefox-b-1
找到问题了。
https://github.com/nose-devs/nose/issues/294
nosetests 有一个隐蔽的问题,如果您将名称中带有 "test" 的函数导入测试脚本,它们会被错误地标记为测试函数和 运行。如果他们接受任何参数,nosetests 将 运行 他们与 none 并立即产生无法追踪的错误。
例如:
foo.py:
def prepare_test_run_program(params):
# some programming here, could be anything
print("...")
if (some conditions): # pseudocode, use your imagination
return True
else:
return False
现在匹配测试脚本test_foo.py:
from foo import prepare_test_run_program
from nose.tools import assert_equal
def test_prepare_test_run_program():
params = ... # some parameter settings to test
assert_equal(prepare_test_run_program(params), True)
现在 运行 来自命令行的测试脚本:
nosetests test_foo.py
你将得到一个只能追溯到 运行Test 和 newFunc(如问题中所述)的 TypeError:
TypeError: prepare_test_run_program() takes at least 1 argument (0 given)
解决此问题的最佳方法:将被误认为是测试的函数中的 test 变量设置为 False。由于特定于站点的格式,我提到的 link 无法正确显示双下划线,因此这是我修改后的示例 test_foo.py,已为 nosetests 修复:
from foo import prepare_test_run_program
from nose.tools import assert_equal
prepare_test_run_program.__test__ = False # tell nose that this function isn't a test
def test_prepare_test_run_program():
params = ... # some parameter settings to test
assert_equal(prepare_test_run_program(params), True)