Python based automated testing framework interfaces (primary articles) with source code

table of Contents

1, Introduction
1, design ideas frame
2, the frame design ideas
3, run the program
4, the source acquires

introduction

    Many people know that the current market many automated testing tools, such as: Jmeter, Postman, TestLink, etc., there are some automated testing platform, why should that development interface automated testing framework it?
Similarities did not say, Let me talk about the limitations of the tool:
1. Test data is not controlled:
    Interface Although the test on the business logic, program code, and actually test data, the number of input data call interface , by asserting return code verification data interface, the entire process around the test data.
    If the returned data is not fixed, it is varied, the assertion failure, you can not know the interface program errors caused, or cause data changes, so we need to test data initialization.
    Interface tools do not have the data initialization function, so can not really test automation interfaces.

To give an example to help understand:
     for example, you want to test a query interface, without the initialization of test data cases, you enter parameters are: id = 1, the assertion is: assert name = 'test', this assertion is that you know in advance Interface What returns. Calling interfaces, the result is an interface name = 'test' assertion success, because you know the database has data of a id = 1.
    This day id = 1 data was deleted, but the interface testing framework you maintain still running and has not been updated test data, results assertion fails, you go up debug, and finally found the problem of test data, this process is time-consuming and laborious, if done test data initialization function, it was entirely avoidable.
    Because the parameters and the parameters are fixed, according to their needs initialization is good, do not worry about the data change caused the assertion failure, then only concerned with the interface of the program code problem.

2. encryption can not test the interface
    , project, most of the interface is not available for external calls will be used for user authentication, signatures, encryption and other means to provide security interface. And general testing tools can not do simulations and generate these encryption algorithms.

3. The lack of scalability
    tools are always tools, there are some limitations, can not generate customized test reports, send e-mail can not be customized, continuous integration and timed tasks.

4. The level of support to the business
    tools is relatively low business support program, can not be customized according to different business development, and automated testing framework to do this in a more flexible support for business.

Framework design ideas

1. The process flow is substantially:

Here Insert Picture Description

2. Interface test automation framework processes:

  • First, prepare a test data initialization script, maintain a number of test data to the database, and before each initialization, emptied the original data, thus ensuring that the data is up to date and only (to avoid duplication).
  • Interface call to the system under test, passing parameters, the request parameter is a dictionary, and (insert data initialization) data is consistent with the database data.
  • System interface based on the parameters, test queries to the database.
  • Results assembled into a certain data format (dict, json), the process returns to the test frame.
  • Testing framework assertion return data interface, and generates the test result (test report).

Framework

Here Insert Picture Description

Frame description:

1. The role of each directory:

  1. common /: Reports, Logs and other public folder storage module
  2. config /: file path, the configuration information is stored
  3. db_init /: Initialization test data processing program
  4. logs /: generate a log file
  5. pies /: Pie store
  6. report /: storage of test report
  7. testcase /: used to write test cases
  8. run_main.py execution of the main program test set

The main program file to run run_main.py:

# -*- coding: utf-8 -*-
'''
@author: liudinglong

@software: pycharm

@file:  run_main.py

@time: 2020/2/23 0023 13:46

'''

import time ,sys
# 引用模块路径
sys.path.append('./testcase')
sys.path.append('./db_fixture')
from common.HTMLTestRunner3 import HTMLTestRunner
from unittest import defaultTestLoader
from db_init import data_init

# 指定测试用例为当前文件夹下的 interface 目录
test_dir = './testcase'
# 自动获取interface 目录下的测试用例
testsuit = defaultTestLoader.discover(test_dir,pattern='*test.py')


if __name__ == '__main__':
    # 初始化接口测试数据
    data_init.init_data()
    # 获取当前时间
    now = time.strftime("%Y-%m-%d %H_%M_%S")
    # 定制报告名称
    filename = './report/' + now + '_result.html'
    # 向报告写入测试结果数据
    fp = open(filename, 'wb')
    runner = HTMLTestRunner(stream=fp,
                            title='接口自动化测试报告',
                            description='运行环境:环境:windows 10 浏览器:chrome 语言: Python3')
    # 运行测试集
    runner.run(testsuit)
    # 关闭报告文件
    fp.close()

Run the program

operation result:

......FFFFFF.................
Time Elapsed: 0:00:00.208256

Test log:
Here Insert Picture Description
Test Report:
Here Insert Picture Description

Errors do not be afraid to see the error message, and then change it, run:

Here Insert Picture Description

    Before the test, the test environment should be prepared, if the interface is a formal environment, you need to create a separate test database, only use this set as a project test environment.
    When the database initialization, database connection test environment, test data they need to go initialization, each time program execution, are initialized again, so the role of official data and prevent data conflicts, and to prevent duplication and test data accumulated in the database in.

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/104457379