Unit testing method, device, electronic device and storage medium

By generating test scripts and cases by reflecting operations and syntax analysis of the tested program, the problem of inefficient writing of unit test cases is solved, and the efficiency and cost reduction of automated unit tests is achieved.

CN113051163BActive Publication Date: 2024-12-27INDUSTRIAL AND COMMERCIAL BANK OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110313436.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-03-24
Publication Date
2024-12-27
Estimated Expiration
2041-03-24

AI Technical Summary

Technical Problem

Existing unit test cases usually require handwriting, especially for complex test programs, resulting in high cost and inefficiency problems.

Method used

By performing reflection operations on the tested program, obtaining class information, function information and field information, generating test scripts, and performing default data filling processing according to predetermined rules, performing syntax analysis to generate test cases, and finally testing operations on the tested program.

Benefits of technology

It effectively reduces the cost of writing scripts and cases of unit tests, improves testing efficiency, reduces dependence on environment and external applications, and improves the work efficiency of automated unit tests.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113051163B_ABST
    Figure CN113051163B_ABST
Patent Text Reader

Abstract

The present invention discloses a unit testing method, device, electronic device and storage medium, and relates to the field of information security technology, wherein the method comprises: performing a reflection operation on a tested program to obtain test information of the tested program, wherein the test information comprises: class information, function information and field information; generating a test script according to the class information, function information and field information of the tested program, and performing default data filling processing on the test script according to a predetermined rule; performing syntax analysis on the tested program, and generating a test case according to the analysis result; and performing a test operation on the tested program according to the test script and the test case after the default data filling processing. Through the present invention, the cost of writing unit test scripts and cases can be effectively reduced, and the test efficiency can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] At present, unit test cases are generally written manually in Java based on functional requirements or for the program being tested. When the program being tested has complex input parameters and return results and has many external interactions, the efficiency of manually writing cases is low. Especially for existing programs, if you want to establish complete unit test cases, the cost will be high and the work efficiency will be low. Summary of the invention

[0003] In view of this, the present invention provides a unit testing method, device, electronic device and storage medium to solve at least one of the above-mentioned problems.

[0004] According to a first aspect of the present invention, there is provided a unit testing method, the method comprising:

[0005] Performing reflection operation on the tested program to obtain test information of the tested program, wherein the test information includes: class information, function information and field information;

[0006] Generate a test script according to the class information, function information and field information of the tested program, and perform default data filling processing on the test script according to a predetermined rule;

[0007] Performing syntax analysis on the tested program and generating test cases according to the analysis results;

[0008] The tested program is tested according to the test script and the test case after the default data filling process.

[0009] According to a second aspect of the present invention, there is provided a unit testing device, the device comprising:

[0010] A test information acquisition unit, used to perform a reflection operation on the tested program to acquire test information of the tested program, wherein the test information includes: class information, function information and field information;

[0011] A test script generating unit, used to generate a test script according to the class information, function information and field information of the tested program;

[0012] A filling unit, used for filling the test script with default data according to a predetermined rule;

[0013] A test case generating unit, used for performing syntax analysis on the tested program and generating a test case according to the analysis result;

[0014] The testing unit is used to perform testing operations on the tested program according to the test script and the test case after filling in the default data.

[0015] According to a third aspect of the present invention, there is provided an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the above method when executing the program.

[0016] According to a fourth aspect of the present invention, there is provided a computer-readable storage medium having a computer program stored thereon, wherein the computer program implements the steps of the above method when executed by a processor.

[0017] It can be seen from the above technical scheme that the test information of the tested program is obtained through reflection-based operations, and then a test script is generated according to the test information, and the test script is filled with default data according to predetermined rules. At the same time, a syntax analysis is performed on the tested program, and a test case is generated according to the analysis results. Then, the tested program is tested according to the test script and test case filled with default data. Compared with the manually written test scripts and cases in the prior art, this technical scheme can effectively reduce the cost of writing unit test scripts and cases and improve testing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0019] Figure 1 is a flowchart of a unit testing method according to an embodiment of the present invention;

[0020] Figure 2 is a detailed flow chart of a unit testing method according to an embodiment of the present invention;

[0021] Figure 3 is a structural block diagram of a unit testing device according to an embodiment of the present invention;

[0022] Figure 4 is an exemplary structural block diagram of an automated unit testing system according to an embodiment of the present invention;

[0023] Figure 5 It is a schematic block diagram of the system structure of the electronic device 600 according to an embodiment of the present invention. DETAILED DESCRIPTION

[0024] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings 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.

[0025] At present, there are problems of high cost and low efficiency in manually writing test cases. Based on this, an embodiment of the present invention provides a unit testing solution, which can effectively reduce the cost of writing unit test scripts and cases and improve test efficiency. The following is a detailed description of the embodiment of the present invention in conjunction with the accompanying drawings.

[0026] Figure 1 is a flowchart of a unit testing method according to an embodiment of the present invention. Figure 1 As shown, the method includes:

[0027] Step 101, performing reflection operation on the tested program to obtain test information of the tested program, wherein the test information includes class information, function information and field information.

[0028] Step 102, generating a test script according to the class information, function information and field information of the tested program, and performing default data filling processing on the test script according to predetermined rules.

[0029] The predetermined rules here can be rules for filling default data in test scripts based on the prior art. For example, for a field of int type, the default value is 1, and for a field of String type, the default value is "1", etc. The present invention does not limit this.

[0030] Step 103: perform syntax analysis on the tested program and generate test cases according to the analysis results.

[0031] Specifically, firstly, the tested program is parsed grammatically to generate a grammar node tree including conditional judgment nodes; then, test cases of multiple data combinations are generated according to the grammar node tree.

[0032] In a specific implementation process, when a modification operation is performed on the tested program, the test case may be updated according to the modified tested program.

[0033] Step 104 , performing a test operation on the tested program according to the test script and the test case after the default data filling process.

[0034] Through reflection-based operations, test information of the tested program is obtained, and then a test script is generated according to the test information. The test script is filled with default data according to predetermined rules, and at the same time, a syntax analysis is performed on the tested program. A test case is generated according to the analysis result, and then the tested program is tested according to the test script and test case filled with default data. Compared with the manual writing of test scripts and cases in the prior art, the embodiments of the present invention can effectively reduce the cost of writing scripts and cases for unit testing and improve testing efficiency.

[0035] During the unit test, when the tested program needs to call an external program function, baffle information can be generated according to the external program function, and then the calling operation is tested according to the baffle information. In other words, the external program function to be called is not actually called, but the baffle information set in advance is directly returned.

[0036] In order to better understand the present invention, the following describes in detail an example process of unit testing, which mainly includes the following steps 1 to 4:

[0037] Step 1: For the program under test, automatically generate a unit test script and fill in default data.

[0038] Specifically, first, the unit test script is automatically generated. Taking the JAVA program as an example, for the program class being tested, through the JAVA Reflection reflection mechanism (this mechanism is a JAVA technology that can obtain all the properties and methods of the class), all parent classes, fields, functions and other information of the class are dynamically obtained. The object of the tested class is automatically generated in the test class, and then the test function Test_Method is automatically generated for each function of the tested class, such as Method. The input parameter object of the Method function is automatically generated in this test function, and this Method tested function is called.

[0039] After that, the initial default test data is automatically filled in. For the input object, the information of the input class is obtained, and all fields of the class are obtained through reflection (loop call to obtain the fields nested in the sub-objects), and then each field is automatically assigned a default value (if the field is of int type, the default value is 1; if it is of String type, the default value is "1"), and common fields (such as cardno card number, etc.) are assigned meaningful values ​​(such as the 17-digit correct card number starting with 6222).

[0040] Through step 1, the null pointer problem can be avoided, effectively reducing the cost of writing unit test scripts and cases.

[0041] Step 2: Automatically enrich the test path and generate multiple test cases.

[0042] Perform syntax analysis on the program function being tested, generate a syntax node tree, and then automatically generate a full coverage test case combination.

[0043] In one embodiment, the most commonly used condition coverage is taken as an example: all conditional judgment nodes IfStatement, SwitchCase, ForStatement, WhileStatement, DoStatement, etc. are obtained. For the condition of this node, if it is a comparison operator (><=), it is determined whether its PrimaryPrefix (such as its Name is Cardno) is in the case data column. If so (such as the case data has a column Cardo), then for RelationalExpression and EqualityExpression and their specific values, the coverage value of this case data column is automatically generated. If Cardno>1, the column automatically generates three coverage values ​​of 2, 1, and 0. Then, test cases for all data combinations are automatically generated.

[0044] Step 3: For the external interaction part of the program, automatically generate the baffle MOCK function.

[0045] For the program functions outside the tested program called by the tested program, the baffle MOCK function is automatically generated, that is, the currently called program function is not actually called, but the pre-set data is directly returned). Taking the generation of mockit code as an example, it includes the new Expectations() code segment, which automatically generates the functions that need MOCK (such as SpringContainer.getContext().getBean) and the MOCK values ​​that need to be returned. At the same time, Mock fields are automatically generated, and @Injectable fields are automatically generated for the @autowired fields in the tested program. The returned Mock value automatically generates all fields and fills in default values ​​according to the class of the returned object.

[0046] By generating a MOCK baffle and automatically filling in the returned default values, the unit test's dependence on the environment and external applications can be reduced, and the efficiency of automated unit testing can be improved.

[0047] Step 4: Adaptively repair unit test cases to confirm whether the program functions after repair are questionable.

[0048] For unit test cases with modified programs, perform adaptive repairs and combine the original confirmed process to confirm whether the program modification function is suspicious. For existing programs that have unit test scripts and cases, if the program has been modified, analyze the program modification points and repair the unit test cases. Based on the adaptively repaired unit test cases, perform test verification to confirm whether the program function modification is suspicious.

[0049] Through adaptive repair operations, the reuse rate of existing unit test cases is improved, and the benefits accumulated from existing work can be maximized.

[0050] Figure 2 is a detailed flow chart of a unit testing method according to an embodiment of the present invention, such as Figure 2 As shown, the whole process is divided into two parts. The first part is mainly responsible for the generation of unit test scripts and case data, including steps S01 to S05; the second part generates a baffle MOCK to complete the unit test, adapts the unit test script, and verifies the unit test results during the dynamic initiation of the unit test, including steps S06 to S09. Each step is described below.

[0051] Step S01: Analyze the interface source code. Taking the JAVA program as an example, obtain all parent class, field, function and other information of the tested program through reflection operation.

[0052] Step S02: Generate a unit test script based on the information obtained from the reflection operation, automatically generate an object of the tested class in the test program, and then automatically generate a test function Test_Method for each function of the tested class, such as Method, automatically generate an input parameter object of the tested function in the test function, and call the function to be tested.

[0053] Step S03: Fill in the initial default test data. For the input parameter object initiated by the test, all fields are obtained through reflection and automatically assigned default values. For commonly used fields (such as cardno card number, etc.), meaningful values ​​(such as the 17-digit correct card number starting with 6222) are assigned.

[0054] Step S04: Perform syntax analysis on the program function being tested to generate a syntax node tree.

[0055] Step S05: Generate a test case combination that fully covers the test path according to the syntax node tree.

[0056] Step S06: For the program functions outside the tested program that are called by the tested program, a baffle MOCK function program is automatically generated.

[0057] Step S07: For the MOCK value returned by the baffle MOCK function program, all fields are automatically generated and filled with default values ​​according to the class of the returned object.

[0058] Step S08: For an existing program with existing unit test scripts and cases, if a modification of the program is detected during the execution of the unit test, the modification points of the program are automatically analyzed and the unit test cases are automatically repaired.

[0059] Step S09: Based on the adaptively repaired unit test cases, test verification is performed to confirm whether there is any doubt about the program function modification.

[0060] Based on similar inventive concepts, an embodiment of the present invention further provides a unit testing device. Preferably, the device can be used to implement the process of the above method embodiment.

[0061] Figure 3 This is the structural diagram of the unit test device, such as Figure 3 As shown, the unit test device includes: a test information acquisition unit 301, a test script generation unit 302, a filling unit 303, a test case generation unit 304 and a test unit 305, wherein:

[0062] The test information acquisition unit 301 is used to perform a reflection operation on the tested program to acquire the test information of the tested program, where the test information includes: class information, function information and field information.

[0063] The test script generating unit 302 is used to generate a test script according to the class information, function information and field information of the tested program.

[0064] The filling unit 303 is used to fill the test script with default data according to a predetermined rule.

[0065] The predetermined rule here may be a rule for filling default data in a test script based on the prior art, and the present invention is not limited to this.

[0066] The test case generating unit 304 is used to perform syntax analysis on the tested program and generate a test case according to the analysis result.

[0067] Specifically, the test case generation unit includes: a syntax node tree generation module and a test case generation module, wherein:

[0068] A syntax node tree generation module, used for performing syntax analysis on the tested program and generating a syntax node tree including conditional judgment nodes;

[0069] A test case generation module is used to generate test cases of multiple data combinations according to the syntax node tree.

[0070] The testing unit 305 is used to perform a testing operation on the tested program according to the test script and the test case after the default data filling process.

[0071] Through reflection-based operations, the test information acquisition unit 301 acquires the test information of the tested program, and then the test script generation unit 302 generates a test script according to the test information. The filling unit 303 performs default data filling processing on the test script according to predetermined rules. At the same time, the test case generation unit 304 performs syntax analysis on the tested program and generates a test case according to the analysis results. Thereafter, the test unit 305 performs test operations on the tested program according to the test script and test case after the default data filling processing. Compared with the manual writing of test scripts and cases in the prior art, the embodiments of the present invention can effectively reduce the cost of script and case writing for unit testing and improve testing efficiency.

[0072] In actual operation, the above device further includes: a baffle information generating unit and a calling test unit, wherein:

[0073] a baffle information generating unit, configured to generate baffle information according to the external program function in response to the tested program calling the external program function;

[0074] The calling test unit is used to test the calling operation according to the baffle information.

[0075] In one embodiment, the apparatus further comprises: an updating unit, configured to update the test case according to the modified program under test in response to a modification operation of the program under test.

[0076] The specific execution process of the above-mentioned units and modules can be found in the description of the above-mentioned method embodiment, which will not be repeated here.

[0077] In actual operation, the above-mentioned units and modules may be provided in combination or individually, and the present invention is not limited thereto.

[0078] In order to further understand the present invention, an example system is given below.

[0079] Figure 4 is an example structural block diagram of an automated unit testing system according to an embodiment of the present invention. Figure 4 As shown, the system includes: a script generating device 1, a case data generating device 2, a MOCK generating device 3, and a test self-adapting device 4. Figure 4 Describe each device in detail.

[0080] Script generation device 1: Generates unit test scripts by analyzing interface source code and fills in initial default test data. Script generation device 1 includes: source code analysis module 11, script automatic generation module 12, initial data generation module 13, wherein:

[0081] Source code analysis module 11: Taking the JAVA program as an example, for the tested program, all parent classes, fields, functions and other information of the tested program are obtained through reflection.

[0082] Script automatic generation module 12: Automatically generate objects of the tested class in the test program, and then automatically generate a test function Test_Method for each function of the tested class, such as Method, automatically generate the input parameter object of Method in the test function, and call the function to be tested.

[0083] Specifically, the execution steps of the script automatic generation module are as follows:

[0084] Step 1: For the tested program, use the JAVA reflection mechanism, use getSuperclass (a function) to obtain its parent class, getDeclaredFields (a function) to obtain all field attributes, getDeclaredMethods (a function) to obtain all functions, getConstructors (a function) to obtain all constructors, and save and record them.

[0085] Step 2: Generate a test program. You can save the file with the standard name of "tested program + Test.java" and process it in the following order:

[0086] (1) Import the required packages or classes.

[0087] (2) Generate a class object annotated with @Tested (which identifies an instance of the object being tested).

[0088] (3) Traverse the field attributes of the tested program. For fields annotated with @Autowired (Spring framework's dependency injection identifier), generate private variables annotated with @Injectable (Mocked object for instance).

[0089] (4) Traverse the functions of the program under test and generate a test function for each function. This test function includes three functional modules: the given module mainly defines local variables as data and baffle MOCK preparation; the when module triggers the call of the function under test; the then module serves as an assertion (comparing whether the returned test results meet expectations) to determine whether the test is successful. The code snippet example is shown below:

[0090] / / Given

[0091] input.ctrlCommV10.userFlag=2;

[0092] / / When

[0093] Response <remitsumoutput>response=atsRemitDoSum.doSumMaintain(input);

[0094] / / Then

[0095] Assertions.assertEquals(OperationResult.FAIL,response.getResult());

[0096] Assertions.assertEquals("9800",response.getErrorCode());

[0097] Initial data generation module 13: used to fill in the initial default test data. For the input object, obtain the Class type corresponding to the input object, obtain all the fields of the class through reflection (loop call to obtain the fields nested in the sub-objects), and then automatically assign a default value to each field, and assign a meaningful value (such as a 17-digit correct card number starting with 6222) to commonly used fields (such as cardno card number, etc.).

[0098] Specifically, define a function to get the default input value, the parameter is the input object. For the input object, get all the field attributes of the type through getClass().getDeclaredFields(), and then get the type name through getType().getSimpleName(), and process it in the following order:

[0099] (1) If it is a FINAL attribute, it is skipped and not processed.

[0100] (2) First, set the field attribute to be externally accessible using setAccessible(true).

[0101] (3) Determine the type of the field attribute in turn. If it is int, assign a default value of 0; if it is char, assign a default value of '0'; if it is Date, assign a default value of 2020-08-08, and so on.

[0102] (4) For special commonly used fields, assign meaningful values. For example, for cardno, assign the correct 17-digit card number starting with 6222.

[0103] (5) If the field type names do not conform to the conventional types, it can be considered as a custom class. Therefore, you can use getType().newInstance() to create a new instance object and recursively call the function to obtain the default input value.

[0104] Case data generation device 2: performs syntax analysis on the program function being tested, generates a syntax node tree, and then generates a combination of test cases with full coverage. Case data generation device 2 includes: syntax analysis module 21 and data combination module 22, wherein:

[0105] Syntax analysis module 21: Taking the most commonly used conditional coverage as an example, all conditional judgment nodes IfStatement, SwitchCase, ForStatement, WhileStatement, DoStatement, etc. are obtained. As shown below, the syntax tree shows that the current program contains For loops and If judgments.

[0106]

[0107] Data combination module 22: For the condition of this node, if it is a comparison operator (><=), determine whether its PrimaryPrefix (such as its Name is Cardno) is in the case data column. If so (such as the case data has a column Cardo), then for RelationalExpression and EqualityExpression and their specific values, generate coverage values ​​for this case data column. If Cardno>1, then the column automatically generates three coverage values ​​of 2, 1, and 0. Then generate test cases for all data combinations.

[0108] MOCK generation device 3: Generates a MOCK and corresponding data fields for the program functions outside the tested program called by the tested program. MOCK generation device 3 includes: MOCK function generation module 31, MOCK data generation module 32, wherein:

[0109] MOCK function generation module 31: For the program function outside the tested program called by the tested program, the baffle mock function is automatically generated. Taking the code for generating mockit as an example, it includes the new Expectations() code segment, which automatically generates the method that needs to be mocked (such as SpringContainer.getContext().getBean) and the MOCK value that needs to be returned.

[0110] MOCK data generation module 32: Automatically generate MOCK fields, and automatically generate @Injectable fields for @autowired fields in the tested program. The returned MOCK value automatically generates all fields and fills in default values ​​according to the class of the returned object (if the field is of int type, the default value is 1; if it is of String type, the default value is "1").

[0111] Test self-adaptation device 4: For unit test cases with modified programs, adaptive repair is performed, and combined with the original confirmed process, whether the program modification function is suspicious. Test self-adaptation device 4 includes: data adaptive module 41, program modification verification module 42, wherein:

[0112] Data adaptive module 41: For an existing program with existing unit test scripts and cases, if the program is modified, the modification points of the program are obtained through analysis and the unit test cases are repaired.

[0113] Specifically, for example, a function FunctionX of a program has a corresponding test function Test_FunctionX in the original unit test script. Now, a program fragment of if branch judgment is added inside the function FunctionX, as shown below:

[0114]

[0115] In order to test the coverage of this newly added program fragment, a new test function Test_FunctionX_A_1() is copied based on Test_FunctionX, and A is assigned a value of 1 in this new test function, so that the test process can meet the if condition, so that the test path can cover this if branch program segment, thereby achieving a relatively complete test coverage rate.

[0116] Program modification verification module 42: Based on the adaptively repaired unit test cases, test verification is performed to determine whether the assertions are satisfied and whether the test paths are complete, to confirm whether there is any doubt about the program function modification.

[0117] The automated unit testing system provided by the embodiment of the present invention can automatically generate unit test scripts and automatically fill in default data, and can also automatically generate MOCKs and automatically enrich test paths, generate multiple test data combinations, and can adaptively repair unit test cases.

[0118] This embodiment also provides an electronic device, which may be a desktop computer, a tablet computer, a mobile terminal, etc., but this embodiment is not limited thereto. In this embodiment, the electronic device may be implemented with reference to the above method embodiment and the unit test device / system embodiment, the contents of which are incorporated herein, and the repeated parts are not repeated.

[0119] Figure 5 FIG. 6 is a schematic block diagram of a system structure of an electronic device 600 according to an embodiment of the present invention. Figure 5 As shown, the electronic device 600 may include a central processor 100 and a memory 140; the memory 140 is coupled to the central processor 100. It should be noted that this figure is exemplary; other types of structures may also be used to supplement or replace this structure to implement telecommunication functions or other functions.

[0120] In one embodiment, the unit test function may be integrated into the central processing unit 100. The central processing unit 100 may be configured to perform the following control:

[0121] Performing reflection operation on the tested program to obtain test information of the tested program, wherein the test information includes: class information, function information and field information;

[0122] Generate a test script according to the class information, function information and field information of the tested program, and perform default data filling processing on the test script according to a predetermined rule;

[0123] Performing syntax analysis on the tested program and generating test cases according to the analysis results;

[0124] The tested program is tested according to the test script and the test case after the default data filling process.

[0125] From the above description, it can be seen that the electronic device provided in the embodiment of the present application obtains the test information of the tested program through reflection operation, then generates a test script according to the test information, and fills the test script with default data according to predetermined rules, and performs syntax analysis on the tested program at the same time, generates a test case according to the analysis results, and then tests the tested program according to the test script and test case after the default data filling process. Compared with the manually written test scripts and cases in the prior art, the embodiment of the present invention can effectively reduce the cost of script and case writing for unit testing and improve testing efficiency.

[0126] In another embodiment, the unit testing device / system may be configured separately from the central processing unit 100. For example, the unit testing device / system may be configured as a chip connected to the central processing unit 100, and the unit testing function is implemented under the control of the central processing unit.

[0127] like Figure 5 As shown, the electronic device 600 may further include: a communication module 110, an input unit 120, an audio processing unit 130, a display 160, and a power supply 170. It is worth noting that the electronic device 600 does not necessarily have to include Figure 5 In addition, the electronic device 600 may also include Figure 5 For components not shown, reference may be made to the prior art.

[0128] like Figure 5 As shown, the central processor 100 is sometimes also referred to as a controller or an operation control, and may include a microprocessor or other processor devices and / or logic devices. The central processor 100 receives inputs and controls the operations of various components of the electronic device 600 .

[0129] The memory 140 may be, for example, one or more of a cache, a flash memory, a hard drive, a removable medium, a volatile memory, a non-volatile memory or other suitable devices. The above-mentioned information related to the failure may be stored, and a program for executing the relevant information may also be stored. The CPU 100 may execute the program stored in the memory 140 to implement information storage or processing.

[0130] The input unit 120 provides input to the CPU 100. The input unit 120 is, for example, a key or a touch input device. The power supply 170 is used to provide power to the electronic device 600. The display 160 is used to display display objects such as images and text. The display may be, for example, an LCD display, but is not limited thereto.

[0131] The memory 140 may be a solid-state memory, such as a read-only memory (ROM), a random access memory (RAM), a SIM card, etc. It may also be a memory that saves information even when the power is off, can be selectively erased, and is provided with more data, examples of which are sometimes referred to as EPROMs, etc. The memory 140 may also be some other type of device. The memory 140 includes a buffer memory 141 (sometimes referred to as a buffer). The memory 140 may include an application / function storage unit 142, which is used to store application programs and function programs or processes for executing the operation of the electronic device 600 through the central processor 100.

[0132] The memory 140 may also include a data storage unit 143 for storing data, such as contacts, digital data, pictures, sounds, and / or any other data used by the electronic device. The driver storage unit 144 of the memory 140 may include various drivers for communication functions of the electronic device and / or for executing other functions of the electronic device (such as messaging applications, address book applications, etc.).

[0133] The communication module 110 is a transmitter / receiver 110 that transmits and receives signals via an antenna 111. The communication module (transmitter / receiver) 110 is coupled to the central processor 100 to provide input signals and receive output signals, which may be the same as the case of a conventional mobile communication terminal.

[0134] Based on different communication technologies, multiple communication modules 110 may be provided in the same electronic device, such as a cellular network module, a Bluetooth module and / or a wireless LAN module. The communication module (transmitter / receiver) 110 is also coupled to a speaker 131 and a microphone 132 via an audio processor 130 to provide an audio output via the speaker 131 and receive an audio input from the microphone 132, thereby realizing a common telecommunication function. The audio processor 130 may include any suitable buffer, decoder, amplifier, etc. In addition, the audio processor 130 is also coupled to the central processor 100, so that the sound can be recorded on the local machine through the microphone 132, and the sound stored on the local machine can be played through the speaker 131.

[0135] An embodiment of the present invention further provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned unit testing method are implemented.

[0136] In summary, the embodiment of the present invention provides an automated unit testing solution that can automatically generate unit test scripts and automatically fill in default data, automatically generate mocks and automatically enrich test paths, generate multiple test data combinations, and adaptively repair unit test cases. Specifically, the solution includes the following advantages:

[0137] (1) For existing programs, unit test scripts can be automatically generated. The system already has a method for testing and verifying each program of the tested program. The default values ​​of the calling input parameters are automatically filled in to avoid the null pointer problem, effectively reducing the cost of writing unit test scripts and cases.

[0138] (2) It can automatically generate MOCK baffles and automatically fill in the returned default values, greatly reducing the unit test's dependence on the environment and external applications, and improving the efficiency of automated unit testing.

[0139] (3) Unit test cases with modified programs can be repaired adaptively, and combined with the original confirmed process to confirm whether the program modification function is suspicious, thereby improving the reuse rate of existing unit test cases and maximizing the benefits accumulated from existing work.

[0140] The preferred embodiments of the present invention are described above with reference to the accompanying drawings. Many features and advantages of these embodiments are clear from this detailed description, and therefore the claims are intended to cover all such features and advantages of these embodiments that fall within their true spirit and scope. In addition, since many modifications and changes are easily conceivable to those skilled in the art, it is not intended to limit the embodiments of the present invention to the precise structure and operation illustrated and described, but all suitable modifications and equivalents falling within its scope may be covered.

[0141] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0142] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0143] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0144] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0145] The present invention uses specific embodiments to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of ​​the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.< / remitsumoutput>

Claims

1. A unit testing method, characterized in that: The method comprises: Performing reflection operation on the tested program to obtain test information of the tested program, wherein the test information includes: class information, function information and field information; Generate a test script according to the class information, function information and field information of the tested program, and perform default data filling processing on the test script according to a predetermined rule; Performing syntax analysis on the tested program and generating test cases according to the analysis results; Performing a test operation on the tested program according to the test script and the test case after the default data filling processing; The generating of the test script according to the class information, function information and field information of the tested program comprises: Import the required packages or classes; Generate a class object annotated with an instance annotation that identifies an object under test; Traverse the field attributes of the tested program, and generate private variables annotated with the Mocked object annotation for the instance when the tested program has fields annotated with the Spring framework's dependency injection identifier. Traverse the functions of the tested program and generate a test function for each function. This test function includes three functional modules: the given module mainly defines local variables as preparation for data and baffle MOCK; the when module triggers the call of the tested function; the then module is used to compare whether the returned test results meet expectations and determine whether the test is successful; Performing syntax analysis on the tested program and generating test cases according to the analysis results include: Performing grammatical analysis on the tested program to generate a grammatical node tree including conditional judgment nodes; Generate test cases of multiple data combinations according to the syntax node tree; Get all conditional judgment nodes IfStatement, SwitchCase, ForStatement, WhileStatement, DoStatement; For the condition of this node, if it is a comparison operator, determine whether its PrimaryPrefix is ​​in the case data column. If so, generate the coverage value of this case data column for RelationalExpression and EqualityExpression and their specific values.

2. The method according to claim 1, characterized in that The method further comprises: In response to the tested program calling an external program function, generating baffle information according to the external program function; A test of the calling operation is performed according to the baffle information.

3. The method according to claim 1, characterized in that The method further comprises: In response to the tested program being modified, the test case is updated according to the modified tested program.

4. A unit testing device, characterized in that: The device comprises: A test information acquisition unit, used to perform a reflection operation on the tested program to acquire test information of the tested program, wherein the test information includes: class information, function information and field information; A test script generating unit, used to generate a test script according to the class information, function information and field information of the tested program; A filling unit, used for filling the test script with default data according to a predetermined rule; A test case generating unit, used for performing syntax analysis on the tested program and generating a test case according to the analysis result; A testing unit, used for performing a testing operation on the tested program according to the test script and the test case after filling in the default data; The test script generating unit is used for: Import the required packages or classes; Generate a class object annotated with an instance annotation that identifies an object under test; Traverse the field attributes of the tested program, and generate private variables annotated with the Mocked object annotation for the instance when the tested program has fields annotated with the Spring framework's dependency injection identifier. Traverse the functions of the tested program and generate a test function for each function. This test function includes three functional modules: the given module mainly defines local variables as preparation for data and baffle MOCK; the when module triggers the call of the tested function; the then module is used to compare whether the returned test results meet expectations and determine whether the test is successful; The test case generating unit comprises: A syntax node tree generation module, used for performing syntax analysis on the tested program and generating a syntax node tree including conditional judgment nodes; A test case generation module, used to generate test cases of multiple data combinations according to the syntax node tree; Get all conditional judgment nodes IfStatement, SwitchCase, ForStatement, WhileStatement, DoStatement; For the condition of this node, if it is a comparison operator, determine whether its PrimaryPrefix is ​​in the case data column. If so, generate the coverage value of this case data column for RelationalExpression and EqualityExpression and their specific values.

5. The device according to claim 4, characterized in that The device also includes: a baffle information generating unit, configured to generate baffle information according to the external program function in response to the tested program calling the external program function; The calling test unit is used to test the calling operation according to the baffle information.

6. The device according to claim 4, characterized in that The device also includes: An updating unit is used for updating the test case according to the modified program under test in response to the modification operation of the program under test.

7. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the program, the steps of the method according to any one of claims 1 to 3 are implemented.

8. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 3 are implemented.