【发布时间】:2018-10-27 15:11:39
【问题描述】:
我使用 Flask 和 WTForms 以及标准和自定义表单验证器编写了一个相对简单的 Web 应用程序。我现在正在尝试学习 Python 测试。我编写了以下可与 unittest 配合使用的 tests.py 文件,但具有冗余性。有没有更好、更有效、更 Pythonic (DNRY) 的方法来做到这一点?我看过一些 pytest 教程,想知道固定装置是否会有所帮助,但我不太理解它们。而且我认为有一种更 Pythonic 的方式可以使用 unittest 来做到这一点。看来我需要一个可以传递不同 dict 参数的方法,但我不知道该怎么做。
from fly_app import app
import unittest
class FlaskTestCase(unittest.TestCase):
def test_city_code(self):
tester = app.test_client(self)
response = tester.post('/flight_search/extensive', data=dict(origin="xxxx"))
self.assertIn(b'That does not appear to be a valid city code', response.data)
def test_code_pairs(self):
tester = app.test_client(self)
response = tester.post('/flight_search/extensive', data=dict(origin="HFD", destination="CAS"))
self.assertIn(b'This origin-destination pair is not in searchable cache', response.data)
def test_duration_range(self):
tester = app.test_client(self)
response = tester.post('/flight_search/extensive', data=dict(origin="MSP", destination="NYC", min_duration=20))
self.assertIn(b'Number must be between 1 and 15', response.data)
def test_duration_integer(self):
tester = app.test_client(self)
response = tester.post('/flight_search/extensive', data=dict(origin="MSP", destination="NYC", min_duration='abc'))
self.assertIn(b'Not a valid integer value', response.data)
def test_duration_pair(self):
tester = app.test_client(self)
response = tester.post('/flight_search/extensive', data=dict(origin="MSP", destination="NYC", min_duration=10, max_duration=7))
self.assertIn(b'Maximum trip length cannot be less than minimum', response.data)
if __name__ == "__main__":
unittest.main()
【问题讨论】:
标签: python flask python-unittest flask-wtforms wtforms