Python interface automated testing framework 2.0 allows you to write test cases like Postman, supporting multi-environment switching, multi-business dependencies, database assertions, etc.

Project Introduction

Interface Automation Test Project 2.0

Software Architecture

This framework is mainly based on the interface automation framework implemented by Python + unittest + ddt + HTMLTestRunner + log + excel + mysql + enterprise WeChat notification + Jenkins.

Preface

The company suddenly requires you to do automation, but you don’t know how to do it without a coding foundation? Or you have the foundation for automation, but you don’t know how to do automation systematically, maintain it in excel files, and don’t know how to handle the logic of multi-business dependencies. Or you can write some code for each test case like Postman. Script, but don't know how to do it. Then my automated testing framework in gitee will solve these problems for you.

Framework advantages

This framework does not charge Fenhe fees. Its advantage is that testers directly write test cases, and all test cases can be automatically executed by running a test file.

The framework is mainly written in python language and combined with unittest for secondary development. Users only need to write test cases in excel files. After successful writing, the code of the test cases will be automatically generated. Even novices with zero basic coding can operate it.

This framework supports multi-environment switching, multi-business interface dependencies, mysql database assertions and interface response assertions, and use cases are maintained directly in excel files without writing business code. This framework supports dynamic script function expansion, and functions can be preset internally at any time, or Add a dynamic script to dynamically call the interface unittest framework through the fixed format writing method {{fun()}} to generate HTML reports and excel use case execution reports, and send corporate WeChat notifications/DingTalk notifications/email notifications/Feishu notifications for flexible configuration.

Implement function

  • Test data isolation and achieve data drive.
  • Supports the use of multiple environments. For example, if the test cases are the same but the environments are different, you only need to enable the environment that needs to be executed in Excel.
  • Supports multi-interface data dependency: For example, interface A needs to rely on the response data of interfaces B and C as parameters at the same time, or the assertion of interface c needs to dynamically use the input parameters of interface A as the expected result.
  • Database assertion: You can assert by writing the sql of the query directly in the test case without writing code.
  • Dynamic multiple assertions: If the interface needs to verify response data and SQL verification at the same time, multiple scenario assertions are supported.
  • Supports custom script code (python language), which allows you to write any pre- and post-script code for each test case like Postman. It has built-in action object operations and some methods to conveniently handle pre- or post-scripts.
  • Automatically generate test case code: The tester fills in the test case in the excel file, and the program can directly generate the use case code, which can even be used by novices.
  • Supports custom extension functions: If you need to use your own functions in the use case, you can write the function in the specified directory at any time, and call the function you set at any time through { {func(*args,**kwargs)}}.
  • It has built-in implementation of some random functions and custom common functions: such as: random string, random date and time, random GPS, random user ID card, address, email, corporate information, etc. Users can also define any function by themselves, and there is no threshold for use. .
  • Statistics on the running time of the interface: Expand the function and customize the switch to decide whether it needs to be used.
  • Supports use case timer operation. For example: before use case B is executed, use case A needs to wait 5 seconds before execution starts. You can add the waiting time at will.
  • Log module: Print the log information of each interface. The switch is also customized to determine whether the log needs to be printed.
  • DingTalk, Business WeChat notifications, and email notifications: Supports multiple notification scenarios. After successful execution, you can choose to send DingTalk, Business WeChat, or email notifications.
  • Encapsulates the class for sending MQTT and supports sending MQTT messages.
  • Supports importing json files exported by postman or swargs and automatically processing them into test cases, and then modifying the parameters can be used.

gitee address

Interface automation framework address: Join the group and pick it up: WeChat: Testing_player

Encounter problems

  • Please read the documentation carefully. It can help you avoid almost all problems.
  • You can add WeChat: blue-blue-lemon. Adding WeChat will pull you into the automated communication group. There are many enthusiastic friends in the group, but the premise is that you have read all the content in the document.
  • You can also ask the author to answer your questions. Of course, when I have free time, I can also ask the author to provide one-on-one service for you, for a fee of course.

Catalog overview:

 

Detailed directory structure

