UnitTestTypeError:此构造函数不接受参数

2024-10-04 03:25:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用unittest.TestSuite()运行来自类TestNothing的所有测试。在

我的__init__.py是:

import unittest
from .test_nothing import TestNothing


def suite():
    """
    Define suite
    """
    test_suite = unittest.TestSuite()
    test_suite.addTests([
        unittest.TestLoader().loadTestsFromTestCase(TestNothing),
    ])
    return test_suite


if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite())

我的test_nothing.py是:

^{pr2}$

运行python test_nothong.py时出现以下错误:

Traceback (most recent call last):
  File "test_nothing.py", line 19, in <module>
    unittest.TextTestRunner(verbosity=2).run(suite())
  File "test_nothing.py", line 13, in suite
    unittest.TestLoader().loadTestsFromTestCase(TestNothing)
  File 

"/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 56, in loadTestsFromTestCase
        loaded_suite = self.suiteClass(map(testCaseClass, testCaseNames))
    TypeError: this constructor takes no arguments

Tags: inpytestimportlineunittestfilesuite
1条回答
网友
1楼 · 发布于 2024-10-04 03:25:43

事实上,构造函数不需要任何参数。 我建议您让您的测试类TestNothing继承自unittest.TestCase. 像这样,类的构造函数将从构造函数继承unittest.TestCase这需要争论。在

import unittest
class TestNothing(unittest.TestCase):
    def test_0010_test_nothing(self):
        self.assertEqual(200, 200)

您还可以通过以下链接继续操作python unittest 25.3.1

相关问题 更多 >