CN114185801A - Method and device for generating Mock test script based on unit test - Google Patents

Method and device for generating Mock test script based on unit test Download PDF

Info

Publication number
CN114185801A
CN114185801A CN202111541142.5A CN202111541142A CN114185801A CN 114185801 A CN114185801 A CN 114185801A CN 202111541142 A CN202111541142 A CN 202111541142A CN 114185801 A CN114185801 A CN 114185801A
Authority
CN
China
Prior art keywords
mock
generating
test script
frame
source code
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.)
Pending
Application number
CN202111541142.5A
Other languages
Chinese (zh)
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.)
Industrial and Commercial Bank of China Ltd ICBC
Original Assignee
Industrial and Commercial Bank of China Ltd ICBC
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 Industrial and Commercial Bank of China Ltd ICBC filed Critical Industrial and Commercial Bank of China Ltd ICBC
Priority to CN202111541142.5A priority Critical patent/CN114185801A/en
Publication of CN114185801A publication Critical patent/CN114185801A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3688Test management for test execution, e.g. scheduling of test suites
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases

Abstract

The invention provides a method and a device for generating a Mock test script based on unit test, wherein the method for generating the Mock test script based on the unit test comprises the following steps: receiving a source code and a Mock frame corresponding to a Mock test script to be generated; analyzing the source code to obtain a second layer method; and generating the Mock test script according to the second layer method and the Mock framework. The method and the device for generating the Mock test script based on the unit test can automatically generate the Mock script, improve the coverage rate of the test method, reduce the unit test pressure of developers and improve the quality of the unit test script.

Description

Method and device for generating Mock test script based on unit test
Technical Field
The invention relates to the technical field of computer information processing, in particular to the technical field related to program code testing, and specifically relates to a method and a device for generating a Mock test script based on unit testing.
Background
In the prior art, developers generally write test scripts by hand when performing unit tests, that is, first, a second layer method in a tested class is found manually, and then a Mock script is generated by using one of Mock frameworks (EasyMock, Mockito, PowerMock, and the like).
Specifically, the following steps can be taken for a developer to perform an interface Mock test by using EasyMock:
1. jar is introduced;
2. generating a Mock object by using Easymock;
3. recording expected behaviors and output of the Mock object;
4. switching the Mock object to a playing state;
5. calling a Mock object method to perform unit testing;
6. and verifying the behavior of the Mock object.
For the Mockito framework, the usage steps are similar to EasyMock, but this time step 3 is to define (non-record) the expected behavior and output of the Mock object (expectelations part), and step 4 is not needed. The PowerMock framework can realize the simulation of static functions, constructors, private functions, final functions and system functions on the basis of Mock frameworks such as Mockito and the like. The above method has the following disadvantages:
on the one hand, the existing unit test Mock script generation mode finds out the second layer method in the tested class by manpower, then calls the Mock frame to carry out Mock, thereby wasting time and labor, and having the risks that the second layer method is not completely covered, is easy to miss and the like.
On the other hand, the EasyMock framework and the Mockito framework are mainly used for interface testing and can meet most requirements in unit testing, but because the dynamic proxy uses inheritance and polymorphic characteristics facing to objects, the dynamic proxy of JDK (Java Development Kit-software Development Kit) only acts on the interface, and essentially generates an implementation class for the interface, while CGLIB (Code Generation Library-Code Generation) can act on the class and essentially takes the class itself as the base class. Therefore, neither of these two frameworks can implement simulation of static functions, constructors, proprietary functions, final functions, and system functions, but these methods are essential functions in large systems. Therefore, when the user selects the Mock frame, the user needs to know the characteristics of each Mock frame, the learning cost is high, and the unit testing pressure of developers is increased.
Disclosure of Invention
Aiming at the problems in the prior art, the method and the device for generating the Mock test script based on the unit test can automatically generate the Mock script, improve the coverage rate of the test method, reduce the unit test pressure of developers and improve the quality of the unit test script.
In a first aspect, the present invention provides a method for generating a Mock test script based on a unit test, including:
receiving a source code and a Mock frame corresponding to a Mock test script to be generated;
analyzing the source code to obtain a second layer method;
and generating the Mock test script according to the second layer method and the Mock framework.
In an embodiment, the analyzing the source code to obtain the second layer method includes:
converting the source code into a Statement object;
parsing the State object to generate a syntax tree;
determining the second-level method from the syntax tree.
In one embodiment, the determining the second layer method according to the syntax tree includes:
determining a node type in the syntax tree;
determining the second tier method based on the node type.
In one embodiment, the Mock frame is a Mock frame, and the second layer method includes: a member method; the generating the Mock test script according to the second layer method and the Mock frame comprises the following steps:
and generating the Mock test script according to the member method.
In one embodiment, the Mock frame is a Mock frame and a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; the generating the Mock test script according to the second layer method and the Mock frame comprises the following steps:
and generating the Mock test script according to the member method, the static function, the constructor, the private function, the Final function and the system function.
In one embodiment, the Mock frame is a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; the generating the Mock test script according to the second layer method and the Mock frame comprises the following steps:
and generating the Mock test script according to the member method, the static function, the constructor, the private function, the Final function and the system function.
In a second aspect, the present invention provides an apparatus for generating a Mock test script based on unit test, the apparatus comprising:
the source code receiving module is used for receiving source codes corresponding to the Mock test scripts to be generated and a Mock frame;
the source code analysis module is used for analyzing the source codes to obtain a second layer method;
and the test script generating module is used for generating the Mock test script according to the second layer method and the Mock frame.
In one embodiment, the source code parsing module includes:
the source code conversion unit is used for converting the source code into a State element object;
the syntax tree generating unit is used for analyzing the Statement object to generate a syntax tree;
a method determination unit for determining the second layer method according to the syntax tree.
In one embodiment, the method determination unit comprises:
a node type determining unit, configured to determine a node type in the syntax tree;
a method determination subunit to determine the second-tier method according to the node type.
In one embodiment, the Mock frame is a Mock frame, and the second layer method includes: a member method; the test script generation module is specifically used for generating the Mock test script according to the member method.
In one embodiment, the Mock frame is a Mock frame and a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; the test script generation module is specifically used for generating the Mock test script according to the member method, the static function, the constructor function, the private function, the Final function and the system function.
In one embodiment, the Mock frame is a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; the test script generation module is specifically used for generating the Mock test script according to the member method, the static function, the constructor function, the private function, the Final function and the system function.
In a fourth aspect, the present invention provides a computer-readable storage medium having stored thereon a computer program which, when executed by a processor, implements the steps of a method for generating a Mock test script based on a unit test.
As can be seen from the above description, in the method and apparatus for generating a Mock test script based on a unit test provided in the embodiments of the present invention, first, a source code and a Mock frame corresponding to a Mock test script to be generated are received; then, analyzing the source code to obtain a second layer method; and finally generating a Mock test script according to the second layer method and the Mock framework. Aiming at the pain points that the existing Mock frame generation unit test script is manually compiled or used in the prior art, time and labor are wasted, omission is easy, and the learning cost is high, the invention provides the method for automatically generating the Mock script, and the method supports the source code analysis and the Mock frame selection of developers and automatically generates the Mock script, thereby improving the coverage rate of the test method, reducing the unit test pressure of the developers and improving the quality of the unit test script.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below, and it is obvious that the drawings in the following description are some embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to these drawings without creative efforts.
FIG. 1 is a schematic flow chart of a method for generating a Mock test script based on unit testing in an embodiment of the present invention;
FIG. 2 is a flow chart illustrating step 200 according to an embodiment of the present invention;
FIG. 3 is a flowchart illustrating step 203 in an embodiment of the present invention;
FIG. 4 is a flowchart of step 300 according to an embodiment of the present invention;
FIG. 5 is another flow chart illustrating step 300 according to an embodiment of the present invention;
FIG. 6 is a flowchart illustrating a method for generating a Mock test script based on unit testing in an embodiment of the present invention;
FIG. 7 is a schematic diagram of a system for generating a Mock test script based on unit testing according to an embodiment of the present invention;
FIG. 8 is a schematic diagram illustrating an apparatus for generating a Mock test script based on unit testing in an embodiment of the present invention;
fig. 9 is a schematic diagram illustrating the composition of the source code parsing module 20 according to an embodiment of the present invention;
FIG. 10 is a schematic diagram of the composition of the method determination unit 20c in an embodiment of the present invention;
fig. 11 is a schematic structural diagram of an electronic device in an embodiment of the invention.
Detailed Description
In order to make the objects, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are some, but not all, embodiments of the present invention. All other embodiments, which can be derived by a person skilled in the art from the embodiments given herein without making any creative effort, shall fall within the protection scope of the present invention.
As will be appreciated by one skilled in the art, embodiments of the present invention may be provided as a method, system, or computer program product. Accordingly, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the present invention may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
It should be noted that the terms "comprises" and "comprising," and any variations thereof, in the description and claims of this application and the above-described drawings, are intended to cover a non-exclusive inclusion, such that a process, method, system, article, or apparatus that comprises a list of steps or elements is not necessarily limited to those steps or elements expressly listed, but may include other steps or elements not expressly listed or inherent to such process, method, article, or apparatus.
It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The present application will be described in detail below with reference to the embodiments with reference to the attached drawings.
The embodiment of the invention provides a specific implementation mode of a Mock test script generation method based on unit test, and referring to fig. 1, the method specifically comprises the following steps:
step 100: and receiving the source code and the Mock frame corresponding to the Mock test script to be generated.
Specifically, a source code to be tested provided by a user and a Mock frame selected by the user are received, and it can be understood that the user can also select a testing method and a project type, and a case design file is generated according to the information. The Mock frame comprises:
mockito framework: the open source Java test framework based on the MT protocol can verify which functions are called, and eliminates the dependence on expected behaviors.
PowerMock framework: the Mock frames such as Mockito and the like and the more powerful Mock frames are expanded. Simulation operations are implemented by using custom class loaders and bytecode operations.
Step 200: and analyzing the source code to obtain a second layer method.
It is understood that the second layer methods refer to member methods, static methods, construction methods, private methods, final methods, and system methods in the Java class. Such as: the queryUser () method of class B is called in class a, where queryUser () belongs to the second tier method in class a. For another example: a- > B, C; if the entry method a calls the second layer methods B and C, the recording process and the obtained set of valid data are as follows: entry of entry method a, entry of second tier method B, return of second tier method B, entry of second tier method C, return of second tier method C, and return of entry method a.
In the implementation of step 200, a Java server class (a tool that can parse Java source codes into a syntax tree and then analyze and modify Java codes based on the syntax tree) is used to convert the source codes of a class to be tested into a Statement object defined by the Java server class, and then a viewer class is used to find a second layer method of the class to be tested, which includes member methods, static functions, construction functions, private functions, final functions, and system functions.
Step 300: and generating the Mock test script according to the second layer method and the Mock framework.
The Mock test refers to a test method for creating a virtual object (Mock object) for testing an object which is not easy to construct (such as an http Servlet request must be constructed in a Servlet container) or an object which is not easy to acquire and is relatively complex (such as a ResultSet object in JDBC) in the test process.
In step 300, specifically, a Mock test script is generated according to the second layer method found in step 200 and the test framework pre-selected by the user in step 100 under the test framework. Specifically, if the Mockito framework is selected independently, the Mock test script can be generated only by member methods in the Mock second layer method. If the combination of the Mockito and the PowerMock frames is selected, all Mock of the second layer method can be realized, and a Mock test script is generated, wherein the use sequence of the Mockito frames is prior to that of the PowerMock. If the PowerMock frame is selected independently, all Mock of the second layer method can be realized, and a Mock test script is generated.
As can be seen from the above description, in the method for generating a Mock test script based on a unit test provided in the embodiment of the present invention, first, a source code and a Mock frame corresponding to a Mock test script to be generated are received; then, analyzing the source code to obtain a second layer method; and finally generating a Mock test script according to the second layer method and the Mock framework. Aiming at the pain points that the existing Mock frame generation unit test script is manually compiled or used in the prior art, time and labor are wasted, omission is easy, and the learning cost is high, the invention provides the method for automatically generating the Mock script, and the method supports the source code analysis and the Mock frame selection of developers and automatically generates the Mock script, thereby improving the coverage rate of the test method, reducing the unit test pressure of the developers and improving the quality of the unit test script.
In one embodiment, referring to fig. 2, step 200 comprises:
step 201: converting the source code into a Statement object;
statement is an important interface for Java to perform database operations, and is used to send SQL statements to be executed to a database on the basis that a database connection is established. A State object for executing simple SQL statements without parameters. By default, each Statement object can only open one ResultSet object at a time. Thus, if reading one ResultSet object crosses reading the other, the two objects must be generated by different state objects. If there is a current ResultSet object that is open for a Statement, all execution methods in the State element interface implicitly close it.
The State object is used to send SQL statements into the database. There are actually three Statement objects that all act as containers for executing SQL statements on a given connection: statement, PreparedStatement (which is inherited from Statement), and CallableStatement (which is inherited from PreparedStatement). They are all dedicated to sending specific types of SQL statements: the State object is used for executing a simple SQL Statement without parameters; the PreparedStatement object is used for executing a precompiled SQL statement with or without an IN parameter; the CallableStatement object is used to perform calls to stored procedures that already exist in the database.
The Statement interface provides the basic method of executing statements and obtaining results. The preparedState interface is added with a method for processing IN parameters; while CallableStatement adds a method to handle OUT parameters.
The DBMS treats each statement in the stored process as an independent statement; while others treat the entire process as a compound statement. This difference becomes very important when auto-commit is enabled, as it affects when the commit method is invoked. In the former case, each statement is submitted separately; in the latter case, all statements commit at the same time.
Step 202: parsing the State object to generate a syntax tree;
step 203: determining the second-level method from the syntax tree.
In step 201 and step 203, the source code is parsed into a syntax tree of the componential unit type (where content is the source code content of the String type) by using a componential unit java source, and then a second layer method in the source code is determined according to the syntax tree.
In one embodiment, referring to fig. 3, step 203 comprises:
step 2031: determining a node type in the syntax tree;
step 2032: determining the second tier method based on the node type.
In step 2031 and step 2032, a specific method is called according to the node type required in the syntax tree for processing, that is, the second layer method is determined. Specifically, if the development project is a Springboot project, all member variables can be acquired according to the automatic annotation; if the project is not a Springboot project, all classes and interfaces in the source code file need to be found out, and a method which does not need Mock is filtered, such as: and (4) taking the input parameter of the method as a member variable without an internal interface of the method body, and obtaining a second-layer method set according to the method characteristics, for example, judging the method with the capital letter as a static method.
In one embodiment, the Mock frame comprises: a Mockito framework, the second layer method comprising: a member method; referring to fig. 4, step 300 now includes:
step 301: and generating the Mock test script according to the member method.
In one embodiment, the Mock frame is a Mock frame and a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; referring to fig. 5, step 300 now includes:
step 302: and generating the Mock test script according to the member method, the static function, the constructor, the private function, the Final function and the system function.
In one embodiment, the Mock frame is a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; step 300 is still step 302.
In the above embodiment, it is understood that the user may select one or a combination of the Mockito framework and the PowerMock framework as the framework for generating the Mock test script. The second layer method comprises the following steps: member methods, static functions, constructors, private functions, Final functions, and system functions.
And if the user independently selects the Mockito framework, only the member method in the Mock second layer method can be used for generating the Mock test script.
When the Mock frame is a Mock frame and a Powermock frame, the Mock test script can be generated according to the member method, the static function, the constructor function, the private function, the Final function and the system function.
If the combination of the Mockito framework and the PowerMock framework is selected, the Mock tests of all second-layer methods can be realized, and a Mock test script is generated, wherein the use sequence of the Mockito framework is prior to that of the PowerMock. Of course, if the PowerMock frame is selected independently, all Mock of the second layer method can be realized, and a Mock test script is generated. And WHEN the case design file is analyzed, generating an annotation and an @ Test method by utilizing the supplementary GIVEN-WHEN-THEN information in the file, wherein the method name is generated according to a corresponding template. Such as: the GIVEN of a positive case in the case design file is listed as that a current teller has client searching authority, the WHEN is listed as that a search button is clicked after client codes are input, the THEN is listed as that client information searching is successful, and the generated annotation and @ Test method is named as follows:
/**
@ GIVEN current teller having client search authority
Clicking search button after inputting client code
Success of the search for the @ THEN user information
*/
@ Testpublic void child _ user information search success __ given _ current teller has client search authority (), last mail
// parameter preparation: customer code
}
A unit test Mock script and test case output module 4: the module realizes the encapsulation of the generated Mock script and the unit Test case in the module 3 to generate a new Test class with the class name of the original tested class name + Test.
To further explain the scheme, a specific application example of the method for generating a Mock test script based on a unit test is provided by taking a bank project as an example, and the specific application example specifically includes the following contents, and refer to fig. 6.
The invention also provides a generating system of Mock test script based on unit test, referring to fig. 7, the system includes:
the system comprises a case design file uploading module 1, a developer source code analyzing module 2, a data processing and assembling module 3 and a unit test Mock script and test case output module 4.
Case design file uploading module 1: the module realizes the functions of testing method, item type, Mock frame selection, case design file uploading and the like. The test method supports equivalence class, path methods. The project type supports SpringBoot and non-SpringBoot. The Mock frame can be one or two of Mockito and Powermock. The case design file is designed and generated by using an inline case design tool, and a user needs to supplement GIVEN-WHEN-THEN information in the file so as to be used WHEN a unit test case is automatically generated in the following process.
The developer source code analysis module 2: the module realizes the function of analyzing the second layer method call in the code by scanning the source code of the developer, and the used tool is JavaParser. Firstly, a source code of a tested class is converted into a State object defined by the JavaParser class by using the JavaParser class, and then a second-layer method of the tested class is found by using the Visator class, wherein the second-layer method comprises member methods, static functions, constructors, private functions, final functions and system functions.
Data processing and combination module 3: the module realizes the function of generating a second layer method Mock script and analyzing a case design file to generate a test case, and the Mock frame selected in the module 1 is used for performing Mock on the second layer method obtained in the module 2.
A unit test Mock script and test case output module 4: the module realizes the encapsulation of the generated Mock script and the unit Test case in the module 3 to generate a new Test class with the class name of the original tested class name + Test.
Based on the device for generating the Mock test script based on the unit test, the method for generating the Mock test script based on the unit test, which is provided by the specific application example of the invention, comprises the following steps:
step S101: the user designs and generates case design files using an inline case design tool.
Step S102: and (4) selecting a test method, a project type and a Mock frame by a user, and uploading case design files.
Step S103: and the system background analyzes the second-layer method call in the source code of the developer by using a JavaParser tool.
When a developer source code is parsed, a componentization unit java source is first parsed into a componentization unit type syntax tree (where content is String type source code content), and then a specific processing method is called according to a required node type.
Step S104: and acquiring the Mock frame selected by the user in the step S102, and judging whether the user independently selects the Mockito frame.
Step S105: and realizing the Mock of the second layer method by using Mokito or Powermock, and generating a Mock test script.
Step S106: analyzing the case design file, and generating the annotation and the @ Test method by utilizing the GIVEN-WHEN-THEN information supplemented in the file.
Step S107: and packaging the generated Mock script and the unit test case to generate a new test class.
As can be seen from the above description, in the method for generating a Mock test script based on a unit test provided in the embodiment of the present invention, first, a source code and a Mock frame corresponding to a Mock test script to be generated are received; then, analyzing the source code to obtain a second layer method; and finally generating a Mock test script according to the second layer method and the Mock framework. Specifically, the second-layer method call in the codes is analyzed by scanning the source codes of developers, a user can select a case design method (an equivalence class or a path method) and upload a case design file, then one or two of Mockit or PowerMock frames can be selected to use the analyzed second-layer method for mock, and a unit test script and a test case are automatically generated.
In summary, the method for generating a Mock test script based on a unit test provided by the embodiment of the present invention has the following beneficial effects:
1. the invention realizes automatic generation of the unit test Mock script by using a technical means, provides good technical support for the unit test of the Java program, and makes up the defects that manual writing of the unit test script wastes time and labor, and is incomplete in coverage and easy to omit.
2. The invention can provide Java source code analysis, automatic generation of unit test mock script and automatic generation of unit test case, effectively reduce the risk of test omission and improve the unit test quality.
3. The invention supports the reuse of case design files in the function test by developers, can reduce the development test communication cost, intensifies the unit test and function test cost, and effectively reduces the unit test workload of the developers.
Based on the same inventive concept, the embodiment of the present application further provides a device for generating a Mock test script based on a unit test, which can be used to implement the method described in the above embodiments, such as the following embodiments. Because the problem solving principle of the generating device of the Mock test script based on the unit test is similar to the generating method of the Mock test script based on the unit test, the implementation of the generating device of the Mock test script based on the unit test can be implemented by referring to the generating method of the Mock test script based on the unit test, and repeated parts are not repeated. As used hereinafter, the term "unit" or "module" may be a combination of software and/or hardware that implements a predetermined function. While the system described in the embodiments below is preferably implemented in software, implementations in hardware, or a combination of software and hardware are also possible and contemplated.
The embodiment of the invention provides a specific implementation mode of a generating device of a Mock test script based on a unit test, which can realize the generating method of the Mock test script based on the unit test, and referring to fig. 8, the generating device of the Mock test script based on the unit test specifically comprises the following contents:
the source code receiving module 10 is used for receiving source codes corresponding to a Mock test script to be generated and a Mock frame;
a source code analyzing module 20, configured to analyze the source code to obtain a second layer method;
and the test script generating module 30 is configured to generate the Mock test script according to the second layer method and the Mock frame.
In an embodiment, referring to fig. 9, the source code parsing module 20 includes:
a source code conversion unit 20a for converting the source code into a Statement object;
a syntax tree generating unit 20b for parsing the Statement object to generate a syntax tree;
a method determining unit 20c for determining the second layer method according to the syntax tree;
in one embodiment, referring to fig. 10, the method determination unit 20c includes:
a node type determining unit 20ca for determining a node type in the syntax tree;
a method determination subunit 20cb for determining the second layer method according to the node type;
in one embodiment, the Mock frame is a Mock frame, and the second layer method includes: a member method; the test script generation module is specifically used for generating the Mock test script according to the member method.
In one embodiment, the Mock frame is a Mock frame and a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; the test script generation module is specifically used for generating the Mock test script according to the member method, the static function, the constructor function, the private function, the Final function and the system function.
In one embodiment, the Mock frame is a PowerMock frame, and the second layer method includes: member methods, static functions, constructors, private functions, Final functions and system functions; the test script generation module is specifically used for generating the Mock test script according to the member method, the static function, the constructor function, the private function, the Final function and the system function.
As can be seen from the above description, the apparatus for generating a Mock test script based on a unit test according to the embodiment of the present invention first receives a source code and a Mock frame corresponding to a Mock test script to be generated; then, analyzing the source code to obtain a second layer method; and finally generating a Mock test script according to the second layer method and the Mock framework. Aiming at the pain points that the existing Mock frame generation unit test script is manually compiled or used in the prior art, time and labor are wasted, omission is easy, and the learning cost is high, the invention provides the method for automatically generating the Mock script, and the method supports the source code analysis and the Mock frame selection of developers and automatically generates the Mock script, thereby improving the coverage rate of the test method, reducing the unit test pressure of the developers and improving the quality of the unit test script.
An embodiment of the present application further provides a specific implementation manner of an electronic device, which is capable of implementing all steps in the method for generating a Mock test script based on a unit test in the foregoing embodiment, and referring to fig. 11, the electronic device specifically includes the following contents:
a processor (processor)1201, a memory (memory)1202, a communication Interface 1203, and a bus 1204;
the processor 1201, the memory 1202 and the communication interface 1203 complete communication with each other through the bus 1204; the communication interface 1203 is used for implementing information transmission between related devices such as server-side devices and client-side devices;
the processor 1201 is configured to call the computer program in the memory 1202, and the processor executes the computer program to implement all the steps in the method for generating a Mock test script based on a unit test in the foregoing embodiments, for example, the processor executes the computer program to implement the following steps:
step 100: receiving a source code and a Mock frame corresponding to a Mock test script to be generated;
step 200: analyzing the source code to obtain a second layer method;
step 300: and generating the Mock test script according to the second layer method and the Mock framework.
An embodiment of the present application further provides a computer-readable storage medium capable of implementing all the steps in the method for generating a Mock test script based on a unit test in the foregoing embodiment, where the computer-readable storage medium stores a computer program, and the computer program implements all the steps of the method for generating a Mock test script based on a unit test in the foregoing embodiment when being executed by a processor, for example, the processor implements the following steps when executing the computer program:
step 100: receiving a source code and a Mock frame corresponding to a Mock test script to be generated;
step 200: analyzing the source code to obtain a second layer method;
step 300: and generating the Mock test script according to the second layer method and the Mock framework.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the hardware + program class embodiment, since it is substantially similar to the method embodiment, the description is simple, and the relevant points can be referred to the partial description of the method embodiment.
The foregoing description has been directed to specific embodiments of this disclosure. Other embodiments are within the scope of the following claims. In some cases, the actions or steps recited in the claims may be performed in a different order than in the embodiments and still achieve desirable results. In addition, the processes depicted in the accompanying figures do not necessarily require the particular order shown, or sequential order, to achieve desirable results. In some embodiments, multitasking and parallel processing may also be possible or may be advantageous.
Although the present application provides method steps as in an embodiment or a flowchart, more or fewer steps may be included based on conventional or non-inventive labor. The order of steps recited in the embodiments is merely one manner of performing the steps in a multitude of orders and does not represent the only order of execution. When an actual apparatus or client product executes, it may execute sequentially or in parallel (e.g., in the context of parallel processors or multi-threaded processing) according to the embodiments or methods shown in the figures.
For convenience of description, the above devices are described as being divided into various modules by functions, and are described separately. Of course, in implementing the embodiments of the present description, the functions of each module may be implemented in one or more software and/or hardware, or a module implementing the same function may be implemented by a combination of multiple sub-modules or sub-units, and the like. The above-described embodiments of the apparatus are merely illustrative, and for example, the division of the units is only one logical division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, devices or units, and may be in an electrical, mechanical or other form.
Those skilled in the art will also appreciate that, in addition to implementing the controller as pure computer readable program code, the same functionality can be implemented by logically programming method steps such that the controller is in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers and the like. Such a controller may therefore be considered as a hardware component, and the means included therein for performing the various functions may also be considered as a structure within the hardware component. Or even means for performing the functions may be regarded as being both a software module for performing the method and a structure within a hardware component.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
The embodiments of this specification may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The described embodiments may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media including memory storage devices.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment. In the description herein, references to the description of the term "one embodiment," "some embodiments," "an example," "a specific example," or "some examples," etc., mean that a particular feature, structure, material, or characteristic described in connection with the embodiment or example is included in at least one embodiment or example of an embodiment of the specification. In this specification, the schematic representations of the terms used above are not necessarily intended to refer to the same embodiment or example. Furthermore, the particular features, structures, materials, or characteristics described may be combined in any suitable manner in any one or more embodiments or examples. Furthermore, various embodiments or examples and features of different embodiments or examples described in this specification can be combined and combined by one skilled in the art without contradiction.
The above description is only an example of the embodiments of the present disclosure, and is not intended to limit the embodiments of the present disclosure. Various modifications and variations to the embodiments described herein will be apparent to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the embodiments of the present specification should be included in the scope of the claims of the embodiments of the present specification.

