[PYTHON] I want to pin Datetime.now in Django tests

Introduction

I really wanted to fix Datetime.now (timezone.now) when I was making a Django test. That memo.

things to do

code

Code to test

test_app/views.py


from django.utils import timezone
...

def hogehoge():
    #Get the current time including the time zone ex. datetime.datetime(2020, 10, 30, 15, 35, 29, 482661, tzinfo=<UTC>)
    return timezone.now()

I want to test code like this.

Test code

test_app/tests.py


from unittest import mock
...

class TestClass(TestCase):
    @mock.patch("test_app.views.timezone.now")
    def test_hogehoge(self, mocked_now):
        now = timezone.make_aware(timezone.datetime(2020, 10, 30, 19, 30))
        mocked_now.return_value = now  #Set return value
        r = hogehoge()
        return self.assertEqual(r, now)

This will replace timezone.now in your code with a mock.

Finally

PyCharm's auto test is convenient for testing. If you rewrite the code, it will be automatically re-executed.

Recommended Posts

I want to pin Datetime.now in Django tests
[Django] I want to log in automatically after new registration
I want to pin Spyder to the taskbar
I want to print in a comprehension
I want to embed Matplotlib in PySimpleGUI
I want to do Dunnett's test in Python
I want to create a window in Python
I want to store DB information in list
I want to merge nested dicts in Python
I want to use the Django Debug Toolbar in my Ajax application
I implemented Google's Speech to text in Django
I want to display the progress in Python!
I want to upload a Django app to heroku
[Django memo] I want to set the login user information in the form in advance
I want to write in Python! (1) Code format check
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to transition with a button in flask
I want to use self in Backpropagation (tf.custom_gradient) (tensorflow)
I want to scroll the Django shift table, but ...
I want to write in Python! (2) Let's write a test
Even in JavaScript, I want to see Python `range ()`!
I want to randomly sample a file in Python
TemplateView patterns you'll want to learn first in Django
I want to work with a robot in python.
I want to write in Python! (3) Utilize the mock
I want to use the R dataset in python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
I want to solve Sudoku (Sudoku)
I want to get an error message in Japanese with django Password Change Form
I want to easily delete columns containing NA in R
I want to do something like sort uniq in Python
I want to make a blog editor with django admin
I made a command to generate a table comment in Django
I can't log in to the admin page with Django3
I want to make the Dictionary type in the List unique
[Introduction to Pytorch] I want to generate sentences in news articles
I want to count unique values in arrays and tuples
I want to align the significant figures in the Numpy array
I want to be able to run Python in VS Code
I want to make input () a nice complement in python
I didn't want to write the AWS key in the program
Errors related to memcached in django
I want to understand systemd roughly
How to reflect CSS in Django
I want to scrape images to learn
I want to do ○○ with Pandas
Pin factory_boy seed value in Django
I want to copy yolo annotations
I want to debug with Python
[Linux] I want to know the date when the user logged in
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to run Rails with rails s even in vagrant environment
LINEbot development, I want to check the operation in the local environment
[Python / AWS Lambda layers] I want to reuse only module in AWS Lambda Layers
I want to create a pipfile and reflect it in docker
I want to make the second line the column name in pandas
I want to pass the G test in one month Day 1
I want to know the population of each country in the world.
I want to change the color by clicking the scatter point in matplotlib