缺少必需的位置参数 - nosetests

missing required positional argument - nosetests

我只是想执行下面的一个简单测试用例列表,

# testinheritence.py

import unittest

class heleClass(object):
    def execute_test_plan(self, test_plan):
        self.assertEqual("a", "a")


class TestParent(heleClass):
    def testName(self):
        test_plan = {"a": 1}
        self.execute_test_plan(test_plan)


class SqlInheritance(unittest.TestCase, TestParent):
    print ("get into inheritance")


if __name__ == "__main__":
    unittest.main()

然后用这个命令测试它:"nosetests3 -s testinheritence.py" 但我一直遇到这些异常,它抱怨说,

======================================================================
ERROR: testinheritence.TestParent.execute_test_plan
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
TypeError: execute_test_plan() missing 1 required positional argument: 'test_plan'

======================================================================
ERROR: testinheritence.TestParent.testName
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/nose/case.py", line 198, in runTest
    self.test(*self.arg)
  File "/home/dave/scripts/testinheritence.py", line 16, in testName
    self.execute_test_plan(test_plan)
  File "/home/dave/scripts/testinheritence.py", line 10, in execute_test_plan
    self.assertEqual("a", "a")
AttributeError: 'TestParent' object has no attribute 'assertEqual'

----------------------------------------------------------------------
Ran 4 tests in 0.003s

运行 它和 "python -m unittest testinheritence",测试用例将成功通过,我用谷歌搜索了这个但没有找到修复它的方法,我在这里遗漏了什么吗?非常感谢任何回复!

这里有几个问题。你的 heleClass 不是一个合适的单元测试 class(你使用 object 作为你的 parent。结果它没有 self.assertEqual() 方法。此外,nose 认为 "execute_test_plan" 是一个测试,并将其作为测试的一部分调用,但它失败了,因为它需要一个参数。尝试将 execute_test_plan 标记为 @nottest:

import unittest
from nose.tools import nottest

class heleClass(unittest.TestCase):
    @nottest
    def execute_test_plan(self, test_plan):
        self.assertEqual("a", "a")


class TestParent(heleClass):
    def testName(self):
        test_plan = {"a": 1}
        self.execute_test_plan(test_plan)        

if __name__ == "__main__":
    unittest.main()

nose 如果您将测试助手方法移动到测试模块中的顶级函数,也会报错。

我可以通过在测试模块中将函数设为私有,将名称更改为以 _ 开头来解决这个问题。然后nose会忽略它。