flask框架使用unittest单元测试

发布时间 2023-05-24 11:05:46作者: Ethan_feng
user_labels_api.py
 1 # -*- coding: utf-8 -*-
 2 # Author : Ethan
 3 # Time : 2023/5/16 12:50
 4 from flask import Flask,jsonify,render_template,request,json
 5 import requests
 6 # from flask_sqlalchemy import SQLAlchemy
 7 app = Flask(__name__)
 8 # bd = SQLAlchemy(app)
 9 app.debug = True  #调试模式,调试代码不需要重启
10 @app.route('/feign/user/labels/',methods = ['GET','POST'])
11 def getlabels():
12     url = 'http://*******.cf93fe1dde8584346a2d81575c79aff9e.cn-shenzhen.alicontainer.com/feign/user/labels'
13     response = requests.post(url=url,json=request.json)
14     if isinstance(request.values,dict):
15         return jsonify(response.json())
16     else:
17         return jsonify({"code":500,"msg":"参数错误!"})
18 
19 if __name__ == '__main__':
20     app.run("0.0.0.0",1234)

test.py
 1 # -*- coding: utf-8 -*-
 2 # Author : Ethan
 3 # Time : 2023/5/16 11:35
 4 import json
 5 import unittest
 6 from  user_labels_api import app
 7 class Flask01_Test(unittest.TestCase):
 8     def setUp(self):
 9         self.app = app
10         app.config['TESTING'] = True
11         self.client = app.test_client()
12 
13     def test01(self):
14         """新用户"""
15         data = [{"userId":"20211009102601932734","firstDimension":"TAKEOUT","thirdDimension":"","secondDimension":"STOREID:21022"}]
16         response = self.client.post('/feign/user/labels/',json=data)
17         resp_json = response.json
18         self.assertEqual('TAKEOUT_STORE_NEW' , resp_json[0]['userLabels'][0],resp_json[0]['userLabels'][0])
19 
20     def test02(self):
21         """活跃用户"""
22         data = [{"userId":"20211009102601932734","firstDimension":"TAKEOUT","thirdDimension":"","secondDimension":"STOREID:20627"}]
23         response = self.client.post('/feign/user/labels/', json=data)
24         resp_json = response.json
25         self.assertEqual('TAKEOUT_STORE_ACTIVE' , resp_json[0]['userLabels'][0],resp_json[0]['userLabels'][0])
26 
27     def test03(self):
28         """流失用户"""
29         data = [{"userId":"20211009102601932734","firstDimension":"TAKEOUT","thirdDimension":"","secondDimension":"STOREID:20999"}]
30         response = self.client.post('/feign/user/labels/', json=data)
31         resp_json = response.json
32         self.assertEqual('TAKEOUT_STORE_LOSS' , resp_json[0]['userLabels'][0],resp_json[0]['userLabels'][0])
33 
34     def test04(self):
35         """用户id为空"""
36         data = [{"userId": "", "firstDimension": "TAKEOUT", "thirdDimension": "",
37                  "secondDimension": "STOREID:20999"}]
38         response = self.client.post('/feign/user/labels/', json=data)
39         resp_json = response.json
40         self.assertEqual(222000001, resp_json['code'],resp_json['code'])
41 
42     def test05(self):
43         """批量获取用户标签"""
44         data = [{"userId":"20211009102601932734","firstDimension":"TAKEOUT","thirdDimension":"","secondDimension":"STOREID:20627"},
45                        {"userId": "20211009102601932734", "firstDimension": "GROUP", "thirdDimension": "",
46                         "secondDimension": "STOREID:20627"}]
47         response = self.client.post('/feign/user/labels/', json=data)
48         resp_json = response.json
49         for i in resp_json:
50             if i['firstDimension'] == 'TAKEOUT':
51                 assert 'TAKEOUT_STORE_ACTIVE' == i['userLabels'][0], i['userLabels'][0]
52             elif i['firstDimension'] == 'GROUP':
53                 self.assertEqual( 'TAKEOUT_STORE_LOSS', i['userLabels'][0], i['userLabels'][0])
54 
55 
56 if __name__ == '__main__':
57     unittest.main()