Unit testing method, device and storage medium

By analyzing and generating an abstract syntax tree during the compilation stage, and using the annotation processor to switch the associated class as a proxy class, the problem of difficult to simulate unexecutable attributes and judgment conditions in real-machine environments is solved, and unit testing efficiency is improved.

CN114840411BActive Publication Date: 2025-02-11ALIBABA (CHINA) CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210267915.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-03-17
Publication Date
2025-02-11
Estimated Expiration
2042-03-17

AI Technical Summary

Technical Problem

When performing unit testing in a real machine environment, it is difficult to simulate objects containing unexecutable attributes or complex judgment conditions, resulting in low testing efficiency and usually requires modifying the source code for testing.

Method used

By parsing the source code of the mock object and the source code used to generate the proxy class during the compilation stage, an abstract syntax tree is generated, and using the annotation processor to add statements, the associated class of the mock object is switched to the proxy class, so that no modification is made Unit testing is performed in the case of source code.

Benefits of technology

It realizes that the unexecutable attributes and judgment conditions are completed without modifying the source code of the mock object, which improves unit testing efficiency and enhances the comprehensiveness of the test.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114840411B_ABST
    Figure CN114840411B_ABST
Patent Text Reader

Abstract

The present disclosure relates to a unit testing method, device and storage medium. The present disclosure generates an abstract syntax tree by parsing the source code of a simulation object and the source code for generating a proxy class in a compilation phase; adds a first statement in the abstract syntax tree by using an annotation processor, and uses the first statement to switch the associated class of the simulation object including non-executable attributes to the proxy class including executable attributes corresponding to the simulation object, thereby realizing the use of the proxy class to replace the associated class of the simulation object to participate in the unit test, so that the test including non-executable attributes is completed without modifying the source code of the simulation object, thereby improving the unit testing efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a unit testing method, device and storage medium. Background Art

[0002] Unit testing is a low-level testing activity to be performed during the software development process. During unit testing, independent units of the software will be tested in isolation from the rest of the program. During unit testing, the code to be tested has a lot of complex specific data and specific environments, and the interfaces are directly dependent on each other. For some data that is not easy to construct or obtain, a virtual object can be created for testing. This virtual object is called a mock object. A mock object is a substitute for an actual object during unit testing.

[0003] In the prior art, in the scenario of testing in a real machine environment, a simulation test framework is usually used to create a simulation object to replace the original object. For example, for Android, the Mockito simulation test framework can be used. However, when using the method of creating simulation objects based on the simulation test framework, some non-executable attributes cannot be simulated. Exemplarily, the non-executable attributes include the following two types: one is that objects containing non-simulatable attributes cannot be simulated, and the other is that it is difficult or impossible to create test data for simulation for judgment conditions of complex logic; for the above two situations, the prior art usually tests by modifying the relevant source code, and then restores the modified source code after the test is completed, resulting in low test efficiency. Summary of the invention

[0004] In order to solve the above technical problems or at least partially solve the above technical problems, the embodiments of the present disclosure provide a unit testing method, device, electronic device and storage medium, which can complete unit testing without modifying the source code, thereby improving testing efficiency.

[0005] In a first aspect, an embodiment of the present disclosure provides a unit testing method, including:

[0006] Obtaining source code of a simulation object and source code for generating a proxy class; wherein the simulation object includes a non-executable attribute;

[0007] Parsing the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree;

[0008] Adding a first statement at a preset position of the abstract syntax tree using an annotation processor, wherein the first statement is used to switch an associated class of a simulation object in the abstract syntax tree to the proxy class;

[0009] Generate a bytecode file based on the abstract syntax tree to which the first statement is added, wherein the bytecode file includes the proxy class;

[0010] Run the bytecode file to perform unit testing.

[0011] In a second aspect, an embodiment of the present disclosure provides a unit testing device, including:

[0012] An acquisition module, used to acquire source code of a simulation object and source code for generating a proxy class; wherein the simulation object includes a non-executable attribute;

[0013] A parsing module, used for parsing the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree;

[0014] An adding module, configured to add a first statement at a preset position of the abstract syntax tree using an annotation processor, wherein the first statement is used to switch an associated class of a simulation object to a proxy class;

[0015] A generating module, configured to generate a bytecode file based on the abstract syntax tree to which the first statement is added, wherein the bytecode file includes the proxy class;

[0016] The running module is used to run the bytecode file to perform unit testing.

[0017] In a third aspect, an embodiment of the present disclosure further provides an electronic device, comprising: a processor; a memory for storing instructions executable by the processor; the processor for reading the executable instructions from the memory and executing the executable instructions to implement any of the unit testing methods provided in the embodiments of the present disclosure.

[0018] In a fourth aspect, an embodiment of the present disclosure further provides a computer-readable storage medium, wherein the storage medium stores a computer program, and the computer program is used to execute any of the unit testing methods provided by the embodiments of the present disclosure.

[0019] The unit testing method, device, electronic device and storage medium provided by the embodiments of the present disclosure generate an abstract syntax tree by parsing the source code of the simulation object and the source code for generating the proxy class in the compilation stage; adding a first statement in the abstract syntax tree by using the annotation processor, and using the first statement to switch the associated class of the simulation object including the non-executable attribute to the proxy class including the executable attribute corresponding to the simulation object, thereby realizing the use of the proxy class instead of the associated class of the simulation object to participate in the unit test, so that the test including the non-executable attribute is completed without modifying the source code of the simulation object, thereby improving the unit testing efficiency

[0020] In some embodiments, the non-executable attribute may include a non-simulatable attribute or a judgment condition. For a simulation object including a non-simulatable attribute, the unit testing method, device, electronic device and storage medium provided by the embodiments of the present disclosure implement the removal of the non-simulatable attribute in the proxy class, and switch the associated class of the simulation object including the non-simulatable attribute to the proxy class for execution. Therefore, the test of the simulation object including the non-simulatable attribute can be completed without modifying the source code of the simulation object, thereby improving the unit testing efficiency. For a simulation object including a judgment condition, the unit testing method, device, electronic device and storage medium provided by the embodiments of the present disclosure implement the replacement of the judgment condition with a function with a preset return value in the proxy class, and switch the associated class of the simulation object including the judgment condition to the proxy class for execution. Therefore, the test of the simulation object including the judgment condition can be completed without modifying the source code of the simulation object, thereby improving the unit testing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0022] Figure 1 A flowchart of a unit testing method provided by an embodiment of the present disclosure;

[0023] Figure 2 for Figure 1 A schematic diagram of the technical implementation of the illustrated embodiment;

[0024] Figure 3 A flowchart of another unit testing method provided by an embodiment of the present disclosure;

[0025] Figure 4 for Figure 3 A schematic diagram of the technical implementation of the illustrated embodiment;

[0026] Figure 5 This is a schematic diagram of an implementation process of a specific embodiment of the present disclosure;

[0027] Figure 6 This is a schematic diagram of an implementation process of another specific embodiment of the present disclosure;

[0028] Figure 7 A schematic diagram of the structure of a unit testing device provided in an embodiment of the present disclosure;

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

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

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

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

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

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

[0035] Normally, in the scenario of testing in a real machine environment, a simulation test framework is usually used to create a simulation object to replace the original object. However, when using the simulation test framework to create a simulation object, some non-executable attributes cannot be simulated. For example, the non-executable attributes include the following two types: one is that the simulation object containing non-simulatable attributes cannot be simulated, and the other is that it is difficult or impossible to create test data for simulation for the judgment conditions of complex logic; for the above two situations, the prior art usually tests by modifying the relevant source code, and then restores the modified source code after the test is completed, resulting in low test efficiency.

[0036] The technical solution provided by the embodiment of the present disclosure generates an abstract syntax tree by parsing the source code of the simulation object and the source code for generating the proxy class during the compilation stage; using the annotation processor to add a first statement in the abstract syntax tree, and using the first statement to switch the associated class of the simulation object including non-executable attributes to the proxy class including executable attributes corresponding to the simulation object, thereby realizing the use of the proxy class instead of the associated class of the simulation object to participate in the unit test, so that the test including non-executable attributes is completed without modifying the source code of the simulation object, thereby improving the unit test efficiency.

[0037] Among them, for simulation objects including non-simulatable properties, the technical solution provided by the embodiments of the present disclosure realizes the removal of non-simulatable properties in the proxy class, for example, using a hook function to switch the associated class of the simulation object including non-simulatable properties to the proxy class for execution; therefore, the test of the simulation object including non-simulatable properties can be completed without modifying the source code of the simulation object, thereby improving the unit test efficiency. For simulation objects including judgment conditions, the technical solution provided by the embodiments of the present disclosure realizes the replacement of the judgment conditions with functions with preset return values ​​in the proxy class, for example, using a hook function to switch the associated class of the simulation object including judgment conditions to the proxy class for execution; therefore, the test of the simulation object including judgment conditions can be completed without modifying the source code of the simulation object, thereby improving the unit test efficiency.

[0038] The technical solutions for the above two situations are respectively described through the following two embodiments.

[0039] In the first aspect, for the case where an object containing non-simulatable properties cannot be simulated, the embodiment of the present disclosure provides a unit testing method. Figure 1 A flow chart of a unit testing method provided for an embodiment of the present disclosure can be applied to the case of unit testing in a real machine environment. The unit testing method can be executed by a unit testing device, which can be implemented in software and / or hardware and can be integrated on any electronic device with computing capabilities, such as a terminal, specifically including a smart phone, a PDA, a tablet computer, a wearable device with a display screen, a laptop computer, etc.

[0040] like Figure 1 As shown, the unit testing method provided by the embodiment of the present disclosure mainly includes the following steps:

[0041] S101. Obtain source code of a simulation object and source code for generating a proxy class; wherein the simulation object includes non-simulatable properties.

[0042] Mocking frameworks usually simulate by creating anonymous subclasses, so any aspects of the subclass that cannot be broken by the parent class will also exist on the subclass, making the anonymous subclass impossible to simulate. Therefore, when using mock testing frameworks such as Mockito for simulation, some objects cannot be simulated because they include non-simulatable properties. Therefore, it is necessary to process objects that include non-simulatable properties in order to achieve the purpose of simulation.

[0043] In this step, the source code of the simulation object in this embodiment includes the source code of the attributes that cannot be simulated using the simulation test framework, and the proxy class removes the attributes that cannot be simulated.

[0044] Optionally, the source code of the mock object includes the private, static or final keywords, so that it includes non-mockable properties. Among them, private, static or final are commonly used keywords in the development process and can be used to modify classes, methods, interfaces, properties, etc. Due to the characteristics of the above keywords themselves, such as restrictions on access rights, mock objects of classes, methods, interfaces, and properties modified with the above keywords cannot be simulated when using mock testing frameworks such as Mockito for simulation.

[0045] In this embodiment, the proxy in the proxy class provides a proxy object for the simulation object to control access to the simulation object. The proxy class is responsible for preprocessing messages for the proxied class, filtering messages and forwarding messages, and performing subsequent processing after the message is executed by the proxy class.

[0046] S102: Parse the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree.

[0047] Specifically, in this embodiment, parsing the source code of the simulation object and the source code for generating the proxy class includes the following steps:

[0048] The first is lexical analysis, which reads the source code byte by byte to find the language keywords defined in the lexical structure. The result of lexical analysis is to find some standardized token streams from the source code, and distinguish which are punctuation marks, which are verbs, and which are nouns.

[0049] The second is syntax analysis, which is to analyze the token stream obtained from the lexical analysis to check whether the combination of these keywords conforms to the Java language specification. The result of syntax analysis is to form an abstract syntax tree that conforms to the Java language specification.

[0050] It can be understood that the abstract syntax tree is a structured grammatical expression. Its function is to organize the main vocabulary of the language in a structured form. When necessary, the abstract syntax tree can be reorganized according to new rules.

[0051] S103: Use an annotation processor to add a first statement at a preset position of the abstract syntax tree, wherein the first statement is used to switch an associated class of a simulation object in the abstract syntax tree to a proxy class.

[0052] In this step, the annotation processor is a tool of the JAVA language compiler (javac), which is used to scan and process the classes marked with annotations during compilation. You can use the default annotation processor in java or customize the annotation processor.

[0053] The disclosed embodiment implements a custom annotation processor to parse the abstract syntax tree corresponding to the source code of the simulation object annotated with a specific annotation, and then uses the aspect technology to add a first statement in a preset position of the abstract syntax tree. The first statement is used to switch the simulation operation corresponding to the simulation object in the abstract syntax tree to the proxy class for execution.

[0054] In some embodiments, the first statement may be a hook function.

[0055] Specifically, a set of event distribution mechanisms are maintained in various operating systems. The hook function is a technology used to change the execution result of the Application Programming Interface (API), and can redirect the execution of the system's API function.

[0056] It can be understood that in the embodiment of the present disclosure, a hook function is used to switch the simulation operation corresponding to the simulation object to the proxy class for execution.

[0057] S104: Generate a bytecode file based on the abstract syntax tree to which the first statement is added, where the bytecode file includes a proxy class.

[0058] In the disclosed embodiment, the proxy class may be generated based on the associated class of the simulation object and the source code for generating the proxy class, and the proxy class includes a public keyword.

[0059] Among them, when the simulation object is at the class level, that is, the entire class is simulated, and the associated class of the simulation object is the simulation object itself; when the simulation object is at the method or interface level, that is, the method or interface is simulated, and the associated class of the above simulation object is the class where the method or interface is located; when the simulation object is at the attribute level, that is, the attribute is simulated, the associated class of the simulation object is the class where the attribute is located, and the proxy class needs to implement all functions of the associated class of the simulation object based on the associated class of the simulation object. The disclosed embodiment does not limit the level of the simulation object.

[0060] Specifically, the source code for generating the proxy class defines the logic for generating the proxy class. The logic for generating the source code for generating the proxy class includes replacing or removing keywords such as private, static or final in the simulation object.

[0061] In some embodiments, the proxy class may include a public keyword. Public is a commonly used keyword in the development process, which can be used to modify classes, methods, interfaces, and properties. Because of the characteristics of the keyword itself, users can simulate when using a simulation test framework such as Mockito. For example, the simulation object includes a private keyword, and the source code used to generate the proxy defines that if the simulation object contains the private keyword, the private keyword is replaced with the public keyword. Therefore, the proxy class generated based on the above source code for generating the proxy class includes the public keyword.

[0062] In other embodiments, the private, static or final keywords are removed from the proxy class. For example, if the simulation object contains the final keyword, the source code used to generate the proxy class defines that if the simulation object contains the final keyword, the final keyword is removed from the proxy class. Therefore, the proxy class generated based on the above source code for generating the proxy class removes the final keyword.

[0063] In some embodiments, the proxy class can be generated during compilation according to the associated class of the simulation object and the source code for generating the proxy class. Specifically, the proxy class can be generated using a code generation framework (eg, javapoet).

[0064] In other embodiments, the proxy class can generate bytecodes corresponding to the associated classes of the simulation objects and bytecodes for generating the proxy class that conform to the Java virtual machine specification according to the associated classes of the simulation objects and the source code for generating the proxy class through a code generator, for example, the bytecodes are .class files. The bytecodes for generating the proxy class are loaded by using Javassist, ASM, etc., and the bytecodes corresponding to the associated classes of the simulation objects are parsed to generate the proxy class. The present disclosure does not limit the method for generating the proxy class.

[0065] Then a bytecode file is generated, for example, a bytecode file is a .dex file. It is understandable that the bytecode file includes a proxy class, which implements all functions of the associated class of the simulated object on the one hand, and replaces or removes the private, static or final keywords in the source code of the simulated object on the other hand.

[0066] S105. Run the above bytecode file to perform unit testing.

[0067] In this step, unit testing is implemented by running a bytecode file, which includes the bytecode corresponding to the source code of the simulation object, the bytecode corresponding to the source code for generating the proxy class, the bytecode corresponding to the first statement, and the bytecode of the proxy class. The first statement can switch the associated class of the simulation object to the proxy class, thereby implementing unit testing without changing the source code of the simulation object.

[0068] Figure 2 for Figure 1 The technical implementation principle diagram of the embodiment shown in FIG. Figure 2 As shown, when performing unit testing, it is necessary to call (Call) the source code to be tested. For some data that is not easy to construct or cannot be constructed, a mock object can be simulated (Mock) for testing. The above-mentioned mock object may include non-simulatable properties, that is, the above-mentioned mock object in the embodiment of the present disclosure, and its source code includes private, static or final keywords. For the associated class (Class1) of the above-mentioned mock object, in this embodiment, the annotation processor (APT) is used to add the first statement by operating the abstract syntax tree, and the mock object associated class is switched to the proxy class (Class_mock1). In the generated proxy class, private, static or final keywords have been replaced or removed. For example, the proxy class can include the Public keyword, and the proxy class is used to replace (real mock) the associated class of the mock object to participate in the unit test, so that the simulation of the mock object including non-simulatable properties is completed without modifying the source code of the mock object, thereby improving the efficiency of the unit test.

[0069] Secondly, for complex logic judgment conditions, it is difficult to generate test data or impossible to generate data for simulation under the existing test framework. The embodiment of the present disclosure provides another unit testing method. Figure 3 A flow chart of another unit testing method provided by an embodiment of the present disclosure, the method specifically includes the following steps: Figure 3 The following steps are shown:

[0070] S301. Obtain source code of a simulation object and source code for generating a proxy class; wherein the simulation object includes a judgment condition.

[0071] In this step, the source code of the simulation object in this embodiment includes: source code containing judgment conditions, and the proxy class includes a function with a preset return value.

[0072] Optionally, the source code of the simulation object includes a single judgment condition and / or a compound judgment condition.

[0073] In this embodiment, the proxy in the proxy class provides a proxy object for the simulation object to control access to the simulation object. The proxy class is responsible for preprocessing messages for the proxied class, filtering messages and forwarding messages, and performing subsequent processing after the messages are executed by the proxied class.

[0074] S302: Parse the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree.

[0075] Specifically, in this embodiment, parsing the source code of the simulation object and the source code for generating the proxy class includes the following steps:

[0076] The first is lexical analysis, which reads the source code byte by byte to find the language keywords defined in the lexical structure. The result of lexical analysis is to find some standardized token streams from the source code, and distinguish which are punctuation marks, which are verbs, and which are nouns.

[0077] The second is syntax analysis, which is to analyze the token stream obtained from the lexical analysis to check whether the combination of these keywords conforms to the Java language specification. The result of syntax analysis is to form an abstract syntax tree that conforms to the Java language specification.

[0078] It can be understood that the abstract syntax tree is a structured grammatical expression. Its function is to organize the main vocabulary of the language in a structured form. When necessary, the abstract syntax tree can be reorganized according to new rules.

[0079] S303: Use an annotation processor to add a first statement at a preset position of the abstract syntax tree, wherein the first statement is used to switch an associated class of a simulation object in the abstract syntax tree to a proxy class.

[0080] In this step, the annotation processor may be a tool of the JAVA language compiler (javac), which is used to scan and process the classes marked with annotations during compilation. There is a default annotation processor in java, and a custom annotation processor may also be provided.

[0081] The disclosed embodiment implements a custom annotation processor to parse the abstract syntax tree corresponding to the source code of the simulation object annotated with a specific annotation, and then uses the aspect technology to add a first statement in a preset position of the abstract syntax tree. The first statement is used to switch the simulation operation corresponding to the simulation object in the abstract syntax tree to the proxy class for execution.

[0082] In some embodiments, the first statement may be a hook function.

[0083] Specifically, a set of event distribution mechanisms are maintained in various operating systems. The hook function is a technology used to change the result of API execution and can redirect the execution of the system's API functions.

[0084] It can be understood that in the embodiment of the present disclosure, a hook function is used to switch the simulation operation corresponding to the simulation object to the proxy class for execution.

[0085] S304: Generate a bytecode file based on the abstract syntax tree to which the first statement is added, where the bytecode file includes a proxy class.

[0086] In the disclosed embodiment, the proxy class may be generated based on the associated class of the simulation object and the source code for generating the proxy class, and the proxy class includes a function with a preset return value of true, false or a specific value.

[0087] Among them, when the simulation object is at the class level, that is, the entire class is simulated, and the associated class of the simulation object is the simulation object itself; when the simulation object is at the method or interface level, that is, the method or interface is simulated, and the associated class of the above simulation object is the class where the method or interface is located; when the simulation object is at the attribute level, that is, the attribute is simulated, the associated class of the simulation object is the class where the attribute is located, and the proxy class needs to implement all functions of the simulation object's associated class based on the simulation object's associated class. The disclosed embodiment does not limit the level of the simulation object.

[0088] The above source code for generating a proxy class defines the logic for generating a proxy class, and the logic for generating a proxy class includes replacing the judgment condition with a function having a preset return value of true, false or a specific value.

[0089] Exemplarily, in some embodiments, the entire judgment condition is first replaced with a boolean member function, and then the function is simulated into the required return value. Wherein, boolean is a data type, and its return value is yes (true) or no (false). For example: replace the judgment condition with a boolean member function, example: if (condtion) is replaced with if (condtionFun ()), and then condtionFun () is simulated into the required true / false. It can be understood that condtion is the judgment condition, and condtionFun () is a member function with a return value of true / false. When condtion needs to be simulated, it is only necessary to simulate condtionFun ().

[0090] In other embodiments, the judgment condition may be replaced with a function of a specific value, and then the specific value may be simulated as a required return value. For example, the judgment condition may be replaced with a function, and then the return value of the function may be set to a specific value 1. Therefore, when the judgment condition needs to be simulated, only the above function needs to be simulated.

[0091] In some embodiments, the proxy class can be generated during compilation according to the associated class of the simulation object and the source code for generating the proxy class. Specifically, the proxy class can be generated using a code generation framework (eg, javapoet).

[0092] In other embodiments, the proxy class can be generated by a code generator according to the associated class of the simulation object and the source code for generating the proxy class to generate the bytecode corresponding to the associated class of the simulation object and the bytecode for generating the proxy class that complies with the Java virtual machine specification, for example, the bytecode is a .class file. The bytecode for generating the proxy class is loaded and parsed using Javassist, ASM, etc. to generate the bytecode corresponding to the associated class of the simulation object.

[0093] Then a bytecode file is generated, for example, a bytecode file is a .dex file. It can be understood that the bytecode file includes a proxy class, which implements all functions of the simulated object association class on the one hand, and realizes the function of replacing the simulated object with the simulated object on the other hand.

[0094] S305: Run the above bytecode file to perform unit testing.

[0095] In this step, the unit test is implemented by running the bytecode file, which includes the bytecode corresponding to the source code of the simulation object, the bytecode corresponding to the source code for generating the proxy class, the bytecode corresponding to the first statement, and the bytecode of the proxy class. The first statement can switch the associated class of the simulation object to the proxy class, thereby implementing unit testing without changing the source code of the simulation object.

[0096] Figure 4 for Figure 3 The technical implementation principle diagram of the embodiment shown in FIG. Figure 4 As shown, when performing unit testing, it is necessary to call (Call) the source code to be tested. For some data that is not easy to construct or cannot be constructed, a simulation object can be simulated (Mock) for testing. The above-mentioned simulation object may include a single or compound judgment condition. For the association class (Class2) of the above-mentioned simulation object, in this embodiment, the annotation processor (APT) is used to add the first statement by operating the abstract syntax tree, and the simulation object association class is switched to the proxy class (Class_mock2). In the generated proxy class, the judgment condition has been replaced with a function with a preset return value of true, false or a specific value, so that the proxy class is used to replace (real_mock) the association class of the simulation object to participate in the unit test. It makes it possible to simulate complex judgment conditions without modifying the source code of the simulation object, thereby improving the efficiency of unit testing.

[0097] In addition, through the unit testing method in the embodiment of the present disclosure, the judgment condition is replaced with a function with a preset return value of true, false or a specific value, so that more branches can be tested, thereby making the unit test more comprehensive.

[0098] Figure 5 FIG. 1 is a schematic diagram of an implementation process of a specific embodiment of the present disclosure, such as Figure 5 As shown, the implementation process includes the following:

[0099] First, before performing unit testing, it is necessary to obtain source code related to the unit testing, such as S101 and S301 in the above two embodiments. The above steps can be simply summarized as obtaining simulation objects and source code for generating proxy classes.

[0100] Secondly, the source code (.java) is read by the JAVA language compiler (javac) for compilation. During the compilation process, the source code is subjected to lexical and grammatical analysis to generate an abstract syntax tree. The annotation processor (APT) is used to scan the annotations and parse the abstract syntax tree to add hook functions. For example, S103 and S303 in the above two embodiments are simply summarized as adding a hook function at a preset position of the abstract syntax tree using the annotation processor;

[0101] Then, the proxy class is generated according to the source code used to generate the proxy class. For example, in S104 and S404 in the above two embodiments, the proxy class of the simulation object is generated according to the code generation framework (for example, javapoet), and then compiled into a .class file, and then compiled to generate an executable .Dex file in the real machine environment (for example, Android system).

[0102] Finally, the .dex file is run to complete the unit test. For example, in the above two embodiments, S105 and S305, the above steps are simply summarized as running the .dex file to complete the unit test.

[0103] Figure 6 FIG. 1 is a schematic diagram of an implementation process of another specific embodiment of the present disclosure, such as Figure 6 As shown, the implementation process includes the following:

[0104] First, before performing unit testing, it is necessary to obtain source code related to the unit testing, such as S101 and S301 in the above two embodiments. The above steps can be simply summarized as obtaining simulation objects and source code for generating proxy classes.

[0105] Secondly, the source code (.java) is read by the JAVA language compiler (javac) for compilation, and the source code is subjected to lexical and grammatical analysis during the compilation process to generate an abstract syntax tree, and the annotation processor (APT) is used to scan the annotation and parse the abstract syntax tree to add hook functions, and the JAVA compiler is used to analyze the syntax tree to generate a .class file. For example, S103 and S303 in the above two embodiments are simply summarized as using the annotation processor to add a hook function at a preset position in the abstract syntax tree.

[0106] Then, according to Javassist, ASM, etc., load and parse the .Class file to generate a proxy class; compile and generate a .Dex file that can be executed in a real machine environment (such as an Android system). For example, in S104 and S404 in the above two embodiments, Javassist, ASM, etc. are used to load and parse the .class file to generate a proxy class for the simulation object, and compile and generate a .Dex file that can be executed in a real machine environment (such as an Android system).

[0107] Finally, the .dex file is run to complete the unit test. For example, in the above two embodiments, S105 and S305, the above steps are simply summarized as running the .dex file to complete the unit test.

[0108] In some embodiments, the above two embodiments can also be used in combination, that is, replacing or removing the private, static, and final keywords in the proxy class and replacing the judgment conditions with functions with preset return values ​​of true, false or specific values. The present disclosure does not limit the use of the above two embodiments alone or in combination.

[0109] Figure 7 A schematic diagram of the structure of a unit test device provided in an embodiment of the present disclosure can be applied to testing a real machine environment. The device can be implemented in software and / or hardware and can be integrated on any electronic device with computing capabilities, such as a terminal, specifically including a smart phone, a PDA, a tablet computer, a wearable device with a display screen, a laptop computer, etc.

[0110] like Figure 7 As shown, the unit testing device 70 provided in the embodiment of the present disclosure includes:

[0111] The acquisition module 71 is used to acquire the source code of the simulation object and the source code for generating the proxy class; wherein the simulation object includes a non-executable attribute. .

[0112] The parsing module 72 is used to parse the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree.

[0113] The adding module 73 is used to add a first statement at a preset position of the abstract syntax tree by using an annotation processor, wherein the first statement is used to switch an associated class of the simulation object to a proxy class.

[0114] The generating module 74 is configured to generate a bytecode file based on the abstract syntax tree to which the first statement is added, wherein the bytecode file includes the proxy class.

[0115] The running module 75 is used to run the bytecode file to perform unit testing.

[0116] Optionally, the non-executable attribute is a non-simulatable attribute, and the proxy class removes the non-simulatable attribute.

[0117] Optionally, the source code of the above simulation object includes private, static or final keywords.

[0118] Optionally, the proxy class is generated based on an associated class of the simulation object and source code for generating the proxy class, and the proxy class includes a public keyword.

[0119] Optionally, the non-executable attribute is a judgment condition, and the proxy class includes a function with a preset return value. Optionally, the source code of the simulation object includes a single judgment condition and / or a compound judgment condition.

