Interface testing method and device

Through the interface testing method, the request header and request information are converted into dictionary format, and the interface request is sent with the private attributes of the class, which solves the problems of high testing difficulty and cost in the prior art, and realizes automated interface request and response message verification. Improve testing efficiency and reduce technical requirements.

CN113391972BActive Publication Date: 2025-01-07BEIJING FOUNDER ELECTRONICS CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202110868495.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-07-30
Publication Date
2025-01-07
Estimated Expiration
2041-07-30

AI Technical Summary

Technical Problem

The existing interface automation testing methods require testers to have strong coding capabilities, resulting in high testing difficulty, high time and labor costs.

Method used

Provide an interface testing method, by obtaining the request header and request information input by the user, converting it into a dictionary format, and assigning it to the private attributes of the class, sending a request to the interface to be tested according to the private attributes, and converting the response message into an object Verification of formats reduces the need to write code and the steps to parse response messages.

Benefits of technology

It reduces the difficulty of testing and technical requirements for testers, reduces time and labor costs, and improves testing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113391972B_ABST
    Figure CN113391972B_ABST
Patent Text Reader

Abstract

The present application provides an interface testing method and device, the method comprising: obtaining a request header and request information input by a user, the request information comprising request parameters and / or request body, converting the request header and request information into a dictionary format, assigning the data obtained after the format conversion to a private attribute of a class, sending an interface request to an interface to be tested according to the private attribute of the class, so that the interface to be tested returns a response message after processing the interface request, converting the response message into an object format, and verifying the response message after the format conversion to obtain a verification result. In this way, in this process, the tester does not need to write too much code to implement the setting and conversion of the request header and request message, nor does he need to parse the response message, thereby reducing the difficulty of the test and the technical requirements for the tester, and reducing the time cost and labor cost.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of automated testing technology, and in particular to an interface testing method and device. Background Art

[0002] With the development of automated testing technology, interface automated testing methods provide services for more and more platforms. Users can customize the services they need based on the interfaces provided by the platform. The main purpose of the interface automated testing process is to reduce manual participation and improve testing efficiency.

[0003] In the prior art, interface automation testing can be performed by calling HTTP related class libraries of various programming languages, or by keyword-driven automation testing. Specifically, based on the above two methods, the tester pre-writes the code for the request header, request parameters and request body corresponding to the interface to be tested that meets the requirements, sends the interface request message corresponding to the code to the interface to be tested, receives the response message fed back by the interface to be tested, parses the response message into a format that is convenient for obtaining field values, and further verifies the response message.

[0004] However, the above two methods require testers to have strong coding skills and need to perform a lot of coding, which increases the difficulty of testing and consumes more time and manpower costs. Summary of the invention

[0005] The present application provides an interface testing method and device to solve the problem that interface testing in the prior art is difficult and requires a lot of time and manpower costs.

[0006] In the first aspect, an embodiment of the present application provides an interface testing method, which includes: obtaining a request header and request information input by a user, the request information including request parameters and / or a request body; converting the request header and request information into a dictionary format, and assigning the data obtained after the format conversion to the private attributes of the class; sending an interface request to the interface to be tested according to the private attributes of the class, so that the interface to be tested processes the interface request and then returns a response message; converting the response message into an object format, and verifying the response message after the format conversion to obtain a verification result.

[0007] In a possible implementation, before assigning the data obtained after format conversion to the private attributes of the class, it also includes: encapsulating the private attributes of the class with the methods of the class; accordingly, sending an interface request to the interface to be tested based on the private attributes of the class, including: sending an interface request to the interface to be tested by calling the method encapsulated with the private attributes of the class.

[0008] In one possible implementation, keyword input rules are configured, wherein the keyword input rules include at least one of the following: input rules for data transmission between interfaces, input rules for multi-database verification, and input rules for specific text or symbols; accordingly, the request header and request information are converted into a dictionary format, including: according to the keyword input rules, the request header and request information are converted into a dictionary format.

[0009] In a possible implementation, an interface request is sent to the interface to be tested by calling a method encapsulated with the private properties of the class, including one or more of the following: sending an interface request to the interface to be tested by calling a post method, the interface request is used for the interface to be tested to submit a first type of data to the server, the first type of data is data with a length less than a preset value or string type data; sending an interface request to the interface to be tested by calling a post_files method, the interface request is used for the interface to be tested to submit a second type of data to the server, the second type of data is data with a length greater than a preset value or file type data; sending an interface request to the interface to be tested by calling a get method, the interface request is used for the interface to be tested to obtain resources from the server for display on the browser; sending an interface request to the interface to be tested by calling a put method, the interface request is used for the interface to be tested to update resources from the server; sending an interface request to the interface to be tested by calling a delete method, the interface request is used for the interface to be tested to request the server to delete resources.

[0010] In one possible implementation, converting the response message into an object format includes: obtaining the number of fields in the response message through a preset keyword; obtaining the length of each field in the response message; and converting the response message into an object format based on the number of fields and the length of each field.

[0011] In one possible implementation, the response message includes: an HTTP status code and an interface response code, and the format-converted response message is verified to obtain a verification result, including: determining whether the interface request connection is successful based on the HTTP status code and the interface response code; if so, verifying the format-converted response message using an object value-taking method using a stored expected value to obtain a verification result; if not, determining that the verification result is that the interface request connection failed.

[0012] In one possible implementation, the expected value includes the converted request header and request information, and the response message after the format conversion is verified by using the stored expected value using the object value method to obtain a verification result, including: extracting keywords from the converted request header and request information, and using the object value method to query whether the keywords exist in the response message after the format conversion to obtain a verification result.

[0013] In the second aspect, an embodiment of the present application provides an interface testing device, including: an acquisition module, used to obtain a request header and request information input by a user, the request information including request parameters and / or a request body; a processing module, used to convert the request header and request information into a dictionary format, and assign the data obtained after the format conversion to the private attributes of the class; a sending module, used to send an interface request to the interface to be tested according to the private attributes of the class, so that the interface to be tested processes the interface request and then returns a response message; a verification module, used to convert the response message into an object format, and verify the response message after the format conversion to obtain a verification result.

[0014] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a processor, a memory, and a computer program; wherein the computer program is stored in the memory and is configured to be executed by the processor, and the computer program includes instructions for executing the interface testing method as described in any one of the first aspects.

[0015] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the interface testing method as described in any one of the first aspects.

[0016] In a fifth aspect, an embodiment of the present application provides a computer program product, including a computer program, which, when executed by a processor, implements the interface testing method as described in any one of the first aspects.

[0017] To summarize, the embodiments of the present application provide an interface testing method and device, which can, after obtaining the request header and request information input by the user, convert the format of the request header and request information and assign them to the private properties of the class, send an interface request to the interface to be tested through the private properties of the class, and convert the format of the response message returned by the interface to be tested after processing the interface request. In this way, in this process, the tester does not need to write too much code to implement the setting and conversion of the request header and request message, nor does he need to parse the response message, thereby reducing the difficulty of testing and the technical requirements for testers, and reducing time and labor costs. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] Figure 1A schematic diagram of an application scenario of an interface testing method provided in an embodiment of the present application;

[0019] Figure 2 A flowchart of an interface testing method provided in an embodiment of the present application;

[0020] Figure 3 A method class diagram of an interface testing method provided in an embodiment of the present application;

[0021] Figure 4 A schematic diagram of the principle of an interface testing method provided in an embodiment of the present application;

[0022] Figure 5 A schematic diagram of the structure of an interface testing device provided in an embodiment of the present application;

[0023] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0024] In order to facilitate the clear description of the technical solutions of the embodiments of the present application, in the embodiments of the present application, the words "first", "second" and the like are used to distinguish the same items or similar items with substantially the same functions and effects. For example, the first device and the second device are only used to distinguish different devices, and their order is not limited. Those skilled in the art can understand that the words "first", "second" and the like do not limit the quantity and execution order, and the words "first", "second" and the like do not necessarily limit them to be different.

[0025] It should be noted that, in this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in this application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.

[0026] In the present application, "at least one" means one or more, and "plurality" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. The character " / " generally indicates that the previous and next associated objects are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b, or c can mean: a, b, c, ab, ac, bc, or abc, where a, b, c can be single or multiple.

[0027] The embodiments of the present application are introduced below with reference to the accompanying drawings. Figure 1 This is a schematic diagram of an application scenario of an interface testing method provided in an embodiment of the present application. The interface testing method provided in the present application can be applied to: Figure 1 In the application scenario shown. The application scenario includes: user 101, client 102 and server 103. In this application scenario, when user 101 needs to query certain information, he can enter certain information in client 102 and perform related query operations. Further, client 102 sends an interface request to server 103. Correspondingly, server 103 receives the interface request, completes data exchange, and returns a response message. In this way, user 101 can query the result through client 102.

[0028] In one possible implementation, when the client and the server exchange data, the server usually calls HTTP-related libraries of various programming languages ​​to perform interface automation testing, for example, calling the HTTP library or the Requests library in the Robot Framework to perform interface automation testing.

[0029] In a possible implementation, keyword-driven interface automation testing can also be used, such as interface automation testing based on HttpClient and HttpCore in Java, or interface automation testing based on HttpRequest in Python.

