How to test auto_now_add in django

The universal way to use Factory Boy with Django with DateTime with auto_now_add=True

  1. Introduce custom DjangoFactory, with arg _fake_time to freeze time during object creation
from contextlib import suppress
from factory.django import DjangoModelFactory
from freezegun import freeze_time
from functools import partial

class CustomDjangoModelFactory(DjangoModelFactory):

    @classmethod
    def create(cls, _fake_time=None, **kwargs):
        wrapper = partial(freeze_time, time_to_freeze=_fake_time) if _fake_time else suppress
        with wrapper(_fake_time):
            return super().create(**kwargs)
  1. Add/change your factory to be based on CustomDjangoModelFactory
import factory
from django.db import models

class MyModel(models.Model):
    name = models.CharField(max_length=255)
    created = models.DateTimeField(auto_now_add=True)

class MyModelFactory(CustomDjangoModelFactory):
    name = factory.Sequence(lambda n: "Model No. %03d" % n)

    class Meta:
        model = MyModel
  1. Use it with _fake_time or without
my_model1 = MyModelFactory(name='first', _fake_time='2020-12-1')
my_model2 = MyModelFactory(name='second', _fake_time=datetime(2020, 11, 30))
my_model3 = MyModelFactory(name='third')

You can use mock:

import pytz
from unittest import mock

def test_get_registration_date(self):
    mocked = datetime.datetime(2018, 4, 4, 0, 0, 0, tzinfo=pytz.utc)
    with mock.patch('django.utils.timezone.now', mock.Mock(return_value=mocked)):
        user = factories.UserFactory.create()
        self.assertEqual(user.get_registration_date(), mocked)

You can use the package freeze gun. https://github.com/spulec/freezegun which patchs datetime.now().

from freezegun import freeze_time
...
    @freeze_time("2017-06-23 07:28:00")
    def test_get_registration_date(self):
        user = factories.UserFactory.create()
        self.assertEqual(
            datetime.strftime(user.get_registration_date(), "%Y-%m-%d %H:%M:%S")
            "2017-06-23 07:28:00"
        )

To simplify your job with @neverwalkaloner 's answer, you can define a more specific context manager:

import pytz
from unittest import mock
from contextlib import contextmanager

@contextmanager
def current_time(dt):
    with mock.patch('django.utils.timezone.now', mock.Mock(return_value=dt)):
        yield dt

and use it as:

def test_get_registration_date(self):
    with current_time(datetime(2021, 9, 12, 0, 0, 0, tzinfo=pytz.utc)) as mocked:
        user = factories.UserFactory.create()
        self.assertEqual(user.get_registration_date(), mocked)