Super easy-to-use interface automation framework, lemon-easytest internal beta release, hurry up and use it~

easytest

easytest is an interface automation framework.

Features:

  • Support http interface test

  • Support  json, html, xml response assertion in format

  • Support for database assertions

  • Support for use case tag filtering

  • Support use case failure re-run

  • Support multithreading

Install

pip install lemon_easytest

quick use

There is no need to write any code, all you need to do is to write the use case document according to the rules, and then run the command  easytest.

easytest Use case documentation in  supported  yaml formats and  formats.excel

Create a file in any directory  singe_test.yamlwith the following content:


test:                                 # 表名这是单个测试用例
  title: 一个简单的测试                 # 用例名称
  url: http://httpbin.org/get         # url
  method: get                         # 请求方法
  request:                            # 请求参数字段
    headers:                          # 请求头
      CustomerHeader: lemonban        # 头信息
    params:                           # url参数
      search: lemonban                # url参数键值对
  res_type: json                      # 响应数据类型
  status_code: 200                    # 状态码
  assertion:                          # 断言表达式
    -
      - eq                            # 相等
      - $..Customerheader             # 结果提取表达式
      - lemonban                      # 期望值
    -
      - eq
      - $..search
      - lemonban

 If you want to learn automated testing, here I recommend a set of videos for you. This video can be said to be the first interface automation testing tutorial on the entire network at station B. At the same time, the number of online users has reached 1,000, and there are notes to collect and various Lu Dashen Technical Exchange: 798478386      

[Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (the latest version of actual combat)_哔哩哔哩_bilibili [Updated] The most detailed collection of practical tutorials for automated testing of Python interfaces taught by station B (actual combat) The latest version) has a total of 200 videos, including: 1. [Interface Automation] The current market situation of software testing and the ability standards of testers. , 2. [Interface Automation] Fully skilled in the Requests library and the underlying method call logic, 3. [Interface Automation] interface automation combat and the application of regular expressions and JsonPath extractors, etc. For more exciting videos, please pay attention to the UP account. https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337&vd_source=488d25e59e6c5b111f7a1a1a16ecbe9a  Then run on the command line

easytest yourpath/single_test.yaml
INFO 2021-10-30 14:53:26,081 :==========single_test测试开始============
INFO 2021-10-30 14:53:26,081 :用例【一个简单的测试】开始测试>>>>>>>>
INFO 2021-10-30 14:53:26,591 :用例【一个简单的测试】测试结束<<<<<<<<<
INFO 2021-10-30 14:53:26,591 :==========single_test测试结束============
用例总数:1,成功:1个,跳过:0,失败:0个,错误:0个

Call easytest through python code

Can be called directly through python easytest

import easytest
easytest.main()

You can also pass parameters

easytest.main(['test_dir', '--debug', '--logfile', 'test.log'])

Write use cases

test case

To write a single test case in easytest,  yaml you can use format or Excel file.

Excel format

It is very simple to write a single test case using an Excel file. For example, the format of writing the above case into an Excel file is as follows: !

picture

When writing a use case in an Excel file, keep the data tidy, and do not have any data in other cells, so as not to fail to load the use case data. easytest will organize the use cases according to the sheetname of the Excel file, so please delete other sheets for a single use case.

YAML format

When using a YAML file to write a single test case, the outermost key must be test, because easytest determines that the data in a YAML file is a single test case based on it.

test:                                 # 表名这是单个测试用例
  title: 一个简单的测试                 # 用例名称
  url: http://httpbin.org/get         # url
  method: get                         # 请求方法
  request:                            # 请求参数字段
    headers:                          # 请求头
      CustomerHeader: lemonban        # 头信息
    params:                           # url参数
      search: lemonban                # url参数键值对
  res_type: json                      # 响应数据类型
  status_code: 200                    # 状态码
  assertion:                          # 断言表达式
    -
      - eq                            # 相等
      - $..Customerheader             # 结果提取表达式
      - lemonban                      # 期望值
    -
      - eq
      - $..search
      - lemonban

 

test suite

The test suite in easytest represents a set of sequential test cases. When multi-threading is started, the test suite is given to the thread to execute the test cases in the order of the suite. Note that the order of execution between suites and suites is not fixed.