[0030] Specifically, based on the above two methods, according to the format requirements of the request header, request parameters and request body in the interface document corresponding to the interface to be tested, the tester needs to pre-write the code of the request header, request parameters and request body corresponding to the interface to be tested that meets the requirements by calling the relevant class library of the relevant programming language, and further, send the interface request message corresponding to the code to the interface to be tested. After receiving the response message fed back by the interface to be tested, according to the format of the response message defined in the interface document, the response message is parsed into a format that is convenient for obtaining field values, and further, the response message is verified.

[0031] However, the above two methods require testers to have strong coding skills and need to perform a lot of coding, which increases the difficulty of testing and consumes more time and manpower costs.

[0032] Therefore, an embodiment of the present application provides an interface testing method, which can, after obtaining the request header and request information input by the user, convert the format of the request header and request information and assign them to the private properties of the class, send an interface request to the interface to be tested through the private properties of the class, and can convert the format of the response message returned after the interface to be tested processes the interface request. In this way, in this process, the tester does not need to write too much code to implement the setting and conversion of the request header and request message, nor does he need to parse the response message, thereby reducing the difficulty of testing and the technical requirements for testers, and reducing time and labor costs.

[0033] For example, Figure 2 A flow chart of an interface testing method provided in an embodiment of the present application is as follows: Figure 2 As shown, the method of the embodiment of the present application includes:

[0034] S201. Obtain a request header and request information input by a user, where the request information includes request parameters and / or a request body.

[0035] In an embodiment of the present application, the request header may refer to a JSON string that carries the client's request type information, which is used to tell the server some information. For example, it can tell the server the data types supported by the client, the encoding used by the client, the data compression format supported by the client, the client's language environment, the host name that the client wants to access, the cache time of the resource, the client's software environment, and other information.

[0036] The request parameter may refer to the parameter data of the formatted string that formally encodes the user input information, and the request body may refer to the parameter data that formally encodes the user input information and is greater than a certain threshold, such as the parameter data of the file type. It should be noted that the embodiment of the present application does not specifically limit the value of the threshold, and the threshold can be designed according to the specific situation.

[0037] Exemplarily, the server may obtain a request header and request information input by a user, wherein the request information includes request parameters and / or a request body. For example, the request header is Accept:json, which is used to tell the server that the request is in json format.

[0038] It can be understood that the request information may include request parameters, may include a request body, or may include request parameters and a request body, and this embodiment of the present application does not specifically limit this.

[0039] S202: Convert the request header and request information into a dictionary format, and assign the data obtained after the format conversion to a private attribute of the class.

[0040] In the embodiment of the present application, the dictionary format may refer to a mapping format, which is represented as follows: each key-value pair is separated by a colon ":", each key-value pair is separated by a comma ",", and the entire dictionary is included in curly braces "{}", for example, d = {key1:value1, key2:value2}. The private attributes of a class may refer to attributes that are changed by a specific method. Private attributes cannot be called directly, but can be called through class methods.

[0041] Exemplarily, the server may convert the request header and request information input by the user into a dictionary format, and further assign the data obtained after the format conversion to the private attributes of the class. For example, the request header and request information input by the user may be converted into a dictionary format of d={key1:value1, key2:value2}, and further assign d={key1:value1, key2:value2} to the private attributes of the class.

[0042] S203: Send an interface request to the interface to be tested according to the private attributes of the class, so that the interface to be tested processes the interface request and returns a response message.

[0043] In an embodiment of the present application, a response message may refer to information returned by the server's tested interface and may include a response status code and a response body. The response status code may refer to a status of the interface request and response that the server tells the client, and may include an HTTP status code and an interface response code. The response body may refer to all content data information returned. The HTTP status code and the interface response code indicate the return result of the client's HTTP request and mark whether the server-side processing is normal or an error occurs. They are composed of three digits and a reason phrase. For example, 200 OK indicates that the server has successfully processed the interface request and 403 Forbidden indicates that the server has rejected the access.

[0044] Exemplarily, the interface request required by the user can be sent to the interface to be tested of the server according to the private attributes of the class, so that the interface to be tested processes the interface request and then returns a response message. For example, when searching for a place "Beijing" on the browser, the server converts "Beijing" into a dictionary format and assigns it to the private attributes of the class. Further, according to the private attributes of the class, an interface request is sent to the interface to be tested, so that the interface to be tested processes the interface request and then returns a response message about "Beijing".

[0045] S204: Convert the response message into an object format, and verify the response message after the format conversion to obtain a verification result.