[0120] Optionally, the proxy class is generated based on an associated class of the simulation object and source code for generating the proxy class, and the proxy class includes a function with a preset return value of true, false or a specific value.

[0121] Optionally, the first statement above is a hook function.

[0122] Figure 8 A structural diagram of an electronic device provided for an embodiment of the present disclosure is used to exemplarily illustrate an electronic device that implements any unit testing method in the embodiment of the present disclosure, and should not be understood as a specific limitation on the embodiment of the present disclosure.

[0123] like Figure 8 As shown, the electronic device 800 may include a processor (e.g., a CPU, a graphics processor, etc.) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage device 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the electronic device 800 are also stored. The processor 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.

[0124] Typically, the following devices may be connected to the I / O interface 805: input devices 806 including, for example, a touch screen, a touchpad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; output devices 807 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; storage devices 808 including, for example, a magnetic tape, a hard disk, etc.; and communication devices 809. The communication device 809 may allow the electronic device 800 to communicate with other devices wirelessly or by wire to exchange data. Although an electronic device 800 having various devices is shown, it should be understood that it is not required to implement or have all the devices shown. More or fewer devices may be implemented or have alternatively.

[0125] In particular, according to an embodiment of the present disclosure, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present disclosure includes a computer program product, which includes a computer program carried on a non-transitory computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device 809, or installed from a storage device 808, or installed from a ROM 802. When the computer program is executed by the processor 801, the functions defined in any unit test method provided in the embodiment of the present disclosure can be executed.

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

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

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

[0129] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: parses the source code file to generate an abstract syntax tree; edits the statements corresponding to the simulation objects in the abstract syntax tree; processes the edited abstract syntax tree to obtain a bytecode file; and performs unit testing on the application to be tested by running the bytecode file.

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

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

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

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

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

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

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

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

Claims

1. A unit testing method, comprising: Obtaining source code of a simulation object and source code for generating a proxy class; wherein the simulation object includes a non-executable attribute; Parsing the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree; Adding a first statement at a preset position of the abstract syntax tree using an annotation processor, wherein the first statement is used to switch an associated class of a simulation object in the abstract syntax tree to the proxy class; Generate a bytecode file based on the abstract syntax tree to which the first statement is added, wherein the bytecode file includes the proxy class; Run the bytecode file to perform unit testing.

2. The method according to claim 1, wherein: The non-executable attribute is an unsimulatable attribute, and the proxy class removes the unsimulatable attribute.

3. The method according to claim 2, wherein: The source code of the simulation object includes private, static or final keywords.

4. The method according to claim 3, wherein: The proxy class is generated based on the associated class of the simulation object and the source code used to generate the proxy class, and the proxy class only includes a public keyword.

5. The method according to claim 1, wherein: The non-executable attribute is a judgment condition, and the proxy class includes a function with a preset return value.

6. According to the method of claim 5, the source code of the simulation object includes a single judgment condition and / or a compound judgment condition.

7. The method according to claim 5, wherein: The proxy class is generated based on the associated class of the simulation object and the source code used to generate the proxy class. The proxy class includes a function with a preset return value of true, false or a specific value.

8. The method according to claim 1, wherein: The first statement is a hook function.

9. A unit testing device, comprising: An acquisition module, used to acquire source code of a simulation object and source code for generating a proxy class; wherein the simulation object includes a non-executable attribute; A parsing module, used for parsing the source code of the simulation object and the source code for generating the proxy class to generate an abstract syntax tree; An adding module, configured to add a first statement at a preset position of the abstract syntax tree using an annotation processor, wherein the first statement is used to switch an associated class of a simulation object to a proxy class; A generating module, configured to generate a bytecode file based on the abstract syntax tree to which the first statement is added, wherein the bytecode file includes the proxy class; The running module is used to run the bytecode file to perform unit testing.

10. A computer-readable storage medium storing a computer program, wherein the computer program is used to execute the unit testing method according to any one of claims 1 to 8.

Citation Information

Patent Citations

  • Interface test method and device, computer equipment and storage medium

    CN112363941A

  • Unit test method, device and system and storage medium

    CN114168466A