单元测试是用来对一个模块、一个函数或者一个类来进行正确性检验的测试工作。
一、unittest
测试在类中进行,测试类必须继承自unittest.TestCase
# test_basic.py
class BasicsTestCase(unittest.TestCase):
'''
继承自unittest.TestCase类。
'''
def setUp(self):
self.app = create_app('testing')
self.app_context = self.app.app_context()
self.app_context.push()
db.create_all()
def tearDown(self):
db.session.remove()
db.drop_all()
self.app_context.pop()
def test_app_exists(self):
self.assertFalse(current_app is None) #断言表达式:current_app is None是错误的
def test_app_is_testing(self):
self.assertTrue(current_app.config['TESTING']) #断言:正确
二、setUp()和tearDown()
setUp()函数在测试开始前执行,一般用于初始化。
tearDown()函数在测试结束之后执行。
三、测试函数以test_开头
类似于:test_app_exists(),test_app_is_testing()
四、断言:assertXXX方法
self.assertFalse(current_app is None) #断言表达式:current_app is None是错误的
self.assertTrue(current_app.config['TESTING']) #断言:正确