Reputation: 16242
calling nosetests
gives me the following:
======================================================================
ERROR: Failure: TypeError (__init__() takes exactly 2 arguments (1 given))
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sheena/WORK/CriticalID/workspace/flow_env2/local/lib/python2.7/site-packages/nose-1.3.4-py2.7.egg/nose/loader.py", line 519, in makeTest
return self._makeTest(obj, parent)
File "/home/sheena/WORK/CriticalID/workspace/flow_env2/local/lib/python2.7/site-packages/nose-1.3.4-py2.7.egg/nose/loader.py", line 578, in _makeTest
return MethodTestCase(obj)
File "/home/sheena/WORK/CriticalID/workspace/flow_env2/local/lib/python2.7/site-packages/nose-1.3.4-py2.7.egg/nose/case.py", line 345, in __init__
self.inst = self.cls()
TypeError: __init__() takes exactly 2 arguments (1 given)
as well as some other stuff.
My directory structure looks like:
MyStuff
./__init__.py
./tests
./some_tests.py
./other_tests.py
./ ... lots more
./a_useful_group_of_tests
./more_tests.py
./tasty_tests.py
./ ...lots more
./other_files_and_directories
Now there are a lot of tests in a lot of files and this error gives me no indication of where in my code the error came from. Any ideas about how I can find it? The best I can come up with so far is to get rid of all the test files and then put them back one by one but that is not exactly ideal.
Upvotes: 3
Views: 1785
Reputation: 8307
So I spent way too much time on this issue and want to give back.
If you run nosetests with --with-xunit
you'll notice that the test that the failure is booked under the path nose.failure.Failure.runTest
. If you try to run that test, it will say it doesn't exist. Something weird is going on here.
Interestingly if you run it under --collect-only --verbose
you'll get Failure: TypeError (__init__() takes exactly 2 arguments (1 given)) ... ok
, so something is first being recognized as a test when it probably shouldn't, and second it fails immediately. Evily, the two packages adjacent to the failing test both pass so even figuring out which file is failing is not straightforward.
As it turns out, there's a non-test file someone created that had test
in its name. Nose tried to interpret it as as test and hence the error.
Sadly I had to manually run every package to figure this out. One way you can do this is to find /path/to/package -name "*test*.py
and then grep for anything that doesn't live in a tests directory.
Upvotes: 1
Reputation: 31
The usual reason for this is that you are using a class in one of those tests that is not imported in it or that has broken imports in it. Or otherwise have an individually broken test or imported class that when initialised with the other tests via the global nose test case runner environment is OK due for example to the missing import being imported in another test. But on initial loading of the test class or individual loading - it would raise a not found or other error. But nose doesnt make that very clear although it is probably showing the initialisation error for the class.
So using your sample code in the test environment something in 'goodies' is not being passed an argument at module initialisation time, that is required for it to be initialized correctly. But gets it if initialized later on in the tests.
Alternatively you have a method accidentally called test_something in a class that isn't meant to be a test class (eg. somewhere in goodies or a class used by it) and nose finds and initialises it without the required parameters.
You can try going through the imports one by one putting them back to the module level - then initialising the problem class on the python shell to fix it.
Upvotes: 3
Reputation: 16242
The solution:
remove import statements from the top of the script.
Why:
After locating the test file giving me issues I executed nosetests
with the -vv
option as per Evert's suggestion. It turned out that the error message wasn't coming from any specific test. Ie, the tests were running as expected, those errors were just tagged onto the output. The output looked something like:
Failure: TypeError (__init__() takes exactly 2 arguments (1 given)) ... ERROR
Failure: TypeError (__init__() takes exactly 2 arguments (1 given)) ... ERROR
...
test_clear_instructions (the_calculator2.tests.model_tests.workflow_tests.Workflow_tests) ...
...all my tests follow
The only things not in test cases were import statements. So I just moved them to where they were used.
But why would this happen? Bonus points to anyone who knows
again, I dont feel like reading through reams of code to find the answer
Illustrative code:
from my.stuff import goodies #<----------Error from this line
class My_tests(unittest.TestCase):
def test_one(self):
do stuff
def test_two(self):
do other stuff
No error in this code:
class My_tests(unittest.TestCase):
def test_one(self):
from my.stuff import goodies
do stuff
def test_two(self):
from my.stuff import goodies
do other stuff
Upvotes: 1