程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Third: python+pytest+jenkins+allure email sending + nailing robot notification

編輯:Python

One . Realize the idea

0. Mail and nails should be executed after the completion of automatic execution , Otherwise, the data and reports are not up-to-date ( a key )
1. Because the report is displayed online , I'm local , Just use selenium Go to login screenshot 2 Pages of reports , You can also cut more . Save to local
2. Email sending report is realized through email sending screenshot attachment
3.jenkins The drop-down code is stored in the workspace , Save workspace locally , Find the data file of the report to see if there is any... That can count the test results
4. Process the original report data file and send it to the nail robot

Two . Report screenshots

''' obtain jenkins The report screenshot is based on selenium'''
import math
from selenium import webdriver
from time import sleep
def ctrl_alt():
def driver_(test=1):
if test == 1:
# Run the browser in the background , Don't open , But it is not complete to open the screenshot like this , Not solved yet , It uses the method of opening the browser , use Jenkins Run time is traceless , Very good 
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=chrome_options)
else:
# Launch the browser 
driver = webdriver.Chrome()
return driver
driver = driver_(2)
driver.get('http://127.0.0.1:1111/login?from=%2F')
driver.maximize_window()
a = driver.find_elements_by_class_name('normal')
a[0].send_keys('lcf') # account number 
a[1].send_keys(123456) # password 
driver.find_element_by_class_name('submit-button').click() # Sign in 
# Report page 1
print(' Here's the screenshot ')
driver.get('http://127.0.0.1:1111/job/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95-gitee%E4%BB%A3%E7%A0%81/allure/')
sleep(1)
driver.get_screenshot_as_file(r'F:\jb\baogao.png') # Screenshot operation 
# Report page 2
driver.get('http://127.0.0.1:1111/job/%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95-gitee%E4%BB%A3%E7%A0%81/allure/#graph')
sleep(1)
driver.get_screenshot_as_file(r'F:\jb\baogao1.png')
print(' End of Screenshot ')
driver.quit()

3、 ... and . E-mail

1. Use qq If the email is sent, configure server = smtplib.SMTP(‘smtp.qq.com’)
2. The actual use server = smtplib.SMTP(‘smtp.163.com’)
''' Send local allure Report picture mail '''
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.application import MIMEApplication
from allure_dd import ctrl_alt
# Configure mailbox information 
ctrl_alt()# Screenshot 
sender = '[email protected]' # Sender's address 
password = 'KWKYGKdfdsfOBH' # Here is the authorization code we just obtained in the email 
receivers = '[email protected]' # Email recipient's email address , You can configure multiple , Achieve mass distribution , Note that this is a string 
# Email content settings 
content = MIMEText("<html><h2> Test report , False reply </h2>", _subtype="html", _charset="utf-8")
msg = MIMEMultipart('related')
msg.attach(content)
# Add a picture attachment 
imageFile = r"F:\jb\baogao.png"
imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile)
msg.attach(imageApart)
imageFile = r"F:\jb\baogao1.png"
imageApart = MIMEImage(open(imageFile, 'rb').read(), imageFile.split('.')[-1])
imageApart.add_header('Content-Disposition', 'attachment', filename=imageFile)
msg.attach(imageApart)
# Mail header settings 
msg['Subject'] = 'python Send attachment test - picture 、Excel'
# Sender information 
msg['From'] = sender
# Recipient information 
msg['To'] = receivers
# By authorization code , Login mailbox , And send mail 
try:
server = smtplib.SMTP('smtp.163.com') # Configure Netease mailbox smtp Server address 
server.login(sender, password)
server.sendmail(msg['From'], msg['To'].split(','), msg.as_string())
print(' Send successfully ')
server.quit()
except smtplib.SMTPException as e:
print('error', e)

3. Email sending results

Four . The nail robot sends the test results

1. Now the nail robot does not support sending local pictures , For the time being, do not send report pictures to nailing .

''' Send interface test information push pin message '''
import csv
import json
import requests
a=[]
# This is a jenkins Pull down where the code is stored , Test result information file generated during construction 
f = csv.reader(open(r'C:\Users\keking\AppData\Local\Jenkins\.jenkins\workspace\ automated testing -gitee Code \allure-report\data\suites.csv','r',encoding='UTF8'))
for i in f:
if i[0]!='Status':
a.append(i)
print(a)
# Sit down and process the raw data 
api_run_count=len(a)
api_passed_count=0
api_failed_count=0
failed_case_name=[]
failed_case_path=[]
for i in a:
if i[0]=='passed':
api_passed_count+=1
elif i[0]=='failed':
failed_case_path.append(i[1])
failed_case_name.append(i[3])
api_failed_count+=1
print(api_passed_count)
print(api_failed_count)
print(failed_case_name)
print(failed_case_path)
def dd_robot():
HEADERS = {
"Content-Type": "application/json;charset=utf-8"}
key = "b7ff3fd2fd9b2dc5f78xxxxxxxxxxxxx0ff1056d172d922cab"
url = f" https://oapi.dingtalk.com/robot/send?access_token={
key}"
#content You should set keywords inside The keyword I set for the robot is ' Interface test results :'
data_info = {

"msgtype": "text",
"text": {

"content": " Interface test results :"+f'\n All in all {
api_run_count} Use cases '
+ f'\n success {
api_passed_count} Use cases '
+ f'\n Failure {
api_failed_count} Use cases '
+ f'\n Failed case name {
str(failed_case_name)}'
+ f'\n Failed case address {
str(failed_case_path)}'
},
"isAtAll": False
# This is a configuration requirement @ People who 
# ,"at": {"atMobiles": ["15xxxxxx06",'18xxxxxx1']}
}
value = json.dumps(data_info)
response = requests.post(url,data=value,headers=HEADERS)
if response.json()['errmsg']!='ok':
print(response.text)
if __name__ == '__main__':
dd_robot()

2. Read the file information under the report , The information in this file can be used for statistics suites.csv File information structure


3. Send results

5、 ... and .jenkins Configure running pins 、 Email script

1. Configure mail delivery ( The end of the key build triggers another task build )

2. The configuration specifies that the construction of the current task will be executed after the construction of another task is completed


3. Configure pin sending

4. Mailbox and nail task run

6、 ... and . Run the test

1. Check whether the number of mail failure cases and nail failure cases changes by modifying the number of use case errors





2. The task of sending mailbox will be automatically built after each build of automation test , To achieve mailbox nailing

Reprint :https://blog.csdn.net/aaaaaaaaanjjj/article/details/122700657


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved