Application program testing method and device, equipment and medium

By analyzing the bytecode of the application target class, determining the variable call relationship, and constructing and initializing the target object based on the relationship, generating the application's test code, the problem of high complexity and time-consuming analysis in the prior art is solved, and the coverage and efficiency of the test code are improved.

CN119557200APending Publication Date: 2025-03-04BEIJING ZITIAO NETWORK TECH CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202311104056.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2023-08-29
Publication Date
2025-03-04

AI Technical Summary

Technical Problem

In the process of testing code generation in the prior art, the analysis process is high and the time is too long, resulting in deterioration in effect and low code coverage.

Method used

During the unit testing process of the application, the bytecode of the target class where the objective function is located is obtained, the variable calling relationship is analyzed, the target object is constructed based on the relationship and initialized, and the test code of the objective function is generated, where the target object includes multiple key variables required to test the objective function.

Benefits of technology

Simplifies analysis complexity, reduces the time-consuming process of generating test codes, and improves the coverage and anthropomorphism of test codes.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119557200A_ABST
    Figure CN119557200A_ABST
Patent Text Reader

Abstract

The embodiment of the invention relates to an application program testing method and device, equipment and a medium, and the method comprises the steps: in the unit testing process of an application program, in response to a test code generation instruction for a target function in the application program, obtaining a byte code of a target class where the target function is located; analyzing the byte codes of the target class, and determining a variable calling relationship; a target object is constructed based on the variable calling relation, the target object is initialized, a test code of the target function is generated, and the target object comprises a plurality of key variables needed for testing the target function. Object construction and test code generation are achieved based on the variable calling relation of the target class where the target function is located, the variable calling relation only analyzes the key variables capable of influencing execution logic of the method, object construction only constructs the key variables, conciseness and effectiveness are achieved, analysis complexity is reduced, and efficiency is improved. And the coverage range of the test code is expanded.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of testing technology, and in particular to a method, device, equipment and medium for testing an application. Background Art

[0002] In the related art, in order to test the application, the test environment can be injected into the Evosuite tool using the Robolectric test module, so that the Evosuite tool has a test environment, so that the Evosuite tool can be used to generate test cases for testing. The test environment can be, for example, an Android environment. In the above test case generation process, the construction and initialization of the object can be analyzed by constructing a dependency graph, but this method has the problem of poor effect and low code coverage due to the high complexity of the analysis process and the long analysis time. Summary of the invention

[0003] In order to solve the above technical problems, the present disclosure provides a method, device, equipment and medium for testing an application.

[0004] The present disclosure provides a method for testing an application program, the method comprising:

[0005] In a unit test process of an application, in response to a test code generation instruction for a target function in the application, obtaining a bytecode of a target class where the target function is located;

[0006] Analyze the bytecode of the target class to determine the variable calling relationship;

[0007] A target object is constructed based on the variable calling relationship, and the target object is initialized to generate a test code for the target function, wherein the target object includes a plurality of key variables required for testing the target function.

[0008] The present disclosure also provides a device for testing an application program, the device comprising:

[0009] An acquisition module, used for acquiring the bytecode of the target class where the target function is located in response to a test code generation instruction for a target function in the application during a unit test of the application;

[0010] A calling relationship module, used to analyze the bytecode of the target class and determine the variable calling relationship;

[0011] A generation module is used to construct a target object based on the variable calling relationship, initialize the target object, and generate a test code for the target function, wherein the target object includes a plurality of key variables required for testing the target function.

[0012] An embodiment of the present disclosure also provides an electronic device, which includes: a processor; a memory for storing executable instructions of the processor; the processor is used to read the executable instructions from the memory and execute the instructions to implement a testing method for an application as provided in an embodiment of the present disclosure.

[0013] The embodiment of the present disclosure further provides a computer-readable storage medium, wherein the storage medium stores a computer program, and the computer program is used to execute the test method of the application program provided by the embodiment of the present disclosure.

[0014] The technical solution provided by the embodiment of the present disclosure has the following advantages over the prior art: the test solution of the application provided by the embodiment of the present disclosure, in the unit test process of the application, in response to the test code generation instruction for the target function in the application, obtains the bytecode of the target class where the target function is located; analyzes the bytecode of the target class to determine the variable call relationship; constructs the target object based on the variable call relationship, and initializes the target object to generate the test code of the target function, wherein the target object includes a plurality of key variables required for testing the target function. With the above technical solution, in the unit test process of the application, the test code generation instruction for the target function of the application can analyze and determine the variable call relationship of the target class where the target function is located, and implements object construction and test code generation based on the variable call relationship. Since the variable call relationship only analyzes the key variables that can affect the execution logic of the method, the object construction also only constructs the key variables, which is concise and effective, reduces the complexity of analysis, and improves the coverage of the test code. BRIEF DESCRIPTION OF THE DRAWINGS

