第二章 使用unittest模块扩展功能测试

时间:2023-03-09 07:19:52
第二章 使用unittest模块扩展功能测试

2.1使用功能测试驱动开放一个最简单的应用

# functional_tests.py
# -*- coding: utf-8 -*-
from selenium import webdriver browser = webdriver.Chrome()
browser.get('localhost:8000') assert 'To-Do' in browser.title browser.quit()

python3 manage.py runserver 启动服务器,

python3 functional_tests.py 进行测试 将出现assert错误

2.2Python标准库中的unittest模块

# functional_tests.py
# -*- coding: utf-8 -*-
from selenium import webdriver
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') self.assertIn('To-Do',self.browser.title) self.fail('Finish the test!') if __name__ == '__main__':
unittest.main(warnings='ignore') #warnings='ignore'为禁止抛出resourceWarning异常

python3 functional_test.py ,测试失败