Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在回溯中仅使用runTest和newfunc跟踪nosets错误/失败的来源?_Python_Nose - Fatal编程技术网

Python 如何在回溯中仅使用runTest和newfunc跟踪nosets错误/失败的来源?

Python 如何在回溯中仅使用runTest和newfunc跟踪nosets错误/失败的来源?,python,nose,Python,Nose,我在我的nosetests脚本中得到了一个这种格式的神秘错误(而且没有帮助,可能是错误的)(函数名被匿名化为“some_function”,这是我写的,nose没有正确调用它): 此错误没有用处,因为它没有提供有关问题来源的详细信息。此外,手动运行测试脚本中的所有测试函数(例如:nosetests/test_myprogram.py:test_some_function())不会产生错误 我还手动检查了这些测试,以便在测试之间共享变量(以验证来自上一个测试的剩余数据更改不会损坏后续测试) 尽职调

我在我的nosetests脚本中得到了一个这种格式的神秘错误(而且没有帮助,可能是错误的)(函数名被匿名化为“some_function”,这是我写的,nose没有正确调用它):

此错误没有用处,因为它没有提供有关问题来源的详细信息。此外,手动运行测试脚本中的所有测试函数(例如:nosetests/test_myprogram.py:test_some_function())不会产生错误

我还手动检查了这些测试,以便在测试之间共享变量(以验证来自上一个测试的剩余数据更改不会损坏后续测试)

尽职调查:关于该主题的所有搜索均未发现任何有用信息:

    • 发现了问题

      nosetests存在一个潜在的问题,如果您将名称中带有“test”的函数导入到测试脚本中,它们会被错误地标记为测试函数并以此方式运行。如果它们接受任何参数,nosetests将不带任何参数运行它们,并立即产生无法跟踪的错误

      例如:

      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(如问题中所述):

      解决此问题的最佳方法是:将误认为测试的函数中的test变量设置为False。由于特定于站点的格式,我提到的链接无法正确显示双下划线,因此下面是我修改的示例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)
      
      TypeError: prepare_test_run_program() takes at least 1 argument (0 given)
      
      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)