使用模板后的功能测试 - yann学长的运维开发指南24

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@tmp4321·
0.000 HBD
使用模板后的功能测试 - yann学长的运维开发指南24
### 说明
目前的进度及后续的课程
![http://www.iamyann.com/images/20180422160829.png](http://www.iamyann.com/images/20180422160829.png)
<!--more-->

### 介绍
单元测试小有成就,这次来做一下功能测试

今天的具体内容如下:
> * 调试功能测试
> * 总结回顾
> * 如何保存用户输入



### 调试功能测试
上次的进度是配置模板后通过了基本的单元测试,然功能测试没有通过
当时的报错是找不到h1标记,这个东西现在由模版负责

#### 没有h1元素
修改 lists/templates/home.html
```html
<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
    </body>
</html>
```
再测试
```python
python functional_tests.py

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
element: [id="id_new_item"]
```
#### 增加输入框
报错找不到id_new_item元素,没毛病,之前是写在代码里的,不在模板
再修改 lists/templates/home.html
```html
<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <input id="id_new_item" />
    </body>
</html>
```
再测试
```python
python functional_tests.py

AssertionError: '' != 'Enter a to-do item'
```
#### 增加占位符
这次的报错是找不到起提示作用的占位文字,
再修改 lists/templates/home.html
```html
<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <input id="id_new_item" placeholder="Enter a to-do item"/>
    </body>
</html>
```
再测试
```python
python functional_tests.py

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate
element: [id="id_list_table"]
```
#### 增加表格
这次的报错是找不到表格元素,再修改
更新 lists/templates/home.html
```html
<html>
    <head>
        <title>To-Do lists</title>
    </head>
    <body>
        <h1>Your To-Do list</h1>
        <input id="id_new_item" placeholder="Enter a to-do item"/>
        <table id="id_list_table">
        </table>
    </body>
</html>
```
再测试
```python
python functional_tests.py

# 报错更新
File "functional_tests.py", line 43, in
test_can_start_a_list_and_retrieve_it_later
  any(row.text == '1: Buy peacock feathers' for row in rows)
AssertionError: False is not true
```
这次有了很大的进步,内容已经出来了,就是格式还有点不对

### 更改测试输出内容
因为处理客户提交是下一章的内容,现在只改一下输出,证明测试到达这里了
```python
vi functional_tests.py

def test_can_start_a_list_and_retrieve_it_later(self):
        self.browser.get('http://localhost:8000')
        # 用浏览器访问 localhost

        self.assertIn('To-Do', self.browser.title)
        header_text = self.browser.find_element_by_tag_name('h1').text  
        self.assertIn('To-Do', header_text)
        # 断言标题和正文中含有To-Do,网页中有h1元素

        inputbox = self.browser.find_element_by_id('id_new_item')  
        self.assertEqual(
            inputbox.get_attribute('placeholder'),
            'Enter a to-do item'
        )
        # 断言id_new_item元素, 断言占位符文字'Enter a to-do item'

        inputbox.send_keys('Buy peacock feathers')  
        inputbox.send_keys(Keys.ENTER)  
        time.sleep(1)  
        # 在输入框输入 Buy peacock feathers按下回车,并等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),
            "New to-do item did not appear in table"
        )
        # 断言表格元素'id_list_table,断言行标记 tr
        # 在所有行中找匹配文字  Buy peacock feathers

        self.fail('Finish the test!')
        # 主动失败,提示测试结束
```
简单注释了一下测试代码,并在最后加了一句提示信息 New to-do item did not appear in table ,会在报错中输出

```python
python functional_tests.py
AssertionError: False is not true : New to-do item did not appear in table
```
再次测试, 期望达到

### 预告
今天的篇幅比预期要长,总结回顾只能放在下次进行了.

上次是一小步,这次也是一小步,但是走一步算一步,只要坚持不倒退,总会到达目标的.


![http://hero.iamyann.com/blog_webchat.jpg](http://hero.iamyann.com/blog_webchat.jpg)
👍 , , , , , , , , , , , , , ,