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

边栏推荐
- Stm32f103c8t6 firmware library lighting
- Golang anonymous function use
- Hibernate的缓存机制/会话级缓存机制
- Embedded development: seven reasons to avoid open source software
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- Explore Cassandra's decentralized distributed architecture
- Nifi from introduction to practice (nanny level tutorial) - flow
- Mixlab编辑团队招募队友啦~~
- Extraction of the same pointcut
- TCP擁塞控制詳解 | 3. 設計空間
猜你喜欢

探索Cassandra的去中心化分布式架构

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

A survey of state of the art on visual slam

初试scikit-learn库

TCP congestion control details | 3 design space
![App mobile terminal test [4] APK operation](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [4] APK operation
![[statement] about searching sogk1997 and finding many web crawler results](/img/1a/8ed3ca0030ea227adcd95e8b306aca.png)
[statement] about searching sogk1997 and finding many web crawler results

NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
![App mobile terminal test [3] ADB command](/img/f1/4bff6e66b77d0f867bf7237019e982.png)
App mobile terminal test [3] ADB command

Colab works with Google cloud disk
随机推荐
Custom plug-in construction and use of QT plug-in
PHP secondary domain name session sharing scheme
Caching mechanism of Hibernate / session level caching mechanism
Nifi from introduction to practice (nanny level tutorial) - flow
切入点表达式
EditText request focus - EditText request focus
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Mongodb installation and basic operation
记一次jar包冲突解决过程
Low level version of drawing interface (explain each step in detail)
用通达信炒股开户安全吗?
Chinese translation of Tagore's floating birds (1~10)
关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
nifi从入门到实战(保姆级教程)——flow
[redis foundation] understand redis master-slave architecture, sentinel mode and cluster together (Demo detailed explanation)
Multithread 02 thread join
[web security] - [SQL injection] - error detection injection
为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日