啊峰哥

第四章 编写这些测试有什么用

4.1使用Selenium测试用户交互

首先模拟功能,编写功能测试代码functional_tests.py

# functional_tests.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

class NewVisitorTest(unittest.TestCase):
    #setup 和tearDowm是特殊的方法,分别在测试的前后运行,这两个方法与try/except相似
    def setUp(self):
        self.browser = webdriver.Chrome()
        self.browser.implicitly_wait(3)                            #隐式等待 3秒

    def tearDown(self):
        self.browser.quit()

    def test_can_start_a_list_and_retrieve_it_later(self):         #名字以test开头的函数都是测试方法
        self.browser.get('http://localhost:8000')

        #网页的标题和头部是否含有To-Do这个词
        self.assertIn('To-Do',self.browser.title)
        header_text = self.browser.find_element_by_tag_name('id_new_item')
        self.assertIn('To-Do',header_text)
        
        #可输入一个待办事项
        inputbox = self.browser.find_element_by_id('id_new_item')
        self.assertEqual(inputbox.get_attribute('placeholder'),'Enter a to-do item')
        
        #在文本框输入'Buy peacock feathers',并回车
        inputbox.send_keys('Buy peacock feathers')
        inputbox.send_keys(Keys.ENTER)
        #回车后,在首页是否发现table,并测试table文本内容
        table = self.browser.find_element_by_id('id_list-table')
        rows = table.find_element_by_tag_name('tr')
        self.assertTrue(any(row.text == '1: Buy peacock feathers' for row in rows))
        #any函数的参数是个生成器表达式, 在这里,rows中任意一个值=='1: Buy peacock feathers'
 
        self.fail('Finish the test!')

if __name__ == '__main__':
    unittest.main(warnings='ignore')                                #warnings='ignore'为禁止抛出resourceWarning异常

在这里运行肯定会报错的

4.2使用模版重构

新建模版文件,lists/templates/home.html

<html>
    <title>To-Do lists</title>
</html>

python3 manage.py test运行单元测试

无法找到模,因为没有正式在Django中注册lists应用,所以需要在settings.py中添加lists应用

修改setting.py,在APP中添加lists

#setting.py

。。。
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'lists',
]
。。。

首先进行单元测试,通过

修改tests.py,使用辅助函数render_to_string

#tests.py
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.http import HttpRequest
from django.core.urlresolvers import resolve
from lists.views import home_page
from django.template.loader import render_to_string

class HomePageTest(TestCase):
    def test_root_url_resolves_to_home_page_view(self):
        found = resolve('/')
        self.assertEqual(found.func,home_page)

    def test_home_page_return_correct_html(self):
        request = HttpRequest()
        response = home_page(request)
        except_html = render_to_string('home_html')
        self.assertEqual(response.content.decode(),except_html)
        # .deocde()把字节转换为unicode字符串

4.3修改首页

接着修改首页home.html

<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <input id="id_new_item"/>
        <table id="id_list_table">
        </table>
    </body>
</html>

单元测试。报错,功能测试,报错,没事,下一节继续

4.4总结TDD流程

功能测试

单元测试

单元测试/编写代码 的循环

重构

posted on 2017-10-03 21:46  啊峰哥  阅读(262)  评论(0编辑  收藏  举报

导航