I'm trying to learn unit testing with Django/unittest.
These are simple versions of my models:
class Device(models.Model):
name = models.CharField(max_length=100)
def get_ips(self):
return DeviceIP.objects.filter(device=self.id)
class DeviceIP(models.Model):
ip = models.GenericIPAddressField()
device = models.ForeignKey(Device)
And this is the test code I've come up with:
from django.test import TestCase
class DeviceTest(TestCase):
def test_get_ips(self):
device = Device()
device.name = 'My Device'
ip1 = DeviceIP()
ip1.ip = '127.0.0.1'
ip1.device = device
ip1.save()
ip2 = DeviceIP()
ip2.ip = '127.0.0.2'
ip2.device = device
ip2.save()
ip3 = DeviceIP()
ip3.ip = '127.0.0.3'
ip3.device = device
ip3.save()
self.assertEqual(device.get_ips(), [ip1, ip2, ip3])
The test results fails because on an AssertionError
even though the string representations of device.get_ips()
and [ip1, ip2, ip3]
are identical.
If I try using self.assertListEqual
I get an error because device.get_ips()
is a QuerySet and not a list.
If I try self.assertQuerySetEqual
I get an error saying "DeviceTest object has no attribute assertQuerySetEqual
" but I'm not sure why because DeviceTest
extends django.test
's TestCase.
How should I be doing a test like this?
Also, in a "real" project would it make sense to do such a simple test?
device.deviceip_set.all()
is Django's built in way to do what yourget_ips()
method does? Look at Django's documentation for reverse foreign keys. You also don't need to test it then, as it's standard Django behaviour. – Niche