CN110633213B - Unit test method, unit test device, computer equipment and storage medium - Google Patents
Unit test method, unit test device, computer equipment and storage medium Download PDFInfo
- Publication number
- CN110633213B CN110633213B CN201910884316.4A CN201910884316A CN110633213B CN 110633213 B CN110633213 B CN 110633213B CN 201910884316 A CN201910884316 A CN 201910884316A CN 110633213 B CN110633213 B CN 110633213B
- Authority
- CN
- China
- Prior art keywords
- mock
- class
- anonymous
- target interception
- interception method
- 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
Links
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
- G06F11/3668—Software testing
- G06F11/3672—Test management
- G06F11/3684—Test management for test design, e.g. generating new test cases
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F11/00—Error detection; Error correction; Monitoring
- G06F11/36—Preventing errors by testing or debugging software
- G06F11/3668—Software testing
- G06F11/3672—Test management
- G06F11/3688—Test management for test execution, e.g. scheduling of test suites
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Computer Hardware Design (AREA)
- Quality & Reliability (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Debugging And Monitoring (AREA)
Abstract
The application relates to a unit testing method, a unit testing device, computer equipment and a storage medium, and belongs to the technical field of Internet. The method comprises the following steps: intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming through obtaining all methods of the class to be tested to obtain a target interception method, comparing the target interception method with a pre-established anonymous Mock class rewriting method to obtain a comparison result, and returning a test result according to the comparison result. According to the technical scheme provided by the embodiment of the application, the route is performed by facing to the tangent plane programming, the target interception method is directly compared with the pre-established anonymous Mock type rewriting method, the condition that input parameters are the same or different is not needed to be considered, and the problem that the unit test cannot be performed due to the fact that different unit tests adopt the same input parameters is solved.
Description
Technical Field
The application relates to the technical field of Internet, in particular to a unit testing method, a unit testing device, computer equipment and a storage medium.
Background
In recent years, writing unit tests have become an indispensable part of program development. Unit tests are methods written by developers to detect the correctness of object code under certain conditions. The unit test has the advantages of convenience in later reconstruction, optimization of programming, synchronous document recording, regression and the like, and is more and more favored by developers.
The unit testing tools available at present are of various types, and the common unit testing tools are Mock tools, and the Mock tools are classified into different types of unit testing tools, such as Mockito, easyMock, powe rMock, JMockit, junit, testNG and the like, and the different types of testing tools are different in terms of problems. In general, a unit testing tool needs to rely on a Spring framework in the Java development process, and in the existing development method based on the Spring framework, in the original Spring-based design, the class needing Mock is usually realized again and replaced by the original class. In this scheme, different Mock classes return different values according to different parameters of the incoming method, i.e. different unit tests all use a unified routing approach.
However, if different unit tests use the same input parameters, a problem arises in that the unit test cannot be performed.
Disclosure of Invention
In view of the foregoing, it is desirable to provide a unit testing method, apparatus, computer device, and storage medium that do not require consideration of input parameters.
In a first aspect, there is provided a unit testing method comprising:
acquiring all methods of the class to be tested of the unit test;
Intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming to obtain a target interception method;
comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result;
and returning a test result according to the comparison result.
Setting required Mock data through a preprocessing stage, establishing a Mock class corresponding to a to-be-tested class method of unit test, selectively intercepting the annotated to-be-tested class method by adopting tangent plane-oriented programming, comparing a pre-established anonymous Mock class rewriting method with a target interception method, and returning a test result according to a comparison result. The whole unit test process adopts the tangent plane-oriented programming to realize the comparison and judgment of the method of the class to be tested and the Mock rewriting method, and the method of anonymous Mock rewriting is not required to be directly established in the unit test, namely the problem that input parameters are the same or different is not required to be considered is solved, and the problem that the unit test cannot be performed due to the fact that different unit tests adopt the same input parameters is solved.
In one embodiment, returning the test result according to the comparison result includes:
if the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the class to which the target interception method belongs, calling a Mock file corresponding to the target interception method, converting the Mock file into an object and returning the object;
And if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method, calling the target interception method and returning data in the target interception method.
And comparing and judging the intercepted method of the class to be tested with a pre-established method for rewriting the anonymous Mock class by calling an AOP module, and returning a test result according to a judgment result. The two methods are directly compared, namely the Mock class of the class to be tested does not need to be repeatedly rewritten in the unit test, the test result does not need to be returned according to the input parameters, the effect of isolating the route is achieved, and the problems that the returned result is inaccurate or the test result cannot be returned due to the fact that the input parameters are the same or different are solved.
In one embodiment, the anonymous Mock class rewriting method includes a parameter entry assertion of an annotated class, and before invoking a Mock file corresponding to the target interception method, converting the Mock file into an object and returning the object, the method further includes:
determining whether input parameters of a target interception method are consistent with input parameters of an anonymous Mock type rewriting method according to the input parameter assertion of the annotated class;
and if the target interception method is consistent with the target interception method, executing the step of calling the Mock file corresponding to the target interception method and converting the Mock file into the object.
According to the annotated class parameter entering assertion, whether the input parameters of the target interception method are consistent with the input parameters of the anonymous Mock class rewriting method or not is determined, which is equivalent to performing secondary verification on the method of the class to be tested, and further reducing the situations of misjudgment, missed judgment and the like of unit test.
In one embodiment, the method further comprises:
setting Mock data;
and storing the Mock data into a Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
In one embodiment, the format of the Mock file includes any of null, serialized, or JSON formats.
In one embodiment, before the method for establishing anonymous Mock class rewriting corresponding to a Mock file, the method further includes:
establishing a Mock class;
if the Mock class is an interface, the interface is realized;
and if the Mock class is an abstract class or a common class, inheriting the interface.
By presetting the Mock class, the method corresponding to the class is directly called in the unit test, the steps of the method requiring the class to be tested do not need to be rewritten in the unit test body, the writing steps of the unit test are simplified, the problem that different parameters cannot be input by the methods of different classes to be tested in the same unit test by using the same route, and the efficiency and feasibility of the unit test are improved.
In one embodiment, comparing the target interception method with a pre-established anonymous Mock-like overwriting method includes:
and comparing the target interception method with a pre-established anonymous Mock type rewriting method according to the sequence of setting Mock data.
According to the Mock data sequence preset according to the method of the class to be tested, the target interception method is compared with the pre-established anonymous Mock class rewriting method by adopting an AOP module, and the priority of the comparison object is set, which is equivalent to performing emphasis screening on the comparison object, so that the priority acquisition of the emphasis test object of the method of the class to be tested in the unit test by the tester is facilitated.
In a second aspect, there is provided a unit testing apparatus comprising:
the acquisition module is used for acquiring all methods of the class to be tested of the unit test;
the interception module is used for intercepting all methods of the class annotated by the preset rule by adopting tangent plane-oriented programming to obtain a target interception method;
the comparison module is used for comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result;
and the return module is used for returning the test result according to the comparison result.
In one embodiment, the return module further comprises:
the converting unit is used for calling a Mock file corresponding to the target interception method and converting the Mock file into an object and returning if the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the class to which the target interception method belongs;
and the return subunit is used for calling the target interception method and returning data in the target interception method if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method.
In one embodiment, the return module further comprises:
the assertion unit is used for determining whether the input parameters of the target interception method are consistent with the input parameters of the anonymous Mock type rewriting method according to the parameter entering assertion of the annotated class;
and if the target interception method is consistent with the target interception method, executing the step of calling the Mock file corresponding to the target interception method and converting the Mock file into the object.
In one embodiment, the unit testing apparatus further comprises:
the data module is used for setting Mock data; and storing the Mock data into a Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
In one embodiment, the format of the Mock file includes any of null, serialized, or JSON formats.
In one embodiment, before the method for establishing anonymous Mock class overwriting corresponding to the Mock file, the unit testing device further includes an establishing module:
the building module is used for building a Mock class; if the Mock class is an interface, the interface is realized; and if the Mock class is an abstract class or a common class, inheriting the interface.
In one embodiment, the interception module comprises:
and the sequencing unit is used for comparing the target interception method with a pre-established anonymous Mock type rewriting method according to the sequence of setting the Mock data.
In a third aspect, there is provided a computer device comprising a memory storing a computer program and a processor implementing the unit testing method of any of the first aspects above when said computer program is executed.
In a fourth aspect, there is provided a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the unit testing method according to any of the first aspects above.
The beneficial effects that technical scheme that this application embodiment provided include at least:
Intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming through obtaining all methods of the class to be tested to obtain a target interception method, comparing the target interception method with a pre-established anonymous Mock class rewriting method to obtain a comparison result, and returning a test result according to the comparison result. In the method, required Mock data is set in a preprocessing stage, a Mock class corresponding to a method of a class to be tested is established, the class to be tested is selected and intercepted by face-to-face programming, a pre-established anonymous Mock class rewriting method is directly compared with a target interception method, a test result is returned according to the comparison result, and the method of anonymous Mock class rewriting in unit test is not required to be directly established, namely the problem that input parameters are the same or different is not required to be considered, so that the corresponding unit test result is accurately obtained.
Drawings
FIG. 1 is a schematic diagram of an application environment of a unit testing method according to an embodiment of the present application;
FIG. 2 is a flow chart of a unit testing method according to an embodiment of the present application;
FIG. 3 is a flow chart of a unit testing method according to an embodiment of the present application;
FIG. 4 is a flow chart of a unit testing method according to an embodiment of the present application;
FIG. 5 is a flow chart of a unit testing method according to an embodiment of the present application;
FIG. 6 is a timing diagram of a unit testing method according to an embodiment of the present application;
FIG. 7 is a schematic structural diagram of a unit testing device according to an embodiment of the present application;
FIG. 8 is a schematic structural diagram of a unit testing device according to an embodiment of the present application;
FIG. 9 is a schematic structural diagram of a unit testing device according to an embodiment of the present application;
FIG. 10 is a schematic structural diagram of a unit testing device according to an embodiment of the present application;
FIG. 11 is a schematic structural diagram of a unit testing device according to an embodiment of the present application;
fig. 12 is a schematic structural diagram of a unit testing device in an embodiment of the present application.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application will be further described in detail with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the present application.
The unit testing method provided by the application can be applied to an application environment shown in fig. 1. Fig. 1 provides a computer device, which may be a terminal or a server, and its internal structure may be as shown in fig. 1. The computer device includes a processor, a memory, a network interface, a display screen, and an input device connected by a system bus. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The network interface of the computer device is used for communicating with an external terminal through a network connection. The computer program is executed by a processor to implement a unit testing method. The display screen of the computer equipment can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, can also be keys, a track ball or a touch pad arranged on the shell of the computer equipment, and can also be an external keyboard, a touch pad or a mouse and the like.
It should be noted that, in the unit testing method provided in the embodiment of the present application, the execution body may be a unit testing device, and the unit testing device may be implemented in a manner of software, hardware, or a combination of software and hardware to form part or all of the computer device. In the following method embodiments, the execution subject is a computer device.
In one embodiment, as shown in fig. 2, a unit testing method is provided, and the method is applied to the computer device in fig. 1 for illustration, and includes the following steps:
s201, acquiring all methods of the class to be tested by the unit test.
All methods of the class to be tested refer to all methods of the class to be tested in the unit test in the development process.
In this embodiment, the user function calling unit may be used to test the class userserviceill to obtain all methods of the class to be tested, and the middle class of the obtained method annotates the class according to a preset annotation rule for later performing an interception operation on the annotated class, for example, the preset annotation rule may be adding annotations @ Service, @ Component, @ repositive, and the like to the class to be tested, which is not limited in this embodiment.
S202, intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming to obtain a target interception method.
The section-oriented programming (Aspect Oriented Programming, AOP) can isolate each part of the service logic, so that the coupling degree among the parts of the service logic is reduced, and the reusability of the program is improved. The classes annotated by the preset rules may include classes annotated by @ Service, @ Component, and @ restore, etc.
In this embodiment, the characteristic of AOP is adopted, and the Mock class does not need to be continuously rewritten in the test unit, but all methods of the class to be tested are intercepted according to a preset interception rule, for example, according to the preset interception rule, the AOP intercepts all methods of the class to be tested annotated as @ Service, and the method of the class to be tested annotated as @ Service is obtained as a target connection method for comparison with the corresponding method of anonymous Mock class rewriting established in advance. The present embodiment is not limited thereto.
S203, comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result.
The target interception method is a method of a class to be tested, which is obtained after all the methods of the class to be tested are intercepted according to the annotation; the pre-established anonymous Mock class override method includes null realizations or assertions of input parameters. The comparison between the target interception method and the pre-established anonymous Mock type rewriting method can be whether the method signature and the return value type of the two methods are consistent or not.
In this embodiment, the comparison between the target interception method and the pre-established anonymous Mock type rewriting method may be implemented in various manners. For example, whether the signature, the return value type, and the like of the target interception method and the pre-established anonymous Mock class rewriting method are consistent or whether the class to which the target interception method and the pre-established anonymous Mock class rewriting method belong is judged, or whether the target interception method is a subclass of the anonymous Mock class rewriting method or the like is judged. This embodiment is not taken as an example.
S204, returning a test result according to the comparison result.
The method for rewriting the anonymous Mock class is consistent with the target interception method, or the method for rewriting the anonymous Mock class is inconsistent with the target interception method; the corresponding test results are respectively that the unit test passes or error information in the unit test is returned, namely Bug information.
In this embodiment, if the anonymous Mock type rewriting method is a subclass of the target interception method and the anonymous Mock type rewriting method is consistent with the target interception method, a comparison result is consistent, and then the unit test passing information is returned; if the anonymous Mock class rewriting method is not a subclass of the target interception method or the anonymous Mock class rewriting method is inconsistent with the target interception method, the comparison result is inconsistent, error information in the unit test is returned, and the unit test is jumped out. The present embodiment is not limited thereto.
To detect the correctness of the object code under specific conditions, unit testing becomes an indispensable step in the development process. The conventional unit testing tool needs to rely on a Spring framework in the Java development process, and in the original Spring-based design, the class needing Mock is generally required to be rewritten in the unit testing method, and the original class is replaced. In this scheme, different Mock types return different values according to different parameters of the incoming method, i.e. different unit tests all use a unified routing mode, but if different unit tests use the same input parameters, the problem that unit tests cannot be performed is caused. In this embodiment, required Mock data is set in a preprocessing stage, a Mock class corresponding to a class method to be tested is established, a tangent plane-oriented programming is adopted to intercept all methods of classes annotated by preset rules, a pre-established anonymous Mock class rewriting method is compared with a target interception method, and a test result is returned according to a comparison result. The whole unit test process adopts the tangent plane-oriented programming to realize the comparison and judgment of the method of the class to be tested and the Mock rewriting method, and the method of anonymous Mock rewriting is not required to be directly established in the unit test, namely the problem that input parameters are the same or different is not required to be considered is solved, and the problem that the unit test cannot be performed due to the fact that different unit tests adopt the same input parameters is solved.
Based on the embodiment shown in fig. 2, the specific implementation of step 204 "return test result according to comparison result" may include the following two cases:
in the first case, if the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the target interception method, a Mock file corresponding to the target interception method is called, and the Mock file is converted into an output object and returned.
The method for rewriting the anonymous Mock class is a subclass of the target interception method, the target interception method is a parent class, the method for rewriting the anonymous Mock class is a subclass of the method, and the method for rewriting the anonymous Mock class inherits the same characteristics in the parent class. The consistency of the target interception method and the anonymous Mock type rewriting method means that the method signature and the return value type are consistent.
In this embodiment, after determining that the anonymous Mock type rewriting method is a subclass of the target interception method, the target interception method and the anonymous Mock type rewriting method are compared, specifically, whether the method signature and the return value type of the two methods are consistent is determined. The method signature comprises a method name, the number of the method parameters, the type of the method parameters and the like. If the method signature and the return value type of the two methods are consistent, invoking a Mock file corresponding to the target interception method, converting the Mock file into a corresponding output object, and returning the output object layer by layer to the unit test. This embodiment is not taken as an example.
And in the second case, if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method, calling the target interception method and returning data in the target interception method.
In this embodiment, after determining that the method for anonymous Mock class overwriting is not a subclass of the target interception method, or determining that the method for anonymous Mock class overwriting is a subclass of the target interception method, comparing the target interception method with the method for anonymous Mock class overwriting, and determining that the method signatures of the two methods are inconsistent with the return value types, directly calling the target interception method, that is, a real method in the current unit test, and returning data in the target interception method, that is, returning corresponding real data. This embodiment is not taken as an example.
And comparing and judging the intercepted method of the class to be tested with a pre-established method for rewriting the anonymous Mock class by calling the AOP, and returning a test result according to a judgment result. The two methods are directly compared, namely the Mock class of the class to be tested does not need to be repeatedly rewritten in the unit test, the test result does not need to be returned according to the input parameters, the effect of isolating the route is achieved, and the problems that the returned result is inaccurate or the test result cannot be returned due to the fact that the input parameters are the same or different are solved.
Based on the embodiment shown in fig. 2, fig. 3 is a flowchart of another unit testing method provided in this embodiment, where the method relates to a method for anonymous Mock class overwriting, including a parameter entry assertion of an annotated class, and invoking a Mock file corresponding to a target interception method, before "convert Mock file into an object and return" in the first case of the embodiment of step 204, as shown in fig. 3, the method further includes the following steps:
s301, determining whether input parameters of a target interception method are consistent with input parameters of an anonymous Mock type rewriting method according to the input assertion of the annotated class.
The annotation refers to explanation of the corresponding class, the assertion refers to carrying out Boolean verification on the input parameters of the target interception method and the input parameters of the anonymous Mock class rewriting method, the Boolean verification result is classified as true or false, if the Boolean verification result is true, the input parameters of the target interception method and the input parameters of the anonymous Mock class rewriting method are consistent, and if the Boolean verification result is false, the input parameters of the target interception method and the input parameters of the anonymous Mock class rewriting method are inconsistent.
In this embodiment, the method of the annotated class intercepted by the AOP, that is, the input parameter in the target interception method is taken as an actual value, and the input parameter of the method of anonymous Mock class rewriting is taken as an expected value, specifically, whether the actual value is consistent with the expected value is determined. The present embodiment is not limited thereto.
S302, if the target interception method is consistent, executing a Mock file corresponding to the calling target interception method, and converting the Mock file into an object.
And if the input parameters of the target interception method are consistent with the input parameters of the anonymous Mock type rewriting method, determining that the target interception method passes verification, calling a Mock file corresponding to the target interception method, and converting the Mock file into a corresponding output object according to the format of the Mock file.
In this embodiment, if the input parameter in the interception method, that is, the actual value, is consistent with the input parameter in the anonymous Mock type rewriting method, that is, the "actual value= expected value", which indicates that the assertion result is true, the verification is passed, the Mock file corresponding to the target interception method is executed, and the Mock file is converted into the corresponding object; if the actual value does not agree with the expected value, the "actual value-! The expected value indicates that the assertion result is false, and then the error information is thrown, and the unit test is directly jumped out. The present embodiment is not limited thereto.
According to the annotated class parameter entering assertion, whether the input parameters of the target interception method are consistent with the input parameters of the anonymous Mock class rewriting method or not is determined, which is equivalent to performing secondary verification on the method of the class to be tested, and further reducing the situations of misjudgment, missed judgment and the like of unit test.
Based on the embodiment shown in fig. 2, before step 201 of "acquire all methods of class to be tested", fig. 4 is a flowchart of another unit testing method provided in this embodiment, and as shown in fig. 4, the method further includes the following steps:
s401, setting Mock data.
In this embodiment, mock is a method for creating a virtual object for testing for some objects that are not easy to construct or easy to obtain during the testing process, and Mock data is corresponding virtual data. The Mock data is set before the unit test is performed, the Mock data can be stored in the form of a Mock file, and a priority order can be set for each Mock data. The present embodiment is not limited thereto.
S402, storing the Mock data into a Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
Further, the format of the Mock file includes any of null, serialized, or JSON formats.
The method comprises the steps of calling a data stack class DataMockerMock to store Mock data in the form of a Mock file, wherein the format of the Mock file can be null, a serialization format or a JSON format, and the like, and correspondingly establishing an anonymous Mock class rewriting method corresponding to the Mock file.
In this embodiment, preset Mock data is saved in a Mock file, an anonymous Mock class rewriting method corresponding to the Mock file is established, after all methods of a class to be tested are appointed and intercepted by calling AOP, method signature and return value type comparison are performed with the target interception method, and a test result is returned according to a comparison result. The present embodiment is not limited thereto.
On the basis of the embodiment shown in fig. 4, fig. 5 is a flowchart of another unit testing method provided in this embodiment, where the method involves establishing a Mock class, and before step 402 "and establishing a method for anonymous Mock class overwriting corresponding to a Mock file", as shown in fig. 5, the method further includes the following steps:
s501, establishing a Mock class.
Where Mock is a method of creating a virtual object for testing during testing for some objects that are not easily constructed or easily obtained. Establishing a Mock class refers to virtualizing classes that are not easily constructed or easily obtained, and establishing a Mock class for later unit testing. After the Mock class is established, the type of the Mock class is judged, and the Mock class comprises any one of an interface, an abstract class and a method class.
S502, if the Mock class is an interface, the interface is realized.
The interface refers to that in Java language, the characteristics of a method include the name of the method, the number and the kind of parameters, but not the return type of the method, the name of the parameters and exception throwing. If the Mock class is an interface, the method is not included in the class, so that the interface can only be realized.
S503, if the Mock class is an abstract class or a common class, inheriting the interface.
The class containing the abstract method is an abstract class, and no method main body exists; the class which does not contain the abstract method is a common class, and the class can contain a method main body and also can contain an interface. If the Mock class is the two classes, the method in the corresponding class is not required to be rewritten, and only the corresponding class is used as a parent class, and all the characteristics and the methods of the corresponding class are inherited.
In this embodiment, the established Mock class is userdaorequsethaser, and the method mainly implemented by the class includes a method for anonymous Mock class overwriting, and/or assertion of an input parameter, or null implementation. If the Mock class comprises an anonymous Mock class rewriting method and/or an input parameter assertion, the Mock class is an abstract class or a common class, the anonymous Mock class rewriting method is used for comparing all methods of the annotated class with a target interception method after intercepting all methods by adopting AOP, and a test result is returned according to a comparison result; the assertion of the input parameter is specifically to judge whether the actual value of the input parameter is consistent with the expected value, that is, whether the input parameter in the target interception method is consistent with the input parameter in the Mock class, and the Mock class inherits the interfaces of the two methods, so that the specific implementation method is not rewritten. If the Mock class contains an empty implementation, judging the class as an interface or an abstract class or a common class, and if the Mock class is an interface, directly implementing the interface; and if the Mock class is an abstract class or a common class, inheriting the interface in the null implementation. The present embodiment is not limited thereto.
By presetting the Mock class, the method corresponding to the class is directly called in the unit test, the steps of the method requiring the class to be tested do not need to be rewritten in the unit test body, the writing steps of the unit test are simplified, the problem that different parameters cannot be input by the methods of different classes to be tested in the same unit test by using the same route, and the efficiency and feasibility of the unit test are improved.
Based on the embodiments shown in fig. 2 and fig. 4, step 203 "compare the target interception method with the pre-established anonymous Mock-like overwriting method" includes: and comparing a target interception method with a pre-established anonymous Mock type rewriting method according to the sequence of setting the Mock data.
The method comprises the steps of setting the sequence of the Mock data according to a method of aiming at the current class to be tested, and setting corresponding Mock data sequences with different priorities.
In this embodiment, according to the Mock data sequence set in advance according to the method of the class to be tested, the AOP module is used to compare the target interception method with the method of pre-established anonymous Mock class overwriting, and by setting the priority of the comparison object, the priority screening of the comparison object is equivalent to the priority acquisition of the important test object of the method of the class to be tested by the tester in the unit test.
For a better unit testing method provided by the present application, the present embodiment will explain the unit testing method provided by the present application in terms of a unit testing method timing chart. As shown in fig. 6, includes:
s601, setting Mock data: setting Mock data through a unit test class base class;
s602, storing Mock data into a data stack class: storing the set Mock data into a data stack class, and simultaneously generating a Mock file for storing the Mock data;
s603, all methods for acquiring the class to be tested: acquiring all methods of the class to be tested by calling the unit test subclass;
s604, all methods of the annotated class to be tested are intercepted by the AOP: the AOP intercepts all methods of the class to be tested according to the annotated class to obtain a target interception method, and compares the target interception method with a pre-established anonymous Mock class rewriting method;
s605, judging whether the Mock data should be acquired: the judgment basis is a comparison result of a target interception method and a pre-established anonymous Mock type rewriting method;
s606, the return value is true: if the comparison result is that the anonymous Mock type rewriting method is a subclass of the target interception method, and the target interception method is consistent with the anonymous Mock type rewriting method, judging that Mock data should be acquired, and returning a value to be true, wherein the Mock data is used for calling a Mock file of the Mock data to be correspondingly converted into an output object;
S607, obtaining Mock data: acquiring the Mock data by calling the data stack class, and calling a Mock file corresponding to the Mock data;
s608, acquiring actual values and expected values of input parameters for assertion verification: invoking a Mock class to acquire an actual value and an expected value of an input parameter for assertion verification, wherein a verification result comprises true or false;
s609, returning a result: if the verification fails, returning error information and jumping out of the unit test;
s610, calling a file conversion object class: calling the class to convert the Mock file into an output object;
s611, converting the file into an output object according to a Mock file format;
s612, returning to an output object: returning the converted output object to the data stack class;
s613, returning to the output object: returning the converted output object to the AOP;
s614, returning to the output object: returning the converted output object to the unit test subclass;
s615, returning to an output object: returning the converted output object to the unit test subclass.
According to the unit testing method, firstly, required Mock data is set through a preprocessing stage, then all methods of a class to be tested are obtained, a tangent plane-oriented programming AOP method is adopted, all methods of the class to be tested are intercepted according to the annotated class, a target interception method is obtained, the target interception method is compared with a pre-established anonymous Mock class rewriting method, a comparison result is obtained, and the testing result is judged to be returned according to the comparison result. The whole unit test process adopts the AOP facing to the tangent plane programming to realize the comparison and judgment of the method to be tested and the Mock rewriting method, and the condition that the input parameters are the same or different does not need to be considered, so that the problem that the unit test cannot be performed due to the fact that the same input parameters are adopted by different unit tests is solved.
It should be understood that, although the steps in the flowcharts of fig. 1-5 are shown in order as indicated by the arrows, these steps are not necessarily performed in order as indicated by the arrows. The steps are not strictly limited to the order of execution unless explicitly recited herein, and the steps may be executed in other orders. Moreover, at least some of the steps in fig. 1-5 may include multiple sub-steps or stages that are not necessarily performed at the same time, but may be performed at different times, nor do the order in which the sub-steps or stages are performed necessarily occur sequentially, but may be performed alternately or alternately with at least a portion of the sub-steps or stages of other steps or steps.
In one embodiment, as shown in FIG. 7, there is provided a unit testing apparatus 800 comprising: an acquisition module 801, an interception module 802, a comparison module 803, and a return module 804, wherein:
an obtaining module 801, configured to obtain all methods of a class to be tested for unit testing;
the interception module 802 is configured to intercept all methods of a class annotated by a preset rule by adopting tangent plane oriented programming, so as to obtain a target interception method;
The comparison module 803 is configured to compare the target interception method with a pre-established anonymous Mock overwriting method to obtain a comparison result;
and a return module 804, configured to return a test result according to the comparison result.
The implementation principle and technical effects of the unit testing device provided in the above embodiment are similar to those of the method embodiment, and are not described herein again.
In one embodiment, as shown in fig. 8, the return module 804 further includes a conversion unit 8041 and a return subunit 8042, wherein:
the conversion unit 8041 is configured to call a Mock file corresponding to the target interception method, convert the Mock file into an object, and return the object if the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the target interception method;
and the return subunit 8042 is configured to call the target interception method and return data in the target interception method if the target interception method is inconsistent with the method for anonymous Mock class overwriting, and/or the anonymous Mock class is not a subclass of the target interception method.
The implementation principle and technical effects of the unit testing device provided in the above embodiment are similar to those of the method embodiment, and are not described herein again.
In one embodiment, as shown in fig. 9, the anonymous Mock class rewriting method includes an enrolled assertion of the annotated class, and the return module 804 further includes an assertion unit 8043:
an assertion unit 8043, configured to determine, according to the parameter assertion of the annotated class, whether the input parameters of the target interception method and the input parameters of the anonymous Mock class rewriting method are consistent; and if the target interception method is consistent with the target interception method, executing the step of calling the Mock file corresponding to the target interception method and converting the Mock file into the object.
In one embodiment, as shown in fig. 10, the unit testing apparatus 800 further includes a data module 805:
a data module 805 configured to set Mock data; and storing the Mock data into a Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
Further, the format of the Mock file includes any of null, serialized, or JSON formats.
The warehouse management device provided in the above embodiment has similar implementation principles and technical effects to those of the above method embodiment, and will not be described herein.
In one embodiment, as shown in fig. 11, the unit testing apparatus 800 further includes a setup module 806:
a building module 806, configured to build a Mock class;
If the Mock class is an interface, the interface is realized;
and if the Mock class is an abstract class or a common class, inheriting the interface.
In one embodiment, as shown in fig. 12, the interception module 802 includes a sorting unit 8021:
a sorting unit 8021, configured to compare the target interception method with a pre-established anonymous Mock type rewriting method according to an order in which the Mock data is set.
The warehouse management device provided in the above embodiment has similar implementation principles and technical effects to those of the above method embodiment, and will not be described herein.
For specific limitations of the unit testing apparatus, reference may be made to the above limitations of the unit testing method, and no further description is given here. The respective modules in the above-described unit test apparatus may be implemented in whole or in part by software, hardware, and combinations thereof. The above modules may be embedded in hardware or may be independent of a processor in the computer device, or may be stored in software in a memory in the computer device, so that the processor may call and execute operations corresponding to the above modules.
It will be appreciated by those skilled in the art that the structure shown in fig. 1 is merely a block diagram of some of the structures associated with the present application and is not limiting of the computer device to which the present application may be applied, and that a particular computer device may include more or fewer components than shown, or may combine certain components, or have a different arrangement of components.
In one embodiment, a computer device is provided comprising a memory and a processor, the memory having stored therein a computer program, the processor when executing the computer program performing the steps of:
acquiring all methods of the class to be tested of the unit test;
intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming to obtain a target interception method;
comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result;
and returning a test result according to the comparison result.
In one embodiment, the test result is returned according to the comparison result, and the processor further realizes the following steps when executing the computer program:
if the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the target interception method, calling a Mock file corresponding to the target interception method, converting the Mock file into an object and returning the object;
and if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method, calling the target interception method and returning data in the target interception method.
In one embodiment, the anonymous Mock class rewriting method includes a parameter entry assertion of an annotated class, calls a Mock file corresponding to the target interception method, and before converting the Mock file into an object and returning, the processor further implements the following steps when executing the computer program:
Determining whether input parameters of a target interception method are consistent with input parameters of an anonymous Mock type rewriting method according to the input parameter assertion of the annotated class;
and if the target interception method is consistent with the target interception method, executing the step of calling the Mock file corresponding to the target interception method and converting the Mock file into the object.
In one embodiment, the processor when executing the computer program further performs the steps of:
setting Mock data;
and storing the Mock data into a Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
In one embodiment, the processor when executing the computer program further performs the steps of:
the format of the Mock file includes any of null, serialized, or JSON formats.
In one embodiment, before the method for anonymous Mock class rewriting corresponding to a Mock file is established, the following steps are further implemented when the processor executes a computer program:
establishing a Mock class;
if the Mock class is an interface, the interface is realized;
and if the Mock class is an abstract class or a common class, inheriting the interface.
In one embodiment, the target interception method is compared with a pre-established anonymous Mock-like overwriting method, and the processor when executing the computer program further performs the steps of:
And comparing the target interception method with a pre-established anonymous Mock type rewriting method according to the sequence of setting Mock data.
The computer device provided in the embodiments of the present application has similar implementation principles and technical effects to those of the above method embodiments, and will not be described herein.
In one embodiment, a computer readable storage medium is provided having a computer program stored thereon, which when executed by a processor, performs the steps of:
acquiring all methods of the class to be tested of the unit test;
intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming to obtain a target interception method;
comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result;
and returning a test result according to the comparison result.
In one embodiment, the test results are returned according to the comparison results, and the computer program when executed by the processor further performs the steps of:
if the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the target interception method, calling a Mock file corresponding to the target interception method, converting the Mock file into an object and returning the object;
And if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method, calling the target interception method and returning data in the target interception method.
In one embodiment, the anonymous Mock class rewriting method includes a parameter entry assertion of an annotated class, calls a Mock file corresponding to the target interception method, and before converting the Mock file into an object and returning, the computer program when executed by the processor further implements the following steps:
determining whether input parameters of a target interception method are consistent with input parameters of an anonymous Mock type rewriting method according to the input parameter assertion of the annotated class;
and if the target interception method is consistent with the target interception method, executing the step of calling the Mock file corresponding to the target interception method and converting the Mock file into the object.
In one embodiment, the computer program when executed by the processor further performs the steps of:
setting Mock data;
and storing the Mock data into a Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
In one embodiment, the computer program when executed by the processor further performs the steps of:
the format of the Mock file includes any of null, serialized, or JSON formats.
In one embodiment, before the method for establishing anonymous Mock class overwrite corresponding to a Mock file, the computer program when executed by the processor further implements the steps of:
establishing a Mock class;
if the Mock class is an interface, the interface is realized;
and if the Mock class is an abstract class or a common class, inheriting the interface.
In one embodiment, comparing the target interception method with a pre-established anonymous Mock-like overwriting method, the computer program when executed by the processor further performs the steps of:
and comparing the target interception method with a pre-established anonymous Mock type rewriting method according to the sequence of setting Mock data.
The computer readable storage medium provided in this embodiment has similar principles and technical effects to those of the above method embodiment, and will not be described herein.
Those skilled in the art will appreciate that implementing all or part of the above described methods may be accomplished by way of a computer program stored on a non-transitory computer readable storage medium, which when executed, may comprise the steps of the embodiments of the methods described above. Any reference to memory, storage, database, or other medium used in the various embodiments provided herein may include non-volatile and/or volatile memory. The nonvolatile memory can include Read Only Memory (ROM), programmable ROM (PROM), electrically Programmable ROM (EPROM), electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double Data Rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous Link DRAM (SLDRAM), memory bus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), among others.
The technical features of the above embodiments may be arbitrarily combined, and all possible combinations of the technical features in the above embodiments are not described for brevity of description, however, as long as there is no contradiction between the combinations of the technical features, they should be considered as the scope of the description.
The above examples merely represent a few embodiments of the present application, which are described in more detail and are not to be construed as limiting the scope of the invention. It should be noted that it would be apparent to those skilled in the art that various modifications and improvements could be made without departing from the spirit of the present application, which would be within the scope of the present application. Accordingly, the scope of protection of the present application is to be determined by the claims appended hereto.
Claims (9)
1. A method of unit testing, the method comprising:
acquiring all methods of the unit test class to be tested;
intercepting all methods of classes annotated by preset rules by adopting tangent plane-oriented programming to obtain a target interception method; the preset rule is a preset interception rule;
comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result;
If the target interception method is consistent with the anonymous Mock class rewriting method and the anonymous Mock class is a subclass of the class to which the target interception method belongs, invoking a Mock file corresponding to the target interception method, converting the Mock file into an output object and returning the output object;
and if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method, calling the target interception method and returning data in the target interception method.
2. The method of claim 1, wherein the anonymous Mock class overwriting method includes an enqueuing assertion of an annotated class, and before the step of calling a Mock file corresponding to the target interception method, converting the Mock file into an object and returning the object, further includes:
determining whether input parameters of the target interception method are consistent with input parameters of the anonymous Mock type rewriting method according to the parameter entering assertion of the annotated class;
and if the target interception method is consistent with the target interception method, executing the Mock file corresponding to the target interception method, and converting the Mock file into an output object.
3. The method according to claim 1 or 2, characterized in that the method further comprises:
Setting Mock data;
and storing the Mock data into the Mock file, and establishing an anonymous Mock class rewriting method corresponding to the Mock file.
4. A method according to claim 3, wherein the format of the Mock file comprises any of null, serialized or JSON formats.
5. The method of claim 3, further comprising, prior to the method of establishing the anonymous Mock-like rewrite corresponding to the Mock file:
establishing a Mock class;
if the Mock class is an interface, the interface is realized;
and if the Mock class is an abstract class or a common class, inheriting the interface.
6. A method according to claim 3, wherein said comparing said target interception method with a pre-established anonymous Mock-like overwriting method comprises:
and comparing the target interception method with a pre-established anonymous Mock type rewriting method according to the sequence of setting the Mock data.
7. A unit testing apparatus, the apparatus comprising:
the acquisition module is used for acquiring all methods of the class to be tested of the unit test;
the interception module is used for intercepting all methods of the class annotated by the preset rule by adopting tangent plane-oriented programming to obtain a target interception method; the preset rule is a preset interception rule;
The comparison module is used for comparing the target interception method with a pre-established anonymous Mock type rewriting method to obtain a comparison result;
the return module is used for calling a Mock file corresponding to the target interception method and converting the Mock file into an output object and returning if the target interception method is consistent with the method for rewriting the anonymous Mock class and the anonymous Mock class is a subclass of the class to which the target interception method belongs; and if the target interception method is inconsistent with the anonymous Mock class rewriting method and/or the anonymous Mock class is not a subclass of the target interception method, calling the target interception method and returning data in the target interception method.
8. A computer device comprising a memory and a processor, the memory storing a computer program, characterized in that the computer program, when executed by the processor, implements the unit testing method according to any of claims 1 to 6.
9. A computer readable storage medium, on which a computer program is stored, characterized in that the program, when being executed by a processor, implements the unit testing method according to any one of claims 1 to 6.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910884316.4A CN110633213B (en) | 2019-09-19 | 2019-09-19 | Unit test method, unit test device, computer equipment and storage medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201910884316.4A CN110633213B (en) | 2019-09-19 | 2019-09-19 | Unit test method, unit test device, computer equipment and storage medium |
Publications (2)
Publication Number | Publication Date |
---|---|
CN110633213A CN110633213A (en) | 2019-12-31 |
CN110633213B true CN110633213B (en) | 2023-07-04 |
Family
ID=68971632
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201910884316.4A Active CN110633213B (en) | 2019-09-19 | 2019-09-19 | Unit test method, unit test device, computer equipment and storage medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN110633213B (en) |
Families Citing this family (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN112631942A (en) * | 2020-12-31 | 2021-04-09 | 广州华多网络科技有限公司 | Unit testing method, unit testing device, computer equipment and storage medium |
CN115344502B (en) * | 2022-10-18 | 2023-03-10 | 中建电子商务有限责任公司 | Method for automatically generating mock data and automatically loading mock data based on real data |
Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101276271A (en) * | 2007-03-29 | 2008-10-01 | 北京邮电大学 | Method and interceptor system facing to tangent plane programming |
CN108388519A (en) * | 2018-03-19 | 2018-08-10 | 车智互联(北京)科技有限公司 | Mock test methods based on SpringBoot services |
CN108427631A (en) * | 2017-02-14 | 2018-08-21 | 北京京东尚科信息技术有限公司 | A kind of application test systems, method, electronic equipment and readable storage medium storing program for executing |
CN108647143A (en) * | 2018-05-09 | 2018-10-12 | 平安普惠企业管理有限公司 | MOCK interface test methods, device, computer equipment and storage medium |
CN110147326A (en) * | 2019-05-23 | 2019-08-20 | 四川新网银行股份有限公司 | Micro services automated testing method based on Redis database |
Family Cites Families (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US8954929B2 (en) * | 2010-03-29 | 2015-02-10 | Microsoft Corporation | Automatically redirecting method calls for unit testing |
US9535823B2 (en) * | 2011-02-28 | 2017-01-03 | Typemock Ltd. | Method and apparatus for detecting software bugs |
US8966636B2 (en) * | 2012-10-16 | 2015-02-24 | International Business Machines Corporation | Transforming unit tests for security testing |
US11010283B2 (en) * | 2016-11-08 | 2021-05-18 | Microsoft Technology Licensing, Llc | Mock-based unit test(s) for an end-to-end test of a code snippet |
-
2019
- 2019-09-19 CN CN201910884316.4A patent/CN110633213B/en active Active
Patent Citations (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN101276271A (en) * | 2007-03-29 | 2008-10-01 | 北京邮电大学 | Method and interceptor system facing to tangent plane programming |
CN108427631A (en) * | 2017-02-14 | 2018-08-21 | 北京京东尚科信息技术有限公司 | A kind of application test systems, method, electronic equipment and readable storage medium storing program for executing |
CN108388519A (en) * | 2018-03-19 | 2018-08-10 | 车智互联(北京)科技有限公司 | Mock test methods based on SpringBoot services |
CN108647143A (en) * | 2018-05-09 | 2018-10-12 | 平安普惠企业管理有限公司 | MOCK interface test methods, device, computer equipment and storage medium |
CN110147326A (en) * | 2019-05-23 | 2019-08-20 | 四川新网银行股份有限公司 | Micro services automated testing method based on Redis database |
Non-Patent Citations (2)
Title |
---|
云服务自动化集成测试研究;柯栋梁;《系统仿真学报》;20131231;全文 * |
教育单元测试mock框架优化之路;网易数帆;《https://zhuanlan.zhihu.com/p/38471528》;20180625;全文 * |
Also Published As
Publication number | Publication date |
---|---|
CN110633213A (en) | 2019-12-31 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11010283B2 (en) | Mock-based unit test(s) for an end-to-end test of a code snippet | |
US9740585B2 (en) | Flexible configuration and control of a testing system | |
CN107526676B (en) | Cross-system test method and device | |
Zhou et al. | Towards a practical approach to test aspect-oriented software | |
CN111797026A (en) | Test case generation method and device, computer equipment and storage medium | |
CN111414309B (en) | Automatic test method of application program, computer equipment and storage medium | |
CN110633213B (en) | Unit test method, unit test device, computer equipment and storage medium | |
US20220294776A1 (en) | Formal verification method for certificate storage smart contract, computer device, and non-transitory computer-readable storage medium | |
CN112286828B (en) | Testing method and system for intelligent contracts of block chains | |
KR102588856B1 (en) | Method for verifying software and apparatus therefor | |
CN111897727A (en) | Software testing method and device, computer equipment and storage medium | |
CN113176995B (en) | Interface testing method, device, equipment and medium based on testing scene | |
CN109582583B (en) | Software testing method, device, computer equipment and storage medium | |
CN112181854A (en) | Method, device, equipment and storage medium for generating flow automation script | |
CN111159025B (en) | Application program interface testing method and device, computer equipment and storage medium | |
EP3608786B1 (en) | Systems and methods of requirements chaining and applications thereof | |
CN109815127B (en) | Automatic script conversion method and device, computer equipment and storage medium | |
CN116561003A (en) | Test data generation method, device, computer equipment and storage medium | |
CN115599683A (en) | Automatic testing method, device, equipment and storage medium | |
CN115114135B (en) | Software data testing method, device, equipment, medium and product | |
CN112416781B (en) | Cross-system interface determination method, device, computer equipment and storage medium | |
Liu et al. | A formal approach to testing programs in practice | |
CN110704218B (en) | Data processing method, data processing device, computer equipment and storage medium | |
CN114528213A (en) | Automatic baffle plate testing method, device, equipment and storage medium | |
CN113220586A (en) | Automatic interface pressure test execution method, device and 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 | ||
GR01 | Patent grant | ||
GR01 | Patent grant |