测试模板文件和重构

unsplash

新知识

  • Selenium检查页面元素
  • Selenium模拟提交
  • Django Test Client 检查使用模板文件名

编程犹如井中提水

  • 若井不深,桶不满,提上来会很轻松
  • 若井很深,桶也满,开始提也会很轻松,但是提的久了就累了
  • TDD就像是一个卡子,可以在半途休息下,而确保桶子不下落

关于简单测试

  1. 如果测试用例很简单,用犹豫的时间马上去做好
  2. 简单的测试用例其实是个placeholder,在变复杂之前就开始起步

利用FT测试To-Do lists完整访问流程

完整的访问流程为:

  1. 打开浏览器,输入地址打开页面(此时,titleHeader应该包含To-Do lists文字)
  2. 输入一个To-Do item,并按回车进行提交
  3. 提交结果在页面能刷新出来

修改functional_test.py如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import unittest

class NewVisitorTest(unittest.TestCase):

def setUp(self):
self.browser = webdriver.Firefox()

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

def test_can_start_a_list_and_retrieve_it_later(self):
# Edith has heard about a cool new online to-do app. She goes
# to check out its homepage
self.browser.get('http://localhost:8000')

# She notices the page title and header mention to-do lists
self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
self.assertIn('To-Do', header_text)

# She is invited to enter a to-do item straight away
inputbox = self.browser.find_element_by_id('id_new_item')
self.assertEqual(
inputbox.get_attribute('placeholder'),
'Enter a to-do item'
)

# She types "Buy peacock feathers" into a text box (Edith's hobby
# is tying fly-fishing lures)
inputbox.send_keys('Buy peacock feathers')

# When she hits enter, the page updates, and now the page lists
# "1: Buy peacock feathers" as an item in a to-do list table
inputbox.send_keys(Keys.ENTER)
time.sleep(1)

table = self.browser.find_element_by_id('id_list_table')
rows = table.find_elements_by_tag_name('tr')
self.assertTrue(
any(row.text == '1: Buy peacock feathers' for row in rows)
)

# There is still a text box inviting her to add another item. She
# enters "Use peacock feathers to make a fly" (Edith is very
# methodical)
self.fail('Finish the test!')

# The page updates again, and now shows both items on her list

使用模板文件

创建lists/templates/home.html

并且输入如下内容

1
2
3
<html>
<title>To-Do lists</title>
</html>

修改lists/views.py

修改函数返回值

1
2
3
4
from django.shortcuts import render

def home_page(request):
return render(request, 'home.html')

superlists/settings.py里注册应用

1
2
3
4
5
6
7
8
9
10
11
# Application definition

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

使用Django Test Client

修改lists/tests.py

并修改相应函数

1
2
3
4
5
6
7
8
9
def test_home_page_returns_correct_html(self):
response = self.client.get('/')

html = response.content.decode('utf8')
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>', html)
self.assertTrue(html.strip().endswith('</html>'))

self.assertTemplateUsed(response, 'home.html')

TDD开发流程概览

参考资料

0%