当前位置:网站首页>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.
边栏推荐
- 一台服务器最大并发 tcp 连接数多少?65535?
- Famous blackmail software stops operation and releases decryption keys. Most hospital IOT devices have security vulnerabilities | global network security hotspot on February 14
- Characteristic polynomial and constant coefficient homogeneous linear recurrence
- 《天天数学》连载56:二月二十五日
- 探索Cassandra的去中心化分布式架构
- [solved] access denied for user 'root' @ 'localhost' (using password: yes)
- Mongodb installation and basic operation
- 远程文件包含实操
- 请求头不同国家和语言的表示
- 2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
猜你喜欢
消息队列消息丢失和消息重复发送的处理策略
远程文件包含实操
[solved] access denied for user 'root' @ 'localhost' (using password: yes)
"Remake Apple product UI with Android" (3) - elegant statistical chart
The mixlab editing team is recruiting teammates~~
面试官:JVM如何分配和回收堆外内存
Mysql 单表字段重复数据取最新一条sql语句
跟我学企业级flutter项目:简化框架demo参考
Multithread 02 thread join
Unreal_DataTable 实现Id自增与设置RowName
随机推荐
Extraction of the same pointcut
Advanced Mathematics (Seventh Edition) Tongji University exercises 2-1 personal solutions
近视:摘镜or配镜?这些问题必须先了解清楚
远程文件包含实操
EditText request focus - EditText request focus
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (4)
【声明】关于检索SogK1997而找到诸多网页爬虫结果这件事
SDNU_ ACM_ ICPC_ 2022_ Winter_ Practice_ 4th [individual]
Explore Cassandra's decentralized distributed architecture
ASEMI整流桥UMB10F参数,UMB10F规格,UMB10F封装
Pyinstaller is not an internal or external command, nor is it a runnable program or batch file
Why does the std:: string operation perform poorly- Why do std::string operations perform poorly?
初试scikit-learn库
记一次jar包冲突解决过程
Top k questions of interview
How to set up SVN server on this machine
Is it safe to open an account with tongdaxin?
[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)
Asemi rectifier bridge umb10f parameters, umb10f specifications, umb10f package
How to thicken the brush in the graphical interface