Django (10) - project combat - test the release management system and obtain test coverage

In the press conference sign-in system, django is used to develop the press conference sign-in system, and
this paper tests the system.

django.test

django.testis a module in the Django framework that provides tools and classes for writing and running tests.

django.testThe module contains some classes and functions for testing, such as:

  • TestCase: This is a base class for writing Django test cases. Inherited from unittest.TestCase, provides some additional functions and methods for dealing with the testing environment of a Django application.

  • SimpleTestCase: This is a more lightweight test base class, suitable for simple test scenarios without database or network access.

  • Client: This is a client class that simulates HTTP requests, used to simulate user requests and verify response results in tests.

  • RequestFactory: This is a factory class for creating HTTP request objects, used to generate HTTP request instances in tests.

  • Other helper functions and decorators, such as override_settingsfor temporarily overriding Django settings during testing, tagfor adding labels to test cases, etc.

By using django.testmodules, you can write unit tests, integration tests, and functional tests to verify and ensure the correctness and stability of Django applications.

Here is a simple sample code that demonstrates how to django.testwrite a test case class using modules:

from django.test import TestCase

class MyTestCase(TestCase):
    def test_my_function(self):
        # 编写测试逻辑
        result = my_function()
        self.assertEqual(result, expected_result)

In summary, django.testthe module provides a set of tools and classes for writing and running Django application tests, which can help developers verify and ensure the correctness and stability of the application.

Test the index view

insert image description here

import os,django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "guest.settings")
import django
django.setup()
from django.test import TestCase
class IndexPageTest(TestCase):
    def test_index_page_renders_index_template(self):
        response = self.client.get("/index/")
        self.assertEqual(response.status_code,200)
        self.assertTemplateUsed(response,'index.html')

The test class Xu Ya integrates TestCase, using the client instance to request get and post HTTP requests,
assert the status code after obtaining the response,
and use the assertTemplateUsed method to assert whether the request uses the index.html template

Test the login view

import os

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "guest.settings")


from django.contrib.auth.models import User
from django.test import TestCase
class LoginActionTest(TestCase):

    def setUp(self) -> None:
        User.objects.create_user("admin1","[email protected]","admin123456") #创建用户
    def test_add_admin(self):
        user=User.objects.get(username="admin1")#查询
        self.assertEqual(user.username,"admin1")
        self.assertEqual(user.email, "[email protected]")
    def test_login_action_username_password_null(self):
        """测试密码为空"""
        test_data={
   
    
    'username':'','password':''}
        response=self.client.post('/login/',data=test_data) #使用self的client可以对urls进行测试
        self.assertEqual(response.status_code,200)
        self.assertIn(b"username or password error",response.content)
    def test_error_password(self):

Guess you like

Origin blog.csdn.net/seanyang_/article/details/132635656