So I’m learning how to practice TDD in Django and I’m having some minor trouble. I created a custom user object that links to authenticated system users in a one to one relationship. I have the following test, which exercises part of my custom user class:
def test_creating_a_user_with_attributes(self):
myuser = Myuser.objects.create_user('Gary', email='me@email.com')
current_time = now()
myuser.birthday = current_time
myuser.save()
first_user = Myuser.objects.all()[0]
self.assertEqual(first_user.birthday, current_time, 'first_user.birthday should be equal to the current_time')
The problem is that my test was failing and I couldn’t immediately see why. The assert failure reported the message I had supplied and I was confused because I was certain that the birthday was set to the value of now. I ended up having to refactor my assert to make the failing value clear.
self.assertEqual(first_user.birthday, current_time,
'first_user.birthday ' + str(first_user.birthday) + ' should equal ' + str(current_time))
This revealed that the birthday was a date field and not a datetime field. My question is wether there exists some alternate form of assert that dumps the expected and actual values as part of the failure message or if I am somehow misusing or misunderstanding the API?
Django doesn’t implement assertEqual, it simply uses Python’s unittest module for that one.
What you need is to set the
longMessageattribute toTruefor your test case class, like so:Which will output something like this if the test fails:
This is explained in Python’s unittest docs.