Reputation: 819
I am not sure what the meaning of this error is. This is my test:
def test_method_delete(self):
test_graph = trie_builder.Graph()
node_name = 'node_0'
test_graph.node(node_name)
# Create a node.
test_graph.delete(node_name)
# Delete the node.
self.assertNotIn(node_name, test_graph.node_list)
node_name = 'node_1'
with self.assertRaises(KeyError('ERROR: Attempt to delete non-existent node.')):
test_graph.delete(node_name)
This is my method:
def delete(self, node_name):
if node_name in self.node_list:
del self.node_list[node_name]
else:
raise(KeyError('ERROR: Attempt to delete non-existent node.'))
And this is the error:
ERROR: test_method_delete (__main__.test_class_Graph)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_trie_builder.py", line 66, in test_method_delete
test_graph.delete(node_name)
File "/Users/juliushamilton/Documents/Work/Nantomics_trie_builder/trie_builder/trie_builder.py", line 25, in delete
raise(KeyError('ERROR: Attempt to delete non-existent node.'))
KeyError: 'ERROR: Attempt to delete non-existent node.'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test_trie_builder.py", line 66, in test_method_delete
test_graph.delete(node_name)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/unittest/case.py", line 184, in __exit__
if not issubclass(exc_type, self.expected):
TypeError: issubclass() arg 2 must be a class or tuple of classes
What could be the problem? What is 'arg2' referring to?
Upvotes: 0
Views: 614
Reputation: 2177
Have you tried passing the KeyError
class (without instantiating an instance) to assertRaises
? It looks like assertRaises
is trying to check that the error it got is of the type of error class you gave it, but KeyError('ERROR: Attempt to delete non-existent node.')
is an instance of type KeyError
, not the type itself.
with self.assertRaises(KeyError):
test_graph.delete(node_name)
It looks like the docs don't make this clear:
https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaises, but from the stack trace your gave it looks like what it does is check that the exception it receives is a subclass of the exception class you passed to it using issubclass
. issubclass
can only accept a type as its second argument, so passing an instance is the error you are getting.
If you want to also check the error text you need to use assertRaisesRegexp
:
https://docs.python.org/2/library/unittest.html#unittest.TestCase.assertRaisesRegexp
Upvotes: 2