Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Time:2021-12-30

This paper aims to share an interface test framework. The environment uses Python 3 + requests + RF test framework, uses Excel to manage test cases and other integrated test data functions, and uses RF to organize test cases and generate test reports. RF can well support the interface of process class and organize various complex process tests.

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases


Test framework processing flow:

1. Initialize the test data in RF, and write the constant directly in the configuration file

1. Excel maintenance test interface

2. Use RF to maintain test cases, that is, use each interface to assemble different test processes, including assertions of each step

3. When RF is executed, the Excel data is parsed according to the assembly sequence

4. After excel is successfully parsed, it will call the interface and send the request

5. After RF is executed, detailed test reports and test logs will be generated automatically

Introduction to test frame structure directory:

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Config /: constant configuration

Framework /: the core processing method of the framework, including excel parsing, sending requests, database interaction, etc

Logs /: log file

Reports /: test reports

Rfsuite /: RF test case

Testdata /: Excel maintenance test interface

Database encapsulation:

1、 config/dbconf. Ini — DB database connection information configuration

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

2、 config/getdbconf. Py — parsing dbconf Ini to obtain the specified configuration item information

def get_ db_ Config (firstline, secondline): — get the value of the specified configuration item, for example, get the dbtype of t0parp

3、 framework/dbhandler. Py — database core method

def get_ db_ Connect (self, SID): — establish a connection to the specified database, such as t0parp

def do_ query_ SQL (self, Sid, SQL, parms = none): — execute query SQL and return query results, that is, select SQL

def do_ commit_ SQL (self, Sid, SQL, parms = none): — execute commit SQL, that is, update and delete SQL

def do_ Func (self, Sid, func, parms): — execute function

def do_ Procedure (self, Sid, pro, parms): — execute procedure

Framework core module:

1. Excel maintenance test interface (main fields)

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Method: required, request method, support post, get, delete, put, postwithmultipart

Header: not required, request header, JSON format, support introducing variables, format ${var_name}

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

URL: required, request address, beginning with /, support variable introduction, format ${var_name}

Parameter: not required, request parameter, JSON format, support to introduce variables, format ${var_name}

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Filename: it is not required. It is a file to be uploaded. It is required to upload a file for postwithmultipart. The full path of the file

assert_ Type: required; assertion type; regex, SQL, in and equal are supported

assert_ Expect: required. Enter an appropriate assertion statement according to the assertion type. It supports the introduction of variables. The format is ${var_name}

Regex: match the regular expression with the response text. The result needs to be > 0. It is applicable to the interface with fixed return result format and dynamic value;

SQL: Format: [email protected] SQL: key. Compare the SQL result with the value corresponding to the key in the response text. The result must be equal. It is applicable to the query class interface;

In: compare the entered value with the response text. The entered value must be included in the response text, which is applicable to the interface that returns HTML, etc;

Equal: compare the entered value with response text. The entered value must be equal to that in response text. It is applicable to the interface with fixed return results;

saved_ var_ Name: not required, variable name to be saved. It is used to handle some dynamic values that need to be passed to subsequent interfaces for use

saved_ var_ Type: not required; dynamic variable value obtaining method; regex and SQL are supported

save_ var_ Get: required. Enter an appropriate value statement according to the dynamic variable value acquisition method. It supports the introduction of variables in the format ${var_name}

Regex: match the regular expression with the response text, and wrap the variables to be extracted with ();

SQL: Format: [email protected] SQL, currently only supports querying 1 field;

2、 Variable setting (2 methods):

1、config/confdata. Py file, applicable to fixed constants

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases


Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

2. It is dynamically saved in the excel test interface and is applicable to dynamic variables. Before calling the interface using dynamic variables, it is necessary to ensure that the variables have been generated

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

3、 framework/requestutil.py  —requestRequest processing

def load_ to_ Dict (data): — convert JSON string to dictionary format, which will be used in the processing of request header and parameter

def do_ Request (method, URL, header = none, param = none, cookie = none, file = none, timeout = 0): — send request

defget_ response_ Value (jsontext, key): — get the returned response text, specify the value value of the key, and then assert the returned value of the interface

4、 framework/excelutil.py  —exceldata processing

def get_ case_ Dict (self, rowno): — get the row specified in Excel_ The data of no is returned in dictionary form [row_no starts from 1, line 0 is the title line]

def format_ case_ Dict (self, sdict): — format the data of Excel rows, replace ${} variables, and return it as a dictionary [sdict returns the dictionary for get_case_dict]

5、 framework/predata.py    —Packaging test data

def get_ case_ Dict (self, filename, sheetname, rowno): — get the data of the specified row of the specified excel file

def get_ case_ col_ Value (self, filename, sheetname, rowno, col_title): — get the cell data of the specified row and column in the specified excel file

6、 framework/doexecutor.py    —Core method of test case execution

def Executor (self, filename, sheetname, rowno, cookie = none): — execute the test case, send the request and keep the variables (if any variables need to be passed)

def assert_ Response (self, assert_type, expect, actual): — test case assertion method

The following is the login cookie interception method of our system. Cookies need to be passed to subsequent interfaces (cookie input parameters). Each system can be customized according to the actual situation.

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases


Interface test case writing

The test cases are maintained by RF process, and the case files are referred to as follows:

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

1__init__.robotThe file is an initialization file. A module executes this file method first and executes other files in order again

   Suite Setup: the whole module is executed once at the beginning, and the kit is initialized

   Suite Teardown: the kit cleaning action is performed once at the end of the whole module

   set global variable ${gloabal_var_name} ${value}: save global variablesgloabal_var_name

Example description:

D – when logging in to the super module, execute it first__ init__. Robot files:

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

2. test case file, based on__init__File action, continue the subsequent test verification action [you can directly reference global variables]

   ***Settings***: dependent library declaration, supported:Suite SetupSuite Teardown, only works on the suite of the current file

   ***Test Cases***:Test cases, supportSetupTeardown, which only applies to the current test case

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

1、     RFExecute use cases in hierarchical order

The following example(Hypothesis module4No child nodes in),The execution sequence is shown by the arrow:

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases


Integration test report

1robotExecute command(2Methods:

1pycharmAdd inexternal toolRobot Run Testsuite

robot -dreports -P $ProjectFileDir$ $FilePathRelativeToProjectRoot$

pycharmSelect the corresponding file and right-click->External Tool-> Robot Run Testsuite

2, command line execution, opencmdwindow

       cdProject root directory

robot -dreports -P .Files or test cases to be executed

2, test reports, and logs are stored inreportsUnder the directory, right-click->Open In Browser

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Based on Python + requests + robot framework, the interface test is realized by using Excel to manage test cases

Recommended Today

Redis featured Q & A

Redis data type type brief introduction characteristic scene String (string) Binary security It can contain any data, such as JPG pictures or serialized objects. One key can store up to 512M It can be used to do the simplest data. It can cache a simple string or a JSON format string. The implementation of redis […]