繁体   English   中英

如何仅在跟踪中使用runTest和newfunc跟踪鼻子测试错误/失败的来源?

[英]How do I trace the source of nosetests error/failure with only runTest and newfunc in the traceback?

我的鼻子测试脚本以这种格式(函数名已匿名化为“ some_function”,这是我写的并且鼻子未正确调用它),在我的鼻子测试脚本中收到一个神秘的(并且无助,可能为假)错误:

  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://github.com/nose-devs/nose/issues/294

鼻子测试存在一个阴险的问题,如果您将名称中带有“ test”的函数导入到测试脚本中,则会将它们错误地标记为测试函数并照此运行。 如果它们接受任何参数,则鼻子测试将不执行任何参数,并立即产生不可追踪的错误。

例如:

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

并且您将得到只能追溯到runTest和newFunc的TypeError(如问题中所述):

TypeError: prepare_test_run_program() takes at least 1 argument (0 given)

解决此问题的最佳方法:将错误地用于测试的函数中的测试变量设置为False。 由于站点特定的格式,我提到的链接无法正确显示双下划线,因此这是我修改后的示例test_foo.py,已针对鼻子测试进行了修复:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM