├── .gitignore ├── LICENSE ├── README.md ├── api ├── __init__.py └── template_api.py ├── app.py ├── config └── config.json ├── data └── test_data.json ├── doc └── requirements.txt ├── pytest.ini ├── scripts ├── __init__.py └── test_template.py └── tools ├── __init__.py ├── config_info.py ├── get_log.py └── read_file.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | 107 | .idea/ 108 | /report 109 | /log 110 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 nonevx 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # auto_api_test 2 | 3 | 4 | > 开发环境: Pycharm 5 | > 6 | > 开发语言&版本: python3.7.8 7 | > 8 | > 测试框架: Pytest、测试报告: Allure 9 | > 10 | > 版本管理: Git 11 | 12 | ## 项目目录结构 13 | 14 | - api -- 模仿PO模式, 抽象出页面类, 页面类内包含页面所包含所有接口, 并封装成方法可供其他模块直接调用 15 | - config -- 配置文件目录 16 | - data -- 测试数据目录 17 | - doc -- 文档存放目录 18 | - log -- 日志 19 | - report -- 测试报告 20 | - scripts -- 测试脚本存放目录 21 | - tools -- 工具类目录 22 | - .gitignore -- git忽略 23 | - app.py -- 命令行启动入口 24 | - pytest.ini -- pytest测试框架配置文件 25 | - README.md -- 开发说明文档 26 | 27 | ## 代码分析 28 | 29 | **pytest.ini** 30 | 31 | > pytest框架的配置文件 32 | 33 | ```python 34 | [pytest] 35 | addopts = --html=../report/report.html # pytest-html报告插件配置 36 | ;addopts = -s --alluredir report # allure-pytest报告插件配置 37 | testpaths = ./scripts # 设置用例目录识别名称 38 | python_files = test*_*.py # 设置测试文件识别名称 39 | python_classes = Test* # 设置测试类识别名称 40 | python_functions = test_* # 设置测试方法识别名称 41 | ``` 42 | 43 | **app.py** 44 | 45 | > 46 | 47 | ```python 48 | # 基础路由(方便在部署环境发生变化时切换全局基础路由) 49 | BASE_URL = "http://xxxx.com" 50 | # 获取脚本的绝对路径(脚本在项目根目录就可以理解为项目路径) 51 | ABS_PATH = os.path.abspath(__file__) 52 | BASE_DIR = os.path.dirname(ABS_PATH) 53 | 54 | # 命令行启动此脚本时执行测试用例 55 | pytest.main(["scripts/"]) 56 | ``` 57 | 58 | **/config/config.json** 59 | 60 | > 配置文件, 目前包含全局请求头配置、类似全局变量的设置, 可通过tools内的工具函数进行读写 61 | > 请求头具体参数根据需要自行配置 62 | 63 | ```json 64 | { 65 | "headers": { 66 | "Host": "xxx.com", 67 | "Connection": "keep-alive", 68 | "Accept": "application/json, text/plain, */*", 69 | "Authorization": "xxxx", 70 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36", 71 | "Content-Type": "application/json;charset=UTF-8", 72 | "Origin": "http://xxx.com", 73 | "Referer": "http://xxx.com/", 74 | "Accept-Encoding": "gzip, deflate", 75 | "Accept-Language": "zh-CN,zh;q=0.9" 76 | } 77 | } 78 | ``` 79 | 80 | **/api/template_api.py** 81 | 82 | > 页面类模板, 包含页面接口的请求方法(增删改查)封装, 主要在此定义好接口和请求入参等内容 83 | 84 | ```python 85 | # 导包 86 | import app 87 | import json 88 | from tools.config_info import get_header 89 | 90 | 91 | class TemplateAPI: 92 | # xx添加接口 93 | api_add_url = app.BASE_URL + "/xxx/xxxx/add" 94 | # xx修改接口 95 | api_upd_url = app.BASE_URL + "/xxx/xxxx/upd" 96 | # xx查询接口 97 | api_get_url = app.BASE_URL + "/xxx/xxxx/get" 98 | # xx删除接口 99 | api_del_url = app.BASE_URL + "/xxx/xxxx/del/{id}" 100 | 101 | # xx添加接口函数实现 102 | def api_add(self, session, attr1, attr2): 103 | post_data = { 104 | "attr1": attr1, 105 | "attr2": attr2 106 | } 107 | return session.post(self.api_add_url, headers=get_header(), data=json.dumps(post_data)) 108 | 109 | # xx修改接口函数实现 110 | def api_upd(self, session, attr1, attr2): 111 | put_data = { 112 | "attr1": attr1, 113 | "attr2": attr2 114 | } 115 | return session.put(self.api_upd_url, headers=get_header(), data=json.dumps(put_data)) 116 | 117 | # xx查询接口函数实现 118 | def api_get(self, session, attr1, attr2): 119 | params = { 120 | "attr1": attr1, 121 | "attr2": attr2 122 | } 123 | return session.get(self.api_get_url, headers=get_header(), params=params) 124 | 125 | # xx删除接口函数实现 126 | def api_del(self, session, uid): 127 | return session.delete(self.api_del_url.format(id=uid), headers=get_header()) 128 | 129 | ``` 130 | 131 | **/scripts/test_template.py** 132 | 133 | > 测试类以Test开头, 测试类和测试方法添加allure装饰器 134 | > 135 | > 前置测试类方法 - 初始化requests请求库的session对象, 创建对应的页面对象 136 | > 137 | > 后置测试类方法 - 关闭session对象 138 | > 139 | > 前置测试方法 - 加休眠 140 | > 141 | > 测试方法中添加可选参数化装饰器, 测试方法中通过页面对象调用页面接口请求方法, 传入requests的session对象和方法需要的必要参数, 进行响应结果的处理和断言等操作 142 | > 143 | > 日志器可通过引入工具调用 144 | 145 | ```python 146 | # 导包 147 | import pytest 148 | import requests 149 | from time import sleep 150 | from api.template_api import TemplateAPI 151 | from tools.get_log import GetLog 152 | from tools.read_file import read_json 153 | import allure 154 | 155 | # 获取日志器 156 | log = GetLog.get_log() 157 | 158 | 159 | @allure.feature('测试类模板') 160 | class TestTemplate: 161 | session = None 162 | 163 | # 初始化方法 164 | @classmethod 165 | def setup_class(cls): 166 | cls.session = requests.Session() # 初始化session对象 167 | cls.template = TemplateAPI() 168 | 169 | # 结束方法 170 | @classmethod 171 | def teardown_class(cls): 172 | cls.session.close() 173 | 174 | @classmethod 175 | def setup(cls): 176 | sleep(1.5) 177 | 178 | # 测试方法 179 | @allure.story("测试方法模板-add") 180 | @pytest.mark.parametrize(("attr1", "attr2", "success", "expect"), read_json("test_add")) 181 | def test_add(self, attr1, attr2, success, expect): 182 | # 添加功能API调用 183 | response = self.template.api_add(self.session, attr1, attr2) 184 | # 打印日志 185 | log.info("添加功能-状态码为: {}".format(response.status_code)) 186 | # 断言状态码 187 | assert response.status_code == expect, "状态码断言失败" 188 | 189 | @allure.story("测试方法模板-upd") 190 | @pytest.mark.parametrize(("attr1", "attr2", "success", "expect"), read_json("test_upd")) 191 | def test_upd(self, attr1, attr2, success, expect): 192 | # 添加功能API调用 193 | response = self.template.api_upd(self.session, attr1, attr2) 194 | # 打印日志 195 | log.info("修改功能-状态码为: {}".format(response.status_code)) 196 | # 断言状态码 197 | assert response.status_code == expect, "状态码断言失败" 198 | 199 | @allure.story("测试方法模板-get") 200 | @pytest.mark.parametrize(("attr1", "attr2", "success", "expect"), read_json("test_get")) 201 | def test_get(self, attr1, attr2, success, expect): 202 | # 添加功能API调用 203 | response = self.template.api_get(self.session, attr1, attr2) 204 | # 打印日志 205 | log.info("查询功能-状态码为: {}".format(response.status_code)) 206 | # 断言状态码 207 | assert response.status_code == expect, "状态码断言失败" 208 | 209 | @allure.story("测试方法模板-del") 210 | @pytest.mark.parametrize(("uid", "success", "expect"), read_json("test_del")) 211 | def test_del(self, uid, success, expect): 212 | # 添加功能API调用 213 | response = self.template.api_del(self.session, uid) 214 | # 打印日志 215 | log.info("删除功能-状态码为: {}".format(response.status_code)) 216 | # 断言状态码 217 | assert response.status_code == expect, "状态码断言失败" 218 | 219 | ``` 220 | 221 | **/data | /tools** 222 | 223 | > 测试数据和具体的操作工具类根据需要自定义 224 | -------------------------------------------------------------------------------- /api/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : __init__.py 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/5 11:35 8 | @Desc : 9 | """ 10 | -------------------------------------------------------------------------------- /api/template_api.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : template_api 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/20 16:54 8 | @Desc : 9 | """ 10 | 11 | # 导包 12 | import app 13 | import json 14 | from tools.config_info import get_header 15 | 16 | 17 | class TemplateAPI: 18 | # xx添加接口 19 | api_add_url = app.BASE_URL + "/xxx/xxxx/add" 20 | # xx修改接口 21 | api_upd_url = app.BASE_URL + "/xxx/xxxx/upd" 22 | # xx查询接口 23 | api_get_url = app.BASE_URL + "/xxx/xxxx/get" 24 | # xx删除接口 25 | api_del_url = app.BASE_URL + "/xxx/xxxx/del/{id}" 26 | 27 | # xx添加接口函数实现 28 | def api_add(self, session, attr1, attr2): 29 | post_data = { 30 | "attr1": attr1, 31 | "attr2": attr2 32 | } 33 | return session.post(self.api_add_url, headers=get_header(), data=json.dumps(post_data)) 34 | 35 | # xx修改接口函数实现 36 | def api_upd(self, session, attr1, attr2): 37 | put_data = { 38 | "attr1": attr1, 39 | "attr2": attr2 40 | } 41 | return session.put(self.api_upd_url, headers=get_header(), data=json.dumps(put_data)) 42 | 43 | # xx查询接口函数实现 44 | def api_get(self, session, attr1, attr2): 45 | params = { 46 | "attr1": attr1, 47 | "attr2": attr2 48 | } 49 | return session.get(self.api_get_url, headers=get_header(), params=params) 50 | 51 | # xx删除接口函数实现 52 | def api_del(self, session, uid): 53 | return session.delete(self.api_del_url.format(id=uid), headers=get_header()) 54 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | 2 | # -*- coding:utf-8 -*- 3 | 4 | """ 5 | @File : app 6 | @Author : Chen 7 | @Contact : nonevxx@gmail.com 8 | @Date : 2021/1/11 15:08 9 | @Desc : 10 | """ 11 | 12 | import os 13 | import pytest 14 | 15 | 16 | # 基础路由 17 | BASE_URL = "http://localhost:5000" 18 | 19 | # 获取脚本的绝对路径 20 | ABS_PATH = os.path.abspath(__file__) 21 | BASE_DIR = os.path.dirname(ABS_PATH) 22 | 23 | pytest.main(["scripts/"]) 24 | -------------------------------------------------------------------------------- /config/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "headers": { 3 | "Host": "xxx.com", 4 | "Connection": "keep-alive", 5 | "Accept": "application/json, text/plain, */*", 6 | "Authorization": "xxxxx", 7 | "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36", 8 | "Content-Type": "application/json;charset=UTF-8", 9 | "Origin": "http://xxx.com", 10 | "Referer": "http://xxx.com/", 11 | "Accept-Encoding": "gzip, deflate", 12 | "Accept-Language": "zh-CN,zh;q=0.9" 13 | } 14 | } -------------------------------------------------------------------------------- /data/test_data.json: -------------------------------------------------------------------------------- 1 | { 2 | "规则": "所有key值保持和测试方法名称一致", 3 | "test_add": [ 4 | { 5 | "attr1": "attr1", 6 | "attr2": "attr2", 7 | "success": "true", 8 | "expect": "200" 9 | }, 10 | { 11 | "attr1": "attr3", 12 | "attr2": "attr4", 13 | "success": "true", 14 | "expect": "200" 15 | } 16 | ], 17 | "test_upd": [ 18 | { 19 | "attr1": "attr5", 20 | "attr2": "attr6", 21 | "success": "true", 22 | "expect": "200" 23 | }, 24 | { 25 | "attr1": "attr7", 26 | "attr2": "attr8", 27 | "success": "true", 28 | "expect": "200" 29 | } 30 | ], 31 | "test_get": [ 32 | { 33 | "attr1": "attr5", 34 | "attr2": "attr6", 35 | "success": "true", 36 | "expect": "200" 37 | }, 38 | { 39 | "attr1": "attr7", 40 | "attr2": "attr8", 41 | "success": "true", 42 | "expect": "200" 43 | } 44 | ], 45 | "test_del": [ 46 | { 47 | "uid": 1, 48 | "success": "true", 49 | "expect": "200" 50 | }, 51 | { 52 | "uid": 2, 53 | "success": "true", 54 | "expect": "200" 55 | } 56 | ] 57 | } -------------------------------------------------------------------------------- /doc/requirements.txt: -------------------------------------------------------------------------------- 1 | pytest==6.2.1 2 | pytest-html==3.1.1 3 | requests==2.25.1 4 | allure-pytest==2.8.31 5 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | ;addopts = --html=../report/report.html 3 | addopts = -s --alluredir report 4 | testpaths = ./scripts 5 | python_files = test*_*.py 6 | python_classes = Test* 7 | python_functions = test_* -------------------------------------------------------------------------------- /scripts/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : __init__.py 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/5 11:33 8 | @Desc : 9 | """ 10 | -------------------------------------------------------------------------------- /scripts/test_template.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : test_template 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/20 20:09 8 | @Desc : 9 | """ 10 | 11 | # 导包 12 | import pytest 13 | import requests 14 | from time import sleep 15 | from api.template_api import TemplateAPI 16 | from tools.get_log import GetLog 17 | from tools.read_file import read_json 18 | import allure 19 | 20 | # 获取日志器 21 | log = GetLog.get_log() 22 | 23 | 24 | @allure.feature('测试类模板') 25 | @pytest.skip("参考模板, 不执行") 26 | class TestTemplate: 27 | session = None 28 | 29 | # 初始化方法 30 | @classmethod 31 | def setup_class(cls): 32 | cls.session = requests.Session() # 初始化session对象 33 | cls.template = TemplateAPI() 34 | 35 | # 结束方法 36 | @classmethod 37 | def teardown_class(cls): 38 | cls.session.close() 39 | 40 | @classmethod 41 | def setup(cls): 42 | sleep(1.5) 43 | 44 | # 测试方法 45 | @allure.story("测试方法模板-add") 46 | @pytest.mark.parametrize(("attr1", "attr2", "success", "expect"), read_json("test_add")) 47 | def test_add(self, attr1, attr2, success, expect): 48 | # 添加功能API调用 49 | response = self.template.api_add(self.session, attr1, attr2) 50 | # 打印日志 51 | log.info("添加功能-状态码为: {}".format(response.status_code)) 52 | # 断言状态码 53 | assert response.status_code == expect, "状态码断言失败" 54 | 55 | @allure.story("测试方法模板-upd") 56 | @pytest.mark.parametrize(("attr1", "attr2", "success", "expect"), read_json("test_upd")) 57 | def test_upd(self, attr1, attr2, success, expect): 58 | # 添加功能API调用 59 | response = self.template.api_upd(self.session, attr1, attr2) 60 | # 打印日志 61 | log.info("修改功能-状态码为: {}".format(response.status_code)) 62 | # 断言状态码 63 | assert response.status_code == expect, "状态码断言失败" 64 | 65 | @allure.story("测试方法模板-get") 66 | @pytest.mark.parametrize(("attr1", "attr2", "success", "expect"), read_json("test_get")) 67 | def test_get(self, attr1, attr2, success, expect): 68 | # 添加功能API调用 69 | response = self.template.api_get(self.session, attr1, attr2) 70 | # 打印日志 71 | log.info("查询功能-状态码为: {}".format(response.status_code)) 72 | # 断言状态码 73 | assert response.status_code == expect, "状态码断言失败" 74 | 75 | @allure.story("测试方法模板-del") 76 | @pytest.mark.parametrize(("uid", "success", "expect"), read_json("test_del")) 77 | def test_del(self, uid, success, expect): 78 | # 添加功能API调用 79 | response = self.template.api_del(self.session, uid) 80 | # 打印日志 81 | log.info("删除功能-状态码为: {}".format(response.status_code)) 82 | # 断言状态码 83 | assert response.status_code == expect, "状态码断言失败" 84 | -------------------------------------------------------------------------------- /tools/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : __init__.py 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/5 11:34 8 | @Desc : 9 | """ 10 | -------------------------------------------------------------------------------- /tools/config_info.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : get_header 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/11 18:34 8 | @Desc : 9 | """ 10 | 11 | import json 12 | import app 13 | from tools.get_log import GetLog 14 | 15 | # 获取日志器 16 | log = GetLog.get_log() 17 | 18 | 19 | # 待废弃, 由get_config代替 20 | def get_header(): 21 | try: 22 | with open(app.BASE_DIR + "/config/config.json", "r", encoding="utf-8") as f: 23 | result = json.load(f) 24 | return result['headers'] 25 | except Exception as ex: 26 | log.error(ex) 27 | 28 | 29 | # 待废弃, 由set_submenu代替 30 | def set_token(token): 31 | try: 32 | with open(app.BASE_DIR + "/config/config.json", "r", encoding="utf-8") as f: 33 | result = json.load(f) 34 | result['headers']['Authorization'] = token 35 | 36 | with open(app.BASE_DIR + "/config/config.json", "w", encoding="utf-8") as f: 37 | json.dump(result, f) 38 | except Exception as ex: 39 | log.error(ex) 40 | 41 | 42 | def set_config(key, value): 43 | try: 44 | with open(app.BASE_DIR + "/config/config.json", "r", encoding="utf-8") as f: 45 | result = json.load(f) 46 | result[key] = value 47 | 48 | with open(app.BASE_DIR + "/config/config.json", "w", encoding="utf-8") as f: 49 | json.dump(result, f) 50 | except Exception as ex: 51 | log.error(ex) 52 | 53 | 54 | def get_config(key): 55 | try: 56 | with open(app.BASE_DIR + "/config/config.json", "r", encoding="utf-8") as f: 57 | result = json.load(f) 58 | return result[key] 59 | except Exception as ex: 60 | log.error(ex) 61 | 62 | 63 | def get_submenu(key_1, key_2): 64 | obj = get_config(key_1) 65 | if key_2 in obj.keys: 66 | return obj[key_2] 67 | else: 68 | return None 69 | 70 | 71 | def set_submenu(key_1, key_2, value): 72 | obj = get_config(key_1) 73 | obj[key_2] = value 74 | set_config(key_1, obj) 75 | 76 | 77 | if __name__ == '__main__': 78 | pass 79 | -------------------------------------------------------------------------------- /tools/get_log.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : get_log 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/11 9:54 8 | @Desc : 9 | """ 10 | 11 | # 导包 12 | import logging.handlers 13 | import app 14 | 15 | 16 | class GetLog: 17 | logger = None 18 | 19 | @classmethod 20 | def get_log(cls): 21 | if cls.logger is None: 22 | # 获取日志器 23 | cls.logger = logging.getLogger() 24 | # 设置级别 25 | cls.logger.setLevel(logging.INFO) 26 | # 获取处理器 27 | th = logging.handlers.TimedRotatingFileHandler(filename=app.BASE_DIR + "/log/log.log", 28 | when="midnight", 29 | interval=1, 30 | backupCount=3, 31 | encoding="utf-8") 32 | # 设置处理器级别 33 | th.setLevel(logging.INFO) 34 | # 获取格式器 35 | fmt = "%(asctime)s %(levelname)s [%(name)s] [%(filename)s (%(funcName)s:%(lineno)d] - %(message)s" 36 | fm = logging.Formatter(fmt) 37 | # 将格式器添加到处理器 38 | th.setFormatter(fm) 39 | # 将处理器添加到日志器 40 | cls.logger.addHandler(th) 41 | # 返回日志器 42 | return cls.logger 43 | -------------------------------------------------------------------------------- /tools/read_file.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | """ 4 | @File : read_txt 5 | @Author : Chen 6 | @Contact : nonevxx@gmail.com 7 | @Date : 2021/1/11 9:54 8 | @Desc : 9 | """ 10 | 11 | import app 12 | import json 13 | 14 | 15 | def read_txt(filename): 16 | filepath = app.BASE_DIR + "/data/" + filename 17 | with open(filepath, "r", encoding="utf-8") as f: 18 | return f.readlines() 19 | 20 | 21 | # 规定test_data.json格式固定, 一级key值固定为api文件同名, attr为key值 22 | def read_json(attr): 23 | # 1、新建空列表,接收解析结果 24 | arr = list() 25 | # 2、解析文件组织数据添加进列表 26 | with open(app.BASE_DIR + "/data/test_data.json", "r", encoding="utf-8") as f: 27 | result = json.load(f) 28 | for i in result[attr]: 29 | row = i.values() 30 | arr.append(list(row)) 31 | return arr 32 | --------------------------------------------------------------------------------