unittest- TypeError:此构造函数不带参数

时间:2015-12-08 11:24:45

标签: python python-unittest

我正在使用TestNothing从课程unittest.TestSuite()运行所有测试。

我的__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是:

import unittest


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


def suite():
    "Test suite"
    test_suite = unittest.TestSuite()
    test_suite.addTests(
        unittest.TestLoader().loadTestsFromTestCase(TestNothing)
    )
    return test_suite


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

我在运行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

1 个答案:

答案 0 :(得分:0)

实际上,实际上TestNothing的构造函数并没有引用参数......而且它必须。 我建议你让你的测试类TestNothing继承自unittest.TestCase。像这样,你的类的Ctor将从构造器unittest.TestCase继承,它接受参数。

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

您还可以点击此链接python unittest 25.3.1