. └── cases/ // 测试用例数据文件夹 └── cases/ // 测试用例 └── test_cases.xlsx └── templates/ // 测试用例模板 └── template.xlsx └── temporary_file/ // swagger| postman 导出的 json文件存放位置 └── openapi.json └── postman.json └── common/ // 配置及核心功能代码 └── bif_functions/ // 内置函数工具 └── __init__.py └── bif_datetime.py // 内置日期时间函数 └── bif_hashlib.py // 内置hash 函数 └── bif_json.py // 内置json解析函数 └── bif_list.py // 内置列表操作函数 └── bif_random.py // 内置随机函数 └── bif_re.py // 内置正则函数 └── bif_str.py // 内置字符串函数 └── bif_time.py // 内置时间函数 └── random_tools.py // 内置其他随机函数 └── crypto/ // 加密工具 └── __init__.py └── encrypt_data.py // 执行数据加密封装入口 └── encryption_aes.py // aes 加密 └── encryption_base64_to_image.py // base64 转图片工具 └── encryption_main.py // 执行加入函数入口,对应excel中的加密方法选项,结合 extensions 中的 sign 可自定义加密规则 └── encryption_rsa.py // rsa 加密 └── encryption_str.py // 常用的字符串加密工具函数 └── data_extraction/ // 数据提取器 └── __init__.py └── analysis_json.py // json 字典分析函数,类似 jsonpath └── assert_dict.py // 旧断言字段函数 (暂时舍弃) └── data_extractor.py // 数据提取函数 └── dependent_parameter.py // 数据替换函数 └── dict_get.py // 旧路径提取函数 └── database/ // 数据库操作工具 └── __init__.py └── execute_sql_files.py // sql 批处理工具 └── mongodb_client.py // mongoDB 操作工具 └── mysql_client.py // mysql 操作工具 └── psycopg_client.py // presto 操作工具 └── redis_client.py // redis 操作工具 └── file_handling/ // 文件操作 └── __init__.py └── do_excel.py // excel 处理 └── excel.py // excel 处理 └── file_utils.py // json、yarm等文件操作以及一些目录操作 └── http_client/ // http_client 发送http的工具 └── __init__.py └── http_client.py // http 请求封装 └── MQclient/ // MQ 工具 └── __init__.py └── mqtt_client.py // mqtt 工具 └── rabbit_mq_client.py // rmq 工具 └── random_tools/ // 常用随机数工具包 └── credit_cards/ // 随机银行卡 └── __init__.py └── bankcard.py └── cardbin.csv └── credit_identifiers/ // 随机企业唯一信用代码 └── __init__.py └── address.json └── credit_identifier.py └── unified_social_credit_identifier.py └── emails/ // 随机邮箱 └── __init__.py └── email.py └── free_email.csv └── identification/ // 随机身份证 └── __init__.py └── area.csv └── id_card.py └── names/ // 随机姓名 └── __init__.py └── first_name_boy.csv └── first_name_girl.csv └── last_name.csv └── name.py └── phone_numbers/ // 随机手机号 └── __init__.py └── phone.py └── phone_area.csv └── __init__.py └── utils/ // 工具包 └── __init__.py └── captcha.py // 识别图片转字符串,常用于验证码登录 └── load_and_execute_script.py // 动态加载自定义python文件工具 └── logger.py // loggin 日志封装 └── mylogger.py // loguru 日志封装 └── parsing_openapi.py // 解析 swagger 文件 转测试用例 └── parsing_postman.py // 解析 postman 文件 转测试用例 └── singleton.py // 单例 └── WxworkSms.py // 企业微信 └── validation/ // 断言及加载动条函数工具包 └── __init__.py └── comparator_dict.py // 自定义比较器名词释义 └── comparators.py // 自定义比较器 └── extractor.py // 提取器 └── load_modules_from_folder.py // 动态从模块中加载函数 └── loaders.py // 动态加载函数及加载内置比较器等工具包 └── validator.py // 断言校验器 └── __init__.py └── action.py // 核心类 └── config.py // 配置文件 └── variables.py // 数据操作类 └── extensions/ // 动态扩展模块目录 └── __init__.py └── ext_method_online.py // 动态扩展方法模块 └── sign.py // 加签模块 └── image/ └── wx.jpg └── zfb.jpg └── OutPut/ // 日志报错 └── log/ └── 2023-06-25.log └── 2023-07-04.log └── 2023-07-05.log └── reports/ └── history.json └── scripts/ // 动态脚本目录 └── __init__.py └── prepost_script_安全纯净大屏_2.py // 对应每条测试用例的前后置脚本(可选) └── request_script_sheetname_id.py └── test_script/ // 测试脚本核心模块 └── __init__.py └── test_api.py └── directory_tree.txt └── excel_converter.py // postman、swagger 文件转换主入口模块 └── generate_tree.py // 项目目录树模块 └── main_personal_information.py // 随机个人信息核心入口 └── pipenv_command.text └── Pipfile └── Pipfile.lock └── README.md └── run.py // 自动化测试执行入口模块