[0015] The above and other features, advantages and aspects of the embodiments of the present disclosure will become more apparent with reference to the following detailed description in conjunction with the accompanying drawings. Throughout the accompanying drawings, the same or similar reference numerals represent the same or similar elements. It should be understood that the drawings are schematic and the originals and elements are not necessarily drawn to scale.

[0016] Figure 1 A flowchart of a method for testing an application program provided by an embodiment of the present disclosure;

[0017] Figure 2 A schematic diagram of a process of constructing a call relationship tree provided in an embodiment of the present disclosure;

[0018] Figure 3 A schematic diagram of a call relationship tree provided in an embodiment of the present disclosure;

[0019] Figure 4 A schematic diagram of a target object structure provided by an embodiment of the present disclosure;

[0020] Figure 5 A schematic diagram of the structure of a testing device for an application program provided in an embodiment of the present disclosure;

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

[0022] Embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present disclosure are shown in the accompanying drawings, it should be understood that the present disclosure can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided for a more thorough and complete understanding of the present disclosure. It should be understood that the drawings and embodiments of the present disclosure are only for exemplary purposes and are not intended to limit the scope of protection of the present disclosure.

[0023] It should be understood that the various steps described in the method embodiments of the present disclosure may be performed in different orders and / or in parallel. In addition, the method embodiments may include additional steps and / or omit the steps shown. The scope of the present disclosure is not limited in this respect.

[0024] The term "including" and its variations used herein are open inclusions, i.e., "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one additional embodiment"; the term "some embodiments" means "at least some embodiments". The relevant definitions of other terms will be given in the following description.

[0025] It should be noted that the concepts such as "first" and "second" mentioned in the present disclosure are only used to distinguish different devices, modules or units, and are not used to limit the order or interdependence of the functions performed by these devices, modules or units.

[0026] It should be noted that the modifications of "one" and "plurality" mentioned in the present disclosure are illustrative rather than restrictive, and those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".

[0027] The names of the messages or information exchanged between multiple devices in the embodiments of the present disclosure are only used for illustrative purposes and are not used to limit the scope of these messages or information.

[0028] In order to solve the problems existing in the process of generating test code for an application, an embodiment of the present disclosure provides a method for testing an application, which is introduced below in conjunction with a specific embodiment.

[0029] Figure 1 The present invention provides a flowchart of a method for testing an application program, which can be executed by a testing device for the application program, wherein the device can be implemented by software and / or hardware and can generally be integrated in an electronic device. Figure 1 As shown, the method includes:

[0030] Step 101: During the unit testing process of the application, in response to a test code generation instruction for a target function in the application, the bytecode of the target class where the target function is located is obtained.

[0031] Among them, the application can be any program that needs to be tested, for example, the application can be a video application, a reading application, etc., without specific limitation. The test of the application in the disclosed embodiment can be to generate corresponding test code for each method included in each class that needs to be tested to test each method through the test code, that is, each method is regarded as a unit to be tested, and the application is tested by unit testing. The target function can be a method in the target class (target class) of the application that needs to be tested. A class can encapsulate multiple methods, functions and variables, and the number of target functions can be one or more. The test code generation instruction can be an instruction to generate a unit test code for the target function of the application. The bytecode of the target class can be understood as an intermediate code generated by assembling the code of the target class.

[0032] Specifically, when it is necessary to perform unit testing on an application, a test request for the application can be initiated, and the test device of the application can enter the unit test process of the application when receiving the test request, and after receiving the test code generation instruction for the target function in the application, the bytecode of the target class where the target function is located can be obtained for subsequent use. The test of the application in the embodiment of the present disclosure can be performed in the test environment of the Evosuite tool injected with the Robolectric test module.

[0033] Step 102: Analyze the bytecode of the target class to determine the variable calling relationship.

[0034] The variable calling relationship may represent the calling relationship between multiple methods and multiple variables included in the target class, which may be specifically represented by a call relationship tree (CallChainTree).

[0035] The test device of the application can traverse the bytecode of the target class and build a call relationship tree by analyzing the call relationship between multiple methods and multiple variables included in the target class.

[0036] The call relationship tree is a tree with the target class (class) of the target function as the root node, and the method (method) and key variables as child nodes. Key variables can be variables that can affect the execution logic of the method, and can specifically include context variables (ContextVar) and static variables (StaticVar), where context variables include member variables and method input parameters. In the disclosed embodiment, local variables defined inside the method can be ignored.

[0037] In some embodiments, the bytecode of the target class is traversed, and a call relationship tree is constructed by analyzing the calling relationships between multiple methods and multiple variables included in the target class. This may include: in the process of traversing the bytecode of the target class, creating a class node of the target class, and using the class node as the root node of the call relationship tree; creating a method node corresponding to the method of the target class, and using the method node as the first-level child node of the call relationship tree, wherein the method node is a child node of the root node; creating a variable node for the key variable of the method, and using the variable node as the second-level child node of the call relationship tree, wherein the variable node is a child node of the method node.

[0038] The application test device can traverse the bytecode of the target class through the bytecode tool, that is, analyze the target class of the target function. When encountering the target class, a class node of the target class can be created, and the class node can be used as the root node of the call relationship tree; then the content of the target class can be traversed, and when encountering a method, a method node can be created as a child node of the above root node, that is, the method node is the first-level child node of the call relationship tree; further, the method content can be traversed, and for key variables within the method scope, variable nodes can be created as child nodes of the above method nodes, that is, the variable nodes are the second-level child nodes of the call relationship tree. If member nodes of the variable node are involved, child nodes can continue to be created. The above bytecode tool can be, for example, ASM, which can be a toolkit for operating bytecode files.

[0039] Optionally, creating a method node corresponding to the method of the target class may include: if there is no nested call to the method of the target class, creating a method node corresponding to the method under the root node; wherein, creating a variable node for the key variables of the method includes: for the key variables of the method, creating corresponding variable nodes according to the variable type.

[0040] When creating a method node corresponding to a method of the target class, the application's test device can first determine whether the method's call depth is equal to 0. The call depth can indicate the number of methods called under the current method. When the call depth is 0, it indicates that there is no method call under the current method, that is, there is no nested call. Then, a method node can be created as a child node of the root node. In this case, the key variables of the method are collected, and variable nodes of each key variable are created with the variable type. For member variables in the context variables in the key variables, variable nodes of the current class type can be created. For method input parameters, variable nodes of other types of variable types can be created.

[0041] Optionally, creating a variable node for a key variable of a method may also include: if there are nested calls in the method of the target class and after creating a corresponding variable node according to the variable type, for the attribute instructions (FieldInsn) of the method and its sub-methods, determining whether there is a target variable node in the call relationship tree that matches the variable type of the current variable in the attribute instruction; if there is a target variable node in the call relationship tree, creating a variable node for the current variable under the target variable node; otherwise, when the current variable is a member variable of a static variable, creating a variable node for the current variable and a variable node of the attribute type under the static variable.

[0042] After creating the corresponding variable node according to the variable type, the test device of the application can traverse each specific implementation (instructions) of the method; when it is determined that the above call depth is not equal to 0, it means that there is a method call under the current method, that is, there is a nested call, and each specific implementation of the sub-method can also be traversed. When traversing each specific implementation of the method, when encountering the attribute instruction of the member variable, the current call relationship tree can be searched to determine whether there is a target variable node with the same variable type as the current variable in the attribute instruction. If so, the variable node of the current variable is created as a child node of the target variable node. The variable node of the current variable is of attribute type, which also affects the operation logic of the method; otherwise, it is determined whether the current variable is a static variable. If so, a variable node of the current variable and a variable node of the attribute type are created, and the variable node of the current variable is used as a child node under the static variable, and the variable node of the attribute type is used as a child node of the variable node of the current variable; if the current variable is not a static variable, it is considered that the current variable belongs to the local variable of the method and can be discarded.

[0043] In the context of unit test code generation, the construction of the test code for the method of the target function only needs to consider factors that can affect the execution logic of the method. Therefore, the disclosed embodiment can only use the member variables of the class and the method input parameters as context variables, ignoring the local variables defined inside the method (reducing the scope of local variables considered). Since static variables can also affect the execution logic of the method, static variables are also taken into consideration. Variables that can affect the execution logic of the method will be created under the nodes of the context variables and static variables, and nodes will be created with the variable type. If a specific member access variable is involved, a child node will be created under the corresponding type node.

[0044] Step 103: construct a target object based on the variable calling relationship, initialize the target object, and generate test code for the target function, wherein the target object includes multiple key variables required for testing the target function.

[0045] The target object may include multiple key variables that affect the operating logic of the target function.

[0046] In the disclosed embodiment, the testing device of the application can extract a variable subtree with the target function as the root node according to the call relationship tree; create multiple key variables included in the target object according to the variable subtree using the variable creation process, that is, traverse the variable subtree to create context variables and static variables under the root node of the target function; generate a calling code statement after initializing the multiple key variables included in the target object, and obtain the test code of the target function, and then the target function can be tested through the test code, thereby realizing the testing of the application.

[0047] Among them, the variable subtree can be a branch with the target function as the root node obtained by searching in the call relationship tree according to the target function. The variable creation process can include reusing existing variables, creating variables, or creating based on the return value type; creating variables here can include: first collecting the constructor (generator) of the current type, which can include a constructor and a method call that can return the current type; then selecting a constructor and instantiating it with the creation method to obtain the variable; finally instantiating the variable. The above creation method can include constructing all method input parameters, each method input parameter can be implemented using the above variable creation process, and after constructing all method input parameters, the calling code statement of the method is generated. When constructing each method input parameter, it can be constructed based on the above call relationship tree.

[0048] From the perspective of code writing, all codes are composed of object calling methods and attributes, that is, a combination of object.method() and object.var, where methods (method) will also fall on the operation of object.var, so the composition of the code can be completely regarded as a set of variable (var) operations, and only variable operation instructions will be seen after the code is converted into assembly. The disclosed embodiment focuses on analyzing the call chain relationship of variables from the level of assembly instructions and bytecodes. In the scenario of single test generation, complex objects are constructed based on the call relationship, and a suitable context is constructed for the test of the target function, thereby improving the coverage and anthropomorphism of the generated test code.

[0049] The test scheme for an application provided by the embodiment of the present disclosure, in the unit test process of the application, in response to the test code generation instruction for the target function in the application, obtains the bytecode of the target class where the target function is located; analyzes the bytecode of the target class to determine the variable call relationship; constructs the target object based on the variable call relationship, and initializes the target object to generate the test code for the target function, wherein the target object includes a plurality of key variables required for testing the target function. By adopting the above technical scheme, in the unit test process of the application, the test code generation instruction for the target function of the application can analyze and determine the variable call relationship of the target class where the target function is located, and implements object construction and test code generation based on the variable call relationship. Since the variable call relationship only analyzes the key variables that can affect the execution logic of the method, the object construction also only constructs the key variables, which is concise and effective, reduces the complexity of analysis, and improves the coverage of the test code.

[0050] In some embodiments, the application testing method may further include: deleting leaf nodes in the variable nodes of the call relationship tree.

[0051] Among them, a leaf node can be a node without child nodes. The variable corresponding to the leaf node can only be invalid (null) or create an object that does not need recursive initialization, so no special processing is required. After generating the call relationship tree, the test device of the application can prune the call relationship tree. Specifically, the first-level child nodes under the method node, that is, the variable nodes, can be judged. If a variable node is a leaf node, the node can be directly deleted. By pruning the call relationship tree, the simplicity of the call relationship tree can be improved and the complexity can be reduced, thereby improving the efficiency of test code generation.

[0052] In some embodiments, the test code generation instructions are instructions in an iterative process based on a genetic algorithm, and the application program testing method may further include: inserting a step of constructing a target object based on a variable calling relationship in each generation of the iterative process.

[0053] After the test environment is injected into the Evosuite tool through the Robolectric test module, the Evosuite tool injected into the test environment is the Evodroid tool. The test of the EvoDroid tool is based on the iterative process of the genetic algorithm, so the fusion of the construction of the target object and the original process may include: inserting the steps of constructing the target object based on the variable call relationship in the first generation of the iterative process to improve the quality of the initial population, searching for the correct value through iteration, or on the basis of the above first generation, inserting the steps of constructing the target object based on the variable call relationship in the mutation stage during the iterative mutation, that is, inserting the steps of constructing the target object based on the variable call relationship in each generation of the iterative process, so that the call relationship in the final population is more complete and the overall quality of the population is higher. By inserting the steps of constructing objects based on the variable call relationship in each generation of the iterative process, the search accuracy can be guaranteed.

[0054] Next, the method of the embodiment of the present disclosure is specifically described by a specific example. Figure 2 A schematic diagram of a process of constructing a call relationship tree provided in an embodiment of the present disclosure, Figure 3 A schematic diagram of a call relationship tree provided in an embodiment of the present disclosure, wherein the call relationship tree (CallChainTree) contains a minimum information set for constructing a complex object and initializing it, such as Figure 2 As shown, construct Figure 3 The process of building a call relationship tree of a target class in the target class may include: Step 201, target class analysis. Analyze the target class, that is, use ASM to traverse the bytecode of the target class. Step 202, access the class. Step 203, add a class node to the call relationship tree. When encountering a Class, create a corresponding class node in CallChainTree, such as Figure 3 , and continue to traverse its contents. Step 204, access the method. When traversing the Class content, if you encounter Method, then further traverse its contents. Step 205, whether the call depth is zero, if so, execute step 206; otherwise, execute step 209. Step 206, add a method node to the call relationship tree. Determine whether the call depth (methodDepth) of the current method is equal to 0. If so, create a corresponding method node in CallChainTree, such as Figure 3 Step 207: Collect key variables. Key variables may include context variables and static variables. Step 208: Create variable nodes of variable types in the child nodes of the method node. Collect the context variables and static variables of the current method, and construct variable nodes accordingly according to the variable types, such as Figure 3, for the variable of the current class, a variable node of the current class (ThisObjType) is created, and for the current input object, a variable node of another type (Obj) is created, that is, a variable node of variable type 2 and a variable node of variable type 3. After step 208, step 209 may be executed.

[0055] Step 209, traverse the specific implementation of the method. For the attribute instruction, execute step 210. Step 210, search for the variable node that matches the current variable type. Step 211, whether it is found, if so, execute step 212; otherwise, execute step 213. The call depth of the current method is not equal to 0. Traverse each specific implementation (instructions) of the method content. When encountering an attribute instruction for member variable access, search the current CallChainTree to find a variable node that matches the variable type (type match) of the current variable. Step 212, add a variable node of the attribute type under the variable node. If there is a variable node with a variable type match, add a variable node of the attribute type after the searched variable node, such as Figure 3 The variable node of attribute 1 of the current class, the variable node of attribute 2 of the current class, and the variable node of attribute 4 of variable type 3 indicate that the member variables of this variable type directly affect the running logic of the method. The variable nodes of member variables are still variable nodes, forming CallChainTree into a relatively deep tree. Step 213: Whether it is a static variable, that is, whether it is a member variable of a static variable. If so, execute step 214; otherwise, execute step 215. Step 214: Create a variable node of the variable type and attribute type under the variable node of the static variable. Step 215: No processing. If there is no variable node that matches the variable type, further check whether the current variable is a member variable of the static variable. If so, create a variable node of the variable type under the static variable, such as Figure 3 In the static variable, select the variable node of variable type 4 and add a variable node of attribute type under the variable node of this variable type, such as Figure 3 The variable node of the attribute 5 of the variable type 4 in the code; if the current variable is not a member variable of a static variable, it can be considered as the program state of the member variable inside the method, and no special construction or processing is required.

[0056] In step 209, for the submethod, the execution returns to step 204. When a nested method calls a submethod, the call depth is increased by 1, and the submethod is traversed. The relevant variable information is added to the context variables and static variables of the current CallChainTree, so that the CallChainTree contains the call chain relationship of all variables that affect the execution of the method logic.

[0057] Through CallChainTree, we can get all the variable call chain relationships that affect the method logic. After the first-level node generates a code statement, the child node creates an object based on the first-level node and recursively initializes it. For example, Figure 4 A schematic diagram of a target object structure provided by an embodiment of the present disclosure, such as Figure 4 As shown, the construction process of the target object may include: creating a target object (create targetObject), the target object is an object of the target class, then creating attributes (addField), creating methods (addMethod) and creating static methods (addMethodFor), and both creating methods and creating static methods belong to creation methods. Creating attributes is to obtain variable reference instances through the variable creation process (create or reuse Var), create code statements (statements) and assign values ​​to attributes (fields), and generate corresponding test codes. The variable creation process can reuse existing variables, create variables, or create based on the return value type; creating variables here may include: first collecting constructors of the current type; then selecting a constructor and instantiating it with addMethod to obtain a variable; and finally instantiating the variable. The above addMethod may include constructing all method input parameters, each method input parameter may be implemented using the above variable creation process, and after constructing all method input parameters, the calling code statement of the method is generated, and the corresponding test code is generated. When constructing each method input parameter, it may be constructed based on the above call relationship tree.

[0058] like Figure 4As shown, when creating a method to generate the corresponding test code, it can be achieved by obtaining the variables of the method (getvars of method) and constructing the target object (objectX create). Obtaining the variables of the method can include: obtaining the call relationship tree to find the root node, class node and method node, and constructing the target object can include: creating context variables and static variables in the method node, finding child nodes, checking availability, and returning to execute the search for child nodes after adding sub-attributes to the object. When generating the corresponding test code for a method (that is, the above-mentioned target function), the variable subtree of the corresponding method can be obtained from CallChainTree, and various variables required for testing the method can be more purposefully constructed according to the variable subtree, that is, the target object is constructed. Specifically, the corresponding method node is obtained by searching callChainTree by the type of the class where the corresponding method is located (class type), thereby obtaining the variable subtree that affects the logic of the method, that is, the variable subtree with the method node as the root node. According to the searched variable subtree, the variable creation process is reused to create context variables and static variables; check the availability and availability compatibility of child nodes, filter out final objects, and for protected variables, search and match the corresponding setter method and getter method according to the type (not shown in the figure); initialize the complete variable subtree according to the depth traversal of the variable subtree, and the target object creation can be completed, and then the call statement of the method, that is, the test code, is generated. The above-mentioned deep traversal will not have a recursive loop. In extreme cases, the recursive depth may be too deep, the call statements are too many, and the construction fails due to exceeding the limit. Therefore, in order to avoid the above situation, the number of statements can be set to a maximum of 100 lines.

[0059] In the unit testing scenario, the disclosed embodiment analyzes the impact of complex objects on the branch coverage of methods (such as the above-mentioned target function), screens out valid variables for constructing call relationship attributes, and constructs nodes with information such as variable names in bytecodes to ensure effectiveness and simplicity; in the test code generation stage, the recursive construction strategy of the call relationship tree for complex objects and the impact of the call relationship tree in the iterative process based on the genetic algorithm are analyzed to determine the reasonable construction timing.

[0060] In the test code generation stage of unit testing, the disclosed embodiment analyzes the deterministic factors that affect the method branch coverage of complex objects, screens out valid variables to construct a call chain relationship tree, and constructs and initializes complex objects based on the call relationship tree to generate test code, effectively solving the problems in related technologies of degraded results and low code coverage due to the high complexity of the analysis process and the long analysis time.

[0061] Figure 5This is a schematic diagram of the structure of a testing device for an application provided by an embodiment of the present disclosure. The device can be implemented by software and / or hardware and can generally be integrated in an electronic device. Figure 5 As shown, the device comprises:

[0062] The acquisition module 501 is used for acquiring the bytecode of the target class where the target function is located in response to a test code generation instruction for a target function in the application during the unit test of the application;

[0063] A calling relationship module 502 is used to analyze the bytecode of the target class and determine the variable calling relationship;

[0064] The generation module 503 is used to construct a target object based on the variable calling relationship, initialize the target object, and generate the test code of the target function, wherein the target object includes a plurality of key variables required for testing the target function.

[0065] Optionally, the variable calling relationship is represented by a calling relationship tree, and the calling relationship module 502 is used to:

[0066] The bytecode of the target class is traversed, and the call relationship tree is constructed by analyzing the call relationships between multiple methods and multiple variables included in the target class.

[0067] Optionally, the call relationship tree is a tree with the target class as the root node and methods and key variables as child nodes.

[0068] Optionally, the call relationship module 502 includes:

[0069] The first unit is used to create a class node of the target class during the traversal of the bytecode of the target class, and use the class node as the root node of the call relationship tree;

[0070] The second unit is used to create a method node corresponding to the method of the target class, and use the method node as a first-level child node of the call relationship tree, wherein the method node is a child node of the root node;

[0071] The third unit is used to create a variable node of a key variable of the method, and use the variable node as a second-level child node of the call relationship tree, wherein the variable node is a child node of the method node.

[0072] Optionally, the second unit is used to:

[0073] If there is no nested call to the method of the target class, create a method node corresponding to the method under the root node;

[0074] Among them, the third unit is used for:

[0075] For the key variables of the method, corresponding variable nodes are created according to the variable type, wherein the key variables include context variables and static variables, and the context variables include member variables and method input parameters.

[0076] Optionally, the third unit is also used for:

[0077] If there is a nested call in the method of the target class and after the corresponding variable node is created according to the variable type, for the attribute instructions of the method and the sub-methods of the method, it is determined whether there is a target variable node in the call relationship tree that matches the variable type of the current variable in the attribute instruction;

[0078] If the target variable node exists in the call relationship tree, a variable node of the current variable is created under the target variable node; otherwise, when the current variable is a member variable of the static variable, a variable node of the current variable and a variable node of the attribute type are created under the static variable.

[0079] Optionally, the device further includes a deletion module, configured to:

[0080] Delete the leaf nodes in the variable nodes of the call relationship tree.

[0081] Optionally, the generating module 503 is used for:

[0082] Extracting a variable subtree with the target function as a root node according to the call relationship tree;

[0083] Creating a plurality of key variables included in the target object using a variable creation process according to the variable subtree;

[0084] After initializing a plurality of key variables included in the target object, a calling code statement is generated to obtain the test code of the target function.

[0085] Optionally, the test code generation instruction is an instruction in an iterative process based on a genetic algorithm, and the device further includes a merging module, which is used to:

[0086] The step of constructing the target object based on the variable calling relationship is inserted in each generation of the iterative process.

[0087] The application testing device provided in the embodiments of the present disclosure can execute the application testing method provided in any embodiment of the present disclosure, and has the corresponding functional modules and beneficial effects of the execution method.

[0088] The embodiments of the present disclosure also provide a computer program product, including a computer program / instruction, which, when executed by a processor, implements the test method of the application program provided by any embodiment of the present disclosure.

[0089] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure.

[0090] The following specific reference Figure 6 , which shows a schematic diagram of the structure of an electronic device 600 suitable for implementing the embodiment of the present disclosure. The electronic device 600 in the embodiment of the present disclosure may include, but is not limited to, mobile terminals such as mobile phones, notebook computers, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 6 The electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present disclosure.

[0091] like Figure 6 As shown, the electronic device 600 may include a processing device (e.g., a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the electronic device 600 are also stored. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.

[0092] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device 600 to communicate with other devices wirelessly or by wire to exchange data. Although Figure 6 The electronic device 600 is shown with various devices, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.

[0093] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 609, or installed from a storage device 608, or installed from a ROM 602. When the computer program is executed by the processing device 601, the above-mentioned functions defined in the test method of the application program of the embodiment of the present disclosure are executed.

[0094] It should be noted that the computer-readable medium disclosed above may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. This propagated data signal may take a variety of forms, including but not limited to an electromagnetic signal, an optical signal, or any suitable combination of the above. The computer readable signal medium may also be any computer readable medium other than a computer readable storage medium, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.

[0095] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.

[0096] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.

[0097] The above-mentioned computer-readable medium carries one or more programs. When the above-mentioned one or more programs are executed by the electronic device, the electronic device: during the unit testing process of the application, in response to the test code generation instruction for the target function in the application, obtains the bytecode of the target class where the target function is located; analyzes the bytecode of the target class to determine the variable calling relationship; constructs a target object based on the variable calling relationship, initializes the target object, and generates the test code of the target function, wherein the target object includes multiple key variables required to test the target function.

[0098] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages ​​or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).

[0099] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0100] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, limit the unit itself.

[0101] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.

[0102] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.

[0103] It is understandable that before using the technical solutions disclosed in the embodiments of the present disclosure, the types, scope of use, usage scenarios, etc. of the information involved in the present disclosure should be informed to the user and the user's authorization should be obtained in an appropriate manner in accordance with relevant laws and regulations.

[0104] The above description is only a preferred embodiment of the present disclosure and an explanation of the technical principles used. Those skilled in the art should understand that the scope of disclosure involved in the present disclosure is not limited to the technical solutions formed by a specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the above disclosed concept. For example, the above features are replaced with the technical features with similar functions disclosed in the present disclosure (but not limited to) by each other to form a technical solution.

[0105] In addition, although each operation is described in a specific order, this should not be understood as requiring these operations to be performed in the specific order shown or in a sequential order. Under certain circumstances, multitasking and parallel processing may be advantageous. Similarly, although some specific implementation details are included in the above discussion, these should not be interpreted as limiting the scope of the present disclosure. Some features described in the context of a separate embodiment can also be implemented in a single embodiment in combination. On the contrary, the various features described in the context of a single embodiment can also be implemented in multiple embodiments individually or in any suitable sub-combination mode.

[0106] Although the subject matter has been described in language specific to structural features and / or methodological logical actions, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. On the contrary, the specific features and actions described above are merely example forms of implementing the claims.

Claims

1. A method for testing an application, characterized in that: include: In a unit test process of an application, in response to a test code generation instruction for a target function in the application, obtaining a bytecode of a target class where the target function is located; Analyze the bytecode of the target class to determine the variable calling relationship; A target object is constructed based on the variable calling relationship, and the target object is initialized to generate a test code for the target function, wherein the target object includes a plurality of key variables required for testing the target function.

2. The method according to claim 1, characterized in that: The variable calling relationship is represented by a calling relationship tree, and the bytecode of the target class is analyzed to determine the variable calling relationship, including: The bytecode of the target class is traversed, and the call relationship tree is constructed by analyzing the call relationships between multiple methods and multiple variables included in the target class.

3. The method according to claim 2, characterized in that The call relationship tree is a tree with the target class as the root node and methods and key variables as child nodes.

4. The method according to claim 2 or 3, traversing the bytecode of the target class, and building the call relationship tree by analyzing the call relationships between multiple methods and multiple variables included in the target class, comprising: In the process of traversing the bytecode of the target class, a class node of the target class is created, and the class node is used as the root node of the call relationship tree; Creating a method node corresponding to the method of the target class, and using the method node as a first-level child node of the call relationship tree, wherein the method node is a child node of the root node; Create a variable node for the key variable of the method, and use the variable node as a second-level child node of the call relationship tree, wherein the variable node is a child node of the method node.

5. The method according to claim 4, characterized in that Creating a method node corresponding to the method of the target class includes: If there is no nested call to the method of the target class, create a method node corresponding to the method under the root node; The variable nodes of the key variables of the method are created, including: For the key variables of the method, corresponding variable nodes are created according to the variable type, wherein the key variables include context variables and static variables, and the context variables include member variables and method input parameters.

6. The method according to claim 5, characterized in that Creating variable nodes for key variables of the method also includes: If there is a nested call in the method of the target class and after the corresponding variable node is created according to the variable type, for the attribute instructions of the method and the sub-methods of the method, it is determined whether there is a target variable node in the call relationship tree that matches the variable type of the current variable in the attribute instruction; If the target variable node exists in the call relationship tree, a variable node of the current variable is created under the target variable node; otherwise, when the current variable is a member variable of the static variable, a variable node of the current variable and a variable node of the attribute type are created under the static variable.

7. The method according to claim 4, characterized in that The method further comprises: Delete the leaf nodes in the variable nodes of the call relationship tree.

8. The method according to claim 2, characterized in that: Constructing a target object based on the variable calling relationship, initializing the target object, and generating a test code for the target function, including: Extracting a variable subtree with the target function as a root node according to the call relationship tree; Creating a plurality of key variables included in the target object using a variable creation process according to the variable subtree; After initializing a plurality of key variables included in the target object, a calling code statement is generated to obtain the test code of the target function.

9. The method according to claim 1, characterized in that: The test code generation instruction is an instruction in an iterative process based on a genetic algorithm, and the method further includes: The step of constructing the target object based on the variable calling relationship is inserted in each generation of the iterative process.

10. A testing device for an application, characterized in that: include: An acquisition module, used for acquiring the bytecode of the target class where the target function is located in response to a test code generation instruction for a target function in the application during a unit test of the application; A calling relationship module, used to analyze the bytecode of the target class and determine the variable calling relationship; A generation module is used to construct a target object based on the variable calling relationship, initialize the target object, and generate a test code for the target function, wherein the target object includes a plurality of key variables required for testing the target function.

11. An electronic device, characterized in that: The electronic device comprises: processor; a memory for storing instructions executable by the processor; The processor is used to read the executable instructions from the memory and execute the instructions to implement the test method of the application program described in any one of claims 1-9.

12. A computer-readable storage medium, characterized in that: The storage medium stores a computer program, and the computer program is used to execute the test method of the application program described in any one of claims 1 to 9.