API Testing - Testing in Flask介绍

# Import all dependencies
import unittest
import json
from flaskr import create_app
from models import setup_db

# Define Test Case Class
# Class name should be intuitive
class AppNameTestCase(unittest.TestCase):
	
	# setUp run before each test
	def setUp(self):
		# Executed before each test. Define test variables and initialize app.
		self.client = app.test_client
		pass
	
	# testDown run after each test
	def tearDown(self):
		# Execute after each test
		pass
	
	# 最主要部分
	# tests client request and evaluate response
	def test_given_behavior(self):
		res = self.client().get('/')
		self.assertEqual(res.status_code, 200)

# Make the tests conveniently executable
if __name__ == "__main__":
	unittest.main()

总结一下:
Unit test Flask Key Structures

  1. Define the test case class for the application (or section of the application, for larger applications).
  2. Define and implement the setUp function. It will be executed before each test and is where you should initialize the app and test client, as well as any other context your tests will need. The Flask library provides a test client for the application.
  3. Define the tearDown method, which is implemented after each test. It will run as long as setUp executes successfully, regardless of test success.
  4. Define your tests. All should begin with test_ and include a doc string about the purpose of the test. In defining the tests, you will need to:
    • Get the response by having the client make a request
    • Use self.assertEqual to check the status code and all other relevant operations.
  5. Run the test suite, by running python test_file_name.py from the command line.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值