Test methods and apparatus
Patent Information
- Application Number
- CN202110616349.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-06-02
- Publication Date
- 2026-08-28
- Estimated Expiration
- 2041-06-02
AI Technical Summary
[0003]在现有技术中,通常采用对接口的请求流量进行录制与重放的方法测试接口,但是录制的请求流量中的内容随着时间变化可能会失效,如时间戳等,从而可能导致测试失败
[0019]In some embodiments of this application, the technical solutions provided involve obtaining and parsing a contract file to determine the dynamic calculation expressions contained in the field values representing the test interface and the field values representing the test parameters. Executing the dynamic calculation expressions yields the results of the test parameters and the dynamic expressions in the test interface corresponding to the execution time point, thereby determining the test parameters and test interface corresponding to the execution time point. Based on the test parameters, the test interface is tested to obtain the test results. This dynamic testing of the test interface avoids test failures caused by the invalidation of request traffic content over time during request traffic recording and replay of the test interface, thus increasing the likelihood of successful testing of the test interface to a certain extent.
Smart Images

Figure CN115437906B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of computer and communication technology, and in particular to a testing method and apparatus. Background Technology
[0002] With the rapid advancement of internet technology, people have increasingly higher demands for applications. Before an application goes live, it is usually necessary to test the interfaces within the application to improve its smoothness and security during operation.
[0003] In existing technologies, the common method for testing interfaces is to record and replay the request traffic of the interface. However, the content in the recorded request traffic may become invalid over time, such as timestamps, which may lead to test failure. Summary of the Invention
[0004] This application aims to provide a testing method and apparatus that can dynamically test a test interface, thereby increasing the likelihood of successful testing of the test interface to a certain extent.
[0005] According to one aspect of the embodiments of this application, a testing method is provided, comprising: obtaining a contract file; parsing the contract file to determine a dynamic calculation expression contained in field values representing a test interface and field values representing test parameters in the contract file; executing the dynamic calculation expression to determine the test parameters and the test interface; and testing the test interface based on the test parameters to obtain a test result.
[0006] According to one aspect of the embodiments of this application, a testing apparatus is provided, comprising: an acquisition module configured to acquire a contract file; a parsing module configured to parse the contract file to determine a dynamic calculation expression contained in field values representing a test interface and field values representing test parameters in the contract file; an execution module configured to execute the dynamic calculation expression to determine the test parameters and the test interface; and a testing module configured to test the test interface based on the test parameters to obtain test results.
[0007] In one embodiment of this application, based on the foregoing scheme, the dynamic calculation expression includes a calculation function and an expression statement. The execution module is configured to: search for a dynamic calculation type that matches the calculation function in the type function library of the dynamic calculation type corresponding to the known type function; call the calculation function to perform the calculation corresponding to the dynamic calculation type on the expression statement to determine the test parameters and the test interface.
[0008] In one embodiment of this application, based on the foregoing scheme, if the dynamic calculation type matching the calculation function is to calculate the result value of a single expression, the execution module is configured to: obtain the calculation function and obtain the single expression statement adjacent to the calculation function in the dynamic calculation expression; call the calculation function to calculate the result value of the single expression statement to obtain a single result value; take the calculation function and the single expression statement adjacent to the calculation function as a single replacement combination, and replace the single replacement combination with the single result value to determine the test parameters and the test interface.
[0009] In one embodiment of this application, based on the foregoing scheme, if the dynamic calculation type matching the calculation function is to calculate the result value of multiple expressions, the execution module is configured to: obtain the calculation function, and obtain multiple expression statements appearing after the calculation function in the dynamic calculation expression; call the calculation function, calculate the result value of the multiple expression statements according to the order in which the multiple expression statements appear, and obtain multiple result values; use the calculation function and the multiple expression statements appearing after the calculation function as multiple combinations to be replaced, and replace the multiple combinations to be replaced with the multiple result values to determine the test parameters and the test interface.
[0010] In one embodiment of this application, based on the foregoing scheme, the execution module is configured to: obtain global variables in the dynamic calculation expression and obtain a dictionary object representing the value of the global variables; locate the key-value pair containing the dictionary object representing the value of the global variables to determine the value of the global variables; replace the global variables and the dictionary object representing the value of the global variables with the value of the global variables to obtain a replaced dynamic calculation expression; and execute the replaced dynamic calculation expression to determine the test parameters and the test interface.
[0011] In one embodiment of this application, based on the foregoing scheme, the execution module is configured to: obtain the address of an external object (other than the test interface) referenced in the dynamic calculation expression; find the value in the external object address as an external value; replace the external object address with the external value to obtain a dynamic calculation expression containing the external value; and determine the test parameters and the test interface based on the dynamic calculation expression containing the external value.
[0012] In one embodiment of this application, based on the foregoing scheme, after replacing the external object address with the external value, the execution module is further configured to: establish a global variable in the contract file as a storage global variable; and store the external value and the external object address in a key-value pair in the dictionary object used to represent the value of the storage global variable.
[0013] In one embodiment of this application, the test parameters include input parameters and expected output. Based on the aforementioned scheme, the test module is configured to: input the input parameters into the test interface to obtain the actual output returned by the test interface; compare the actual output with the expected output to obtain a comparison result; and determine the test result based on the comparison result.
[0014] In one embodiment of this application, the acquisition module is configured to: acquire test data; determine, based on the test data, the field values for representing the test interface and the field values for representing the test parameters; acquire a contract template, the contract template including a test interface area and a test parameter area; fill the field values for representing the test interface into the test interface area, and fill the field values for representing the test parameters into the test parameter area, to form the contract file.
[0015] According to one aspect of the embodiments of this application, a computer-readable program medium is provided, which stores computer program instructions that, when executed by a computer, cause the computer to perform any of the methods described above.
[0016] According to one aspect of the embodiments of this application, an electronic device is provided, including: a processor; and a memory storing computer-readable instructions, which, when executed by the processor, implement the method as described in any of the preceding claims.
[0017] According to one aspect of the embodiments of this application, a computer program product or computer program is provided, which includes computer instructions stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the computer device to perform the methods provided in the various alternative embodiments described above.
[0018] The technical solutions provided by the embodiments of this application may include the following beneficial effects:
[0019] In some embodiments of this application, the technical solutions provided involve obtaining and parsing a contract file to determine the dynamic calculation expressions contained in the field values representing the test interface and the field values representing the test parameters. Executing the dynamic calculation expressions yields the results of the test parameters and the dynamic expressions in the test interface corresponding to the execution time point, thereby determining the test parameters and test interface corresponding to the execution time point. Based on the test parameters, the test interface is tested to obtain the test results. This dynamic testing of the test interface avoids test failures caused by the invalidation of request traffic content over time during request traffic recording and replay of the test interface, thus increasing the likelihood of successful testing of the test interface to a certain extent.
[0020] It should be understood that the above general description and the following detailed description are merely exemplary and do not limit this application. Attached Figure Description
[0021] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the specification, serve to explain the principles of this application.
[0022] Figure 1 A schematic diagram of an exemplary system architecture to which the technical solutions of the embodiments of this application can be applied is shown;
[0023] Figure 2A A schematic diagram of a data sharing system to which an embodiment of the present application can be applied is shown;
[0024] Figure 2B A schematic diagram of a blockchain to which one embodiment of this application may be applied is shown;
[0025] Figure 2C This diagram illustrates the generation of a new block in a blockchain to which one embodiment of this application can be applied;
[0026] Figure 3 A flowchart illustrating a test method according to an embodiment of this application is shown schematically;
[0027] Figure 4 A flowchart illustrating a test method according to an embodiment of this application is shown schematically;
[0028] Figure 5 A block diagram of a test apparatus according to one embodiment of this application is shown schematically;
[0029] Figure 6 This is a hardware diagram of an electronic device according to an exemplary embodiment. Detailed Implementation
[0030] Exemplary embodiments will now be described more fully with reference to the accompanying drawings. However, these exemplary embodiments can be implemented in many forms and should not be construed as limited to the examples set forth herein; rather, these embodiments are provided to make this application more comprehensive and complete, and to fully convey the concept of the exemplary embodiments to those skilled in the art.
[0031] Furthermore, the described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a thorough understanding of embodiments of this application. However, those skilled in the art will recognize that the technical solutions of this application can be practiced without one or more of the specific details, or other methods, components, apparatuses, steps, etc., can be employed. In other instances, well-known methods, apparatuses, implementations, or operations are not shown or described in detail to avoid obscuring various aspects of this application.
[0032] The block diagrams shown in the accompanying drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.
[0033] The flowcharts shown in the accompanying drawings are merely illustrative and do not necessarily include all content and operations / steps, nor do they necessarily have to be performed in the described order. For example, some operations / steps can be broken down, while others can be combined or partially combined; therefore, the actual execution order may change depending on the specific circumstances.
[0034] Figure 1 A schematic diagram of an exemplary system architecture 100 to which the technical solutions of the embodiments of this application can be applied is shown.
[0035] like Figure 1 As shown, system architecture 100 may include client 101, network 102, and server 103. Network 102 is used as a medium to provide a communication link between client 101 and server 103. Network 102 may include various connection types, such as wired communication links, wireless communication links, etc., which are not limited herein.
[0036] It should be understood that Figure 1The number of clients 101, networks 102, and servers 103 shown is merely illustrative. Depending on implementation needs, there can be any number of clients 101, networks 102, and servers 103. For example, server 103 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms. Client 101 can be a smartphone, tablet, laptop, desktop computer, smart speaker, smartwatch, etc., but is not limited to these.
[0037] In one embodiment of this application, server 103 obtains and parses a contract file to determine the dynamic calculation expressions contained in the field values representing the test interface and the field values representing the test parameters in the contract file; by executing the dynamic calculation expressions, the server obtains the results of the test parameters and the dynamic expressions in the test interface corresponding to the execution time point, thereby determining the test parameters and test interface corresponding to the execution time point; and by testing the test interface based on the test parameters, the server obtains the test results to perform dynamic testing on the test interface. This avoids test failures caused by the invalidation of request traffic content over time when recording and replaying request traffic of the test interface, thereby increasing the probability of successful testing of the test interface to a certain extent.
[0038] It should be noted that the testing method provided in this application embodiment is generally executed by server 103, and correspondingly, the testing device is generally set in server 103. However, in other embodiments of this application, client 101 may also have similar functions to server 103, thereby executing the testing method provided in this application embodiment.
[0039] Figure 2A A schematic diagram of an exemplary data sharing system 200 to which the technical solutions of embodiments of the present invention can be applied is shown.
[0040] See Figure 2AThe data sharing system 200 shown refers to a system for data sharing between nodes. Each node 201, during normal operation, can receive input information and maintain shared data within the data sharing system 200 based on the received input information. To ensure information interoperability within the data sharing system 200, information connections can exist between each node 201, allowing information transmission between nodes. For example, when any node 201 in the data sharing system 200 receives input information, other nodes 201 in the data sharing system 200 obtain the input information according to a consensus algorithm and store it as data in the shared data, ensuring that the data stored on all nodes 201 in the data sharing system 200 is consistent.
[0041] Each node 201 in the data sharing system 200 has a corresponding node identifier. Each node 201 can also store the node identifiers of other nodes 201 in the data sharing system 200, so that the generated block can be broadcast to other nodes 201 in the data sharing system 200 based on their node identifiers. Each node 201 can maintain a node identifier list as shown in the table below, storing the node name and node identifier in this list. The node identifier can be an IP (Internet Protocol) address or any other information that can be used to identify the node; Table 1 only uses IP addresses as an example.
[0042] Node 1 117.114.151.174 Node 2 117.116.189.145 … … Node N 119.123.789.258
[0043] Table 1
[0044] Figure 2B A schematic diagram of a blockchain that can be applied to one embodiment of this application is shown.
[0045] Each node in the data-sharing system 200 stores the same blockchain. A blockchain consists of multiple blocks; see [link to relevant documentation]. Figure 2B A blockchain consists of multiple blocks. The genesis block includes a block header and a block body. The block header stores input information feature values, version number, timestamp, and difficulty value, while the block body stores the input information. The next block after the genesis block takes the genesis block as its parent block. The next block also includes a block header and a block body. The block header stores the input information feature values of the current block, the block header feature values of the parent block, version number, timestamp, and difficulty value, and so on. This ensures that the block data stored in each block is related to the block data stored in the parent block, guaranteeing the security of the input information in the blocks.
[0046] Figure 2C A schematic diagram of new block generation in a blockchain that can be applied to one embodiment of this application is shown.
[0047] When generating the individual blocks in the blockchain, see Figure 2C When a node in the blockchain receives input information, it verifies the input information. After verification, it stores the input information in a memory pool and updates its hash tree used to record the input information. Then, it updates the timestamp to the time the input information was received and tries different random numbers multiple times to calculate the feature value, ensuring that the calculated feature value satisfies the following formula:
[0048] SHA256(SHA256(version+prev_hash+merkle_root+ntime+nbits+x))<TARGET
[0049] Wherein, SHA256 is the feature value algorithm used to calculate the feature value; version (version number) is the version information of the relevant block protocol in the blockchain; prev_hash is the block header feature value of the parent block of the current block; merkle_root is the feature value of the input information; ntime is the update time of the update timestamp; nbits is the current difficulty, which is a fixed value for a period of time and is determined again after exceeding the fixed time period; x is a random number; TARGET is the feature value threshold, which can be determined based on nbits.
[0050] Thus, when a random number satisfying the above formula is calculated, the information can be stored accordingly, generating a block header and a block body to obtain the current block. Subsequently, the node where the blockchain resides sends the newly generated block to the other nodes 201 in the data sharing system 200 according to the node identifiers of the other nodes 201 in the data sharing system 200. The other nodes 201 verify the newly generated block and add it to their stored blockchain after verification.
[0051] In one embodiment of this application, the input information stored in the data sharing system 200 may be contract documents, test data, test parameters, or external objects other than the test interface. Using blockchain technology to process the testing method of this application can improve the efficiency and accuracy of testing the test interface.
[0052] The implementation details of the technical solutions in the embodiments of this application are described in detail below:
[0053] Figure 3The flowchart illustrating a test method according to an embodiment of this application is shown in the schematic diagram. The execution entity of the test method may be a server, for example, a... Figure 1 Server 103 is shown in the image.
[0054] Reference Figure 3 As shown, this test method includes at least steps S310 to S340, which are described in detail below:
[0055] In step S310, the contract document is obtained.
[0056] In one embodiment of this application, the contract file may be a file written in a domain-specific language (DSL). The contract file may record the test interface and the test parameters required to test the test interface. The test parameters may include input parameters and expected output parameters, etc., which are not limited here.
[0057] In one embodiment of this application, the contract file may be a file written using an interpreted language, such as Python or Shell. In other embodiments of this application, the contract file may be a file written using an interpreted language combined with a data-interchange language, such as JSON or XML.
[0058] In one embodiment of this application, test data can be obtained; based on the test data, field values for representing test interfaces and field values for representing test parameters can be determined; a contract template can be obtained, which includes a test interface area and a test parameter area; the field values for representing test interfaces are filled into the test interface area, and the field values for representing test parameters are filled into the test parameter area to form a contract file, thereby automatically forming a contract file. Compared with manually writing test code to test the test interface, this can avoid errors that occur during manual writing and improve the accuracy of test results.
[0059] In one embodiment of this application, semantic analysis can be used to determine the field values in the test data that represent the test interface and the field values that represent the test parameters. Semantic analysis can also be used to determine the location of the test interface area and the test parameter area in the contract template.
[0060] In one embodiment of this application, the content used to represent dynamic testing in the test data can be stored in the contract file in the form of dynamic expressions. For example, the expression statements used to represent dynamic testing in the test data can be identified, the calculation function corresponding to the dynamic test content can be selected, and the calculation function and the expression statements used to represent dynamic testing can be combined to form a dynamic calculation expression and stored in the contract file.
[0061] In step S320, the contract file is parsed to determine the dynamic calculation expressions contained in the field values used to represent the test interface and the field values used to represent the test parameters in the contract file.
[0062] In one embodiment of this application, the process of parsing the contract file may include obtaining field values in the contract file that represent test interfaces and field values in the contract file that represent test parameters; then, obtaining a dynamic calculation expression from the field values representing test interfaces and from the field values representing test parameters. The dynamic calculation expression may exist in both the field values representing test interfaces and the field values representing test parameters, or it may exist only in the field values representing test interfaces or only in the field values representing test parameters.
[0063] In one embodiment of this application, if the dynamically calculated expression also exists in other field values in the contract file used to represent other content, then when parsing the contract file, the dynamically calculated expression in the other field values is also obtained, so that the dynamically calculated expression can be executed subsequently to obtain other content recorded in the contract file, which can better test the test interface.
[0064] In one embodiment of this application, the dynamically calculated expression can represent a dynamic test that needs to return content. The returned content can change over time, so the execution result of the dynamically calculated expression can represent the returned content corresponding to the time the dynamically calculated expression was executed. For example, the dynamically calculated expression can be used to obtain the current timestamp; therefore, executing the dynamically calculated expression value can obtain the current timestamp. However, by recording and playing back request traffic, the timestamp recorded in the recorded request traffic is the timestamp at the time of recording, and it is not possible to obtain the current timestamp.
[0065] In one embodiment of this application, a contract parser can be used to parse the contract file.
[0066] In step S330, a dynamic calculation expression is performed to determine the test parameters and test interface.
[0067] In one embodiment of this application, the dynamic calculation expression may include a calculation function and an expression statement. The dynamic calculation type that matches the calculation function can be found in the type function library of the dynamic calculation type corresponding to the known type function. The calculation function is called to perform the calculation corresponding to the dynamic calculation type on the expression statement to determine the test parameters and test interface.
[0068] In one embodiment of this application, the type function and the computation function can be built-in functions in an interpreted language, such as the rich standard library functions built into Python, like the `eval` and `exec` functions, which greatly simplifies the writing of computation expressions. Type functions and the corresponding dynamic computation types they can execute can be stored to form a type function library. The dynamic computation types corresponding to the same type functions in the type function library can be used as the matching dynamic computation types for the computation functions.
[0069] In one embodiment of this application, the calculation result obtained by performing the calculation corresponding to the dynamic calculation type on the expression statement can replace the dynamic expression to obtain test parameters and test interface.
[0070] In one embodiment of this application, a contract resolver can be used to perform dynamic computation of expressions.
[0071] In one embodiment of this application, if the dynamic calculation type matching the calculation function is to calculate the result value of a single expression, then the calculation function can be obtained, and the single expression statement adjacent to the calculation function in the dynamic calculation expression can be obtained; the calculation function is called to calculate the result value of the single expression statement to obtain the single result value; the calculation function and the single expression statement adjacent to the calculation function are used as a single replacement combination, and the single result value is used to replace the single replacement combination to determine the test parameters and test interface.
[0072] In one embodiment of this application, a replacement result obtained by replacing a single combination of values to be replaced in a field value representing a test parameter can be used as a test parameter; a replacement result obtained by replacing a single combination of values to be replaced in a field value representing a test interface can be used as a test interface.
[0073] In one embodiment of this application, if the dynamic calculation type matching the calculation function is to calculate the result value of multiple expressions, then the calculation function can be obtained, and the multiple expression statements appearing after the calculation function in the dynamic calculation expression can be obtained; the calculation function is called, and the result values of the multiple expression statements are calculated according to the order in which they appear, to obtain multiple result values; the calculation function and the multiple expression statements appearing after the calculation function are used as multiple combinations to be replaced, and the multiple result values are used to replace the multiple combinations to be replaced, so as to determine the test parameters and test interface.
[0074] In this embodiment, multiple result values can be multiple values, each corresponding to a multiple expression statement. Multiple result values can replace multiple expression statements in the order of their corresponding multiple expression statements.
[0075] In one embodiment of this application, multiple result values can be used to replace the multiple combinations of values to be replaced in the test parameter field, and these replacement results can be used as test parameters; multiple result values can be used to replace the multiple combinations of values to be replaced in the test interface field, and these replacement results can be used as test interfaces.
[0076] In one embodiment of this application, a calculation process may consist of multiple expression statements. For example, the process of comparing the returned result with the expected result may be more complex, and finally a boolean value is obtained.
[0077] In one embodiment of this application, a contract document is provided, the content of which is as follows:
[0078]
[0079]
[0080] In this embodiment, when the contract parser executes the contract file, it can identify the prefixes of field values to identify the dynamic calculation type of the dynamically evaluated expression. For example, if the dynamic calculation type corresponding to the evaluation function `eval` is to evaluate the result of a single expression, then `#eval#int(datetime.datetime.now(),timestamp())` in the aforementioned contract file indicates that the `eval` function is called to execute `int(datetime.datetime.now(),timestamp())`.
[0081] In this embodiment, if the dynamic calculation type corresponding to the calculation function exec is to calculate the result value of multiple expressions, then the "#exec#import string; all_chars = string.ascii_uppercase + string.digits; exec_result = ''" in the above contract file means that the exec function is called to execute import string; all_chars = string.ascii_uppercase + string.digits. The multiple result values obtained are uniformly assigned to a specific environment variable, namely the exec_result variable mentioned above. After the contract parser executes the exec call, it can read the environment variable to obtain the calculation result.
[0082] In this embodiment, if the field values used to represent the test interface and the field values used to represent the test parameters in the contract file include non-string types or calculation functions that do not match the type function library, then they are ordinary static field values and do not require dynamic calculation.
[0083] In this embodiment, after the above-described analytical calculations, a dynamically calculated contract file can be obtained, the content of which can be as follows:
[0084]
[0085]
[0086] In one embodiment of this application, global variables in a dynamically evaluated expression can be obtained, along with a dictionary object representing the value of the global variables. The key-value pair containing the dictionary object representing the value of the global variables is located to determine the value of the global variables. The global variables and the dictionary object (dict object) representing the value of the global variables are replaced with the values of the global variables to obtain a replaced dynamically evaluated expression. The replaced dynamically evaluated expression is executed to determine test parameters and test interfaces, thereby referencing the values stored in the key-value pairs. The values stored in the key-value pairs can be calculated before the dynamic evaluation expression is executed.
[0087] In this embodiment, the execution results of other expressions obtained before the dynamic evaluation expression is executed can be stored in key-value pairs to reference already evaluated values.
[0088] In one embodiment of this application, if the dynamic calculation expression contains global variables, it can be determined that the dynamic calculation type corresponding to the dynamic calculation expression is a reference to a previously calculated value. For this type, the global variables in the dynamic calculation expression can be obtained, the values of the global variables can be found, and the global variables and the dictionary object used to represent the values of the global variables can be replaced with the values of the global variables to obtain the replaced dynamic calculation expression. The replaced dynamic calculation expression can be executed to determine the test parameters and test interface.
[0089] In one embodiment of this application, executing the replaced dynamic calculation expression may involve obtaining the calculation function and expression statement in the replaced dynamic calculation expression, determining the dynamic calculation type corresponding to the replaced dynamic calculation expression based on the calculation function, and calling the calculation function to perform the calculation of the dynamic calculation type corresponding to the replaced dynamic calculation expression on the expression statement in order to determine the test parameters and test interface. The specific execution steps can be referred to the above embodiment.
[0090] In one embodiment of this application, a parsing context `parsed_context` can be simulated within the contract parser. The contract file, which underwent dynamic computation as described in the previous embodiment, can be stored in this context as a key-value pair for easy reference. For example, a dictionary type (dict) from Python can be used to represent this context. `parsed_context` is initialized as a global variable with an empty dict object as its value. Continuing with the previous example, after parsing and computation, the content of `parsed_context` will become:
[0091]
[0092]
[0093] In one embodiment of this application, the addresses of external objects (excluding the test interface) referenced in the dynamic calculation expression can be obtained; the value in the external object address can be found as the external value; the external object address can be replaced with the external value to obtain a dynamic calculation expression containing the external value; and the test parameters and test interface can be determined based on the dynamic calculation expression containing the external value.
[0094] In one embodiment of this application, the calculation function and expression statement in the dynamic calculation expression containing external values can be obtained. Based on the calculation function, the dynamic calculation type corresponding to the dynamic calculation expression containing external values can be determined. The calculation function is called to execute the dynamic calculation type corresponding to the dynamic calculation expression containing external values on the expression statement, so as to determine the test parameters and test interface. The specific execution steps can be referred to the above embodiment.
[0095] In one embodiment of this application, continuing with the above example, it is assumed that the expected output requires a field value that matches the requested data, which can be directly represented as follows:
[0096]
[0097] This reference operation is actually just a value expression, which can be evaluated using the eval function mentioned above for evaluating single expressions.
[0098] In one embodiment of this application, if the dynamic calculation expression contains an external object address, the dynamic calculation type corresponding to the expression can be determined to be a result referencing an external service. For dynamic calculation types referencing external services, the external object address in the expression can be obtained, and its value can be retrieved as the external value. The external object address can be replaced with the external value to obtain a dynamic calculation expression containing the external value. Based on the dynamic calculation expression containing the external value, test parameters and test interfaces can be determined. For example, if the interface under test is expected to produce a side effect, and database queries are needed to verify whether it meets expectations, the database address can be used as the external object address, and the database can be queried using the dynamic calculation expression. The database can define its import method using a data exchange language, and the absolute path of the database code can be added to the dynamic calculation expression to query the database.
[0099] In one embodiment of this application, the external object can be an external interface, which can be represented by a data exchange language, such as an object type (Object type) in JSON. This object can contain the request address and request parameters for calling the external interface. The contract parser can parse and execute such objects to obtain the call result of the external interface. The content of the contract file can be as follows:
[0100]
[0101] In this embodiment, the approach can be adapted based on the specific method of calling the external object. For example, in a remote call scenario, the external target service and interface ID are required, while in an HTTP API call scenario, the complete URL is needed. After the parser initiates the call accordingly, it saves the response result in key-value pairs into the aforementioned `parsed_context` for use in other logical calculations.
[0102] In one embodiment of this application, after replacing the external object address with an external value, a global variable can be created in the contract file as a storage global variable. During initialization, the dictionary object used to represent the value of the stored global variable can be set to empty. The external value and the external object address are stored as key-value pairs in the dictionary object used to represent the value of the stored global variable, so that other data in the contract file can look up the key-value pair to reference the external value based on the external object address.
[0103] Continue to refer to Figure 3 In step S340, the test interface is tested based on the test parameters to obtain the test results.
[0104] In one embodiment of this application, the test parameters include input parameters and expected output. Input parameters can be input into the test interface to obtain the actual output returned by the test interface; the actual output is compared with the expected output to obtain a comparison result; and the test result is determined based on the comparison result.
[0105] In one embodiment of this application, the test interface is determined to have passed the test if the actual output is the same as the expected output or the error does not exceed a set error value; otherwise, the test interface is determined to have failed the test.
[0106] exist Figure 3 In this embodiment, by obtaining and parsing the contract file, the dynamic calculation expressions contained in the field values representing the test interface and the field values representing the test parameters in the contract file are determined. By executing the dynamic calculation expressions, the results of the test parameters and the dynamic expressions in the test interface corresponding to the execution time point can be obtained, thereby determining the test parameters and test interface corresponding to the execution time point. The test interface is tested based on the test parameters to obtain the test results. This allows for dynamic testing of the test interface, avoiding test failures caused by the invalidation of request traffic content over time when recording and replaying request traffic of the test interface. This, to a certain extent, increases the probability of successfully testing the test interface.
[0107] In this embodiment, contract testing is performed on the test interface using a contract file. This involves testing the interface based on its predefined call constraints and expected behavior definitions, and performing dynamic testing by executing dynamic expressions. This enables the contract test to handle dynamic requests. The testing method described in this application can be applied to general contract testing scenarios for network service interfaces, such as contract testing for remote procedure calls and HTTP APIs. This embodiment can adapt to scenarios where request content changes dynamically, avoiding unnecessary and repeated maintenance of static contract content. Furthermore, by using a unified contract parser instead of manually writing test code, it allows users to focus solely on describing the contract protocol, significantly reducing the cost of implementing contract testing.
[0108] In one embodiment of this application, a management page can be added on top of this technology, so that the operator does not need to directly edit the contract description, but can perform visual operations on the management end, and use database storage to replace the contract file.
[0109] Figure 4 The flowchart illustrating a test method according to an embodiment of this application is shown in the schematic diagram. The execution entity of the test method may be a server, for example, a... Figure 1 Server 103 is shown in the image.
[0110] Reference Figure 4As shown, this test method includes at least steps S410 to S480, which are described in detail below:
[0111] In step S410, the tester writes the contract document;
[0112] In step S420, the tester initiates a test with the contract resolver;
[0113] In step S430, the contract parser reads the contract file;
[0114] In step S440, the contract parser parses the contract file;
[0115] In step S450, the contract parser generates request parameters according to the contract file;
[0116] In step S460, the contract resolver initiates a call to the interface under test;
[0117] In step S470, the interface under test returns the result;
[0118] In step S480, the contract parser compares the expected results and outputs the test conclusion.
[0119] exist Figure 4 In the embodiment, the interface under test is the test interface mentioned above. By using a contract parser to automatically parse the contract file, the interface under test can be automatically tested, and the test conclusions can be obtained accurately and conveniently.
[0120] The following describes an embodiment of the apparatus described in this application, which can be used to perform the test methods described in the above embodiments of this application. For details not disclosed in the apparatus embodiments of this application, please refer to the embodiments of the test methods described in this application.
[0121] Figure 5 A block diagram of a test apparatus according to one embodiment of this application is shown schematically.
[0122] Reference Figure 5 As shown, a test apparatus 500 according to an embodiment of this application includes an acquisition module 501, a parsing module 502, an execution module 503, and a test module 504.
[0123] According to one aspect of the embodiments of this application, based on the foregoing scheme, the acquisition module 501 is configured to acquire a contract file; the parsing module 502 is configured to parse the contract file to determine the dynamic calculation expression contained in the field values used to represent the test interface and the field values used to represent the test parameters in the contract file; the execution module 503 is configured to execute the dynamic calculation expression to determine the test parameters and the test interface; and the testing module 504 is configured to test the test interface based on the test parameters to obtain the test result.
[0124] In one embodiment of this application, based on the aforementioned scheme, the dynamic calculation expression includes a calculation function and an expression statement. The execution module 503 is configured to: search for a dynamic calculation type that matches the calculation function in the type function library of the dynamic calculation type corresponding to the known type function; call the calculation function to perform the calculation corresponding to the dynamic calculation type on the expression statement to determine the test parameters and test interface.
[0125] In one embodiment of this application, based on the aforementioned scheme, if the dynamic calculation type matching the calculation function is to calculate the result value of a single expression, the execution module 503 is configured to: obtain the calculation function and obtain the single expression statement adjacent to the calculation function in the dynamic calculation expression; call the calculation function to calculate the result value of the single expression statement to obtain the single result value; take the calculation function and the single expression statement adjacent to the calculation function as a single replacement combination, and replace the single replacement combination with the single result value to determine the test parameters and test interface.
[0126] In one embodiment of this application, based on the aforementioned scheme, if the dynamic calculation type matching the calculation function is to calculate the result value of multiple expressions, the execution module 503 is configured to: obtain the calculation function and obtain multiple expression statements that appear after the calculation function in the dynamic calculation expression; call the calculation function, calculate the result value of the multiple expression statements according to the order in which the multiple expression statements appear, and obtain multiple result values; take the calculation function and the multiple expression statements that appear after the calculation function as multiple combinations to be replaced, and replace the multiple combinations to be replaced with the multiple result values to determine the test parameters and test interface.
[0127] In one embodiment of this application, based on the aforementioned scheme, the execution module 503 is configured to: obtain global variables in the dynamic calculation expression and obtain a dictionary object used to represent the value of the global variables; find the key-value pair where the dictionary object used to represent the value of the global variables is located to determine the value of the global variables; replace the global variables and the dictionary object used to represent the value of the global variables with the value of the global variables to obtain the replaced dynamic calculation expression; and execute the replaced dynamic calculation expression to determine the test parameters and test interface.
[0128] In one embodiment of this application, based on the aforementioned scheme, the execution module 503 is configured to: obtain the address of an external object (excluding the test interface) referenced in the dynamic calculation expression; find the value in the external object address as the external value; replace the external object address with the external value to obtain a dynamic calculation expression containing the external value; and determine the test parameters and the test interface based on the dynamic calculation expression containing the external value.
[0129] In one embodiment of this application, based on the foregoing scheme, after replacing the external object address with the external value, the execution module 503 is further configured to: establish a global variable in the contract file as a storage global variable; and store the external value and the external object address in a key-value pair into a dictionary object used to represent the value of the stored global variable.
[0130] In one embodiment of this application, the test parameters include input parameters and expected output. Based on the aforementioned scheme, the test module 504 is configured to: input the input parameters into the test interface to obtain the actual output returned by the test interface; compare the actual output with the expected output to obtain a comparison result; and determine the test result based on the comparison result.
[0131] In one embodiment of this application, the acquisition module 501 is configured to: acquire test data; determine, based on the test data, field values for representing test interfaces and field values for representing test parameters; acquire a contract template, the contract template including a test interface area and a test parameter area; fill the field values for representing test interfaces into the test interface area and fill the field values for representing test parameters into the test parameter area to form a contract file.
[0132] Those skilled in the art will understand that various aspects of this application can be implemented as a system, method, or program product. Therefore, various aspects of this application can be specifically implemented in the following forms: a completely hardware implementation, a completely software implementation (including firmware, microcode, etc.), or a combination of hardware and software implementations, collectively referred to herein as a "circuit," "module," or "system."
[0133] The following reference Figure 6 To describe an electronic device 60 according to this embodiment of the present application. Figure 6 The electronic device 60 shown is merely an example and should not impose any limitations on the functionality and scope of use of the embodiments of this application.
[0134] like Figure 6 As shown, the electronic device 60 is presented in the form of a general-purpose computing device. The components of the electronic device 60 may include, but are not limited to: at least one processing unit 61, at least one storage unit 62, a bus 63 connecting different system components (including storage unit 62 and processing unit 61), and a display unit 64.
[0135] The storage unit stores program code that can be executed by the processing unit 61, causing the processing unit 61 to perform the steps described in the "Embodiment Methods" section above according to various exemplary embodiments of this application.
[0136] Storage unit 62 may include a readable medium in the form of a volatile storage unit, such as a random access memory unit (RAM) 621 and / or a cache memory unit 622, and may further include a read-only memory unit (ROM) 623.
[0137] Storage unit 62 may also include a program / utility 624 having a set (at least one) of program modules 625, including but not limited to: an operating system, one or more application programs, other program modules, and program data, each or some combination of these examples may include an implementation of a network environment.
[0138] Bus 63 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local bus using any of the various bus structures.
[0139] Electronic device 60 can also communicate with one or more external devices (e.g., keyboard, pointing device, Bluetooth device, etc.), one or more devices that enable a user to interact with electronic device 60, and / or any device that enables electronic device 60 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 65. Furthermore, electronic device 60 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 66. As shown, network adapter 66 communicates with other modules of electronic device 60 via bus 63. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 60, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0140] Through the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, terminal device, or network device, etc.) to execute the method according to the embodiments of this application.
[0141] According to one embodiment of this application, a computer-readable storage medium is also provided, on which a program product capable of implementing the methods described above in this specification is stored. In some possible implementations, various aspects of this application may also be implemented as a program product comprising program code that, when the program product is run on a terminal device, causes the terminal device to perform the steps of the various exemplary embodiments of this application described in the "Exemplary Methods" section of this specification.
[0142] According to one embodiment of this application, the program product for implementing the above method can be a portable compact disc read-only memory (CD-ROM) and include program code, and can run on a terminal device, such as a personal computer. However, the program product of this application is not limited thereto. In this document, the readable storage medium can be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system, apparatus, or device.
[0143] The program product may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection having one or more wires, a portable disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0144] Computer-readable signal media may include data signals propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable signal medium may also be any readable medium other than a readable storage medium, capable of sending, propagating, or transmitting programs for use by or in conjunction with an instruction execution system, apparatus, or device.
[0145] The program code contained on the readable medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.
[0146] Program code for performing the operations of this application can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java and C++, and conventional procedural programming languages such as C or similar languages. The program code can execute entirely on the user's computing device, partially on the user's device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0147] Furthermore, the above figures are merely illustrative of the processes included in the method according to exemplary embodiments of this application, and are not intended to be limiting. It is readily understood that the processes shown in the above figures do not indicate or limit the temporal order of these processes. Additionally, it is readily understood that these processes may be executed synchronously or asynchronously, for example, in multiple modules.
[0148] It should be understood that this application is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this application is limited only by the appended claims.
Claims
1. A testing method, characterized in that, include: Obtain the contract file, which contains the test interface and the test parameters for testing the test interface; The contract file is parsed to determine the dynamic calculation expressions contained in the field values representing the test interface and the dynamic calculation expressions contained in the field values representing the test parameters. The dynamic calculation expressions represent dynamic tests that need to return content, and the returned content changes over time. Execute the dynamic calculation expression to obtain the return content corresponding to the execution time of the dynamic calculation expression, and replace the dynamic calculation expression with the return content corresponding to the execution time of the dynamic calculation expression to update the test parameters and the test interface in the contract file; Test the updated test interface based on the updated test parameters and obtain the test results; Before obtaining the contract file, the method further includes: identifying the expression statements in the test data used to represent the dynamic test, selecting the calculation function corresponding to the content of the dynamic test, combining the calculation function with the expression statements used to represent the dynamic test to form the dynamic calculation expression, and storing the dynamic calculation expression in the contract file.
2. The test method according to claim 1, characterized in that, The dynamic calculation expression includes a calculation function and expression statements. Executing the dynamic calculation expression yields a return value corresponding to the execution time of the dynamic calculation expression. The dynamic calculation expression is then replaced with the return value corresponding to the execution time of the dynamic calculation expression to update the test parameters and the test interface in the contract file, including: In the type function library of the known type function corresponding to the dynamic calculation type, search for the dynamic calculation type that matches the calculation function; The calculation function is invoked to perform the calculation corresponding to the dynamic calculation type on the expression statement, so as to determine the test parameters and the test interface.
3. The test method according to claim 2, characterized in that, If the dynamic calculation type matching the calculation function is to calculate the result value of a single expression, then calling the calculation function to perform the calculation corresponding to the type of the dynamic calculation expression on the expression statement to determine the test parameters and the test interface includes: Obtain the calculation function, and obtain the single expression statement adjacent to the calculation function in the dynamic calculation expression; The calculation function is invoked to calculate the result value of the single expression statement, and a single result value is obtained; The calculation function and the single expression statement adjacent to the calculation function are taken as a single replacement combination, and the single result value is used to replace the single replacement combination to determine the test parameters and the test interface.
4. The test method according to claim 2, characterized in that, If the dynamic calculation type matching the calculation function is to calculate the result value of multiple expressions, the step of calling the calculation function to perform the calculation corresponding to the type of the dynamic calculation expression on the expression statement to determine the test parameters and the test interface includes: Obtain the calculation function, and obtain multiple expression statements that appear after the calculation function in the dynamic calculation expression; The calculation function is called to calculate the result values of the multiple expression statements in the order in which they appear, resulting in multiple result values. The calculation function and the multiple expression statements that appear after the calculation function are treated as multiple combinations to be replaced. The multiple result values are used to replace the multiple combinations to be replaced in order to determine the test parameters and the test interface.
5. The test method according to claim 1, characterized in that, The process of executing the dynamic calculation expression, obtaining the return content corresponding to the execution time of the dynamic calculation expression, and replacing the dynamic calculation expression with the return content corresponding to the execution time of the dynamic calculation expression to update the test parameters and the test interface in the contract file includes: Obtain the global variables in the dynamically calculated expression, and obtain a dictionary object to represent the values of the global variables; Locate the key-value pair containing the dictionary object that represents the value of the global variable to determine the value of the global variable; The global variable and the dictionary object used to represent the value of the global variable are replaced with the value of the global variable to obtain the replaced dynamic calculation expression; The replaced dynamic calculation expression is executed to determine the test parameters and the test interface.
6. The test method according to claim 1, characterized in that, The process of executing the dynamic calculation expression, obtaining the return content corresponding to the execution time of the dynamic calculation expression, and replacing the dynamic calculation expression with the return content corresponding to the execution time of the dynamic calculation expression to update the test parameters and the test interface in the contract file includes: Obtain the address of any external object referenced in the dynamic calculation expression, other than the test interface; Find the value in the address of the external object and use it as the external value; Replace the external object address with the external value to obtain a dynamic calculation expression containing the external value; The test parameters and the test interface are determined based on the dynamic calculation expression that includes external values.
7. The test method according to claim 6, characterized in that, After replacing the external object address with the external value, the method further includes: Establish global variables in the contract file to store global variables; The external value and the address of the external object are stored as key-value pairs in the dictionary object used to represent the value of the stored global variable.
8. The test method according to claim 1, characterized in that, The test parameters include input parameters and expected outputs. The test results obtained by testing the updated test interface based on the updated test parameters include: Input the input parameters into the updated test interface to obtain the actual output returned by the updated test interface; The actual output is compared with the expected output to obtain the comparison result; Based on the comparison results, the test results are determined.
9. The test method according to claim 1, characterized in that, The acquisition of the contract file includes: Obtain test data; Based on the test data, determine the field values used to represent the test interface and the field values used to represent the test parameters; Obtain the contract template, which includes a test interface area and a test parameter area; The field values representing the test interface are filled into the test interface area, and the field values representing the test parameters are filled into the test parameter area to form the contract file.
10. A testing apparatus, characterized in that, include: The acquisition module is configured to acquire a contract file, which contains a test interface and test parameters for testing the test interface. The parsing module is configured to parse the contract file to determine the dynamic calculation expressions contained in the field values representing the test interface and the dynamic calculation expressions contained in the field values representing the test parameters in the contract file. The dynamic calculation expressions represent dynamic tests that need to return content, and the returned content changes over time. The execution module is configured to execute the dynamic calculation expression, obtain the return content corresponding to the execution time of the dynamic calculation expression, and replace the dynamic calculation expression with the return content corresponding to the execution time of the dynamic calculation expression to update the test parameters and the test interface in the contract file. The test module is configured to test the updated test interface based on the updated test parameters and obtain the test results. The acquisition module is further configured to, before acquiring the contract file, identify the expression statements in the test data used to represent the dynamic test, select the calculation function corresponding to the content of the dynamic test, combine the calculation function with the expression statements used to represent the dynamic test to form the dynamic calculation expression, and store the dynamic calculation expression in the contract file.
11. An electronic device, characterized in that, include: Memory, which stores computer-readable instructions; The processor reads computer-readable instructions stored in the memory to perform the method according to any one of claims 1-9.
12. A computer-readable storage medium, characterized in that, The device stores computer-readable instructions that, when executed by a processor of the electronic device, cause the electronic device to perform the method described in any one of claims 1-9.
13. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the method of any one of claims 1-9.
Citation Information
Patent Citations
Interface testing method and device, computing device and storage medium
CN110175119A
System testing method and device, computer equipment and storage medium
CN110413507A