Disclosure of Invention
The present invention aims to overcome the defects of the prior art and provide a method and a system for automatically generating an interface case.
The purpose of the invention can be realized by the following technical scheme:
an automatic generation method of an interface use case comprises the following steps:
s1: analyzing an interface to be tested to obtain interface information, wherein the interface information comprises an interface address, a parameter name and a parameter type;
s2: generating a request message template according to the interface information, and acquiring a test case template according to the parameter type and the parameter name;
s3: constructing standard message data, generating test data of each parameter according to the sequence of the parameters in the standard message data, and inputting the generated test data into a test case template to obtain a plurality of test cases;
s4: and establishing a test task according to the generated test case, and carrying out interface test on the test case.
Preferably, the interface analysis method in step S1 includes a class method, a plug-in method, and a probe method.
Preferably, the specific step of generating the test data of each parameter includes: and acquiring the name and the type of the parameter, confirming a generation rule corresponding to the parameter, and generating test data of the parameter according to the generation rule.
Preferably, the generation rule includes a type logic rule, an expression rule, a value range rule, a fixed value rule, a database selection rule, and a random generation rule.
Preferably, the type logic rule generates corresponding test data according to the type of the parameter and the parameter logic corresponding to the type;
the expression rule generates corresponding test data according to the regular expression corresponding to the parameters;
the value range rule obtains test data of corresponding types according to a parameter appointed range corresponding to the parameters;
the fixed value rule constructs a data generation library, generates data with fixed values and fixed format types, and selects test data corresponding to the parameter types from the data generation library;
the random generation rule randomly generates test data belonging to the data type according to the data type of the parameter, and the test data generated by the random generation rule comprises the maximum value and the minimum value of the data type and a middle value taken according to a preset step length.
An automatic interface case generation system comprises an interface analysis module, a template acquisition module, a test case acquisition module and a test module,
the interface analysis module is used for analyzing an interface to be tested to acquire interface information, wherein the interface information comprises an interface address, a parameter name and a parameter type;
the template acquisition module is used for generating a request message template according to the interface information and acquiring a test case template according to the parameter type and the parameter name;
the test case acquisition module is used for constructing standard message data, generating test data of each parameter according to the sequence of the parameters in the standard message data, and inputting the generated test data into a test case template to acquire a plurality of test cases;
the test module is used for creating a test task according to the generated test case and carrying out interface test on the test case.
Preferably, the interface analysis module analyzes the interface in a class method, a plug-in method, and a probe method.
Preferably, the specific steps of the test case acquiring module generating the test data of each parameter include: and acquiring the name and the type of the parameter, confirming a generation rule corresponding to the parameter, and generating test data of the parameter according to the generation rule.
Preferably, the generation rule includes a type logic rule, an expression rule, a value range rule, a fixed value rule, a database selection rule, and a random generation rule.
Preferably, the type logic rule generates corresponding test data according to the type of the parameter and the parameter logic corresponding to the type;
the expression rule generates corresponding test data according to the regular expression corresponding to the parameters;
the value range rule obtains test data of corresponding types according to a parameter appointed range corresponding to the parameters;
the fixed value rule constructs a data generation library, generates data with fixed values and fixed format types, and selects test data corresponding to the parameter types from the data generation library;
the random generation rule randomly generates test data belonging to the data type according to the data type of the parameter, and the test data generated by the random generation rule comprises the maximum value and the minimum value of the data type and a middle value taken according to a preset step length.
Compared with the prior art, the invention has the following advantages:
(1) the invention can effectively analyze the interface, obtain the request message template and the test case template and obtain the test data corresponding to each parameter based on the customized generation rule to form the test case, can solve part of scenes related to the service according to the rule, is closer to the real scene, and has more accurate test and better test effect;
(2) the case data is acquired based on the generation rule, so that more repeated and invalid cases are prevented, the cases are controlled to be on a reasonable horizontal line, the time cost is reduced during subsequent re-execution, and the efficiency is improved;
(3) the test case can be conveniently and synchronously pushed to the service based on the corresponding test task, bubble verification is rapidly carried out, and long-term stability and availability of the online service are ensured.
Detailed Description
The invention is described in detail below with reference to the figures and specific embodiments. Note that the following description of the embodiments is merely a substantial example, and the present invention is not intended to be limited to the application or the use thereof, and is not limited to the following embodiments.
Examples
An automatic interface use case generation method is shown in fig. 1, and includes the following steps:
s1: the interface to be tested is analyzed, and interface information is obtained, wherein the interface information comprises an interface address, a parameter name and a parameter type.
The method for analyzing the interface in step S1 includes a class method, a plug-in method, and a probe method.
Specifically, the three modes are as follows:
the class mode is as follows: providing a fixed analysis class, and generating an interface file in a mode of directly executing in a source code;
the plug-in mode: the method can be integrated into a development tool, such as Idea, and executes a command in a plug-in mode to generate an interface file;
the probe mode is as follows: providing a Jar package, starting with an application when the application is started, and directly displaying in a platform in a data acquisition mode;
in this embodiment, bytecode analysis is performed on Java Controller classes, that is, interface implementation classes, to obtain a hierarchical relationship among interface URLs, request manners, parameter names, parameter types, and parameter items, so as to provide basic data for data generation and use case construction.
S2: and generating a request message template according to the interface information, and acquiring a test case template according to the parameter type and the parameter name.
S3: and constructing standard message data, generating test data of each parameter according to the sequence of the parameters in the standard message data, and inputting the generated test data into a test case template to obtain a plurality of test cases.
Specifically, in this embodiment, the specific step of generating the test data of each parameter includes: and acquiring the name and the type of the parameter, confirming a generation rule corresponding to the parameter, and generating test data of the parameter according to the generation rule.
The generation rules comprise type logic rules, expression rules, value range rules, fixed value rules, database selection rules and random generation rules.
And confirming the corresponding generation rule according to the name and the type of each parameter, and generating the test data, wherein the generation flow of each rule is as follows.
The type logic rule generates corresponding test data according to the type of the parameter and the parameter logic corresponding to the type, such as String: NULL, special character, normal character, extra-long character; time: wrong year, month, day, non-time type value, current value; int: non-numeric type, overflow, 0; bool: true, false.
And the expression rule generates corresponding test data according to the regular expression corresponding to the parameters.
Configuring a regular expression, generating data according to the regular expression, generating data in accordance with a specific format or range, and configuring a corresponding data generator according to test requirements; the writing rule of the expression is similar to a regular expression, and the [ ] part represents the range { } part represents the quantity, which can be used in combination, for example, as follows:
100, 200 numbers between 100 and 200
[ a-z,0-9] {2,5 }: generating character strings of length 5 to 5 consisting of lower case letters or numbers
[ wait, processing, success, fail ] randomly taking a value from wait, processing, success, fail
15[6,9] [0-9] {8}, generating a mobile phone number beginning with 156 or 159
[ a-Z, A-Z ] {3,5} [1-9] {2} @126.com. cn: generation of an Email consisting of 3 to 5 letters and 2 digits.
The value range rule obtains test data of corresponding types according to parameter appointed ranges corresponding to the parameters, data are generated based on the value ranges appointed in the parameter annotations in the codes, and the test data are generated according to different types of data.
And the fixed value rule constructs a data generation library, generates data with fixed values and fixed format types, and selects test data corresponding to the parameter types from the data generation library. In this embodiment, the fixed value is, for example, a country, an administrative area, a school name, a school calendar, a driver's license type, or the like; fixed format data such as: mobile phone number, identification card number, license plate, etc.
And the test data generated by the random generation rule comprises the maximum value and the minimum value of the data type and a middle value taken according to a preset step length. Specifically, the data is randomly generated according to the Java data type, the maximum value and the minimum value of the type are taken, and data is fetched according to a certain step length.
In this embodiment, test data is generated in rounds according to the iteration form, and parameters are selected in each round to generate test data until different types of test case situations are covered.
In an embodiment of the present invention, a method for using the generation rule includes: a. and generating an expression for each parameter item associated data generator or filling data in the interface meta information, so that the requirement of generating personalized data is met, and the generated data is more consistent with a service scene. b. Automatically selecting according to the parameter name; and c, setting the priority of different rules, and automatically adapting according to the priority if the rules conflict.
S4: and establishing a test task according to the generated test case, and carrying out interface test on the test case. Specifically, in this embodiment, a plurality of automation tasks are created according to the generated test cases, the execution sequence of the use cases in the test tasks is arranged, and the automatically generated test use cases are executed in a targeted and ordered manner. Meanwhile, in order to meet the requirements of flexible and distributed execution, the idle state of the execution machine is automatically monitored, a reasonable distribution strategy is carried out, and the use case execution efficiency is improved.
Based on the test task result of the invention, an HTML test report is formed by adopting a fixed template, the returned Json message is analyzed, the code and the message in the message are highlighted, and the part of the corresponding request message in which the data is automatically generated is highlighted, so that the interpretation of the subsequent test report is facilitated.
Based on the same inventive concept, the embodiment of the invention also provides an automatic generation system of the interface use case, and as the principle of solving the problems of the devices is similar to the automatic generation method of the interface use case, the implementation of the method can be referred, and repeated parts are not repeated.
An interface case automatic generation system comprises an interface analysis module, a template acquisition module, a test case acquisition module and a test module, wherein the interface analysis module is used for analyzing an interface to be tested and acquiring interface information, and the interface information comprises an interface address, a parameter name and a parameter type; the template acquisition module is used for generating a request message template according to the interface information and acquiring a test case template according to the parameter type and the parameter name; the test case acquisition module is used for constructing standard message data, generating test data of each parameter according to the sequence of the parameters in the standard message data, and inputting the generated test data into the test case template to acquire a plurality of test cases; the test module is used for creating a test task according to the generated test case and carrying out interface test on the test case.
The interface analysis module analyzes the interface in a class method, a plug-in mode and a probe mode.
The specific steps of the test case acquisition module for generating the test data of each parameter comprise: and acquiring the name and the type of the parameter, confirming a generation rule corresponding to the parameter, and generating test data of the parameter according to the generation rule. The generation rules comprise type logic rules, expression rules, value range rules, fixed value rules, database selection rules and random generation rules.
Specifically, the type logic rule generates corresponding test data according to the type of the parameter and the parameter logic corresponding to the type; the expression rule generates corresponding test data according to the regular expression corresponding to the parameters; the value range rule acquires test data of corresponding types according to a parameter appointed range corresponding to the parameters; the fixed value-taking rule constructs a data generation library, generates data with fixed values and fixed format types, and selects test data corresponding to the parameter types from the data generation library; and the test data generated by the random generation rule comprises the maximum value and the minimum value of the data type and a middle value taken according to a preset step length.
The above embodiments are merely examples and do not limit the scope of the present invention. These embodiments may be implemented in other various manners, and various omissions, substitutions, and changes may be made without departing from the technical spirit of the present invention.