当前位置:网站首页>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.

边栏推荐
- 在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- 拼夕夕二面:说说布隆过滤器与布谷鸟过滤器?应用场景?我懵了。。
- [list to map] collectors Tomap syntax sharing (case practice)
- Why can't strings be directly compared with equals; Why can't some integers be directly compared with the equal sign
- PHP二级域名session共享方案
- NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线
- 为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
- The mixlab editing team is recruiting teammates~~
- SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
猜你喜欢

Multithread 02 thread join

Myopia: take off or match glasses? These problems must be understood clearly first

【Proteus仿真】8×8LED点阵屏仿电梯数字滚动显示

Visual SLAM algorithms: a survey from 2010 to 2016

Low level version of drawing interface (explain each step in detail)

How can technology managers quickly improve leadership?

8 cool visual charts to quickly write the visual analysis report that the boss likes to see

From the 18th line to the first line, the new story of the network security industry

Cocos Creator 2.x 自动打包(构建 + 编译)
![[solved] access denied for user 'root' @ 'localhost' (using password: yes)](/img/71/1ff8ed1d773da99054310f96dca3f8.jpg)
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
随机推荐
Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
EditText request focus - EditText request focus
程序猿如何快速成长
[combinatorics] combinatorial identity (sum of combinatorial identity products 1 | sum of products 1 proof | sum of combinatorial identity products 2 | sum of products 2 proof)
Develop team OKR in the way of "crowdfunding"
Unity project optimization case 1
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Mb10m-asemi rectifier bridge mb10m
uploads-labs靶场(附源码分析)(更新中)
Colab works with Google cloud disk
Mixlab编辑团队招募队友啦~~
ThreeJS 第二篇:顶点概念、几何体结构
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Client does not support authentication protocol requested by server; consider upgrading MySQL client
深入理解 SQL 中的 Grouping Sets 语句
[proteus simulation] 74hc595+74ls154 drive display 16x16 dot matrix
Visual SLAM algorithms: a survey from 2010 to 2016
Hibernate的缓存机制/会话级缓存机制
六月 致 -.-- -..- -
Explore Netease's large-scale automated testing solutions see here see here