unittestを継承した後、_init__メソッドエラー:TypeError:_init__() takes 1 positional argument but 2 were given
5916 ワード
イニシャルコードエラー
最初のコードは次のとおりです.
実行後、エラーを報告
原因分析:
サブクラスに__を追加init__親クラスを書き換えた初期化方法です.親のdef init(self,methodName=‘runTest’):2つのパラメータが渡され、1つの値が渡されました.親メソッド:
解決策:
で_init__パラメータにmethod=‘runtest’を追加し、_init__方法内添加:super(TestCase,self).init(methodName)
最初のコードは次のとおりです.
import unittest
class TestCase(unittest.TestCase):
def __init__(self):
self.test='test'
def test_test(self):
print('1')
if __name__ == '__main__':
unittest.main()
実行後、エラーを報告
TypeError: __init__() takes 1 positional argument but 2 were given
原因分析:
サブクラスに__を追加init__親クラスを書き換えた初期化方法です.親のdef init(self,methodName=‘runTest’):2つのパラメータが渡され、1つの値が渡されました.親メソッド:
def __init__(self, methodName='runTest'):
"""Create an instance of the class that will use the named test
method when executed. Raises a ValueError if the instance does
not have a method with the specified name.
"""
self._testMethodName = methodName
self._outcome = None
self._testMethodDoc = 'No test'
try:
testMethod = getattr(self, methodName)
except AttributeError:
if methodName != 'runTest':
# we allow instantiation with no explicit method name
# but not an *incorrect* or missing method name
raise ValueError("no such test method in %s: %s" %
(self.__class__, methodName))
else:
self._testMethodDoc = testMethod.__doc__
self._cleanups = []
self._subtest = None
# Map types to custom assertEqual functions that will compare
# instances of said type in more detail to generate a more useful
# error message.
self._type_equality_funcs = {}
self.addTypeEqualityFunc(dict, 'assertDictEqual')
self.addTypeEqualityFunc(list, 'assertListEqual')
self.addTypeEqualityFunc(tuple, 'assertTupleEqual')
self.addTypeEqualityFunc(set, 'assertSetEqual')
self.addTypeEqualityFunc(frozenset, 'assertSetEqual')
self.addTypeEqualityFunc(str, 'assertMultiLineEqual')
解決策:
で_init__パラメータにmethod=‘runtest’を追加し、_init__方法内添加:super(TestCase,self).init(methodName)
class TestCase():
def __init__(self,methodName='runTest'):
super(TestCase,self).__init__(methodName)
def test_test(self):
print('1')
if __name__ == '__main__':
unittest.main()