I get the following error
TypeError: __init__() takes exactly 1 argument (2 given)
When I run the following python:
import unittest
from unittest import TestCase
class myClass( unittest.TestCase ):
def __init__( self ):
pass
if __name__ == '__main__':
unittest.main( argv=sys.argv, testRunner = unittest.TextTestRunner(verbosity=2))
Any ideas? I need to run init since I want to ONLY do my setup work once. Not once for each test. That would be a big optimization for overall test run time.
Here is the rest of the stack crawl:
Traceback (most recent call last):
File "./RestEditRecord.py", line 1439, in <module>
unittest.main( argv=sys.argv, testRunner = unittest.TextTestRunner(verbosity=2))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 149, in parseArgs
self.createTests()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 155, in createTests
self.test = self.testLoader.loadTestsFromModule(self.module)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/loader.py", line 65, in loadTestsFromModule
tests.append(self.loadTestsFromTestCase(obj))
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: __init__() takes exactly 1 argument (2 given)
Read the docs for the class you're extending; unittest.TestCase
's initializer takes an optional argument, and if you don't accept it, and it gets passed, you'll get this error.
There's rarely a good reason to override __init__
for a TestCase
anyway; test setup is done in setUp
(run once per test so changed state from one test doesn't influence the behavior of another test), not __init__
(run once total). You'll inherit __init__
, and since your __init__
isn't doing anything extra, it's silly to have it at all.
If you do have a legitimate use for it, make sure you delegate initialization up the chain:
class Suite_Edit_AutoEntry( unittest.TestCase ):
def __init__(self, *args, **kwargs): # Accept all unrecognized args for delegation
# Delegate to parent initializer
super().__init__(*args, **kwargs) # On Py2, super(Suite_Edit_AutoEntry, self)...
... do additional initialization here ...
Of course, as noted in the comments, you can have separate class setup (as of Python 2.7/3.2) using setupClass
so you don't have to deal with __init__
's finickiness at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With