Installation tutorial

Dependent libraries

Refer to pipenv --Pipfile virtual environment files, not listed one by one.

First of all, after executing this framework, you need to set up a python environment. The python environment can be set up by Baidu yourself.

  1. Install pipenv environment: pip install pipenv
  2. Create a virtual environment: pipenv install
  3. If some packages cannot be installed, then: pipenv shell enters the environment, and then pipenv install XX package

Instructions for use

  1. For test cases, you only need to fill in the test cases in the cases/cases/xxx.xlsx file. You can refer to the test case template file. Since the use cases in the template file have already processed sensitive information, an error should be reported when executing the use case of the template.
  2. To run a use case, you can directly enter test_script/test_xxx.py and execute the test_xxx.py file, or you can directly run the outermost run.py file.
  3. Personalinformation.py This file is used to generate some test data for functional testing or other general testing.

Open test interface documentation

You can use the login, personal information, collection (add, view, modify, delete) and other functions in the open source interface to write interface automation cases, and then under the test program script is the interface document address, which you can view by yourself (because the open source interface , there are some logical functions in it, such as modifying the deleted URL interface without too much judgment, so only some basic scenarios are written in the use case for your reference only.)

How to create a use case

Introduction to test case related fields in excel

The following is about all the fields in a use case. Not all fields are required.

 

The following explains the function of each field:

  • Id: test case id, unique, required
  • Name: The name of the test case, required
  • Description: Test case description, optional, recommended to fill in, this field will be used in the generated report
  • Run: Whether to run this use case. If not filled in or filled in incorrectly YES/yes, this use case will not be run.
  • Time: timer, number, optional, fill in the number of seconds it will wait before running this use case
  • Method: Request method, supports all http request methods, get/post/put/patchetc.
  • Url: interface path or the entire request address. If it does not contain a domain name, it will automatically be spliced ​​into the init table with the host. Url can be parameterized, such as:/test/api/get/{ {id}}/{ {func(*args,**kwargs)}}
  • Headers: request headers, dictionary filling method, such as:{"Content-Type":"application/json","token":"{ {token}}",'time':'{ {timestamp()}}'}
  • Headers Crypto: Whether the request header is encrypted, you can customize the encryption method, and then enter your encryption method name here, such as MD5\sha1etc.
  • Query Str: Query string parameters, that is, when the URL is too long, you can separate the parameters behind the URL and fill them in here. Fill them in in a dictionary and support parameterization.
  • Request Data Type: Request parameter type, including json,file,params,dataand so on
  • Request Data: Request parameters, filled in in dictionary form, supports parameterization, such as:{"projectId":"{ {projectId}}","tvSequence":"{ {random_string()}}"}
  • Setup Script: Pre-script, you can directly write pthon code, such as classes, functions, etc. If it is a function, the class must be called before it will be executed. Example of how to write:
# 可以写类,可以写函数,可以直接普通python语句, def setup(action): print("获取action示例的属性", action.get_vars()) # 更新属性到action实例的属性中 action.update_vars("999", "99999") print("获取所有的临时变量", action.get_variable()) print("将变量存到临时变量表中以便后续用例{
   
   {key}}使用", action.update_variable("key", "value")) print("将函数存到临时变量表,以便后面调用{
   
   {setup_func()}}也可以", action.update_variable("setup_func", setup)) # 调用方法 setup(action)
  • Request Data Crypto: Whether the request parameters are encrypted, you can fill in the encryption method you need, such as MD5\sha1etc.
  • Extract Request Data: Extract request parameter fields or the entire request parameter dictionary, such as:{"k": "$.projectId", "x": "$.data[0].age[3].a"}
  • Jsonpath: Extract the jsonpath expression of the response result. Example: By default, if the expression result has only one value, it will be saved as a string. If the expression execution result has multiple values, then it will be saved as a list. The expression is filled in as follows :{"k": "$.data", "x": "$.data[0].age[3].a"}
  • Regex: Regular expression, fill in the regular expression string, example:r'"id": (\d+), "name": "(\w+)",'
  • Regex Params List: Fill in a list (list) to store the variable list of the regular expression extraction value, such as: , ["a", "b"]which means using the a, b variables to receive the return value of the above regular expression
  • Retrieve Value: The value method of python dictionary, multiple entries are separated by semicolons, such as:name=data[0].name;ok=data[0].id;an=data[0].age[3].a
  • SQL: Fill in the add, delete, modify, query sql statement. The key of the dictionary is the sql method that needs to be executed, and the value is the sql statement. You can fill in multiple entries. Example of the filling format:
{ "insert": { "insert_data": "INSERT INTO users (name, age, email) VALUES ('John', 25, '[email protected]');" }, "select": { "select_data": "SELECT name, age, email FROM users WHERE age > 20;" }, "delete": { "delete_data": "DELETE FROM users WHERE age > 30;" }, "update": { "update_data": "UPDATE users SET age = 26 WHERE name = 'John';" } }
  • Sql Params Dict: extract the result of sql, the same as jsonpath, such as:{"name": "$.select.select_data.name", "age": "$.select.select_data.age"}
  • Teardown Script: Post-installed python script code, any function or class, the code will be automatically executed after the use case sends a request
  • Expected: expected result, writing example: [{"check":"result.user.name","comparator":"eq","expect":"chenyongzhi"}]or {"check":"result.user.name","comparator":"eq","expect":"chenyongzhi"}, supported assertion comparison methods are as follows:
{ "eq": "eq:实际值与期望值相等", "lt": "lt:实际值小于期望值", "lte": "lte:实际值小于或等于期望值", "gt": "gt:实际值大于期望值", "gte": "gte:实际值大于或等于期望值", "neq": "neq:实际值与期望值不相等", "str_eq": "str_eq:字符串实际值与期望值相同", "length_eq": "length_eq:实际值的长度等于期望长度", "length_gt": "length_gt:实际值的长度大于期望长度", "length_gte": "length_gte:实际值的长度大于或等于期望长度", "length_lt": "length_lt:实际值的长度小于期望长度", "length_lte": "length_lte:实际值的长度小于或等于期望长度", "contains": "contains:期望值包含在实际值中", "contained_by": "contained_by:实际值被包含在期望值中", "type_match": "type_match:实际值的类型与期望值的类型相匹配", "regex_match": "type_match:正则匹配(从字符串的起始位置匹配)", "regex_search": "regex_search:正则匹配(从字符串的任意位置匹配)", "startswith": "startswith:实际值是以期望值开始", "endswith": "endswith:实际值是以期望值结束" }
  • Response: The response result. After the use case is executed, the result will be automatically written back to this excel. You can view the test results clearly at a glance.

Create use case steps

Before starting to fill in the test cases, first take a look at the fields of the init table. I will explain them in detail below:

 

  1. Create the module or system under test under casesthe folder, and then create related exceluse cases under the system (you can copy the template excel file to fill in the test cases)
  2. Add the test case path to the file under commonthe folderconfig.py
  3. Enter the folder, operate the file test_script/in the folder, and replace the variable address with the test case address you just filled in.test_api.pytest_file
  4. The use case in excel must be filled in id. In addition, init sheetsome fields in the global initialization data field in the table must be filled in, such as:
""" # init 表中的字段 id: 序号 environment: 被测环境,建议填写:如测试环境,开发环境,生产环境等等 host:域名地址,如:https://www.baidu.com,非必填,填写了就会取这部分的地址与测试用例中的Url拼接发送请求 path:一小部分的请求路径,比如所有的接口可能都存在:/test/api 这种,就可以填写上去,非必填 databases: 数据库,非必填,{"host": "127.0.0.1","port": 3306,"database": "mysql","user":"root","password":"root@3306"} sheets:你想执行的sheet是哪一个,全部执行就填写 'all',部分执行就填写具体的sheet如["第一个sheet的名称","第二个sheet的名称"...] initialize_data:初始化的一些固定参数,比如登录账号,密码,等等都是基本不变的数据可以填写这里,属于全局变量,填写方式字典{"{
   
   {username}}":"1234","{
   
   {pwd}}":"密码"} run: 决定使用的环境,值为yes表示用这个环境运行,有多个环境都填写yes,只运行第一个yes """

The above has been filled in. Remember to close it first excel, and then you can execute test_api.pythe file separately, or directly run run.pythe main entry file to generate HTML reports and excel test reports.

Let's take a look at how to create a use case

Introduction to relevant fields in use cases

  • Send a screenshot to fill in the basic data for init initialization use case

 

  • The list sorting in sheets in init is effective, and the execution use case is executed according to the sorting of the values ​​in the list.
  • The screenshot below is the description of whether each field in the excel use case is required.
  • Each test case can be customized to enable it. If enabled, it will be executed. If not enabled, it will be skipped. The default execution result is passed.
  • Each sheet can be combined with use cases at will. Variable parameters between different sheets can be shared (that is, the variables are global) and are not designed independently. This is mainly because the purpose of dividing sheets is to manage use cases, but the execution is still all at once.
  • Currently, there is no design for the maximum number of use cases for each sheet. The default is the maximum data volume in Excel. If it is found that the maximum data volume exceeds the memory, it is recommended to write test cases in multiple Excel files.
  • The generator method currently used to read excel is mainly to prevent the one-time memory from being exploded.
  • The test report is generated according to excel. The style of the test report is not differentiated by module. In fact, you can see the report results directly by opening excel.
  • Others that have little effect, such as pass rate, number of skipped items, number of passed items, number of failed items, etc., are not processed separately and are all automatically processed by the HTML report.
  • The screenshot below is an example of filling in the associated parameters.

  

Some commonly used built-in functions

All built-in functions are in common/bif_functionsthis package. Users can customize built-in functions. When designing built-in functions, you must design a return value. You cannot directly return None. It seems that None is not treated specially. The following is an explanation of some built-in functions.

 

  • This MD5 method is generally not used. Generally, whether to use the [Parameter Encryption Mode Field Switch] in Excel to handle encryption
  • When using built-in functions, { {xxx()}}you can pass parameters into (), for example{ {token(999)}}

  • Get some timestamp methods, which can take parameters
  • There are some random numbers in Faker's library. If you need more, you can just add them yourself.

 

Add waiting time to use case

One of the excel fields Time, the field value is set to 数字, unit S

 

Before the use case is executed, the sql needs to be executed separately, or the sql needs to be executed separately after the execution, or the sql needs to be executed immediately after the request and the sql is asserted.

There is a method field in excel. Just set the value to sql. The subsequent SQL and SQL variable fields can be filled in as needed. They must be filled in according to the specifications or fill in the sql statement and sql variable after the request. SQL will be executed first by default. Execute the request again.

 

setup setupclass teardown tearDownClass ?

In fact, this set of codes does not need to be used. You can also use it to customize some initialization functions or use it as a unique login to perform data clearing operations? You can write sql into excel, or you can create a separate sheet in excel to execute sql. There are many methods and it is very flexible.

How to quickly convert postman or swagger files into test cases?

This framework has encapsulated the parsing function. Directly export the postman file, put it in the specified directory and run the excel_converter.py file. You can get the test cases very quickly and easily, without copying and pasting them one by one.

 

 

 

Change to a different database?

There are currently a variety of built-in database link query methods, but they have not been used yet. Only mysql is used. If users need to use other databases such as mongoDB, they can expand this database according to the mysql design method to adapt to the writing of excel test cases. method, you can also contact the author for processing

 

Send corporate WeChat notifications

You can freely modify the templates and other information sent by corporate WeChat as needed.

 

Main function execution entry

 

Log printing decorator

 

Example of log printing results:

 

 

 

basic data

 

 

testing report?

It is available in html or excel, just use it and check it out yourself.

 

Enterprise WeChat test report:

 

 

html test report:

 

 

Statistics of use case running time?

The duration from request to response is counted for each use case. No other time-consuming information is counted. The details can be viewed in html.

other

  • This framework is an upgraded version 2.0. After the upgrade, the functions are now basically maintained in excel, without the need for testers to write code.
  • The difference from version 1.0 is that version 1.0 does not require testers to write code, but the assertion method is more rigid.
  • Version 1.0 has been integrated into the test and development platform and can be used on the platform, specifically gitee

Introduction to independent personal information tools

Directly executing main_personal_information.pythe file can generate great test data. Python randomly generates personal information, including name, gender, age, date of birth, ID number, bank card number, phone number, mobile phone number, email address and other information.

Generate random personal information

PS D:\api-test-project> python .\main_personal_information.py 海省广州市南 请输入你需要生成的数据总数:10 +--------+------+------+------------+--------------------+----------------------+--------------+-------------+-------------------------+----------------------------------------+--------------------+ 门特别行政区 | 用户名 | 性别 | 年龄 | 生日 | 身份证 | 银行卡或信用卡 | 座机 | 手机号 | 邮箱 | 地址 | 统一社会信用代码 | +--------+------+------+------------+--------------------+----------------------+--------------+-------------+-------------------------+----------------------------------------+--------------------+ 西省张家港市 | 刘世 | 男 | 57 | 1965-07-21 | 342422196507219413 | 62263745016607204 | 0744-3715089 | 14705083324 | [email protected] | 甘肃省辽阳县沙湾海口街y座 180548 | 713101514054645086 | | 龙清承 | 男 | 56 | 1966-08-21 | 342400196608212132 | 62284150185529051643 | 0893-4423648 | 13487550352 | [email protected] | 辽宁省玉珍市黄浦重庆街z座 401247 | 93421303115117882W | | 刘娴 | 女 | 37 | 1985-05-23 | 411023198505239964 | 6213058212589619696 | 0562-0004972 | 17303409666 | [email protected] | 福建省拉萨县朝阳胡街X座 479131 | 92341122605351081N | | 朱晨 | 男 | 55 | 1967-10-08 | 142733196710082991 | 45128971703439970 | 0375-7394954 | 18024463495 | [email protected] | 宁夏回族自治区秀云县高坪任路Q座 544781 | 955205231604137955 | | 许天有 | 男 | 51 | 1971-10-17 | 330901197110176212 | 62592648755593670 | 0973-8733227 | 13413236004 | [email protected] | 辽宁省天津市南长深圳路f座 952461 | 91120114649961877C | | 陈锦 | 女 | 29 | 1993-02-12 | 452123199302128568 | 62260020819291645 | 0915-7410180 | 17509030433 | [email protected] | 重庆市西安市沙湾广州街G座 531384 | 92445302298701343Q | +--------+------+------+------------+--------------------+----------------------+--------------+-------------+-------------------------+----------------------------------------+--------------------+

Summarize

The above is what I have prepared for you today. If you want to know more about Python automated testing knowledge and skills, please pay attention!


How to obtain information

【Message 777】

Friends who want to get the source code and other tutorial materials, please like + comment + collect , three times in a row!

After three consecutive rounds , I will send you private messages one by one in the comment area~

Guess you like

Origin blog.csdn.net/GDYY3721/article/details/132232613