Search code examples
djangopython-3.xdjango-testing

Django unit test to verify None fields on model


I was wondering how to unit test None fields on a model?

Imagine the following model:

class Item(models.Model):
    user = models.ForeignKey(user)
    date = DateField(blank=False, null= False)

And the test class:

class ItemCreationTest(TestCase):
    def test_item_without_date(self):
        item = Item()
        # test should fail...

Since I do not provide a date the test should fail. However, the test succeeds... What am I missing here?


Solution

  • Try to save object to DB and test will fail:

    item = Item()
    item.save()
    

    Or shorter version with the same result:

    Item.objects.create()