A single test case will also be covered with a test suite shell. A single test case in yaml format will be encapsulated into a test suite named after a YAML file, and a single test case in Excel format will be encapsulated into a test suite named after a table name in the test suite.

Excel format

There is no difference between writing a test suite in an Excel file and a single test case, just write it in sequence from top to bottom according to the execution order, for example:

picture

Multiple test suites can be written in a single Excel file, and a table is a test suite, so please delete tables that are not test cases or project settings.

YAML format

When writing a test suite in a YAML file, the outermost key must be test_suit, because easytest determines that the data in a YAML file is a test suite based on it. Note that unlike Excel, the YAML format does not support writing multiple test suites in one file, as multiple levels of nested indentation would be a nightmare.

test_suit:
  - title: 一个简单的测试
    url: http://httpbin.org/post
    method: post
    status_code: 200
    res_type: json
    request:
      json:
        username: xinlan
        password: 123456
    assertion:
      - [eq,$..username,xinlan]
      - [eq,$..password,123456]

  - title: 一个不简单的测试
    url: http://httpbin.org/post
    method: post
    status_code: 200
    res_type: json
    request:
      json:
        username: xinlan
        password: 123456
    assertion:
      - [ eq,$..username,xinlan ]
      - [ eq,$..password,123456 ]

 

Use Case Collection Rules

easytest The command accepts a positional parameter  file_or_dir, which can be a use case file or a directory.

When passing in a use case file, it must be in the conforming format  excel or  yaml file mentioned in the previous section. Excel files only support  .xlsx the suffix format, and YAML files support  .yaml the or  .yml suffix.

When a directory is passed in, easytest it will recursively go to this directory to search for all use case files (excel, yaml) that meet the rules, and extract use cases from them. When a format error is encountered, the program will be interrupted, so do not put irrelevant Excel files and YAML files are placed under the use case directory.

Use case field description

  • title

    string, case title

  • url

    String, the requested url, supports the complete url, for example  https://httpbin.org/get, also supports the key corresponding to the interface in the project configuration. For example:register

  • method

    string, http request method

  • request

    JSON objects, parameters carried by http requests, request headers, cookies, etc. The bottom layer calls the python  requests library, and the parameter names are exactly the same.

    • params

      JSON object, the url parameter carried by the http request. For example

request:
  params:
    search: python
  •   data

        JSON object, form parameter carried by http request. For example

request:
  data:
    username: xinlan
    password: 123456

    • JSON

      JSON object, the JSON parameter carried by the http request. For example

      request:  json:    username: xinlan    password: 123456
    • headers

      JSON object, the header carried by the http request. For example

      request:  headers:    X-Lemonban-Media-Type: lemonban.v1​​​​​​​
    • cookie

      JSON object, cookie information carried by http request. For example

request:  cookies:    key: value
  • res_type

    String, http response type, optional values ​​are:json,xml,html

  • status_code

    Integer, http assertion response status code.

  • assertion

    Array object, response result assertion expression. The format is: [[条件符号,提取表达式,期望结果],[条件符号1,提取表达式1,期望结果1],...], for example:

assertion:  - [eq,$..username,xinlan]  - [eq,$..password,123456]

Conditional symbols support:

  • eq: equal

  • gt: greater than

  • gte: greater than or equal to

  • lt: less than

  • lte: less than or equal to

  • in: in it

  • contains: contains

Currently only supports eq

Extract expressions support:

    • regular expression

    • jsonpath expression

    • xpath

  • db_assertion

    Array object, database assertion expression. The format is: [[条件符号,sql语句,期望结果],[条件符号1,sql语句1,期望结果1],...], for example:

db_assertion:
  - [eq,select leave_amount from member where id=#invest1_id#,0]
  - [exist,select id from invest where member_id=#invest1_id# and loan_id=#loan_id# and amount=5000,true]
  - [exist,select id from financelog where pay_member_id=#invest1_id# and amount=5000 and pay_member_money=0 and status=1,true]

Conditional symbols support:

    • eq: equal

    • exist: exists. When using exist, the expected result must be true

  • extract

    Array object, response result extraction expression. The format is for  [[变量名,提取表达式],[变量名2,提取表达式2],...] example:

exract:  - [mobile_phone, $..mobile_phone]  - [token, $..token]
  • The underlying easytest will bind the extracted value to the variable name attribute of the use case class for later use cases to depend on.

    Extract expressions support:

    • jsonpath

    • regular expression

    • xpath expression

  • marks

    String, use case tag, the matching tag use cases can be filtered out in the running parameters

project configuration

easytest The command will read the configuration file named from the current directory  easytest.ini , the following is an example of a complete configuration file:

[project]                                  # 项目配置段
name = xxx项目                             # 项目名称    
host = http://some.api.root.com           # 项目接口根地址
[db_config]                                # 数据库配置
host = dbhost                              # 数据库主机
user = root                                # 数据库用户
password = 123456                         # 数据库密码
db = somedb                                # 数据库名
charset = utf8                            # 字符编码
port = 3306                                # 端口
[interfaces]                              # 接口地址
register: /member/register                # 注册接口对应地址
login: /member/login                      # 登录接口对应地址
withdraw: /member/withdraw
recharge: /member/recharge
add: /loan/add
audit: /loan/audit
invest: /member/invest
[run]                                      # 运行时参数
debug=true                                # 开启调试模式
logfile=a.log                              # 日志文件
marks=success,login                        # 筛选标记
thread_num=10                              # 启动线程数量
retry=3                                    # 失败重跑次数
report=result.json                        # 报告文件
project
project 段,支持 name 和 host
name 项目名称

host 项目接口根地址,注意不要以 / 结尾


db_config
db_config 段,数据库配置,目前仅支持 MySQL
host 数据库主机

user 数据库用户名

password 数据密码

db 数据库名

port 端口

charset 字符串编码


interfaces
interfaces 段,接口名称配置,格式:key=value,key 是接口名称字符串,value 是去掉主机后的接口地址以 / 开头,在用例中 url 字段可以填写 key,easytest 内部会使用项目 host+ 接口地址进行拼接。

run
run 字段,运行时的参数。
debug 调试模式,默认为 false

logfile 生成日志文件,可以是绝对路径或者是相对路径

marks 需要筛选的标记,多个标记使用逗号隔开,例如:success,login,表示会筛选被标记了 success 和 login 的用例。

thread_num 启动线程的数量,默认为 0 表示单线程执行

retry 用例失败后重跑的次数,默认为 0 表示不重跑

report 生成报告的文件名,根据后缀自动生成对应报告,暂只支持 JSON 格式。

注意:命令行参数会覆盖项目配置。


生成模拟测试数据
在测试过程中有时需要动态的生成测试数据,例如手机号码,人名等。easytest 通过 Faker 模块来生产模拟数据,暂时只支持简体中文语言下的接口,详情见 Faker 简体中文 providers。
用例中支持生产模拟测试数据的字段有,url,request,db_assertion。
使用格式为 $生成数据接口名$。
例如在 Faker 中生成手机号码的方法名为 phone_number,那么在用例中使用 $phone_number$ 表示动态生成手机号码。

test:                                 # 表名这是单个测试用例
  title: 一个简单的测试                  # 用例名称
  url: http://httpbin.org/get         # url
  method: get                         # 请求方法
  request:                            # 请求参数字段
    headers:                          # 请求头
      CustomerHeader: lemonban        # 头信息
    params:                           # url参数
      search: lemonban                # url参数键值对
      phone: $phone_number$

The above use case indicates that the url parameter phone is a dynamically generated mobile phone number.

Handling of interface dependencies

easytest In the same test suite, the data returned by the previous test case can be passed to the next use case through variables.

For example, after successful login, pass the returned token value to the next use case that requires token. The transfer steps are as follows:

  1. Add a field in the login use case  extract to extract the response  token value and bind it to the variable name you  admin_token defined

  2. In the following use cases, it can be used in the data part that needs to use the token  #admin_token#to indicate that easytest will automatically replace it

All you need to do is to write use cases according to the rules, and easytest will do the rest.

Description of command line parameters

  • file_or_dir

    String, project path, or use case file that needs to be executed

  • --debug

    Enable log debug mode

  • --logfile

    String, log file path

  • --marks

字符串,运行时选择的标记
  • --thread_num

    Integer, the number of threads started at runtime, the default is 0, which means single-threaded execution

  • --report

    String, test report file path, generate a report in the corresponding format according to the file suffix

Guess you like

Origin blog.csdn.net/m0_73409141/article/details/132279570