Django 测试开发5 unittest测试用例

Django测试用例

Django默认Python的标准库unittest编写测试用例。Django的单元测试类django.test.TestCase 从unittest.TestCase继承而来。在创建Django应用时,默认已经生成了tests.py测试文件。

setUp()初始化方法创建了2条数据,通过下面的测试方法,查询插入的数据,断言数据是否正确。注意:setUp()初始化方法并不会真正向数据库插入数据,所以不用清理测试数据。

千万不能单独运行tests.py文件。Django执行测试文件的命令为:python manage.py test   

from django.test import TestCase

# Create your tests here.
from .models import Event,Guest

class ModelsTest(TestCase):

    def setUp(self):
        Event.objects.create(id = 1,name = 'oneplus 3 event',status = True,limit = 2000,
                              address = 'shenzhen',start_time = '2016-08-31 02:18:22')
        Guest.objects.create(id = 1,event_id = 1,realname = 'alen',phone  = '13723456780',
                              email = 'alen@mail.com',sign=False)

    def test_event_models(self):
        result = Event.objects.get(id=1)
        self.assertEqual(result.address,'shenzhen')
        self.assertTrue(result.status,True)

    def test_guest_models(self):
        result = Guest.objects.get(realname = 'alen')
        self.assertEqual(result.phone, '13723456780')
        self.assertFalse(result.sign, False)

运行测试用例:

运行sign应用下的所有用例:python manage.py test sign

运行sign应用下的tests.py测试文件:python manage.py test sign.tests

运行sign应用下的tests.py测试文件下的ModelTest测试类:python manage.py test sign.tests.ModelTest

使用-p参数模糊匹配测试文件:python manage.py test -p test*.py

客户端测试

django.test.client类充当一个虚拟的网络浏览器。可以测试视图views与django的程序来通过脚本交互。

django.test.client可以模拟GET、POST请求,从HTTP到页面内容。可以检查重定向,再检查每一步的URLstatus_code,测试一个reuqest被django模板渲染

class IndexPageTest(TestCase):
    '''测试index登录首页'''

    def test_index_page_renders_index_teplate(self):
        '''测试index视图'''
        response = self.client.get('/index/')   #通过client.get()方法请求/index/路径。
        self.assertEqual(response.status_code,200)
        # assertTemplateUsed 断言服务器是否给定的是index.html的相应。
        self.assertTemplateUsed(response,'index.html')
from django.test import TestCase
from django.contrib.auth.models import User

class LoginActionTest(TestCase):
    '''测试登录动作'''
    def setUp(self):
        User.objects.create_user('admintest','admintest@mail.com','admintest123456')

    def test_add_admintest(self):
        '''测试添加用户'''
        user = User.objects.get(username = 'admintest')
        self.assertEqual(user.username,'admintest')
        self.assertEqual(user.email, 'admintest@mail.com')

    def test_login_action_username_password_null(self):
        '''用户名密码为空'''
        test_data = {'username':'','password':''}
        response = self.client.post('/login_action/',data=test_data)
        self.assertEqual(response.status_code,200)
        self.assertIn(b'username or password error',response.content)

    def test_login_action_username_password_error(self):
        '''用户名密码错误'''
        test_data = {'username':'123','password':'abc'}
        response = self.client.post('/login_action/',data=test_data)
        self.assertEqual(response.status_code,200)
        self.assertIn(b'username or password error',response.content)

    def test_login_action_success(self):
        '''登录成功'''
        test_data = {'username':'admintest','password':'admintest123456'}
        response = self.client.post('/login_action/',data=test_data)
        self.assertEqual(response.status_code,302)

上面的代码登录成功会自动跳转到特定页面,所以状态码是302重定向而不是200成功。

下面的代码中测试管理页面和搜索页面必须先登录。

class EventMangeTest(TestCase):
    '''发布会管理'''
    def setUp(self):
        User.objects.create_user('admintest','admintest@mail.com','admintest123456')
        Event.objects.create(id=1, name='xiaomiplus', status=True, limit=2000,
                             address='beijing', start_time='2016-08-31 02:18:22')
        self.login_user = {'username':'admintest','password':'admintest123456'}

    def test_event_manage_success(self):
        '''测试发布会:xiaomi i5'''
        response = self.client.post('/login_action/',data=self.login_user)
        response = self.client.post('/event_manage/')
        self.assertEqual(response.status_code,200)
        self.assertIn(b'xiaomiplus',response.content)
        self.assertIn(b'beijing',response.content)
        
    def test_event_manage_search_success(self):
        '''测试发布会搜索'''
        response = self.client.post('/login_action/',data=self.login_user)
        response = self.client.post('/search_name/',{'name':'xiaomiplus'})
        self.assertEqual(response.status_code,200)
        self.assertIn(b'xiaomiplus',response.content)
        self.assertIn(b'beijing',response.content)

嘉宾页面由于需要有发布会和嘉宾的信息,所以不仅要构造和嘉宾的数据而且还需要有发布会的数据。一样要先登录构造登录数据。

class GuestMangeTest(TestCase):
    '''嘉宾管理'''
    def setUp(self):
        User.objects.create_user('admintest','admintest@mail.com','admintest123456')
        Event.objects.create(id=1, name='oneplus 3 event', status=True, limit=2000,
                             address='shenzhen', start_time='2016-08-31 02:18:22')
        Guest.objects.create(id=1, event_id=1, realname='alen', phone='13723456780',
                             email='alen@mail.com', sign=False)
        self.login_user = {'username':'admintest','password':'admintest123456'}

    def test_guest_manage_success(self):
        '''测试发布会:xiaomi i5'''
        response = self.client.post('/login_action/',data=self.login_user)
        response = self.client.post('/guest_manage/')
        self.assertEqual(response.status_code,200)
        self.assertIn(b'alen',response.content)
        self.assertIn(b'13723456780',response.content)

    def test_guest_manage_search_success(self):
        '''测试发布会搜索'''
        response = self.client.post('/login_action/',data=self.login_user)
        response = self.client.post('/search_phone/',{'phone':'13723456780'})
        self.assertEqual(response.status_code,200)
        self.assertIn(b'alen',response.content)
        self.assertIn(b'13723456780',response.content)
class SignIndexActionTest(TestCase):
    '''发布会签到'''
    def setUp(self):
        User.objects.create_user('admintest','admintest@mail.com','admintest123456')
        Event.objects.create(id=1, name='oneplus 3 event', status=True, limit=2000,
                             address='shenzhen', start_time='2016-08-31 02:18:22')
        Event.objects.create(id=2, name='xiaomi plus', status=True, limit=2000,
                             address='beijing', start_time='2016-08-31 02:18:22')
        Guest.objects.create(id=1, event_id=1, realname='alen', phone='13723456780',
                             email='alen@mail.com', sign=1)
        Guest.objects.create(id=2, event_id=2, realname='judywang', phone='13603325863',
                             email='judywang@mail.com', sign=0)
        self.login_user = {'username':'admintest','password':'admintest123456'}

    def test_sign_action_phone_null(self):
        '''手机号为空'''
        response = self.client.post('/login_action/',data=self.login_user)
        response = self.client.post('/sign_index_action/1/',{'phone':''})
        self.assertEqual(response.status_code,200)
        self.assertIn(b'phone error',response.content)

    def test_sign_action_phone_or_event_id_error(self):
        '''手机号或者发布会id错误'''
        response = self.client.post('/login_action/', data=self.login_user)
        response = self.client.post('/sign_index_action/2/', {'phone':'13723456780'})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'phone or event_id error', response.content)

    def test_sign_action_user_sign_has(self):
        '''用户已签到'''
        response = self.client.post('/login_action/', data=self.login_user)
        response = self.client.post('/sign_index_action/1/', {'phone':'13723456780'})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'user has sign in.', response.content)

    def test_sign_action_sign_success(self):
        '''签到成功'''
        response = self.client.post('/login_action/', data=self.login_user)
        response = self.client.post('/sign_index_action/2/',{'phone':'13603325863'})
        self.assertEqual(response.status_code, 200)
        self.assertIn(b'sign in success.', response.content)
原文地址:https://www.cnblogs.com/wzjbg/p/11630937.html