[0046] In an embodiment of the present application, the object format may refer to converting a string in json format into the key and value of a json object, that is, converting the string in json format that the server responds to the client into a json object. For example, the definition format of the json object is as follows: the whole is defined using "[]", where a column or the same result data is defined using "{}", the data in "{}" is defined in the "key:value" format, and multiple attributes are separated by ",", which can be expressed as: [{key1:'value1', key2:'value2'}, {key3:'value3', key4:'value4'}].

[0047] Exemplarily, the server may convert the format of the response message returned after the server's tested interface processes the interface request into an object format. For example, the converted object format is: [{key1:'value1', key2:'value2'}, {key3:'value3', key4:'value4'}], and further verify the response message after the format conversion to obtain a verification result.

[0048] Therefore, the interface testing method provided in the embodiment of the present application can convert the request header and request information format into a dictionary format after obtaining the request header and request information input by the user, and assign them to the private attributes of the class. Furthermore, the interface request can be sent to the interface to be tested through the private attributes of the class, and the response message returned by the interface to be tested after processing the interface request can be converted into an object format for verification. In this way, the sending of the interface request and the format conversion of the response information can be automatically realized according to the request header and request information input by the user. In this process, the tester does not need to write too much code to implement the setting and conversion of the request header and request message, nor does he need to parse the response message, thereby reducing the testing difficulty and technical requirements for testers, speeding up testing efficiency, and reducing time and labor costs.

[0049] In a possible implementation, before assigning the data obtained after format conversion to the private attributes of the class, it also includes: encapsulating the private attributes of the class with the methods of the class; accordingly, sending an interface request to the interface to be tested based on the private attributes of the class, including: sending an interface request to the interface to be tested by calling the method encapsulated with the private attributes of the class.

[0050] The embodiments of the present application can be implemented by programming languages ​​such as Python, wherein the class method may refer to the function in the class, which can be divided into: instance method, class method, static method, and there may be a post (create) method, which indicates a create function, a post_files (create) method, which also indicates a create function, a get (query) method, which indicates a view function, a put (update) method, which indicates an update function, a delete (delete) method, which indicates a delete function, and the like.

[0051] Exemplarily, before the server assigns the data obtained after format conversion to the private attributes of the class, the private attributes of the class can be encapsulated with the class methods; further, the method encapsulated with the private attributes of the class can be called to carry the converted request header and request information to send an interface request to the interface to be tested. For example, calling the delete method to send an interface request to the interface to be tested will automatically carry the converted request header and request information, and further, the deletion step is performed.

[0052] Therefore, by encapsulating the private properties of the class with the class methods, an interface request is sent to the interface to be tested. In this way, the converted request header and request information can be carried when the method is called. Testers do not need to write too much code to set and convert the request header and request message, reducing time and labor costs.

[0053] In one possible implementation, keyword input rules are configured, wherein the keyword input rules include at least one of the following: input rules for data transmission between interfaces, input rules for multi-database verification, and input rules for specific text or symbols; accordingly, the request header and request information are converted into a dictionary format, including: according to the keyword input rules, the request header and request information are converted into a dictionary format.

[0054] In an embodiment of the present application, keyword input rules may refer to rules set by a server for extracting required keywords according to certain rules, input rules for data transmission between interfaces may refer to rules that the data type transmitted conforms to the input requirements of the interface to be tested, input rules for multi-database verification may refer to verification rules that the data type conforms to multiple types of databases, and input rules for specific text or symbols may refer to input rules that there are specific texts or symbols that conform to the data type.

[0055] Exemplarily, keyword input rules may be configured, and further, the server may convert the request header and request information into a required dictionary format according to the configured keyword input rules.

[0056] It should be noted that the embodiments of the present application are not limited to the above-mentioned keyword input rules, but may also include other input rules. As long as the interface testing method of the embodiments of the present application can be executed, the embodiments of the present application do not make specific limitations on this.

[0057] Therefore, by configuring keyword input rules and converting the request header and request information into a dictionary format, the problem of increased testing difficulty caused by a large amount of repeated coding is avoided, the amount of coding is reduced, and the coding efficiency is improved.

[0058] In a possible implementation, an interface request is sent to the interface to be tested by calling a method encapsulated with a private property of a class, including one or more of the following: an interface request is sent to the interface to be tested by calling a post method, the interface request is used for the interface to be tested to submit a first type of data to the server, the first type of data is data with a length less than a preset value or string type data; an interface request is sent to the interface to be tested by calling a post_files method, the interface request is used for the interface to be tested to submit a second type of data to the server, the second type of data is data with a length greater than a preset value or file type data; an interface request is sent to the interface to be tested by calling a get method, the interface request is used for the interface to be tested to obtain resources from the server for display on the browser; an interface request is sent to the interface to be tested by calling a put method, the interface request is used for the interface to be tested to update resources from the server; an interface request is sent to the interface to be tested by calling a delete method, the interface request is used for the interface to be tested to request the server to delete resources.

