【最佳实践】使用 Python3 调用 MeterSphereV3 API 触发测试计划

V2的原官方文档
https://kb.fit2cloud.com/?p=249


import base64
import time
import uuid
from Cryptodome.Cipher import AES # pip install pycryptodomex
import requests


def aesEncrypt(text, secretKey, iv):
    BS = AES.block_size  # 块大小为16字节(128位)

    # 补全函数,使用 PKCS7 padding
    def pad(s):
        padding_length = BS - len(s.encode('utf-8')) % BS
        return s + chr(padding_length) * padding_length

    # 创建 AES 加密器
    cipher = AES.new(secretKey.encode('utf-8'), AES.MODE_CBC, iv.encode('utf-8'))
    # 加密并 base64 编码
    padded_text = pad(text)
    encrypted_bytes = cipher.encrypt(padded_text.encode('utf-8'))
    b64_encrypted = base64.b64encode(encrypted_bytes)
    # 返回字符串形式
    return b64_encrypted.decode('utf-8')


def setHeaders(s11, accessKey, secretKey):
    timeStamp = int(round(time.time() * 1000))
    combox_key = accessKey + '|' + str(uuid.uuid4()) + '|' + str(timeStamp)
    signature = aesEncrypt(combox_key, secretKey, accessKey)
    header = {
        'Content-Type': 'application/json',
        'ACCEPT': 'application/json',
        'accessKey': accessKey,
        'signature': signature,  # No need to decode as it's already a string
        'Connection': 'close'
    }
    s11.headers.update(header)
    return s11

def plan_run(s, host, testPlanId):
    url = host + "/test-plan-execute/single"
    body = {"executeId": testPlanId ,"runMode":"SERIAL","executionSource":"MANUAL"}
    r = s.post(url, json=body)
    return r


def exec_run(accessKey, secretKey, host, testPlanId):
    s22 = requests.session()
    s33 = setHeaders(s22, accessKey, secretKey)  # 设置请求头
    r = plan_run(s33, host, testPlanId)
    print("测试结果", r.json())


# Example usage
if __name__ == '__main__':
    exec_run("sdasada", "sghAaCsdsfyIknQjbV5K", "http://11.8.65.110:8888", 1042422922698752)

testPlanId: 点击执行测试计划得到1042422922698752
accessKey,secretKey ;个人中心获取

image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。