#!/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
chrome,chrome中输入chrome://version/来查看其版本,然后下载对应驱动。
#!/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:UsersadminDesktopDjProGITDjproappdriverchromedriver.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)