1. 方向
1.1. 统计效能节省
走复用自动化平台统计能力方案
1.2. 实时报告查看
走新开发发送钉钉实时报告能力
2. 获取ms自动化平台的key
2.1. ms 入口:
2.2. key信息:
2.2.1. Access Key
2.2.2. Secrec Key
3. 加密方式 - AES
3.1. AES加密方式实现
def aes_encrypt(src: str, secret_key: str, iv: str) -> str:
if not secret_key:
raise ValueError("secret_key is empty")
try:
# Convert secret_key and iv to bytes
secret_key = secret_key.encode('utf-8')
iv = iv.encode('utf-8')
# Create AES cipher object in CBC mode with PKCS5 padding
cipher = AES.new(secret_key, AES.MODE_CBC, iv)
# Pad the input data to a multiple of AES block size
padded_data = pad(src.encode('utf-8'), AES.block_size)
# Encrypt the data
encrypted = cipher.encrypt(padded_data)
# Return the encrypted data as a base64-encoded string
return base64.b64encode(encrypted).decode('utf-8')
except Exception as e:
raise RuntimeError("AES encrypt error") from e
3.2. 生成签名,以header方式放置进api
def get_headers(access_key: str, secret_key: str) -> dict:
timestamp = int(round(time.time() * 1000))
combox_key = access_key + '|padding|' + str(timestamp)
signature = aes_encrypt(combox_key, secret_key, access_key)
return {'accessKey': access_key, 'signature': signature}
4. 自动化平台api调用方法
class MeterSphere:
def __init__(self):
self.domain = "https://ms.chuanyinet.com"
self.access_key = "access_key "
self.secret_key = "secret_key "
def _request(self, path: str, body: dict = None) -> Union[dict, list]:
"""
:param path:
:param body: if body is empty, will use get method
:return:
"""
url = f"{self.domain.rstrip('/')}/{path.lstrip('/')}"
headers = {'Content-Type': 'application/json', 'ACCEPT': 'application/json'}
headers.update(get_headers(self.access_key, self.secret_key))
logging.info("request url: %s", url)
logging.info("request headers: %s", headers)
if body:
resp = requests.post(url, headers=headers, json=body)
else:
resp = requests.get(url, headers=headers)
return resp.json()
def post_factory_api(self):
res = self._request('/data-factory/data/run', body={
"id": 539,
"params": [],
"mode": "run",
"env": "TEST"
})
logging.warning("post_factory_api: %s", res)
return res
5. 主运行入口调用该方法
if __name__ == "__main__":
# 跑用例之前,调用自动化平台造数接口api,实现自动化数据统计
ms = MeterSphere()
ms.post_factory_api()
pytest.main(['-s', '-v', '.\\testcases\data', '--alluredir', 'allure_result', "--clean-alluredir"])
os.system(r"allure generate .\allure_result\ -o .\allure_report\ --clean")
欢迎关注公众号,与Joker一起探索测试之道。
参考记录: