Code testing processing method, device, electronic equipment and storage medium

By introducing custom annotation and annotation processors into code testing, automatically generating and executing test code, the problem of manual addition of test portals in the existing technology affecting efficiency, and the automation and efficiency of code testing are achieved.

CN113867704BActive Publication Date: 2025-04-25WUBA
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111014666.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-31
Publication Date
2025-04-25
Estimated Expiration
2041-08-31

AI Technical Summary

Technical Problem

The existing code testing methods require manual addition of test portals to affect the efficiency of code testing.

Method used

By introducing custom annotations and annotation processors into the source code of the target module to be tested, adding custom annotations to the target method, parsing the annotations at compile time generates the object code, and automatically executing the object code at the time of project startup to obtain the test results.

Benefits of technology

It realizes automatic execution of code to be tested when the project starts, reducing the time to manually add test portals and improving the efficiency of code testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113867704B_ABST
    Figure CN113867704B_ABST
Patent Text Reader

Abstract

The present invention provides a code test processing method, device, electronic device and storage medium. The method comprises: introducing a custom annotation into the source code of a target module to be tested, and an annotation processor for processing the custom annotation, and adding the custom annotation to the target method to be tested in the target module, so as to mark the target method with the custom annotation; compiling the project to which the target module belongs, and in the compilation process of the target module, parsing the custom annotation with the annotation processor to obtain the target code to be tested; when the project is started, automatically executing the target code, and returning the execution result of the target code to obtain the test result of the target code; wherein the custom annotation is an annotation that can act on a class method and is used to mark the class method to be tested, and the target module is any code module in the project.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a code testing processing method, device, electronic equipment and storage medium. Background Art

[0002] With the development of computer technology, many development platforms can provide code testing environments. For example, Android provides its own set of Java testing and Android testing environments.

[0003] However, the test framework can generally only be used in the main program / main project, but cannot well support the code testing of each single module that the main project depends on. For example, if module A depends on modules B and C, then if you want to test whether the code in module B can run normally in module A, you often need to manually add the test entry, which affects the efficiency of code testing. Summary of the invention

[0004] The embodiments of the present invention provide a code testing processing method, device, electronic device and storage medium to solve the problem that the existing code testing method requires manual addition of test entries each time a test is performed, which affects the code testing efficiency.

[0005] In order to solve the above-mentioned technical problems, the present invention is achieved as follows:

[0006] In a first aspect, an embodiment of the present invention provides a code testing processing method, including:

[0007] Introducing a custom annotation into the source code of the target module to be tested, and an annotation processor for processing the custom annotation, and adding the custom annotation to the target method to be tested in the target module, so as to annotate the target method with the custom annotation;

[0008] Compile the project to which the target module belongs, and during the compilation process of the target module, parse the custom annotation through the annotation processor to obtain the target code to be tested;

[0009] When the project is started, the target code is automatically executed, and the execution result of the target code is returned to obtain the test result of the target code;

[0010] The custom annotation is an annotation that can be applied to a class method and is used to mark the class method to be tested, and the target module is any code module in the project.

[0011] Optionally, during the compilation process of the target module, the step of parsing the custom annotation by the annotation processor to obtain the target code to be tested includes:

[0012] During the compilation process of the target module, the annotation processor performs the following steps to parse the custom annotation to obtain the target code to be tested:

[0013] S1, initializing the annotation processor;

[0014] S2, obtaining all custom annotations in the target module processed by the annotation processor;

[0015] S3, obtaining all methods annotated by the custom annotation;

[0016] S4, calling the method annotated by the custom annotation through reflection to generate the target code.

[0017] Optionally, before the step of compiling the project to which the target module belongs and, during the compilation process of the target module, parsing the custom annotation by the annotation processor to obtain the target code to be tested, the method further includes:

[0018] The target source code to be tested is obtained, and the target source code is entered into the target method, so that during the compilation process of the target module, the custom annotation is parsed by the annotation processor to obtain the target code.

[0019] Optionally, before the step of introducing a custom annotation into the source code of the target module to be tested, and an annotation processor for processing the custom annotation, and adding the custom annotation to the target method to be tested in the target module, the method further includes:

[0020] The custom annotation is constructed in the project, as well as an annotation processor for processing the custom annotation.

[0021] Optionally, the step of automatically executing the target code and returning the execution result of the target code when the project is started includes:

[0022] When the project is started, the target method is called at the entry of the project startup to automatically execute the target code and return the execution result of the target code;

[0023] The target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

[0024] Optionally, the project includes a project built based on a compilable language, and the compilable language includes at least one of Java and Kotlin.

[0025] In a second aspect, an embodiment of the present invention provides a code testing processing device, including:

[0026] A code annotation module, used to introduce custom annotations into the source code of the target module to be tested, and an annotation processor for processing the custom annotations, and adding the custom annotations to the target methods to be tested in the target module, so as to annotate the target methods with the custom annotations;

[0027] A code compilation module, used to compile the project to which the target module belongs, and in the compilation process of the target module, parse the custom annotation through the annotation processor to obtain the target code to be tested;

[0028] A code testing module, used to automatically execute the target code when the project is started, and return the execution result of the target code to obtain the test result of the target code;

[0029] The custom annotation is an annotation that can be applied to a class method and is used to mark the class method to be tested, and the target module is any code module in the project.

[0030] Optionally, the code compilation module is specifically used to:

[0031] During the compilation process of the target module, the annotation processor performs the following steps to parse the custom annotation to obtain the target code to be tested:

[0032] S1, initializing the annotation processor;

[0033] S2, obtaining all custom annotations in the target module processed by the annotation processor;

[0034] S3, obtaining all methods annotated by the custom annotation;

[0035] S4, calling the method annotated by the custom annotation through reflection to generate the target code.

[0036] Optionally, the device further comprises:

[0037] The test code injection module is used to obtain the target source code to be tested and enter the target source code into the target method, so that during the compilation process of the target module, the custom annotation is parsed by the annotation processor to obtain the target code.

[0038] Optionally, the device further comprises:

[0039] An annotation tool creates a module for constructing the custom annotation in the project, and an annotation processor for processing the custom annotation.

[0040] Optionally, the code testing module is specifically used to:

[0041] When the project is started, the target method is called at the entry of the project startup to automatically execute the target code and return the execution result of the target code;

[0042] The target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

[0043] Optionally, the project includes a project built based on a compilable language, and the compilable language includes at least one of Java and Kotlin.

[0044] In a third aspect, an embodiment of the present invention further provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the steps of the code testing processing method described in the first aspect are implemented.

[0045] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the code testing processing method described in the first aspect are implemented.

[0046] In the embodiment of the present invention, the target code to be tested is generated by compiling annotations and annotation processors, so that the code to be tested is automatically executed after the project is started. For the code to be tested, only one line of annotation needs to be added and the test code is written, so that the test of the code to be tested can be completed quickly, and the intrusion to the project interface is small. BRIEF DESCRIPTION OF THE DRAWINGS

[0047] Figure 1 is a flowchart of a code testing method in an embodiment of the present invention;

[0048] Figure 2 is a flowchart of another code testing processing method in an embodiment of the present invention;

[0049] Figure 3 is a structural schematic diagram of a code test processing device in an embodiment of the present invention;

[0050] Figure 4 It is a schematic diagram of a calling sequence diagram in a code testing process in an embodiment of the present invention. DETAILED DESCRIPTION

[0051] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0052] Reference Figure 1 , shows a step flow chart of a code testing processing method in an embodiment of the present invention.

[0053] Step 110, introducing a custom annotation and an annotation processor for processing the custom annotation into the source code of the target module to be tested, and adding the custom annotation to the target method to be tested in the target module, so as to annotate the target method with the custom annotation;

[0054] Step 120, compile the project to which the target module belongs, and in the compilation process of the target module, parse the custom annotation through the annotation processor to obtain the target code to be tested;

[0055] Step 130, when the project is started, the target code is automatically executed, and the execution result of the target code is returned to obtain the test result of the target code; wherein the custom annotation can be applied to the class method and is used to mark the annotation of the class method to be tested, and the target module is any code module in the project.

[0056] Since the overall packaging time, compilation time, etc. cannot be shortened, in an embodiment of the present invention, it is considered possible to start from the test entry and automatically add the test entry so that projects such as App (Application) can automatically execute the code to be tested when they are started, and give the execution result after the test passes. This can greatly save the time of manually adding development entries.

[0057] Specifically, custom annotations can be introduced into the source code of the target module to be tested, as well as an annotation processor for processing the custom annotations, and the custom annotations can be added to the target methods to be tested in the target module; the project to which the target module belongs is compiled, and during the compilation process of the target module, the custom annotations are parsed by the annotation processor to obtain the target code to be tested; when the project is started, the target code is automatically executed, and the execution result of the target code is returned.

[0058] Among them, the custom annotation can be applied to class methods and is used to mark the annotations of the class methods to be tested, and the target module is any code module in the project. Annotation, also known as Java annotation, is an annotation mechanism introduced by JDK5.0. The annotation processor can process custom annotations, for example, it can parse the code, generate Java code, etc. Annotation is actually a kind of mark, which can be marked on the key nodes (classes, methods, variables, parameters, packages) in the program code, and then the program can detect these marks at compile time or runtime to perform some special operations. Therefore, the basic process of using custom annotations can be derived:

[0059] The first step is to define annotations, which is equivalent to defining tags;

[0060] The second step is to configure the annotations - put the tags in the program code where they are needed;

[0061] The third step is to parse the annotations - detect the tags at compile time or runtime and perform special operations.

[0062] In the embodiment of the present invention, the annotation processor introduced in the module can be used to parse the custom annotation. Moreover, in the embodiment of the present invention, the specific form of the custom annotation can also be customized according to the needs, which is not limited by the embodiment of the present invention. Generally speaking, in order to accurately mark the method to be tested in this test, it is necessary to ensure that the custom annotation currently used is different from other existing annotations.

[0063] For example, suppose an App includes three modules A, B, and C, and the code in module A needs to be tested, then module A is the target module. Then, a custom annotation with pre-defined settings and an annotation processor for processing the corresponding custom annotation can be introduced into module A. The module can be understood as the code corresponding to the functional module in the project, or the code module, and the division method and division granularity of each module can be customized according to the needs, which is not limited in the embodiment of the present invention.

[0064] Moreover, in the embodiment of the present invention, custom annotations and annotation processors can be introduced into the source code of the target module in any available manner, and the embodiment of the present invention is not limited to this. For example, custom annotations and annotation processors required for testing can be introduced into the gradle configuration file of the target module, etc.

[0065] In the embodiment of the present invention, custom annotations and corresponding annotation processors can also be set in any available way, and the embodiment of the present invention is not limited to this. Moreover, if the annotation is to be recognized, it is inseparable from the annotation processor. The annotation processor is a tool of javac. Whether it is a runtime annotation or a compile-time annotation, the annotation will be scanned and processed by the processor during compilation. There is a default annotation processor in Java, and users can also customize the annotation processor. After registration, the annotation processor is used to process the annotation, for example, the code can be parsed, Java code can be generated, etc., and the effect of the annotation itself can be finally achieved. The annotation processor takes the classes and variables marked with annotations as input content, and generates the Java code to be generated after being processed by the annotation processor. Therefore, the processor can be understood as a tool for generating code, but it is generated by the rules of annotations. The generated code can be regarded as the same as general code and is finally compiled.

[0066] In addition, in order to facilitate the generation of target code based on the annotation processor, it is also necessary to add corresponding custom annotations to the target method that needs to be tested in the target module.

[0067] For example, assuming that the currently set custom annotation is WuBaTestMethod, the custom annotation can also be understood as a test interface. By introducing the custom annotation, the test entry can be automatically added. Then the custom annotation can be added to the target method to be tested in the source code of the target module. The following is an example of adding a custom annotation to the target method:

[0068] Class AndroidUnitTest {

[0069] @WuBaTestMethod

[0070] fun test(context:Context){

[0071] / / Post test

[0072] getDetailManageInfo(infoid "12").subscribe{it:ManagerBeanResponsePrint(it)

[0073] }

[0074] / / Reply test

[0075] getReplyManageInfo(infoid”11”,replyid”18”).subscribe{it:ManagerBeanResponse

[0076] Print(it)

[0077] }

[0078] }

[0079] }

[0080] The test method below "@WuBaTestMethod" is the target method to be tested in the target module. By inserting a custom annotation "WuBaTestMethod" above it, you can mark the method.

[0081] After the custom annotations and annotation processors are introduced and set, the target module can be compiled during the compilation of the project to which the target module belongs. During the compilation of the target module, the custom annotations in the source code can be parsed by the annotation processor introduced therein, thereby obtaining the target code to be tested. That is, at this time, based on the annotation processor, the target code to be tested can be obtained by means of compile-time annotations. Compile-time annotations are annotations processed by the annotation processor during the compilation of a program or project, such as obtaining classes annotated by custom annotations.

[0082] Accordingly, during the compilation process, the executable code of the target program can also be obtained, and the target code to be tested is included therein. Then, after the executable code of the corresponding project is installed on the client or other electronic devices, when the project is started, the corresponding target code can be automatically executed, and the execution result of the target code is returned, thereby obtaining the test result of the target code. In this way, it is only necessary to introduce pre-defined custom annotations and annotation processors in the module where the method to be tested is located, and the custom annotations and annotation processors can be repeatedly referenced, without manually adding development entries or test entries each time a test is performed, thereby greatly saving the time of manually adding interfaces and improving the processing efficiency of code testing.

[0083] In addition, the compile-time annotations are directly woven into the code statically, and the code is directly run at runtime. This is equivalent to putting all the things to be done at runtime in the compile time. However, the runtime annotations will reflect or modify the bytecode. A large amount of reflection is not only inefficient, but also the field caches of various reflections annotated at runtime occupy memory, thus affecting performance. It can be seen that in the embodiments of the present invention, in order to realize automatic code testing when the project is started, it is preferably used in the compile-time annotation method, and the compile-time annotation can also improve the overall efficiency and performance of code testing to a certain extent.

[0084] Optionally, in the embodiment of the present invention, the step 120 may further include: in the compilation process of the target module, the annotation processor executes the following steps to parse the custom annotation to obtain the target code to be tested:

[0085] S1, initializing the annotation processor;

[0086] S2, obtaining all custom annotations in the target module processed by the annotation processor;

[0087] S3, obtaining all methods annotated by the custom annotation;

[0088] S4, calling the method annotated by the custom annotation through reflection to generate the target code.

[0089] In the embodiment of the present invention, after the setting of step 110 is performed, by compiling the project, the custom annotation processor can parse the custom annotation therein to complete the generation of the target code.

[0090] Among them, when the custom annotation is parsed by the annotation processor during each compilation process, in order to avoid the historical use traces of the annotation processor affecting the annotation parsing this time, the annotation processor can be initialized before each use. In the embodiment of the present invention, the annotation processor can be initialized in any available way, and the embodiment of the present invention is not limited to this. For example, generally speaking, an init method is specially provided in the annotation processor, and this method is specially used for the initialization operation related to the annotation processor.

[0091] After the initialization is completed, the above steps S2-S4 can be executed in sequence through the initialized annotation processor to obtain all custom annotations in the target module that need to be processed by the annotation processor, and then obtain all methods annotated by each corresponding custom annotation. After that, each method annotated by the corresponding custom annotation is called through reflection to generate the target code, thereby completing the compile-time annotation process for the custom annotation.

[0092] Reference Figure 2 In the embodiment of the present invention, before step 110, the following may also be included:

[0093] Step T10, constructing the custom annotation in the project, and an annotation processor for processing the custom annotation.

[0094] As mentioned above, in an embodiment of the present invention, custom annotations and annotation processors for processing the custom annotations are introduced, and the annotation processors provided by the development tools may not necessarily meet the current parsing requirements. In this case, the currently required custom annotations and annotation processors for processing the custom annotations can be constructed in the project according to the requirements.

[0095] In the embodiment of the present invention, custom annotations and annotation processors for processing corresponding custom annotations may be created in any available manner, which is not limited by the embodiment of the present invention.

[0096] For example, for the above-mentioned custom annotation "WuBaTestMethod" that can act on class methods, that is, can annotate class methods, you can create a custom annotation in the source code of the project through the following code snippet example:

[0097] @Retention(RetentionPolicy.CLASS)

[0098] @Target(ElementType.METHOD)

[0099] public@interface WuBaTestMethod{

[0100] }

[0101] The code content contained in the above WuBaTestMethod{} can be set as required or remain empty, which is not limited in the embodiment of the present invention. "RetentionPolicy.CLASS" represents a compile-time annotation, which means that @Retention(RetentionPolicy.CLASS) acts on the class bytecode, and its life cycle is only valid between compilers. CLASS means that the annotation is only retained until the compilation stage.

[0102] Reference Figure 2 In the embodiment of the present invention, before step 120, the following may also be included:

[0103] Step T120, obtaining the target source code to be tested, and entering the target source code into the target method, so that during the compilation process of the target module, the custom annotation is parsed by the annotation processor to obtain the target code.

[0104] In the embodiment of the present invention, the target source code to be tested can be entered into the target method annotated with a custom annotation before the project is compiled, or the target source code can be directly written in the target method that needs to be automatically tested. Thus, by compiling the entire project, it is possible to debug whether the code to be tested is correct. That is, during the compilation process of the target module, the custom annotations therein are parsed by the annotation processor to obtain the compiled target code corresponding to the target source code.

[0105] For example, for the above-mentioned source code example of adding a custom annotation to the target method, the function method test defined therein can be used to write the target source code therein, or the target source code to be tested can be entered into the method.

[0106] Optionally, in an embodiment of the present invention, step 130 may further include: when the project is started, calling the target method at the entry point of the project startup to automatically execute the target code and return the execution result of the target code; wherein the target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

[0107] In the embodiment of the present invention, in order to be able to automatically test the target code in time when the project is started, a method for automatically executing the target code can also be created in advance in the project. Then, when the project is started, the target method is called at the entry of the project startup to automatically execute the target code and return the execution result of the target code; wherein the target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

[0108] For example, suppose that the generated target code is called in the module Application to execute the test. The specific code can be as follows:

[0109]

[0110] Among them, the method Bind.bind() is the target method mentioned above.

[0111] In addition, you can define the following Bind class in your project to execute the compiled-in annotated code by emitting calls, which contains the specific execution logic of Bind.bind().

[0112]

[0113]

[0114] In the embodiment of the present invention, the module including the above-mentioned Bind class can be understood as an API code injection module, and the call sequence diagram between it and the custom annotation, annotation processor, and the module to be tested can be as follows: Figure 3 shown.

[0115] Optionally, in an embodiment of the present invention, the project includes a project built based on a compilable language, that is, a project that can implement the above-mentioned compile-time annotations, and the compilable language may include but is not limited to at least one of Java and Kotlin.

[0116] Among them, Kotlin is a static programming language for modern multi-platform applications. Kotlin can be compiled into Java bytecode or JavaScript, which is convenient for running on devices without JVM. Java is an object-oriented programming language that can be used to write desktop applications, web applications, distributed systems, and embedded system applications.

[0117] The project in the embodiment of the present invention may be a project or project that can be written in any of the above-mentioned programming languages ​​with a compilation process to implement compile-time annotations, such as desktop applications, Web applications, distributed systems, and embedded system applications, etc.

[0118] In the embodiment of the present invention, the target code to be tested is generated by compiling annotations and annotation processors, and the code to be tested is automatically executed after the App or other projects are started. That is, the code snippet to be tested can be executed through a line of custom annotations, which is less invasive to the project interface. For the code to be tested, only one line of annotations needs to be added, and the test code needs to be written to quickly complete the test of the code to be tested.

[0119] Reference Figure 4 , shows a schematic diagram of the structure of a code testing processing device in an embodiment of the present invention.

[0120] The code testing processing device of the embodiment of the present invention includes: a code marking module 210 , a code compiling module 220 and a code testing module 230 .

[0121] The functions of each module and the interactions between them are introduced in detail below.

[0122] A code annotation module 210, used to introduce a custom annotation into the source code of the target module to be tested, and an annotation processor for processing the custom annotation, and to add the custom annotation to the target method to be tested in the target module, so as to annotate the target method with the custom annotation;

[0123] A code compiling module 220 is used to compile the project to which the target module belongs, and in the compilation process of the target module, parse the custom annotation through the annotation processor to obtain the target code to be tested;

[0124] The code testing module 230 is used to automatically execute the target code when the project is started, and return the execution result of the target code to obtain the test result of the target code;

[0125] The custom annotation is an annotation that can be applied to a class method and is used to mark the class method to be tested, and the target module is any code module in the project.

[0126] Optionally, in the embodiment of the present invention, the code compilation module may be specifically used for:

[0127] During the compilation process of the target module, the annotation processor performs the following steps to parse the custom annotation to obtain the target code to be tested:

[0128] S1, initializing the annotation processor;

[0129] S2, obtaining all custom annotations in the target module processed by the annotation processor;

[0130] S3, obtaining all methods annotated by the custom annotation;

[0131] S4, calling the method annotated by the custom annotation through reflection to generate the target code.

[0132] Optionally, in the embodiment of the present invention, the device may further include:

[0133] The test code injection module is used to obtain the target source code to be tested and enter the target source code into the target method, so that during the compilation process of the target module, the custom annotation is parsed by the annotation processor to obtain the target code.

[0134] Optionally, in the embodiment of the present invention, the device may further include:

[0135] An annotation tool creates a module for constructing the custom annotation in the project, and an annotation processor for processing the custom annotation.

[0136] Optionally, in the embodiment of the present invention, the code testing module may be specifically used for:

[0137] When the project is started, the target method is called at the entry of the project startup to automatically execute the target code and return the execution result of the target code;

[0138] The target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

[0139] Optionally, in an embodiment of the present invention, the project includes a project built based on a compilable language, and the compilable language includes at least one of Java and Kotlin.

[0140] The code testing processing device provided by the embodiment of the present invention can realize Figure 1 to Figure 2 To avoid repetition, the various processes implemented in the method embodiment are not described here.

[0141] Preferably, an embodiment of the present invention further provides an electronic device, comprising: a processor, a memory, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the various processes of the above-mentioned information processing method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.

[0142] The embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, each process of the above-mentioned information processing method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0143] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.

[0144] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present invention.

[0145] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation modes, which are merely illustrative rather than restrictive. Under the guidance of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the present invention and the claims, all of which are within the protection of the present invention.

[0146] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in the embodiments of the present invention can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0147] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.

[0148] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0149] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0150] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.

[0151] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, mobile hard disks, ROM, RAM, magnetic disks, or optical disks.

[0152] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.

Claims

1. A code testing processing method, characterized in that: include: Introducing a custom annotation into the source code of the target module to be tested, and an annotation processor for processing the custom annotation, and adding the custom annotation to the target method to be tested in the target module, so as to annotate the target method with the custom annotation, wherein the custom annotation is a test interface; Compile the project to which the target module belongs, and during the compilation process of the target module, parse the custom annotation through the annotation processor to obtain the target code to be tested; The step of parsing the custom annotation by the annotation processor to obtain the target code to be tested includes: S1, initializing the annotation processor; S2, obtaining all custom annotations in the target module processed by the annotation processor; S3, obtaining all methods annotated by the custom annotation; S4, calling the method annotated by the custom annotation by reflection to generate the target code; The method annotated by the custom annotation is called by reflection to generate the target code, including: Load the configuration class file through reflection, and call the method of the configuration class file to obtain a list of method names to be tested; Dynamically load and generate a proxy class for business logic according to the split method names and naming rules, call the method of the proxy class through reflection, add it to the list of method names to be tested, and generate the target code; When the project is started, the target code is automatically executed, and the execution result of the target code is returned to obtain the test result of the target code; The custom annotation is an annotation that can be applied to a class method and is used to mark the class method to be tested. The target module is any code module in the project. The annotation processor is a Java default processor or a custom annotation processor.

2. The method according to claim 1, characterized in that Before the step of compiling the project to which the target module belongs and, during the compilation process of the target module, parsing the custom annotation by the annotation processor to obtain the target code to be tested, the method further includes: The target source code to be tested is obtained, and the target source code is entered into the target method, so that during the compilation process of the target module, the custom annotation is parsed by the annotation processor to obtain the target code.

3. The method according to claim 1, characterized in that Before the steps of introducing a custom annotation into the source code of the target module to be tested, and an annotation processor for processing the custom annotation, and adding the custom annotation to the target method to be tested in the target module, the method further includes: The custom annotation is constructed in the project, as well as an annotation processor for processing the custom annotation.

4. The method according to any one of claims 1 to 3, characterized in that The step of automatically executing the target code and returning the execution result of the target code when the project is started includes: When the project is started, the target method is called at the entry of the project startup to automatically execute the target code and return the execution result of the target code; The target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

5. The method according to any one of claims 1 to 3, characterized in that: The project includes a project built based on a compilable language, and the compilable language includes at least one of Java and Kotlin.

6. A code testing processing device, characterized in that: include: A code annotation module, used to introduce a custom annotation into the source code of a target module to be tested, and an annotation processor for processing the custom annotation, and adding the custom annotation to a target method to be tested in the target module, so as to annotate the target method with the custom annotation, wherein the custom annotation is a test interface; A code compilation module, used to compile the project to which the target module belongs, and in the compilation process of the target module, parse the custom annotation through the annotation processor to obtain the target code to be tested; The step of parsing the custom annotation by the annotation processor to obtain the target code to be tested includes: S1, initializing the annotation processor; S2, obtaining all custom annotations in the target module processed by the annotation processor; S3, obtaining all methods annotated by the custom annotation; S4, calling the method annotated by the custom annotation by reflection to generate the target code; The method annotated by the custom annotation is called by reflection to generate the target code, including: Load the configuration class file through reflection, and call the method of the configuration class file to obtain a list of method names to be tested; Dynamically load and generate a proxy class for business logic according to the split method names and naming rules, call the method of the proxy class through reflection, add it to the list of method names to be tested, and generate the target code; A code testing module, used to automatically execute the target code when the project is started, and return the execution result of the target code to obtain the test result of the target code; The custom annotation is an annotation that can be applied to a class method and is used to mark the class method to be tested. The target module is any code module in the project. The annotation processor is a Java default processor or a custom annotation processor.

7. The device according to claim 6, characterized in that The device also includes: The test code injection module is used to obtain the target source code to be tested and enter the target source code into the target method, so that during the compilation process of the target module, the custom annotation is parsed by the annotation processor to obtain the target code.

8. The device according to claim 6, characterized in that The device also includes: An annotation tool creates a module for constructing the custom annotation in the project, and an annotation processor for processing the custom annotation.

9. The device according to any one of claims 6 to 8, characterized in that The code testing module is specifically used for: When the project is started, the target method is called at the entry of the project startup to automatically execute the target code and return the execution result of the target code; The target method is used to obtain the method marked by the custom annotation, and execute the method through reflection to complete the automatic testing of the target code.

10. The device according to any one of claims 6 to 8, characterized in that The project includes a project built based on a compilable language, and the compilable language includes at least one of Java and Kotlin.

11. An electronic device, characterized in that: include: A processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program implements the steps of the code testing processing method according to any one of claims 1 to 5 when executed by the processor.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the code testing processing method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Verification program automatic generation device

    JP2015055967A