[0059] In an embodiment of the present application, the preset value may refer to a threshold value that is set to distinguish the size of a data type. The data type is distinguished by the preset value and can be divided into a first type of data and a second type of data, wherein the first type of data is data with a length less than the preset value or string type data, for example, string type data, and the second type of data is data with a length greater than the preset value, for example, file type, picture type, and video type data.

[0060] For example, Figure 3 A method class diagram of an interface testing method provided in an embodiment of the present application, such as Figure 3As shown, FATL represents the class name. During interface testing, it is abstracted into four attributes of the class, namely, request header (i.e., Headers), request parameters (i.e., Parameters), response status code (i.e., Status) and response message (i.e., Data). Five methods are designed in the class, namely, post method (i.e., fapi_post()), post_files method (i.e., fapi_post_files()), get method (i.e., fapi_get()), put method (i.e., fapi_put()), delete method (i.e., fapi_delete()), where "+" represents a public method, that is, each attribute can execute the method with "+".

[0061] Optionally, an interface request may be sent to the interface to be tested by calling a post method, and the post method may be used to submit a first type of data, such as string data, to the server.

[0062] Optionally, an interface request may be sent to the interface to be tested by calling a post_files method, and the post_files method may be used to submit second type data, such as file, picture, or video data, to the server.

[0063] Optionally, you can send an interface request to the interface to be tested by calling the get method, which can be used to obtain resources from the server for display on the browser. For example, it is used to tell the server what content needs to be obtained, and when requesting a static page, the file content is directly returned to the browser. When requesting a dynamic page, query parameters can be provided to obtain the corresponding content.

[0064] Optionally, an interface request may be sent to the interface to be tested by calling a put method. The put method may be used to update resources from a server, for example, data transmitted from a client to a server replaces the content of a specified document.

[0065] Optionally, an interface request may be sent to the interface to be tested by calling a delete method. The delete method may be used to request the server to delete resources. For example, the server may be requested to delete a specified page.

[0066] Specifically, FATL and the four attributes are in an aggregation relationship. In the figure, the diamond points to the overall FATL, and the arrows point to the four attributes. Each connection will have two endpoints, and each endpoint can have a cardinality, indicating how many instances this class can have. Among them, "1" means there is 1 instance. The four attributes can have corresponding method operations respectively. The request header and request parameters have common method operations, namely set(), to_dictionary() and reset(). set() represents the setting method, to_dictionary() represents the method of converting to a dictionary, and reset() represents the reset method. For example, writing the reset() code can reset the data of the request header and request parameters.

[0067] The method operations corresponding to the response status code are get_status_code(), which represents the method for obtaining the status response code; the method operations corresponding to the response message are field_count(), which represents the method for obtaining the number of fields (columns) in the result set of the most recently executed query; and get_length(), which represents the method for obtaining the length. Among them, "+" represents a public method, which can be called by all attributes, and "-" represents a private method, which can only be called by the corresponding attribute.

[0068] It should be noted that Figure 3 The methods shown are examples only and do not cover all methods corresponding to all properties.

[0069] Therefore, an interface request can be sent to the interface to be tested by calling the method encapsulated with the private properties of the class. In this way, the private properties of the class will be automatically brought when the interface request is sent, thereby reducing the coupling between parameter settings and request sending in interface automation testing, reducing the amount of code written, and reducing time and labor costs.

[0070] In a possible implementation, converting the response message into an object format includes: obtaining the number of fields in the response message through a preset keyword; obtaining the length of each field in the response message; and converting the response message into an object format according to the number of fields and the length of each field.

[0071] In the embodiment of the present application, the keyword may refer to a field that can best summarize the information content that the user wants to find, and may include keywords of attribute names in the data sent by the user, or keywords of method operations, etc. Further, the keyword of the attribute name may include attribute nouns in the request parameter data, such as "name", "number" and other nouns; the method operation keywords may include post, get, put, etc.

[0072] Exemplarily, the server can obtain the number of fields in the response message through a preset keyword; further, obtain the length of each field in the response message; for example, the keyword is set to "area", that is, the regional name, then it is necessary to obtain the number of fields related to the regional name in the response message, that is, area1:value1, area2:value2, area3:value3, area4:value4 can be obtained, and further, the length of each field in the response message is obtained, the field length of value1-value4, and the response message is converted into an object format according to the number of fields and the length of each field, which is [{area 1:'value1', area 2:'value2'}, {area 3:'value3', area 4:'value4'}].

[0073] Therefore, converting the response message into an object format eliminates the need to parse the response message, which simplifies the verification process, thereby reducing the difficulty of testing and the technical requirements for testers and speeding up verification efficiency.

