Based Interface Python + automated testing framework data and code separation (Advanced chapter) with source code

table of Contents

1, Introduction
1, design ideas frame
2, the frame structure
3, run the program
4, the message module
5, the test result is written Excel
6 summarizes
7, acquisition source

introduction

In the previous "Python-based interface to automated testing framework (primary articles) with source code" talked about automated testing framework to build an interface, the core module initialization function is to test the database, look at the previous framework:
Here Insert Picture Description
As can be seen testcase there are many under test, and each test case written request address, request parameters, request class data, there are some assertions of data. If the interface a lot of words, so that each case should write to write, leading to the late heavy workload, maintenance inconvenient.

For the above, the introduction of a python very easy to use third-party libraries ddt, its role is data-driven, the separation of data and code, you can put all the interfaces associated test cases data stored in excel, and then maintain a file API, so You do not need to write a lot of code case.

Framework design ideas

1. The process flow is substantially:

Here Insert Picture Description

2. Interface test automation framework processes:

  • First of all, the test data to initialize, maintain business data to the database, which is the first step in the screenshot
  • The second step, a maintenance test data interfaces Excel (use cases id, request method, request parameters, the request header, the request type, return data, test results, etc.).
  • Just writing a test framework API calling code, the second step of the test data read interface maintenance, and sends a request to the system under test, which is the third step in FIG.
  • When the call interface, queries the database, it is the fourth step.
  • The test framework for return data interface and Excel data, and generates the final test result is written in Excel, and generate test reports.

General process: initializing the test data read test data → → → write test result transmission request generate test reports → → send a test message.
flow chart

Framework

Here Insert Picture Description
Here will not repeat, can refer to primary articles, compare.

Run the program

...............
Time Elapsed: 0:00:01.106878
邮件发送成功!

Here Insert Picture Description
The right side of the mouse pie is put up will automatically zoom.

Mail module

import os,sys
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from config import setting
import smtplib
from main.newReport import new_report
import configparser
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart


def send_mail(file_new):
    """
    定义发送邮件
    :param file_new:
    :return: 成功:打印发送邮箱成功;失败:返回失败信息
    """
    f = open(file_new,'rb')
    mail_body = f.read()
    f.close()
    #发送附件
    con = configparser.ConfigParser()
    con.read(setting.TEST_CONFIG,encoding='utf-8')
    report = new_report(setting.TEST_REPORT)
    sendfile = open(report,'rb').read()
    # --------- 读取config.ini配置文件 ---------------
    HOST = con.get("user","HOST_SERVER")
    SENDER = con.get("user","FROM")
    RECEIVER = con.get("user","TO")
    USER = con.get("user","user")
    PWD = con.get("user","password")
    SUBJECT = con.get("user","SUBJECT")

    att = MIMEText(sendfile,'base64','utf-8')
    att["Content-Type"] = 'application/octet-stream'
    att.add_header("Content-Disposition", "attachment", filename=("gbk", "", report))

    msg = MIMEMultipart('related')
    msg.attach(att)
    msgtext = MIMEText(mail_body,'html','utf-8')
    msg.attach(msgtext)
    msg['Subject'] = SUBJECT
    msg['from'] = SENDER
    msg['to'] = RECEIVER

    try:
        server = smtplib.SMTP()
        server.connect(HOST)
        server.starttls()
        server.login(USER,PWD)
        server.sendmail(SENDER,RECEIVER,msg.as_string())
        server.quit()
        print("邮件发送成功!")
    except Exception as  e:
        print("失败: " + str(e))

After running the code:
Here Insert Picture Description

Written test results Excel

Reports and e-mail have, look at the test results to write back data in Excel:
Here Insert Picture Description

Consistent results and test report results.

to sum up

    This interface automated testing framework development is substantially complete, ddt This paper describes the use of data-driven, implemented separately from the test data and the test code pattern, in order to improve readability, the efficiency and reduce maintenance costs.
    One more thing, this test case the interface is basically no relationship, if there is an associated upper and lower, you can not write, or else the first interface fails, all fail the basic behind. So the test does not only apply to interfaces up and down dependencies.
    Although the code has been completed, there are some details of the place needs to be optimized, such as logging, automatic clean-up report, timed to send a message, Jenkins integration and so on.

Source Gets

Source has hosted gitee, Access: Join test development exchange QQ group: 696 400 122
micro-channel public number: full-stack test development diary,
blog Park Address: blog Garden Address
Here Insert Picture Description

Published 82 original articles · won praise 43 · views 180 000 +

Guess you like

Origin blog.csdn.net/liudinglong1989/article/details/104462702