Django测试

Django测试

 

 

 

推荐阅读:

大佬个人博客:

https://www.phodal.com/blog/

深入浅出Django

 

 

单元测试

自动测试(不推荐)

其他测试

 

 

 

单元测试

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""test"""

from django.urls import resolve
from django.test import TestCase
from .controller.views import index
from .models import Blog


class HomePageTest(TestCase):

    def test_index(self):
        """
        测试index首页
        :return:
        """
        found = resolve('/')
        self.assertEqual(found.func, index)

    def test_Content(self):
        """
        测试内容
        :return:
        """
        Blog.objects.create(NUMBER=1, TITLE='王致和', SECOND_TITLE='老干妈', URL='http://baodu.com', CONTENT='真好吃')
        response = self.client.request('/blog/this_is_a_test.html')
        self.assertEqual(b'This is Blog', response.content)

 

 

自动测试(不推荐)

Selenium是ThoughtWorks出品的一个强大的基于浏览器的开源自动化测试工具,它通常来编写Web应用的自动化测试。

 

首先需要安装:

pip install selenium

 

然后去下载对应的浏览器驱动:

chromechrome中输入chrome://version/来查看其版本,然后下载对应驱动。

fixfox...

 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""test"""

from django.test import LiveServerTestCase
from selenium import webdriver


class HomePageTest(LiveServerTestCase):
    def setUp(self):
        """
        在测试方法执行前都会调用该方法,该方法用于初始化一些资源。比如启动一个浏览器进程
        并执行maximize_window来将窗口最大化
        :return:
        """
        self.selenium = webdriver.Chrome(
            executable_path=r'C:\Users\admin\Desktop\DjProGIT\Djpro\app\driver\chromedriver.exe')
        self.selenium.maximize_window()
        super(HomePageTest, self).setUp()

    def tearDown(self):
        """
        在测试方法执行前都会调用该方法,在该方法中对一些资源的回收处理。比如关闭浏览器进程
        :return:
        """
        self.selenium.quit()
        super(HomePageTest, self).tearDown()

    def test_index(self):
        self.selenium.get('%s%s' % (self.live_server_url, '/'))
        self.assertEqual('Welcome to my blog', self.selenium.title)

 

 

其他测试

使用导入django启动时的uwsgi,省去写django启动代码

from expproject.wsgi import *
from explorer.models import *

if __name__ == "__main__":
    users = User.objects.all()
    print(users)

 

posted @ 2020-08-09 15:48  刘呆哗  阅读(139)  评论(0编辑  收藏  举报