Claims (10)

1. A method for generating a Mock test script based on unit test is characterized by comprising the following steps:
receiving a source code and a Mock frame corresponding to a Mock test script to be generated;
analyzing the source code to obtain a second layer method;
and generating the Mock test script according to the second layer method and the Mock framework.
2. The method for generating a Mock test script according to claim 1, wherein said parsing said source code to obtain a second layer method comprises:
converting the source code into a Statement object;
parsing the State object to generate a syntax tree;
determining the second-level method from the syntax tree.
3. The method of generating a Mock test script according to claim 2, wherein said determining said second level method from said syntax tree comprises:
determining a node type in the syntax tree;
determining the second tier method based on the node type.
4. The method for generating a Mock test script according to claim 1, wherein said Mock frame is a Mock frame, said second layer method comprising: a member method; the generating the Mock test script according to the second layer method and the Mock frame comprises the following steps:
and generating the Mock test script according to the member method.
5. The method for generating a Mock test script according to claim 1, wherein the Mock frame is a Mock frame and a PowerMock frame, and the second layer method comprises: member methods, static functions, constructors, private functions, Final functions and system functions; the generating the Mock test script according to the second layer method and the Mock frame comprises the following steps:
and generating the Mock test script according to the member method, the static function, the constructor, the private function, the Final function and the system function.
6. The method for generating a Mock test script according to claim 1, wherein the Mock frame is a PowerMock frame, and the second layer method comprises: member methods, static functions, constructors, private functions, Final functions and system functions; the generating the Mock test script according to the second layer method and the Mock frame comprises the following steps:
and generating the Mock test script according to the member method, the static function, the constructor, the private function, the Final function and the system function.
7. A generating device of Mock test script based on unit test is characterized by comprising:
the source code receiving module is used for receiving source codes corresponding to the Mock test scripts to be generated and a Mock frame;
the source code analysis module is used for analyzing the source codes to obtain a second layer method;
and the test script generating module is used for generating the Mock test script according to the second layer method and the Mock frame.
8. The apparatus for generating Mock test script according to claim 7, wherein said source code parsing module comprises:
the source code conversion unit is used for converting the source code into a State element object;
the syntax tree generating unit is used for analyzing the Statement object to generate a syntax tree;
a method determination unit for determining the second layer method from the syntax tree;
the method determination unit includes:
a node type determining unit, configured to determine a node type in the syntax tree;
a method determination subunit to determine the second-tier method according to the node type.
9. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, wherein the processor implements the steps of the method for generating a Mock test script according to any one of claims 1 to 6 when executing the program.
10. A computer-readable storage medium, on which a computer program is stored, which, when being executed by a processor, carries out the steps of the method for generating a Mock test script according to the unit test of any one of claims 1 to 6.
CN202111541142.5A 2021-12-16 2021-12-16 Method and device for generating Mock test script based on unit test Pending CN114185801A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202111541142.5A CN114185801A (en) 2021-12-16 2021-12-16 Method and device for generating Mock test script based on unit test

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202111541142.5A CN114185801A (en) 2021-12-16 2021-12-16 Method and device for generating Mock test script based on unit test

Publications (1)

Publication Number Publication Date
CN114185801A true CN114185801A (en) 2022-03-15

Family

ID=80544116

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202111541142.5A Pending CN114185801A (en) 2021-12-16 2021-12-16 Method and device for generating Mock test script based on unit test

Country Status (1)

Country Link
CN (1) CN114185801A (en)

Similar Documents

Publication Publication Date Title
Amalfitano et al. A gui crawling-based technique for android mobile application testing
US7165074B2 (en) Software development test case analyzer and optimizer
US8615750B1 (en) Optimizing application compiling
CN109189469B (en) Reflection-based android application micro-servitization method and system
Krogmann Reconstruction of software component architectures and behaviour models using static and dynamic analysis
CN109189374B (en) Object structure code generation method and system based on object reference chain
US9329985B1 (en) Using emulation to disassociate verification from stimulus in functional test
US20020198868A1 (en) System and method for specification tracking in a Java compatibility testing environment
US20120174068A1 (en) Testing Software Code
US9729677B2 (en) Method of adding client server automation to computer languages for cloud computing
CN111026670B (en) Test case generation method, test case generation device and storage medium
CN112394942A (en) Distributed software development compiling method and software development platform based on cloud computing
US10496379B2 (en) Facilitated production of code for software testing
Lv et al. Potential risk detection system of hyperledger fabric smart contract based on static analysis
CN109491904B (en) Automatic testing method and device for spark SQL application program
Rahman et al. A DSL for importing models in a requirements management system
CN114185801A (en) Method and device for generating Mock test script based on unit test
Kuznetsov et al. What do all these buttons do? statically mining android user interfaces at scale
US20150026523A1 (en) Debugging method and computer program product
CN113220586A (en) Automatic interface pressure test execution method, device and system
Micskei et al. Robustness testing techniques for high availability middleware solutions
Huang et al. A novel approach to regression test selection for j2ee applications
Shafin et al. Automated testing of Web Services system based on OWL-S
CN112597669B (en) Simulation test platform and working method thereof
Hunderi Supporting and Improving the Extensibility of the" Odin" system

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