Reputation: 4733
I've been trying/learning about unit testing works in Python and I came across this weird behavior, am I doing something wrong?
Class
class Entity:
def __init__(self, n):
if not isinstance(n, int):
raise TypeError('n must be a number.')
self.n = n
Test
from src.entity import Entity
class TestEntity(TestCase):
def test__init__types(self):
self.assertRaises(TypeError, Entity.__init__, "2")
self.assertRaises(TypeError, Entity.__init__, 2)
Shouldn't the test fail in the second assertRaises
since 2 is a number thus the TypeError
isn't raised? instead it says OK.
Upvotes: 0
Views: 308
Reputation: 44858
Entity.__init__(self, n)
takes two arguments: self
and n
. You're supplying only one, and getting a TypeError
.
Observe:
>>> class A:
... def __init__(self, n):
... print(n)
...
>>> A.__init__('s')
Traceback (most recent call last):
File "<string>", line 1, in <module>
TypeError: __init__() missing 1 required positional argument: 'n'
To test the constructor, you could do:
with self.assertRaises(TypeError):
Entity("2")
Now, why does this happen and where could one get that self
argument from? The thing is that __init__
is supplied with this argument automatically be A.__new__
, so you could call A.__new__
yourself, which would result in this weird looking code:
self.assertRaises(TypeError, A.__new__(A).__init__, "2")
Upvotes: 1