测试平台 之 缩减版

后端测试平台:
1、连接数据库,定义数据库表字段与创建表
2、使用curl命令将测试脚本上传到平台数据库
3、使用curl命令获取保存在平台数据库中的测试脚本,并保存在本地且命名
4、带参数接口 调用jenkins,jenkins执行测试脚本
勾选 This project is parameterized传参
构建配置:
curl -o %file_name% http://127.0.0.1:8081/get_testcase?name=%name% 【获取平台测试用例保存在本地】
pytest %file_name% --clean-alluredir --alluredir="./%name%" 【pytest执行测试用例,生成allure报告】
jar -cfM0 %name%.zip %name% 【将测试报告文件夹 压缩成zip压缩包 的形式】
curl -F "file=@%name%.zip" -F "name=%name%" http://127.0.0.1:8081/report_upload 【将压缩包上传到测试平台数据库中】
5、获取jenkins执行结果,并保存在平台数据库
  1 import os.path
  2 import random
  3 from flask import Flask, request
  4 from flask_restful import Resource, Api
  5 from flask_sqlalchemy import SQLAlchemy
  6 from jenkinsapi.jenkins import Jenkins
  7 
  8 """
  9 采用flask,flask_restful,flask_sqlalchemy,版本均为目前最新版本
 10 """
 11 app = Flask(__name__)
 12 api = Api(app)
 13 app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:root@127.0.0.1/gin?charset=utf8mb4"
 14 with app.app_context():
 15     db = SQLAlchemy(app)
 16 
 17     class TestCase(db.Model):
 18         __tablename__ = "test_case"
 19         name = db.Column(db.String(80), primary_key=True)
 20         description = db.Column(db.String(100), unique=False, nullable=True)
 21         file_name = db.Column(db.String(100), unique=False, nullable=True)
 22         content = db.Column(db.String(1024), unique=False, nullable=False)
 23         report = db.relationship("Report", backref="test_case", lazy=True)
 24 
 25         def __repr__(self):
 26             return "<TestCase %r>" % self.name
 27 
 28     class Report(db.Model):
 29         id = db.Column(db.Integer, primary_key=True)
 30         description = db.Column(db.String(100), unique=False, nullable=True)
 31         dir = db.Column(db.String(300), unique=False, nullable=False)
 32         testcase_id = db.Column(db.String(80),  db.ForeignKey('test_case.name'), nullable=False)
 33 
 34         def __repr__(self):
 35             return "<Report %r>" % self.id
 36 
 37     # 创建数据库
 38     # db.drop_all()
 39     # db.create_all()
 40 
 41 
 42 class TestCaseServer(Resource):
 43     # 上传用例命令: curl -F "file=@starttime.py" http://127.0.0.1:8081/testcasefile -v
 44     # 上传用例命令:  curl -F "file=@starttime.py" -F "name=starttime" http://127.0.0.1:8081/testcasefile
 45     def post(self):
 46         if "file" in request.files and "name" in request.form:
 47             # 判断请求中是否包含文件名
 48             f = request.files["file"]
 49             name = request.form["name"]
 50             # 提取文件的文件名
 51             file_name = f.filename
 52             # 获取文件内容
 53             content = f.read()
 54             # 存入数据库
 55             testcase = TestCase(name=name, file_name=file_name, content=content)
 56             db.session.add(testcase)
 57             db.session.commit()
 58             # # 设置文件本地保存的路径
 59             # f.save(os.path.join("./upload", file_name))
 60             return {"status": "success", "message": "file uploaded successfully"}
 61         else:
 62             return {"status": "failed", "message": "file uploaded failed"}
 63 
 64 
 65 # curl获取脚本并保存在本地且命名为abc.py: curl.exe -o "abc.py" http://127.0.0.1:8081/testcaserun
 66 @app.route('/get_testcase', methods=['GET'])
 67 def get_testcase():
 68     if "name" in request.args:
 69         name = request.args["name"]
 70         print(name)  # 打印get请求中的name参数值
 71     # 返回存在数据库中的脚本
 72     testcase = TestCase.query.filter_by(name=name).first()
 73     # print(type(testcase.content))  # <class 'str'>
 74     # print(testcase.content)  # 文件的内容
 75     return testcase.content
 76 
 77 
 78 # 平台实现使用接口启动jenkins(run)
 79 @app.route('/run', methods=['GET'])
 80 def run():
 81     if "name" in request.args:
 82         name = request.args["name"]
 83         testcase = TestCase.query.filter_by(name=name).first()
 84         j = Jenkins('http://127.0.0.1:8080/jenkins', username='hankai', password='11f229d6bf0a95971de5297aba499d0f29')
 85         print(j.keys())  # 打印jenkins的job
 86         j["cekai16"].invoke(build_params={"name": name, "file_name": testcase.file_name})
 87         return {"status": "success", "message": "call successfully"}
 88 
 89 
 90 @app.route('/report_upload', methods=['POST'])
 91 def report_upload():
 92     if "file" in request.files and  "name" in request.form:
 93         file = request.files["file"]
 94         name = request.form["name"]
 95         DIR = r"C:\Users\love8\Desktop\tmp"
 96         file_name = file.filename
 97         dir = os.path.join(DIR, file_name)
 98         file.save(dir)
 99         report = Report(dir=dir, testcase_id=name)
100         db.session.add(report)
101         db.session.commit()
102         return {"status": "success", "message": "call successfully"}
103 
104 
105 api.add_resource(TestCaseServer, '/testcasefile')
106 
107 
108 if __name__ == '__main__':
109     app.run(host='0.0.0.0', port=8081, debug=True)

 

posted @ 2023-08-22 22:15  韩凯1202  阅读(17)  评论(0编辑  收藏  举报