introduction
Basic knowledge about unit testing is no longer here, it simply: Unit testing is a piece of code to test another piece of code. The most commonly used framework is unittest, which is python unit testing framework, django unit testing framework test.TestCase inherited the python's unittest.TestCase.
Unittest.TestCase TestCase is also carried out further packaging, eliminating the need to write a lot of code repetition, such as defining a self.client, Email Service provides a convenient method of sending mail.
Django model is well known MTV model, where T is the template is the HTML files, for HTML, there is no measurable code, basically to write the dead, even if there is not an important logic code. So during the time unit test, focusing on M and V expansion, that is, models and views.
the way
Unit tests performed in two ways:
1. django framework comes document tests.py unit testing;
2. custom created test.py file;
both are the same, but the execution is not the same run-time directory.
Note: I am here only the first.
Model test
Start by importing public library use:
from django.test import TestCase from django_web.models import Event,Guest from django.contrib.auth.models import User # Create your tests here. import datetime get_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
Test Model
class DjangoWebModelTest(TestCase): "" "Test model" "" def setUp(self) -> None: Event.objects.create(id=1,name='小米5',status=True,address='深圳',limit=3,start_time=get_now) Guest.objects.create(id=1,event_id=1,realname='老王',phone=15099925893,email='[email protected]',sign=False) def test_event_model(self): "" "Test the conference table." "" result = Event.objects.get(name='小米5') self.assertEqual(result.address,'深圳') self.assertTrue(result.status) def test_guest_model(self): "" "Test guest list." "" result = Guest.objects.get(phone='15099925893') self.assertEqual(result.realname,'老王') self.assertFalse(result.sign)
View function test
class IndexPageTest(TestCase): "" "Test index Log Home" "" def test_index_page(self): "" "Test index view." "" response = self.client.get('/index/') self.assertEqual(response.status_code,200) self.assertTemplateUsed(response,'index.html') class LoginAction(TestCase): "" "Test login action." "" def setUp(self) -> None: "" "Create a User Data: Create user in two different ways." "" User.objects.create(username='admin') User.objects.create_user(username='admin2',email='[email protected]',password='123456') def test_add_admin(self): "" "Add users admin test" "" user = User.objects.get(username='admin') self.assertEqual(user.username,'admin') def test_add_admin2(self): "" "Add User admin2 test" "" user = User.objects.get(username='admin2') self.assertEqual(user.username,'admin2') self.assertEqual(user.email,'[email protected]') def test_login_username_password_null(self): "" "Username password is empty." "" test_data = {'username':'','password':''} response = self.client.post('/login_action/',data=test_data) self.assertEqual(response.status_code,302) def test_login_username_password_error(self): """username or password is wrong""" test_data = {'username':'test','password':'123456'} response = self.client.post('/login_action/',data=test_data) self.assertEqual(response.status_code,302) def test_login_action_success(self): """login successful""" test_data = {'username':'admin2','password':'123456'} response = self.client.post('/login_action/',data=test_data) self.assertEqual(response.status_code,302) class EventManageTest(TestCase): "" "Conference Management" "" def setUp(self) -> None: # Create a user account User.objects.create_user('admin','[email protected]','123456') Event.objects.create(name='小米3',limit=3,address='深圳',status=True,start_time=get_now) self.login_user = {'username':'admin','password':'123456'} # Pre-registered self.client.post('/login_action/', data=self.login_user) def test_add_event_data(self): "" "Test add conference: millet 3" "" event = Event.objects.get(name="小米3") self.assertEqual(event.address, "深圳") def test_event_success(self): "" "Test Conference: millet 3" "" response = self.client.post('/event_manager/') self.assertEqual(response.status_code,200) self.assertIn("小米3".encode('utf-8'),response.content) def test_event_search_success(self): "" "Test Conference search for" "" response = self.client.post('/search_name/') self.assertEqual(response.status_code,200) self.assertIn('小米3'.encode('UTF-8'),response.content) class GuestManageTest(TestCase): "" "Guest management." "" def setUp(self) -> None: User.objects.create_user('admin','[email protected]','123456') Event.objects.create(id=1,name='小米2',limit=3,address='深圳',status=True,start_time=get_now) Guest.objects.create(realname='小李子',phone=15099925798,email='[email protected]',sign=0,event_id=1) self.login_user = {'username':'admin','password':'123456'} # Pre-registered self.client.post('/login_action/',data=self.login_user) def test_add_guest(self): "" "Test Add guests: Survival Song" "" guest =Guest.objects.get(realname='小李子') self.assertEqual (guest.realname, 'Survival Song') self.assertEqual(guest.phone,'15099925798') self.assertEqual(guest.email,'[email protected]') self.assertFalse(guest.sign) def test_guest_success(self): "" "Test Guest List: Survival Song" "" response = self.client.post('/guest_manager/') self.assertEqual(response.status_code,200) self.assertIn ( 'Survival Song' .encode ( 'UTF-8'), response.content) self.assertIn('15099925798'.encode('utf-8'),response.content) def test_guest_search_success(self): "" "Test guests search for" "" response = self.client.post('/search_phone/') self.assertEqual(response.status_code,200) self.assertIn ( 'Survival Song' .encode ( 'utf-8'), response.content) self.assertIn('15099925798'.encode('utf-8'),response.content) class SignIndexActionTest(TestCase): "" "Conference attendance" "" def setUp(self) -> None: User.objects.create_user('admin','[email protected]','123456') Event.objects.create(id=1, name='小米1', limit=3, address='广州', status=True, start_time=get_now) Event.objects.create(id=2, name='小米9', limit=3, address='北京', status=True, start_time=get_now) Guest.objects.create (realname = 'Zhang', phone = 15099925798, email='[email protected] ', sign = 0, event_id = 1) # sign is not Guest.objects.create (realname = 'old weeks', phone = 15099925700, email='[email protected]', sign = 1, event_id = 2) # sign is not self.login_user = {'username':'admin','password':'123456'} self.client.post('/login_action/',data=self.login_user) def test_phone_null(self): "" "Testing the phone number is empty." "" response =self.client.post('/sign_index_action/1/',{"phone":""}) self.assertEqual(response.status_code,200) self.assertIn ( 'Enter phone number.'. encode ( 'utf-8'), response.content) def test_phone_error(self): "" "Phone number wrong." "" response = self.client.post('/sign_index_action/2/',{"phone":"15099925732398"}) self.assertEqual(response.status_code,200) self.assertIn ( "the wrong telephone number.". encode ( 'UTF-8'), response.content) def test_phone_or_eventid_error(self): "" "Phone number is not part of the conference guests." "" response = self.client.post('/sign_index_action/2/',{"phone":"15099925798"}) self.assertEqual(response.status_code,200) self.assertIn ( "phone number is not part of the conference guests.". encode ( 'UTF-8'), response.content) def test_already_sign(self): "" "User sign" "" response = self.client.post('/sign_index_action/2/',{"phone":"15099925700"}) self.assertEqual(response.status_code,200) self.assertIn ( "You have to sign!.". encode ( 'utf-8'), response.content) def test_sign_success(self): "" "Sign success" "" response = self.client.post('/sign_index_action/1/',{"phone":"15099925798"}) self.assertEqual(response.status_code,200) self.assertIn ( "sign success!". encode ( 'utf-8'), response.content)
Running unit test code
""" Run all use cases: python3 manage.py test Django_web run applications under all the use cases: python3 manage.py test django_web tests.py sign the application file to run under use cases: python3 manage.py test django_web.tests tests.py file applications running django_web in DjangoWebModelTest test class: python3 manage.py test django_web.tests.DjangoWebModelTest Django_web applications running under the test method (use cases) DjangoWebModelTest test class: python3 manage.py test django_web.tests.DjangoWebModelTest.test_event_model Fuzzy matching test file Run python3 manage.py test django_web -p test * .py ...... """
Results of the
D:\my_django_guest>python3 manage.py test django_web Creating test database for alias 'default'... System check identified no issues (0 silenced). ................... ---------------------------------------------------------------------- Ran 19 tests in 3.080s OK Destroying test database for alias 'default'...