[0074] In one possible implementation, the response message includes: an HTTP status code and an interface response code, and the format-converted response message is verified to obtain a verification result, including: determining whether the interface request connection is successful based on the HTTP status code and the interface response code; if so, verifying the format-converted response message using the object value-taking method using the stored expected value to obtain a verification result; if not, determining that the verification result is that the interface request connection failed.

[0075] In the embodiment of the present application, the expected value may refer to the result value that needs to be queried for the user input information, and the method of obtaining the value of the object may include: using the dot method, for example, obj.ab; using the bracket method, for example, obj["abc"].

[0076] It can be understood that in the embodiments of the present application, the method of obtaining the value of an object can use either the dot method or the bracket method, and there is no specific limitation on this, as long as the desired effect is achieved.

[0077] Exemplarily, whether the interface request connection is successful is determined based on the HTTP status code and the interface response code; if "200OK" is returned, it means that the server has successfully processed the interface request, and the format-converted response message can be verified using the object value-taking method through the stored expected value to obtain a verification result; if "403Forbidden" is returned, it means that the server rejects the access, and the verification result is determined to be a failure of the interface request connection.

[0078] It should be noted that HTTP status codes and interface response codes have many forms, among which 1xx means that the request has been received and can be processed further; 2xx means that the request has been successfully received; 3xx means that further operations must be performed to complete the request; 4xx means that the request has a syntax error or the request cannot be implemented; 5xx means that the server failed to implement a legal request; xx represents a number, which is not listed here one by one.

[0079] Therefore, verifying the response message after format conversion and obtaining the verification result can greatly reduce the code writing, reduce the testing difficulty and technical requirements for testers, and speed up the verification efficiency.

[0080] In one possible implementation, the expected value includes the converted request header and request information, and the response message after the format conversion is verified by using the stored expected value using the object value method to obtain a verification result, including: extracting keywords from the converted request header and request information, and using the object value method to query whether the keywords exist in the response message after the format conversion to obtain a verification result.

[0081] Exemplarily, the server can extract keywords from the converted request header and request information, and further use the object value method to query whether the keywords exist in the response message after the format conversion to obtain the verification result. For example, if you need to query the relevant information of "beijing", the server can extract the keywords in the converted request header and request information, and there is the noun "beijing". Then you need to use the object value method to query whether the noun "beijing" exists in the response message after the format conversion. If the noun "beijing" exists, it means that the verification is successful and the interface completes the query task. If the noun "beijing" does not exist, it means that the verification fails and the interface does not complete the query task.

[0082] Specifically, there is a one-to-one correspondence between the keywords in the converted request header and request information and the verification results. If there are keywords in the response message after format conversion, the keywords can be used to compare the keywords in the converted request header and request information to obtain the verification results. The verification results may include whether the interface completes the task specified by the user. If there are no keywords in the response message after format conversion, it means that the interface has not completed the task specified by the user.

[0083] Therefore, by verifying whether the keywords in the converted request header and request information are consistent with the keywords in the converted response message, a verification result is obtained, which improves the credibility of the verification and further improves the accuracy.

[0084] In combination with the above embodiments, Figure 4The following is a schematic diagram of the principle of an interface testing method provided in an embodiment of the present application. Figure 4 As shown, the execution method steps of the embodiment of the present application include:

[0085] Step A: The test starts. The request header and request body (i.e., request information) are set through the public methods of setting the request header and request body. The built-in request header and built-in request body refer to converting the request header and request body into a dictionary format, and assigning the data obtained after the format conversion to the private attributes of the class. Among them, the private attributes of the class and the class methods have been encapsulated together, and further execute step B.

[0086] Step B: Send a request to the interface to be tested and execute step C.

[0087] Step C: Receive the response message, convert the response message into an object format, verify the response message after the format conversion, and execute step D.

[0088] Step D: Get the verification result and the test ends.

[0089] In the foregoing embodiments, the interface testing method provided by the embodiments of the present application is introduced, and in order to implement the various functions in the method provided by the embodiments of the present application, the electronic device as the execution subject may include a hardware structure and / or a software module, and the above functions are implemented in the form of a hardware structure, a software module, or a hardware structure plus a software module. Whether one of the above functions is executed in the form of a hardware structure, a software module, or a hardware structure plus a software module depends on the specific application and design constraints of the technical solution.

[0090] For example, Figure 5 A schematic diagram of the structure of an interface testing device provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, the device includes: an acquisition module 510, a processing module 520, a sending module 530 and a verification module 540, wherein the acquisition module 510 is used to obtain the request header and request information input by the user, and the request information includes request parameters and / or request body; the processing module 520 is used to convert the request header and request information into a dictionary format, and assign the data obtained after the format conversion to the private attributes of the class; the sending module 530 is used to send an interface request to the interface to be tested according to the private attributes of the class, so that the interface to be tested processes the interface request and returns a response message; the verification module 540 is used to convert the response message into an object format, and verify the response message after the format conversion to obtain a verification result.

[0091] In one possible implementation, before assigning the data obtained after format conversion to the private attributes of the class, the processing module 520 is also used to encapsulate the private attributes of the class with the class methods; accordingly, the sending module 530 is specifically used to send an interface request to the interface to be tested by calling the method encapsulated with the private attributes of the class.

[0092] In one possible implementation, the processing module 520 is also used to configure keyword input rules, wherein the keyword input rules include at least one of the following: input rules for data transmission between interfaces, input rules for multi-database verification, and input rules for specific text or symbols; accordingly, according to the keyword input rules, the request header and request information are converted into a dictionary format.

[0093] In one possible implementation, the sending module 530 is specifically used to send an interface request to the interface to be tested by calling the post method, the interface request is used for the interface to be tested to submit a first type of data to the server, the first type of data is data with a length less than a preset value or string type data; send an interface request to the interface to be tested by calling the post_files method, the interface request is used for the interface to be tested to submit a second type of data to the server, the second type of data is data with a length greater than a preset value or file type data; send an interface request to the interface to be tested by calling the get method, the interface request is used for the interface to be tested to obtain resources from the server for display on the browser; send an interface request to the interface to be tested by calling the put method, the interface request is used for the interface to be tested to update resources from the server; send an interface request to the interface to be tested by calling the delete method, the interface request is used for the interface to be tested to request the server to delete resources.

[0094] In a possible implementation, the verification module 540 is specifically used to obtain the number of fields in the response message through a preset keyword; obtain the length of each field in the response message; and convert the response message into an object format according to the number of fields and the length of each field.

[0095] In one possible implementation, the response message includes: an HTTP status code and an interface response code. The verification module 540 is specifically used to determine whether the interface request connection is successful based on the HTTP status code and the interface response code; if so, the format-converted response message is verified using the object value method using the stored expected value to obtain a verification result; if not, the verification result is determined to be an interface request connection failure.

[0096] In one possible implementation, the expected value includes the converted request header and request information. The verification module 540 is specifically used to extract keywords from the converted request header and request parameters, and use the object value method to query whether the keywords exist in the response message after the format conversion to obtain the verification result.

[0097] The present application also provides a schematic diagram of the structure of an electronic device. Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 6 As shown, the electronic device may include: a memory 601 and a processor 602; the memory 601 stores a computer program; the processor 602 executes the computer program stored in the memory 601, so that the processor 602 executes the method described in any of the above embodiments.

[0098] The memory 601 and the processor 602 may be connected via a bus 603 .

[0099] The specific implementation principles and effects of the interface testing device provided in the embodiment of the present application can be found in the relevant descriptions and effects corresponding to the above embodiments, and will not be elaborated here.

[0100] An embodiment of the present application also provides a computer-readable storage medium, which stores computer program execution instructions. When the computer program execution instructions are executed by a processor, they are used to implement the interface testing method in any of the aforementioned embodiments of the present application.

[0101] An embodiment of the present application also provides a chip for executing instructions, which is used to execute the interface testing method executed by the electronic device in any of the aforementioned embodiments of the present application.

[0102] An embodiment of the present application also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, it can implement the interface testing method performed by the electronic device in any of the aforementioned embodiments of the present application.

[0103] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of modules is only a logical function division. There may be other division methods in actual implementation, such as multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.

[0104] The modules described as separate components may or may not be physically separated, and the components shown as modules may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the modules may be selected according to actual needs to implement the solution of this embodiment.

[0105] In addition, each functional module in each embodiment of the present application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The above-mentioned module-composed unit can be implemented in the form of hardware or in the form of hardware plus software functional units.

[0106] The above-mentioned integrated module implemented in the form of a software function module can be stored in a computer-readable storage medium. The above-mentioned software function module is stored in a storage medium, including a number of instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) or a processor to perform some steps of the method described in each embodiment of the present application.

[0107] It should be understood that the processor may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application specific integrated circuits (ASIC), etc. A general-purpose processor may be a microprocessor or the processor may be any conventional processor, etc. The steps of the method disclosed in the application may be directly implemented as being executed by a hardware processor, or may be implemented by a combination of hardware and software modules in the processor.

[0108] The memory may include high-speed random access memory (RAM), and may also include non-volatile memory (NVM), such as at least one disk memory, and may also be a USB flash drive, a mobile hard disk, a read-only memory, a disk or an optical disk, etc.

[0109] The bus can be an industry standard architecture (ISA) bus, a peripheral component interconnect (PCI) bus, or an extended industry standard architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, the bus in the drawings of this application is not limited to only one bus or one type of bus.

[0110] The above storage medium can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic memory, flash memory, magnetic disk or optical disk. The storage medium can be any available medium that can be accessed by a general or special purpose computer.

[0111] An exemplary storage medium is coupled to a processor so that the processor can read information from the storage medium and write information to the storage medium. Of course, the storage medium can also be a component of the processor. The processor and the storage medium can be located in an application specific integrated circuit (ASIC). Of course, the processor and the storage medium can also exist as discrete components in an electronic device or a main control device.

[0112] The above is only a specific implementation of the embodiment of the present application, but the protection scope of the embodiment of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the embodiment of the present application should be included in the protection scope of the embodiment of the present application. Therefore, the protection scope of the embodiment of the present application should be based on the protection scope of the claims.

Claims

1. An interface testing method, characterized in that: include: Obtaining a request header and request information input by a user, wherein the request information includes request parameters and / or a request body; Convert the request header and request information into a dictionary format, encapsulate the private attributes of the class with the methods of the class, and assign the data obtained after the format conversion to the private attributes of the class; By calling the method encapsulated with the private attribute of the class and carrying the converted request header and request information, an interface request is sent to the interface to be tested, so that the interface to be tested processes the interface request and returns a response message; The number of fields in the response message is obtained through a preset keyword; the length of each field in the response message is obtained; the response message is converted into an object format according to the number of fields and the length of each field, and the response message after the format conversion is verified to obtain a verification result.

2. The method according to claim 1, characterized in that: Also includes: Configuring keyword input rules, wherein the keyword input rules include at least one of the following: input rules for data transmission between interfaces, input rules for multi-database verification, and input rules for specific characters or symbols; Accordingly, the request header and request information are converted into a dictionary format, including: The request header and request information are converted into a dictionary format according to the keyword input rule.

3. The method according to claim 1, characterized in that Send an interface request to the interface to be tested by calling the method encapsulated with the private properties of the class, including one or more of the following: Send an interface request to the interface to be tested by calling the post method, wherein the interface request is used for the interface to be tested to submit a first type of data to the server, wherein the first type of data is data with a length less than a preset value or data of a string type; Send an interface request to the interface to be tested by calling the post_files method, wherein the interface request is used for the interface to be tested to submit second type data to the server, wherein the second type of data is data with a length greater than a preset value or file type data; Send an interface request to the interface to be tested by calling the get method, wherein the interface request is used for the interface to be tested to obtain resources for display on the browser from the server; Send an interface request to the interface to be tested by calling the put method, wherein the interface request is used for the interface to be tested to update resources from the server; An interface request is sent to the interface to be tested by calling the delete method, wherein the interface request is used for the interface to be tested to request the server to delete resources.

4. The method according to claim 1, characterized in that The response message includes: HTTP status code and interface response code, and the response message after format conversion is verified to obtain a verification result, including: Determine whether the interface request connection is successful according to the HTTP status code and the response code of the interface; If yes, then the response message after format conversion is verified by using the stored expected value using the object value method to obtain the verification result; If not, it is determined that the verification result is that the interface request connection failed.

5. The method according to claim 4, characterized in that The expected value includes the converted request header and request information. The response message after format conversion is verified by using the stored expected value using the object value method to obtain the verification result, including: Extract the keywords in the converted request header and request information, use the object value method to query whether the keywords exist in the response message after format conversion, and obtain the verification result.

6. An interface testing device, characterized in that: include: An acquisition module, used to acquire a request header and request information input by a user, wherein the request information includes request parameters and / or a request body; A processing module, converting the request header and request information into a dictionary format, encapsulating the private attributes of the class with the methods of the class, and assigning the data obtained after the format conversion to the private attributes of the class; The sending module sends an interface request to the interface to be tested by calling the method encapsulated with the private attribute of the class and carrying the converted request header and request information, so that the interface to be tested processes the interface request and returns a response message; The verification module obtains the number of fields in the response message through a preset keyword; obtains the length of each field in the response message; converts the response message into an object format according to the number of fields and the length of each field, and verifies the response message after the format conversion to obtain a verification result.

7. An electronic device, characterized in that: include: A processor, a memory and a computer program; wherein the computer program is stored in the memory and is configured to be executed by the processor, and the computer program includes instructions for executing the interface testing method as described in any one of claims 1-5.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-executable instructions, and when the computer-executable instructions are executed by a processor, they are used to implement the interface testing method according to any one of claims 1 to 5.

Citation Information

Patent Citations

  • Python-based interface automation test method

    CN109189684A