CN114265776B - Unit test code generation method, device, equipment and medium - Google Patents

Unit test code generation method, device, equipment and medium Download PDF

Info

Publication number
CN114265776B
CN114265776B CN202111585215.0A CN202111585215A CN114265776B CN 114265776 B CN114265776 B CN 114265776B CN 202111585215 A CN202111585215 A CN 202111585215A CN 114265776 B CN114265776 B CN 114265776B
Authority
CN
China
Prior art keywords
target
simulation object
test
calling
mapping relation
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN202111585215.0A
Other languages
Chinese (zh)
Other versions
CN114265776A (en
Inventor
王宏波
邱庆举
王洋
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Zhejiang Geely Holding Group Co Ltd
Zhejiang Shikong Daoyu Technology Co Ltd
Original Assignee
Zhejiang Geely Holding Group Co Ltd
Zhejiang Shikong Daoyu Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Zhejiang Geely Holding Group Co Ltd, Zhejiang Shikong Daoyu Technology Co Ltd filed Critical Zhejiang Geely Holding Group Co Ltd
Priority to CN202111585215.0A priority Critical patent/CN114265776B/en
Publication of CN114265776A publication Critical patent/CN114265776A/en
Application granted granted Critical
Publication of CN114265776B publication Critical patent/CN114265776B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Abstract

The application provides a unit test code generation method, a unit test code generation device, a unit test code generation apparatus, a unit test code generation medium and a program product. Determining a target method and a target simulation object contained in executable code of a target test object; acquiring a first mapping relation table and a second mapping relation table; determining a target parameter value having a mapping relation with a target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table; instantiating a calling method of a simulation object to be called and a target simulation object; and configuring codes or data such as target parameter values to a preset area in the unit test file to generate target unit test codes. The automation of generating the target unit test code can be realized by automatically determining the parameter value corresponding to the target method and calling the simulation object calling method, thereby simplifying the method for writing the unit test code and reducing the time consumed by writing the unit test code.

Description

Unit test code generation method, device, equipment and medium
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method, an apparatus, a device, a medium, and a program product for generating unit test codes.
Background
Unit testing (unit testing) refers to checking and verifying the smallest testable unit in a software program, and the unit testing occupies an important position in the software system development process. The workload of writing unit test code even exceeds the workload of developing software system code.
In the prior art, developers mainly write unit test codes manually, the method for writing the unit test codes is complex, and the time for writing the unit test codes is long.
Disclosure of Invention
The application provides a unit test code generation method, a unit test code generation device, a unit test code generation medium and a program product, and aims to solve the problems that a developer manually compiles a unit test code, the method for compiling the unit test code is complex, and the time consumed for compiling the unit test code is long.
In a first aspect, the present application provides a unit test code generation method, including:
acquiring a target test object; the target test object comprises executable code of the target test object;
determining a target method and a target simulation object contained in executable code of the target test object; the target simulation object is of a preset type;
acquiring a first mapping relation table between parameter values and methods in all types of unit test codes which are constructed in advance, and a second mapping relation table between simulation objects to be called and simulation object calling methods to be called and between parameter values and parameters;
determining a target parameter value having a mapping relation with the target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table;
defining a constructor and instantiating the simulation object calling method to be called and the target simulation object;
and configuring the target parameter value, the construction function, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file to generate a target unit test code.
In a second aspect, the present application provides a unit test code generating apparatus, including:
the object acquisition module is used for acquiring a target test object; the target test object comprises executable code of the target test object;
a determination module for determining a target method and a target simulation object contained in an executable code of the target test object; the target simulation object is of a preset type;
the system comprises a relation table acquisition module, a relation table acquisition module and a simulation model execution module, wherein the relation table acquisition module is used for acquiring a first mapping relation table between each parameter value and each method in all types of unit test codes which are constructed in advance, and a second mapping relation table between each simulation object to be called and each simulation object to be called calling method and between each parameter value and each method;
the determining module is further used for determining a target parameter value having a mapping relation with the target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table;
the instantiation module is used for defining a constructor and instantiating the simulation object calling method to be called and the target simulation object;
and the configuration module is used for configuring the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file so as to generate a target unit test code.
In a third aspect, the present application provides an electronic device, comprising: a processor, and a memory communicatively coupled to the processor;
a circuit interconnection between the processor and the memory;
the memory stores computer-executable instructions;
the processor executes the computer execution instructions stored in the memory to implement the unit test code generation method described above.
In a fourth aspect, the present application provides a computer-readable storage medium having stored therein computer-executable instructions, which when executed by a processor, are used for implementing the unit test code generation method described above.
In a fifth aspect, the present application provides a computer program product comprising computer executable instructions that when executed by a processor implement the unit test code generation method described above.
The unit test code generation method, device, equipment, medium and program product provided by the application obtain a target test object; determining a target method and a target simulation object contained in executable code of the target test object; acquiring a first mapping relation table between parameter values and methods in all types of unit test codes which are constructed in advance, and a second mapping relation table between simulation objects to be called and simulation object calling methods to be called and between parameter values and parameters; determining a target parameter value having a mapping relation with the target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table; defining a constructor and instantiating the simulation object calling method to be called and the target simulation object; and configuring the target parameter value, the construction function, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file to generate a target unit test code. According to the first mapping relation table, a target parameter value having a mapping relation with the target method can be determined, according to the second mapping relation table, a simulation object calling method needing to be called and having a mapping relation with the target parameter value can be determined, and automation of generating the target unit test code can be achieved by automatically determining the parameter value corresponding to the target method and the simulation object calling method needing to be called, so that the method for writing the unit test code is simplified, and time consumed by writing the unit test code is reduced.
Drawings
The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and together with the description, serve to explain the principles of the application.
Fig. 1 is a schematic view of an application scenario of the present application;
FIG. 2 is a flowchart of a method for generating unit test codes according to an embodiment of the present disclosure;
FIG. 3 is a schematic diagram of a component structure of a unit test file provided in the present application;
FIG. 4 is a flowchart of a unit test code generation method according to a second embodiment of the present application;
FIG. 5 is a flowchart of a method for generating unit test codes according to a third embodiment of the present application;
fig. 6 is a schematic structural diagram of a unit test code generating apparatus according to a fourth embodiment of the present application;
fig. 7 is a schematic structural diagram of an electronic device according to a fifth embodiment of the present application.
With the above figures, there are shown specific embodiments of the present application, which will be described in more detail below. The drawings and written description are not intended to limit the scope of the inventive concepts in any manner, but rather to illustrate the concepts of the application by those skilled in the art with reference to specific embodiments.
Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. The following description refers to the accompanying drawings in which the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.
The terms "first", "second", etc. are used for descriptive purposes only and are not to be construed as indicating or implying relative importance or implicitly indicating the number of technical features indicated. In the description of the following examples, "plurality" means two or more unless specifically limited otherwise.
The terms referred to in the present application are explained first:
POJO type: classes that contain only set, get attributes, typically exist as parameter objects.
AST (abstract syntax tree): the structure, context and the like of the source code can be obtained through the AST algorithm.
Analog objects, also Mock objects: one method calls another, and in the unit test, the object calls are simulated by means of Mock.
Constructors are a special method. The method is mainly used for initializing the object when the object is created, namely, the object member variable is endowed with an initial value and is always used in a statement for creating the object together with a new operator. The constructor provided for a class may automatically complete the initialization task of the object.
Robustness means that various conditions which may cause program crash are fully considered in a program, and corresponding processing is carried out, so that the program can still normally work when encountering abnormal conditions, and the program cannot be halted.
For a unit in unit test, the specific meaning of the unit is determined according to the actual situation, for example, a unit in C language refers to a function, a unit in Java refers to a class, and graphical software can refer to a window or a menu. In general, the unit is the minimum tested functional module specified by human. Unit testing is the lowest level of testing activity to be performed during software development, and individual units of software will be tested in isolation from other parts of the program.
Aiming at the problems of complex method for writing unit test codes and much consumed time in the prior art, the inventor finds that although the writing of the unit test codes is complex, the reference between the test units is realized by using a Mock (simulation/pile driving) mode generally, so that the automatic generation of the unit test codes can be realized only by identifying the simulation object needing to be Mock (simulation/pile driving) in the test units and determining the calling method of the simulation object, developers are not required to write the unit test codes manually, and the consumed time for writing the unit test codes is reduced.
Fig. 1 is a schematic view of an application scenario of the present application, as shown in fig. 1, a target test object needs to call a real object when testing the target test object, but the real object may not be easily constructed or easily obtained, and may affect the test of the target test object due to the factors of the real object, so that the behavior of the real object is simulated by using a simulation object, so that the target test object calls the simulation object instead of the real object when testing the target test object.
The following describes the technical solutions of the present application and how to solve the above technical problems with specific embodiments. The following several specific embodiments may be combined with each other, and details of the same or similar concepts or processes may not be repeated in some embodiments. Embodiments of the present application will be described below with reference to the accompanying drawings.
Example one
Fig. 2 is a flowchart of a unit test code generation method according to an embodiment of the present application, which is provided to solve the problems that a developer manually writes a unit test code, the unit test code writing method is complex, and the unit test code writing time is long. The method in this embodiment is applied to a unit test code generation apparatus, which may be located in an electronic device. Among other things, the electronic device may be a digital computer that represents various forms. Such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers.
As shown in fig. 2, the method comprises the following specific steps:
and step S101, acquiring a target test object.
In the embodiment of the present application, the target test object includes an executable code of the target test object, and the target test object refers to a smallest testable unit to be tested in the software program.
Illustratively, the target test object may be obtained by obtaining a target test object file, and the target test object file includes executable code of the target test object. The implementation mode of obtaining the target test object is not specifically limited, and can be set and adjusted according to the requirements of the actual application scene.
Step S102, determining a target method and a target simulation object contained in the executable code of the target test object.
The target simulation object is a preset type of attribute, and the target method refers to a method to be tested in the target test object.
Specifically, determining a target method and attributes contained in executable code of a target test object, wherein the attributes comprise attribute values; judging whether the attribute is of a preset type; and determining the attribute of the preset type as the identification object. For example, the preset type may be an attribute whose attribute value is not a basic attribute and is not an attribute of a POJO class, and if the attribute only has a get method and a set method, the attribute is not an attribute of the preset type and is not a target simulation object; if the attribute is not a basic attribute and there are other methods besides the get method and the set method, the attribute is a preset type of attribute, and the attribute is determined to be an identification object.
Optionally, an implementation manner of this step may be: determining a target method contained in an executable code of a target test object by adopting an abstract syntax tree algorithm AST; and determining a target simulation object contained in the executable code of the target test object by adopting an abstract syntax tree algorithm AST or a reflection algorithm.
Specifically, a target method and attributes contained in an executable code of the target test object can be determined by a method of analyzing a syntax tree of the target test object, and a target simulation object is determined according to the attributes; or the target method contained in the executable code of the target test object can be determined by a method of analyzing the syntax tree of the target test object, the attribute contained in the executable code of the target test object is obtained by a reflection algorithm, and the target simulation object is determined according to the attribute.
Optionally, the target method may also be determined in a manual input manner, and the specific implementation manner of the target determining method and the target simulation object is not specifically limited in this embodiment.
Step S103, a first mapping relation table between each parameter value and each method in all types of unit test codes which are constructed in advance and a second mapping relation table between each simulation object to be called and each simulation object to be called calling method and parameter value are obtained.
The first mapping relation table refers to a data table containing mapping relations between parameter values and methods, and the first mapping relation table may further include information such as expected return values corresponding to the parameter values in the methods, serial numbers of the parameter values, test objects corresponding to the parameter values, and the like. The second mapping relation table is a data table containing mapping relations between the simulation object and the simulation object calling method (i.e. the return value method) and the parameter value to be called, and the second mapping relation table may further include information such as the type of the simulation object and the method for calling the simulation object. The simulation object to be called means that the target method needs to call the simulation object.
For example, the first mapping relationship table and the second mapping relationship table may be stored in a database, and the first mapping relationship table and the second mapping relationship table are obtained by looking up the identifier of the first mapping relationship table and the identifier of the second mapping relationship table in the database.
And step S104, determining a target parameter value having a mapping relation with the target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table.
Specifically, after the target method is determined and the first mapping relation table is obtained, the target method may be searched in the first mapping relation table to determine a target parameter value having a mapping relation with the target method; after the target parameter value is determined and the second mapping relation table is obtained, the target parameter value can be searched in the second mapping relation table to determine the simulation object to be called and the calling method thereof, wherein the simulation object has the mapping relation with the target parameter value.
In the embodiment of the present application, the manner of searching for the target parameter value in the second mapping relationship table may be to search for a serial number of the target parameter value, or may also be to search for a parameter name and a parameter value corresponding to the target parameter value.
And S105, defining a constructor, and instantiating a calling method of the simulation object to be called and a target simulation object.
Specifically, a rule base may be preset, and the rule base may include a rule defining a constructor and a rule for instantiating a target simulation. A preset rule base can be obtained, and a constructor is defined according to a rule for defining the constructor in the rule base; instantiating the target simulation object according to the instantiation target simulation rule in the rule base; and instantiating a simulation object calling method to be called. Wherein instantiating the rule for the target simulation includes declaring the rule for the target simulation.
For example, one implementation manner for instantiating the to-be-called simulation object calling method may be: and configuring the name of the simulation object to be called to the corresponding position of the simulation object calling method to be called so as to complete the instantiation of the simulation object calling method to be called.
And S106, configuring the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file to generate a target unit test code.
The calling code corresponding to the target method refers to a code for calling the target method, the preset rule base may include a rule for generating the calling code corresponding to the target method, the calling code corresponding to the target method may be generated according to the rule in the rule base, and the calling code corresponding to the target method is configured to a preset area in the unit test file. The embodiment of the present application does not specifically limit the implementation manner of determining the calling code corresponding to the target method.
Specifically, the preset area may include a target parameter value, a constructor, a call code corresponding to a target method, a code corresponding to an instantiated call simulation object call method, and a corresponding area of an instantiated target simulation object, and the target parameter value, the constructor, the call code corresponding to the target method, the code corresponding to the instantiated call simulation object call method, and the instantiated target simulation object are configured in the preset area in the unit test file, respectively.
Optionally, the preset area may include: the system comprises a target simulation object sounding area, a test code construction area and a test area; the test zone includes: a target parameter area, a calling declaration area for calling a simulation object and a test code calling area; configuring target parameter values, a construction function, a calling code corresponding to a target method, a code corresponding to an instantiated calling simulation object calling method and an instantiated target simulation object to a preset area in a unit test file, wherein the method comprises the following steps:
configuring the instantiated target simulation object to a target simulation object declaration area; configuring a constructor to a test code constructing area; configuring a target parameter value to a target parameter area; configuring codes corresponding to the instantiated simulation object calling method to a simulation object calling declaration area to be called; and configuring the calling code corresponding to the target method to a test code calling area.
The test area may include one or more target parameter areas corresponding to target methods, a simulation object calling declaration area to be called, and a test code calling area, where one target method corresponds to one target parameter area, one simulation object calling declaration area to be called, and one test code calling area.
Exemplarily, fig. 3 is a schematic diagram of a component structure of a unit test file provided in the present application, and as shown in fig. 3, the preset area includes: a target simulation object declaration area, a test code construction area and a test area; the test zone includes: a target parameter area, a calling declaration area for calling a simulation object and a test code calling area; the preset area further comprises: a unit test name area, the test area further comprising: the method tests the name field. And configuring the unit test name corresponding to the target test object to the unit test name area, and configuring the method test name corresponding to the target method to the method test name area. The method for determining the unit test name and the method test name in the embodiment of the present application is not specifically limited, for example, the rule base may include a rule for generating the unit test name and a rule for generating the method test name, the unit test name may be determined according to the rule for generating the unit test name in the rule base, and the method test name may be determined according to the rule for generating the method test name in the rule base, where the rule for generating the method test name may be test target method name + sequential number.
In the embodiment of the application, a target test object is obtained; determining a target method and a target simulation object contained in executable codes of a target test object; the target simulation object is a preset type of attribute; acquiring a first mapping relation table between each parameter value and each method in all types of unit test codes which are constructed in advance, a calling method of each simulation object to be called and each simulation object to be called, and a second mapping relation table between parameter values and parameter values; determining a target parameter value having a mapping relation with a target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table; defining a constructor, and instantiating a calling method of a simulation object to be called and a target simulation object; and configuring the target parameter value, the construction function, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file to generate a target unit test code. According to the first mapping relation table, a target parameter value having a mapping relation with a target method can be determined, according to the second mapping relation table, a simulation object calling method needing to be called and having a mapping relation with the target parameter value can be determined, and automation of generating a target unit test code can be achieved by automatically determining the parameter value corresponding to the target method and the simulation object calling method needing to be called, so that a method for writing the unit test code is simplified, and time consumed by writing the unit test code is reduced.
Example two
Fig. 4 is a flowchart of a method for generating a unit test code according to a second embodiment of the present application, where on the basis of the second embodiment, in this embodiment, step S106 configures a target parameter value, a constructor, a call code corresponding to a target method, a code corresponding to an instantiated simulation object call method to be called, and a target simulation object after instantiation to a preset area in a unit test file to generate a target unit test code, and then the method further includes:
the following steps are executed in a loop until the path coverage is greater than or equal to the preset path coverage threshold, as shown in fig. 4, the specific steps are as follows:
step S201, a jacoco plug-in is adopted to generate a test report for the target unit test code, and the test report comprises the path coverage rate of the target unit test code.
In the embodiment of the application, a Jacoco plug-in can be adopted, and a test report is generated in a mode of executing an mvn verify command, wherein Jacoco is an open-source test coverage rate tool. In addition, other external commands may be executed to generate the test report, and a specific implementation manner of generating the test report in this embodiment is not limited.
In this embodiment of the present application, after the test report is generated, the path coverage of the target unit test code in the test report is compared with a preset path coverage threshold, if the path coverage is greater than the preset path coverage threshold, the process is ended, and if the path coverage is less than the preset path coverage threshold, step S302 is executed to increase the parameter value of each parameter in the first mapping relation table.
Step S202, if the path coverage rate is determined to be smaller than the preset path coverage rate threshold value, increasing parameter values of all parameters in the first mapping relation table to obtain an adjusted first mapping relation table; and adjusting the second mapping relation table according to the increased parameter values to obtain an adjusted second mapping relation table.
Specifically, the parameter values of the parameters are added to the first mapping relation table, and the information such as the expected return values corresponding to the added parameter values, the serial numbers of the parameter values, the test objects corresponding to the parameter values, and the like can be updated to obtain the adjusted first mapping relation table; and adding the added parameter values and the corresponding second mapping relation of the simulation object to be called in the second mapping relation table to obtain an adjusted second mapping relation table.
And step S203, regenerating the target unit test code according to the adjusted first mapping relation table and the second mapping relation table.
In the embodiment of the application, after the target unit test code is regenerated, step S301 is executed to generate a test report for the target unit test code by using a jacoco plug-in, and the path coverage of the target unit test code in the test report is compared with a preset path coverage threshold.
It should be understood that the path coverage refers to covering all possible paths to be executed, and the path coverage refers to the rate of executing all possible paths in the program after the tested program is run during the test. The unit test based on the safety function usually requires that the path coverage rate reaches a certain requirement, and the path coverage rate is the strongest coverage rate, so the higher the path coverage rate is, the more the robustness of the tested program can be ensured.
In the embodiment of the application, the path coverage rate of the target unit test code can be increased by increasing the parameter value in the first mapping relation table. By circularly increasing the parameter values to generate the target unit test codes and comparing the path coverage rate of the target unit test codes with the preset path coverage rate threshold, the target unit test codes with the path coverage rate greater than the preset path coverage rate threshold can be finally obtained, so that the target unit test codes meet the requirement of the path coverage rate, and the robustness of the target test object can be further ensured.
EXAMPLE III
On the basis of the foregoing embodiment, the embodiment relates to a specific implementation manner of generating a first mapping relationship table and a second mapping relationship table before acquiring, in step S103, a first mapping relationship table between each parameter value and method in all types of unit test codes that are constructed in advance, and a second mapping relationship table between each simulation object to be called and the simulation object to be called, and between each parameter value and each simulation object to be called.
Fig. 5 is a flowchart of a unit test code generation method provided in the third embodiment of the present application, and as shown in fig. 5, the step of constructing the first mapping relationship table includes:
step S301, executable codes of test objects corresponding to all types of unit test codes are obtained.
In the embodiment of the present application, an implementation manner of obtaining the executable code of the test object is similar to the implementation manner of obtaining the target test object in the first embodiment, and details are not repeated here.
Step S302, determining the method to be mapped and all simulation objects contained in the executable codes of the test objects corresponding to the unit test codes by adopting an abstract syntax tree algorithm AST.
The method to be mapped refers to all methods contained in the executable code of the test object.
In the embodiment of the present application, the implementation manners of determining the to-be-mapped method and all simulation objects included in the executable code of the test object are similar to those in the first embodiment, and are not described in detail herein.
Step S303, analyzing each to-be-mapped method, if a certain to-be-mapped method is determined to contain parameters, assigning the parameters, constructing a first mapping relation between each assigned parameter value and the to-be-mapped method, and generating a first mapping relation table according to the first mapping relation.
Specifically, an abstract syntax tree algorithm AST may be used to analyze each to-be-mapped method, if it is determined that a certain to-be-mapped method contains a parameter, the parameter is assigned, and a record containing the to-be-mapped method and the assigned parameter value is automatically generated, so as to construct a first mapping relationship between the to-be-mapped method and the assigned parameter value.
In this embodiment of the present application, the generated first mapping relation table includes multiple records, where one record corresponds to one parameter value, and the record may further include information about an input expected return value, a serial number of the parameter value, and a test object corresponding to the parameter value.
As shown in fig. 5, the step of constructing the second mapping relationship table includes:
step S304, analyzing the method to be mapped containing the parameters, and if determining that the simulation objects need to be called in the method to be mapped, determining the type of each simulation object need to be called according to the context information of each simulation object need to be called in the corresponding method to be mapped.
Specifically, the type of each simulation object to be called may be a parameter return value, a parameter-free return value, or an exception. If the parameter return value exists in the method to be mapped corresponding to the simulation object to be called according to the context information of the simulation object to be called in the method to be mapped, determining the type of the simulation object to be called as the parameter return value, and determining the parameter and the parameter value which have a second mapping relation with the simulation object to be called; if the parameter-free return value in the corresponding method to be mapped of the simulation object to be called is determined according to the context information of the simulation object to be called in the corresponding method to be mapped, the type of the simulation object to be called is determined to be the parameter-free return value; and if the returned value in the method to be mapped corresponding to the simulation object to be called is determined to be abnormal according to the context information of the simulation object to be called in the method to be mapped, determining the type of the simulation object to be called to be abnormal.
For example, the method for determining the parameter having the second mapping relationship with the simulation object to be called may be: and determining parameters in the method to be mapped corresponding to the simulation object to be called through the first mapping relation table, and determining parameters having a second mapping relation with the simulation object to be called from the parameters in the method to be mapped corresponding to the simulation object to be called. For example, parameters in the method to be mapped corresponding to the simulation object to be called are displayed, and parameters having a second mapping relation with the simulation object to be called are determined through selection operation of a developer.
And S305, determining the calling method of each simulation object to be called according to the type of each simulation object to be called.
Specifically, the rule base further includes a rule for generating a calling method corresponding to the type of the simulation object such as a return value, no parameter, an exception, and the like, and the calling method of each simulation object to be called can be determined according to the rule base and the type of each simulation object to be called.
And S306, constructing a second mapping relation table according to the simulation objects to be called, the simulation object calling methods to be called and the corresponding parameter values.
Specifically, the method to be mapped, the simulation object to be called, the simulation object calling method to be called, and the parameter value having the second mapping relationship with the simulation object to be called are recorded in the second mapping relationship table. In addition, if the type of the simulation object to be called has a parameter return value, the parameter return value information corresponding to the parameter value of the simulation object to be called with the second mapping relation is recorded, one parameter return value information corresponds to one record, and the parameter return value information can be input by a developer. And recording information such as the type of the simulation object to be called, the serial number of the simulation object to be called, the test object corresponding to the simulation object to be called and the like in the second mapping relation table.
The method for recording the parameter value having the second mapping relation with the simulation object to be called may be to record the serial number of the parameter value in the first mapping relation table.
In the embodiment of the application, the method to be mapped is analyzed by adopting an abstract syntax tree algorithm AST, and a parameter with a first mapping relation with the method to be mapped can be identified so as to construct a first mapping relation table; and identifying a simulation object to be called with a second mapping relation with the method to be mapped, determining the type of the simulation object to be called, and determining the simulation object calling method to be called according to the rules in the rule base so as to construct a second mapping relation table. After the first mapping relationship table and the second mapping relationship table are constructed, the target unit test code may be generated according to the first mapping relationship table and the second mapping relationship table.
Optionally, on the basis of the foregoing embodiment, after configuring, in step S106, the target parameter value, the constructor, the code corresponding to the instantiated simulation object calling method to be called, and the instantiated target simulation object in a preset area in the unit test file, so as to generate the target unit test code, the method further includes: and testing the target test object by adopting the target unit test code.
In particular, target unit test code may be run to complete testing of the target test object.
In the embodiment of the application, the target test object is tested by adopting the target unit test code, so that whether the target test object has errors or not can be checked, developers can improve the target test object, and the robustness of the target test object can be ensured.
Example four
Fig. 6 is a schematic structural diagram of a unit test code generation apparatus according to a fourth embodiment of the present application. The unit test code generation apparatus provided in the embodiment of the present application may execute the processing procedure provided in the embodiment of the unit test code generation method. As shown in fig. 6, the unit test code generating apparatus 60 includes: an object acquisition module 601, a determination module 602, a relationship table acquisition module 603, an instantiation module 604, and a configuration module 605.
Specifically, the object obtaining module 601 is configured to obtain a target test object; the target test object comprises executable code of the target test object;
a determining module 602, configured to determine a target method and a target simulation object included in the executable code of the target test object; the target simulation object is a preset type of attribute;
a relation table obtaining module 603, configured to obtain a first mapping relation table between each parameter value and method in all pre-constructed unit test codes of all types, and a second mapping relation table between each simulation object to be called and each simulation object to be called, and between each parameter value and each simulation object to be called;
the determining module 602 is further configured to determine, according to the first mapping relation table, a target parameter value having a mapping relation with a target method, and determine, according to the second mapping relation table, a simulation object calling method to be called, which has a mapping relation with the target parameter value;
an instantiation module 604, configured to define a constructor and instantiate a simulation object calling method to be called and a target simulation object;
the configuration module 605 is configured to configure the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated simulation object calling method to be called, and the instantiated target simulation object to a preset area in the unit test file, so as to generate the target unit test code.
The apparatus provided in the embodiment of the present application may be specifically configured to execute the method embodiment provided in the first embodiment, and specific functions are not described herein again.
Optionally, the determining module 602 is specifically configured to determine, by using an abstract syntax tree algorithm AST, a target method included in the executable code of the target test object; and determining a target simulation object contained in the executable code of the target test object by adopting an abstract syntax tree algorithm AST or a reflection algorithm.
Optionally, the preset area includes: a target simulation object declaration area, a test code construction area and a test area; the test zone includes: a target parameter area, a calling declaration area for calling a simulation object and a test code calling area; a configuration module 605, configured to configure the instantiated target simulation object to a target simulation object declaration area; configuring a constructor to a test code constructing area; configuring a target parameter value to a target parameter area; configuring codes corresponding to the instantiated simulation object calling method to a simulation object calling declaration area to be called; and configuring the calling code corresponding to the target method to a test code calling area.
Optionally, the unit test code generating apparatus 60 further includes an adjusting module, and after the configuring module 605 configures the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated simulation object calling method to be called, and the instantiated target simulation object into the preset area in the unit test file to generate the target unit test code, the adjusting module is configured to circularly execute the following steps until the path coverage rate is greater than or equal to the preset path coverage rate threshold value: generating a test report for the target unit test code by adopting a jacoco plug-in, wherein the test report comprises the path coverage rate of the target unit test code; if the path coverage rate is determined to be smaller than the preset path coverage rate threshold value, increasing parameter values of all parameters in the first mapping relation table to obtain an adjusted first mapping relation table; adjusting the second mapping relation table according to the increased parameter values to obtain an adjusted second mapping relation table; and regenerating the target unit test code according to the adjusted first mapping relation table and the second mapping relation table, and executing to generate a test report on the target unit test code by adopting a jacoco plug-in.
Optionally, the unit test code generating apparatus 60 further includes a generating module, before the relationship table obtaining module 603 obtains a first mapping relationship table between each parameter value and method in all types of unit test codes, and a second mapping relationship table between each simulation object to be called and the simulation object to be called to call the method, and between the parameter values and the method, which are constructed in advance, the generating module is configured to obtain executable codes of test objects corresponding to all types of unit test codes; determining a mapping method and all simulation objects contained in the executable codes of the test objects corresponding to the test codes of each unit by adopting an abstract syntax tree algorithm AST; analyzing each method to be mapped, if determining that a certain method to be mapped contains parameters, assigning the parameters, constructing a first mapping relation between each assigned parameter value and the method to be mapped, and generating a first mapping relation table according to the first mapping relation.
Optionally, the generation module is further configured to analyze each method to be mapped, and if it is determined that a certain method to be mapped contains a simulation object to be called, determine the type of each simulation object to be called according to context information of each simulation object to be called in the corresponding method to be mapped; determining a calling method of each simulation object to be called according to the type of each simulation object to be called; and constructing a second mapping relation table according to each simulation object to be called, each simulation object calling method to be called and the corresponding parameter value.
Optionally, the unit test code generating apparatus 60 further includes a test module, and after the configuration module 605 configures the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated simulation object calling method to be called, and the instantiated target simulation object into a preset area in the unit test file to generate the target unit test code, the test module is configured to test the target test object by using the target unit test code.
The apparatus provided in the embodiment of the present application may be specifically configured to execute the method embodiment, and specific functions are not described herein again.
EXAMPLE five
Fig. 7 is a schematic structural diagram of an electronic device according to a fifth embodiment of the present application. As shown in fig. 7, the electronic apparatus 70 includes: a processor 701, a memory 702, a transceiver 703, and computer executable instructions stored on the memory 702 and operable on the processor 701.
Wherein, the processor 701, the memory 702 and the transceiver 703 are interconnected in circuit; the transceiver 703 is used for transceiving data; the processor 701, when executing the computer-executable instructions, implements the unit test code generation method provided by any of the above-described method embodiments.
The embodiment of the present application further provides a computer-readable storage medium, where computer-executable instructions are stored in the computer-readable storage medium, and when the computer-executable instructions are executed by a processor, the method for generating the unit test code provided in any of the above method embodiments is implemented.
An embodiment of the present application further provides a computer program product, where the program product includes: the computer executes the instructions, the computer executes the instructions to be stored in the readable storage medium, and the at least one processor of the electronic device can read the computer executed instructions from the readable storage medium, and the at least one processor executes the computer executed instructions to enable the electronic device to execute the method provided by any one of the method embodiments.
It is obvious to those skilled in the art that, for convenience and simplicity of description, the foregoing division of the functional modules is merely used as an example, and in practical applications, the above function distribution may be performed by different functional modules according to needs, that is, the internal structure of the device is divided into different functional modules to perform all or part of the above described functions. For the specific working process of the device described above, reference may be made to the corresponding process in the foregoing method embodiment, which is not described herein again.
Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the application being indicated by the following claims.
It will be understood that the present application is not limited to the precise arrangements described above and shown in the drawings and that various modifications and changes may be made without departing from the scope thereof. The scope of the application is limited only by the appended claims.

Claims (10)

1. A method for generating unit test code, comprising:
acquiring a target test object; the target test object comprises executable code of the target test object;
determining a target method and a target simulation object contained in executable code of the target test object; the target simulation object is of a preset type;
acquiring a first mapping relation table between parameter values and methods in all types of unit test codes which are constructed in advance, and a second mapping relation table between simulation objects to be called and simulation object calling methods to be called and between parameter values and parameters;
determining a target parameter value having a mapping relation with the target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table;
defining a constructor and instantiating the simulation object calling method to be called and the target simulation object;
and configuring the target parameter value, the construction function, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file to generate a target unit test code.
2. The method of claim 1, wherein determining the target method and target simulation object contained in the executable code of the target test object comprises:
adopting an abstract syntax tree algorithm AST to determine a target method contained in the executable code of the target test object;
determining a target simulation object included in executable code of the target test object using an abstract syntax tree algorithm AST or a reflection algorithm.
3. The method of claim 1, wherein the preset area comprises: a target simulation object declaration area, a test code construction area and a test area; the test zone includes: a target parameter area, a calling declaration area for calling a simulation object and a test code calling area;
the step of configuring the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated simulation object calling method to be called and the instantiated target simulation object to a preset area in a unit test file includes:
configuring the instantiated target simulation object to the target simulation object declaration area;
configuring the constructor to a test code constructing area;
configuring the target parameter value to a target parameter area;
configuring the code corresponding to the instantiated simulation object calling method to a simulation object calling declaration area to be called;
and configuring the calling code corresponding to the target method to a test code calling area.
4. The method according to any one of claims 1 to 3, wherein after the step of configuring the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method, and the instantiated target simulation object to a preset area in a unit test file to generate the target unit test code, the method further comprises:
circularly executing the following steps until the path coverage rate is greater than or equal to a preset path coverage rate threshold value:
generating a test report for the target unit test code by adopting a jacoco plug-in, wherein the test report comprises the path coverage rate of the target unit test code;
if the path coverage rate is determined to be smaller than a preset path coverage rate threshold value, increasing parameter values of all parameters in the first mapping relation table to obtain an adjusted first mapping relation table; adjusting the second mapping relation table according to the increased parameter values to obtain an adjusted second mapping relation table;
and regenerating a target unit test code according to the adjusted first mapping relation table and the second mapping relation table, and executing the jacoco plug-in to generate a test report for the target unit test code.
5. The method according to any one of claims 1 to 3, wherein before obtaining the first mapping relationship table between each parameter value and method in the pre-constructed unit test code of all types and the second mapping relationship table between each simulation object to be called and the parameter value, the method further comprises:
acquiring executable codes of test objects corresponding to all types of unit test codes;
determining a mapping method and all simulation objects contained in the executable codes of the test objects corresponding to the unit test codes by adopting an abstract syntax tree algorithm AST;
analyzing the methods to be mapped, if determining that a certain method to be mapped contains parameters, assigning the parameters, constructing a first mapping relation between the assigned parameter values and the methods to be mapped, and generating a first mapping relation table according to the first mapping relation.
6. The method of claim 5, wherein after generating the first mapping table according to the first mapping, further comprising:
analyzing each method to be mapped, and if determining that a certain method to be mapped contains a simulation object to be called, determining the type of each simulation object to be called according to the context information of each simulation object to be called in the corresponding method to be mapped;
determining a calling method of each simulation object to be called according to the type of each simulation object to be called;
and constructing a second mapping relation table according to the simulation objects to be called and the simulation object calling methods to be called.
7. The method according to any one of claims 1 to 3, wherein after the step of configuring the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated calling method of the simulation object to be called, and the instantiated target simulation object to a preset area in the unit test file to generate the target unit test code, the method further comprises the steps of:
and testing the target test object by adopting the target unit test code.
8. A unit test code generating apparatus, comprising:
the object acquisition module is used for acquiring a target test object; the target test object comprises executable code of the target test object;
a determination module for determining a target method and a target simulation object contained in an executable code of the target test object; the target simulation object is of a preset type;
the system comprises a relation table acquisition module, a relation table acquisition module and a simulation model execution module, wherein the relation table acquisition module is used for acquiring a first mapping relation table between each parameter value and each method in all types of unit test codes which are constructed in advance, and a second mapping relation table between each simulation object to be called and each simulation object to be called calling method and between each parameter value and each method;
the determining module is further used for determining a target parameter value having a mapping relation with the target method according to the first mapping relation table, and determining a simulation object calling method to be called having a mapping relation with the target parameter value according to the second mapping relation table;
the instantiation module is used for defining a constructor and instantiating the calling method of the simulation object to be called and the target simulation object;
and the configuration module is used for configuring the target parameter value, the constructor, the calling code corresponding to the target method, the code corresponding to the instantiated calling simulation object calling method and the instantiated target simulation object to a preset area in a unit test file so as to generate a target unit test code.
9. An electronic device, comprising: a processor, and a memory and transceiver communicatively coupled to the processor;
the processor, the memory and the transceiver are electrically interconnected;
the memory stores computer-executable instructions; the transceiver is used for transceiving data;
the processor executes computer-executable instructions stored by the memory to implement the method of any of claims 1-7.
10. A computer-readable storage medium having computer-executable instructions stored thereon, which when executed by a processor, perform the method of any one of claims 1-7.
CN202111585215.0A 2021-12-22 2021-12-22 Unit test code generation method, device, equipment and medium Active CN114265776B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111585215.0A CN114265776B (en) 2021-12-22 2021-12-22 Unit test code generation method, device, equipment and medium

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111585215.0A CN114265776B (en) 2021-12-22 2021-12-22 Unit test code generation method, device, equipment and medium

Publications (2)

Publication Number Publication Date
CN114265776A CN114265776A (en) 2022-04-01
CN114265776B true CN114265776B (en) 2023-03-14

Family

ID=80829222

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111585215.0A Active CN114265776B (en) 2021-12-22 2021-12-22 Unit test code generation method, device, equipment and medium

Country Status (1)

Country Link
CN (1) CN114265776B (en)

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102591777A (en) * 2012-01-10 2012-07-18 深圳市同洲视讯传媒有限公司 Unit test code generation method and device
CN102855177A (en) * 2011-06-30 2013-01-02 阿里巴巴集团控股有限公司 Unit test method and device
CN107992425A (en) * 2017-12-25 2018-05-04 携程旅游网络技术(上海)有限公司 The automatic generation method and system of unit testing Mock codes
CN113051163A (en) * 2021-03-24 2021-06-29 中国工商银行股份有限公司 Unit testing method, unit testing device, electronic equipment and storage medium
CN113448874A (en) * 2021-07-26 2021-09-28 中国工商银行股份有限公司 Method and device for generating unit test script

Family Cites Families (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US7797687B2 (en) * 2005-08-04 2010-09-14 Microsoft Corporation Parameterized unit tests with behavioral purity axioms
CN100547562C (en) * 2006-10-18 2009-10-07 国际商业机器公司 The method and system of the unit testing use-case of problem when generation can be reproduced operation automatically
CN101110055B (en) * 2007-08-31 2011-07-13 中兴通讯股份有限公司 Device for implementing general use pile function in unit test and implementing method thereof
CA2777434C (en) * 2012-05-18 2019-09-10 Ibm Canada Limited - Ibm Canada Limitee Verifying application security vulnerabilities
CN111221721B (en) * 2018-11-27 2023-09-05 中国移动通信集团上海有限公司 Automatic recording and executing method and device for unit test cases
CN113778871A (en) * 2021-09-07 2021-12-10 未鲲(上海)科技服务有限公司 Mock testing method, device, equipment and storage medium

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN102855177A (en) * 2011-06-30 2013-01-02 阿里巴巴集团控股有限公司 Unit test method and device
CN102591777A (en) * 2012-01-10 2012-07-18 深圳市同洲视讯传媒有限公司 Unit test code generation method and device
CN107992425A (en) * 2017-12-25 2018-05-04 携程旅游网络技术(上海)有限公司 The automatic generation method and system of unit testing Mock codes
CN113051163A (en) * 2021-03-24 2021-06-29 中国工商银行股份有限公司 Unit testing method, unit testing device, electronic equipment and storage medium
CN113448874A (en) * 2021-07-26 2021-09-28 中国工商银行股份有限公司 Method and device for generating unit test script

Non-Patent Citations (4)

* Cited by examiner, † Cited by third party
Title
"Automated isolation for white-box test generation";Dávid Honfi等;《Information and Software Technology》;20200512;第125卷;第1-16页 *
"使用Junit + Mockito实践单元测试";JMCui;《https://www.cnblogs.com/jmcui/p/12802099.html》;20200429;第1-12页 *
"方面单元测试框架的设计与实现";陈凌鹤 等;《计算机工程与应用》;20090101;第45卷(第01期);第80-85+95页 *
"自动化单元测试框架NUnit的改进设计与实现";王桐;《电脑与信息技术》;20130415;第21卷(第02期);第36-41页 *

Also Published As

Publication number Publication date
CN114265776A (en) 2022-04-01

Similar Documents

Publication Publication Date Title
US9524226B2 (en) System and method for display of software quality
US6697961B1 (en) Method and system for describing predicates in disjuncts in procedures for test coverage estimation
US7681180B2 (en) Parameterized test driven development
US8561021B2 (en) Test code qualitative evaluation
Binkley The application of program slicing to regression testing
US6067639A (en) Method for integrating automated software testing with software development
US7596778B2 (en) Method and system for automatic error prevention for computer software
EP2204738A2 (en) Method and system for performing software verification
US10437702B2 (en) Data-augmented software diagnosis method and a diagnoser therefor
US7500149B2 (en) Generating finite state machines for software systems with asynchronous callbacks
US7644394B2 (en) Object-oriented creation breakpoints
US20060253739A1 (en) Method and apparatus for performing unit testing of software modules with use of directed automated random testing
US9983977B2 (en) Apparatus and method for testing computer program implementation against a design model
CN111651366A (en) SDK test method, device, equipment and storage medium
US9058427B2 (en) Iterative generation of symbolic test drivers for object-oriented languages
US10846206B2 (en) Adaptive software testing
CN113742215A (en) Method and system for automatically configuring and calling test tool to perform test analysis
CN114265776B (en) Unit test code generation method, device, equipment and medium
KR102603330B1 (en) Apparatus and method for display the test coverage
US11442845B2 (en) Systems and methods for automatic test generation
Wehrmeister et al. Support for early verification of embedded real-time systems through UML models simulation
Alvaro et al. A software component maturity model (SCMM)
CN113220586A (en) Automatic interface pressure test execution method, device and system
CN112988565B (en) Interface automatic test method, device, computer equipment and storage medium
Sandhu Comparison of Fault Simulation Over Custom Kernel Module Using Various Techniques

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant