Django test file

Time:2021-7-24

Django test file

problem

In the process of developing projects with Django, developers will inevitably test their own code during development. How to test a py file in Django alone and how to write a test script?

answer

Django automatically creates test.py under the application when creating a project. This py file can be used as a test file; You can also manually create a py test file under the application. Either way, you need to write the following code in advance:

From django.test import testcase # test.py does not need to be imported manually, and manually created test files need to be imported


import os

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
    import django
    django.setup()
    #Now you can enter the test code

Recommended Today

Implementation example of go operation etcd

etcdIt is an open-source, distributed key value pair data storage system, which provides shared configuration, service registration and discovery. This paper mainly introduces the installation and use of etcd. Etcdetcd introduction etcdIt is an open source and highly available distributed key value storage system developed with go language, which can be used to configure sharing […]