当前位置:网站首页>Add color to the interface automation test framework and realize the enterprise wechat test report
Add color to the interface automation test framework and realize the enterprise wechat test report
2022-07-03 16:27:00 【Programming simplicity】
Preface
In the new project, the author built python+requests+unittest+HTMLTestRunner Interface automation test framework , Realize environment isolation by modifying the configuration file , One script can execute interface test cases in different environments .
But there is no implementation of any form of message notification , Nor is it integrated into jenkins, The reason is simple , Because I haven't done much yet , And users are not active enough , There are also relatively few problems , Execute only once before and after going online uat and prod Environmental Science .
I want to improve the message notification function these days , Let it have the function of sending messages and reports .
technological process
Code
The process and code functions have been described in the notes
'''
Created on 2021 year 5 month 12 Japan
@author: qguan
'''
import time
import unittest
from Librarys.HTMLTestRunnerNew import HTMLTestRunnerNew
from common.dirs_config import testcases_path, report_dir
from unitconfig import env, conf
from utils.handle_notice import send_weixin, upload_weixin
# current time
curTime = time.strftime("%Y-%m-%d_%H-%M")
# Create test suites and load test cases
suite = unittest.TestSuite()
loader = unittest.TestLoader()
suite.addTest(loader.discover(start_dir=testcases_path, pattern='flaget_*.py'))
# Splicing test report path and file name
report_name = report_dir + "FlagetAPI_{}_report_{}.html".format(env, curTime)
# Report generator
with open(report_name, "wb") as pf:
runner = HTMLTestRunnerNew(stream=pf, title="Flaget Interface automation test report ", tester="joe-tester", description="{} Environmental Science , Business scenario interface test ".format(env))
runner.run(suite)
# Organization report details
msg = "\n \
execution environment :{}\n \
Testers :{}\n \
Starting time :{}\n \
The duration of the :{}\n \
test result :{}, The pass rate is :{} \n \
\n \
The details of the report need to be in PC open , The mobile terminal is opened as HTML Source code !".format(env, runner.tester, runner.startTime, runner.duration, runner.status, runner.passrate)
# Get enterprise wechat key
prod_key = conf.get_value("notice", "prod_key")
uat_key = conf.get_value("notice", "uat_key")
if env == "prod": # Send reports to project stakeholders only in the production environment
media_id = upload_weixin(key=prod_key, filename=report_name)
send_weixin(msg, key=prod_key)
send_weixin(media_id, key=prod_key, v_type="file", k_type="media_id")
elif env == "uat": # uat Send the environmental report to the team
send_weixin(msg, key=uat_key)
Copy code
analysis
python How to request to send a message to enterprise wechat ; This process has been described in other articles , No more details here , Or refer to the configuration instructions of enterprise wechat .
python Realization
def send_weixin(msg,key=None, **kwargs):
'''
Send enterprise wechat notification
'''
headers = {"Content-Type":"application/json"}
# Address
url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}"
if not key:
print("key Can't be empty ")
raise
url = url.format(key)
if not kwargs: # Send text by default
v_type = "text"
k_type = "content"
else: # If there is an incoming, on demand
v_type=kwargs.get("v_type")
k_type=kwargs.get("k_type")
# Request body
data = {
"msgtype": v_type,
v_type: {
k_type: msg
}
}
# Send a request
requests.post(url, json=data, headers=headers)
Copy code
problem : How to upload and send files ?
Carefully read the configuration instructions of enterprise wechat robot , How to upload files and send ; The subject of sending the message selects whether to send text or file according to different parameters ;
msgtype: Support text、file、image、markdown、news And other templates , Next, there are instructions for uploading files in the configuration instructions :
python Realization
def upload_weixin(key=None, filename=None):
"""
Upload the attachment to enterprise wechat , get media_id. Then send a message to inform , Viewable files
"""
if not key:
print("key Can't be empty ")
raise
# Request address
url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={}&type=file".format(key)
# Request header
headers = {"Content-Type":"multipart/form-data"}
# Request data , yes rb Read file stream
data = {"file":open(filename, "rb")}
# Send a request
res = requests.post(url, files=data, headers=headers).json()
# Get the... Returned by the result media_id It is used for the interface parameters of sending messages .
return res.get("media_id")
Copy code
summary
The above analyzes the process code implementation , How to upload files and send messages ; Go back to the first piece of code , The logical understanding is very clear ; This is not integrated for the time being jenkins.
边栏推荐
- [redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
- Golang decorator mode and its use in NSQ
- June to - -------
- 【Proteus仿真】74HC595+74LS154驱动显示16X16点阵
- Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
- Embedded development: seven reasons to avoid open source software
- Unreal_DataTable 实现Id自增与设置RowName
- First!! Is lancet hungry? Official documents
- Golang 装饰器模式以及在NSQ中的使用
- 中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
猜你喜欢
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
The difference between calling by value and simulating calling by reference
[list to map] collectors Tomap syntax sharing (case practice)
Explore Cassandra's decentralized distributed architecture
线程池执行定时任务
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
NSQ源码安装运行过程
Unreal_DataTable 实现Id自增与设置RowName
"Remake Apple product UI with Android" (3) - elegant statistical chart
Remote file contains actual operation
随机推荐
记一次jar包冲突解决过程
Initial test of scikit learn Library
ThreeJS 第二篇:顶点概念、几何体结构
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
Stm32f103c8t6 firmware library lighting
Page dynamics [2]keyframes
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
NSQ source code installation and operation process
Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
Is it safe to open an account with tongdaxin?
Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
8 cool visual charts to quickly write the visual analysis report that the boss likes to see
Remote file contains actual operation
AcWing 第58 场周赛
Effect of ARP package on FTP dump under vxworks-6.6 system
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
高等数学(第七版)同济大学 习题2-1 个人解答
Leetcode binary search tree
From "zero sum game" to "positive sum game", PAAS triggered the third wave of cloud computing
如何在本机搭建SVN服务器