A method and apparatus for code testing

By constructing initial test cases based on the member variables and method call order of the object under test, the problems of poor test results and frequent exceptions in existing technologies are solved. This simplifies test cases and improves stability, ensuring smooth execution of the test process and increased coverage.

CN114840426BActive Publication Date: 2026-01-13ALIPAY (HANGZHOU) INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202210508848.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-05-10
Publication Date
2026-01-13
Estimated Expiration
2042-05-10

AI Technical Summary

Technical Problem

Existing test case generation methods result in poor test performance and are prone to exceptions during execution, failing to effectively guarantee the smooth execution of the test process and improve test effectiveness.

Method used

By determining the member variables and method call order of the object under test, initial test cases are constructed, and the code to be tested is tested by optimizing the test cases, thereby reducing the number of test cases and improving the testing effect.

Benefits of technology

This has resulted in streamlined test cases and improved stability, significantly enhancing testing effectiveness and ensuring smooth execution and coverage of the testing process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114840426B_ABST
    Figure CN114840426B_ABST
Patent Text Reader

Abstract

The specification discloses a code testing method and device, the code testing method comprises the following steps: determining all member variables corresponding to each object under test and variable calling statements corresponding to each member variable, and constructing a method calling statement sequence according to a calling sequence corresponding to each method under test contained in the object under test; constructing an initial test case corresponding to the object under test according to the variable calling statements and the method calling statement sequence, wherein each initial test case corresponds to only one object under test; determining a preferred test case according to each initial test case, and testing the code to be tested through the preferred test case.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present specification relates to the field of software testing, and in particular, to a code testing method and device. BACKGROUND

[0002] In the process of software development, the testing of software code usually plays an indispensable role, and the testing effect often depends on whether the test case is reasonably written. With the development of automatic testing technology, the testing process is gradually changed from manual testing to automatic testing through automatically generated test cases, so as to reduce the labor cost while ensuring the smooth development of software.

[0003] However, the current test case generation method usually contains multiple test objects in a test case, and the calling sequence of each test method contained in the test object is random. The test case constructed by this method is prone to execution exceptions during execution, and the testing effect is poor.

[0004] Therefore, how to ensure the smooth execution of the testing process while improving the testing effect is a problem to be solved. SUMMARY

[0005] The present specification provides a code testing method and device to simplify test case statements, reduce the number of test cases, and improve testing effect.

[0006] The present specification adopts the following technical solutions:

[0007] The present specification provides a code testing method, comprising:

[0008] For each test object, determining all member variables corresponding to the test object and variable calling statements corresponding to each member variable, and constructing a method calling statement sequence according to the calling sequence corresponding to each test method contained in the test object;

[0009] According to the variable calling statements and the method calling statement sequence, constructing an initial test case corresponding to the test object, wherein each initial test case only corresponds to one test object;

[0010] According to each initial test case, determining an optimal test case, and testing the code to be tested through the optimal test case.

[0011] Optionally, for each test object, determining all member variables corresponding to the test object and variable calling statements corresponding to each member variable, specifically comprising:

[0012] For each object under test, determine all member variables corresponding to the object under test, and variable injection statements corresponding to the member variables;

[0013] According to the initial values and the value ranges of the member variables, determine variable initialization statements corresponding to the member variables;

[0014] Combine the variable initialization statements and the variable injection statements to construct variable call statements corresponding to the member variables.

[0015] Optionally, according to the initial test cases, determine the preferred test case, specifically including:

[0016] According to the initial test cases, reconstruct the test cases to obtain candidate test cases;

[0017] According to the candidate test cases, determine the preferred test case.

[0018] Optionally, according to the initial test cases, reconstruct the test cases to obtain candidate test cases, specifically including:

[0019] Process the method parameters corresponding to the methods called in the initial test cases and the member variable parameters corresponding to the member variables called, to obtain processed method parameters and processed member variable parameters;

[0020] According to the processed method parameters and the processed member variable parameters, and the method call statements and the variable call statements contained in the initial test cases, reconstruct the test cases to obtain candidate test cases.

[0021] Optionally, according to the initial test cases, reconstruct the test cases to obtain candidate test cases, specifically including:

[0022] For each initial test case, if the object under test corresponding to the initial test case does not contain the to-be-inserted test method determined, according to the method call statement corresponding to the to-be-inserted test method, the method call statement and the variable call statement contained in the initial test case, reconstruct the test case to obtain a candidate test case.

[0023] Optionally, according to the candidate test cases, determine the preferred test case, specifically including:

[0024] For each candidate test case, determine other method call statements in the candidate test case except for the method call statement contained in the initial test case as target call statements;

[0025] According to a preset probability, at least part of the target call statements contained in the candidate test case are deleted to determine the preferred test case according to the candidate test case after the at least part of the target call statements are deleted.

[0026] Optionally, the preferred test case is determined according to each candidate test case, and specifically includes:

[0027] For each candidate test case, if the comprehensive coverage corresponding to the candidate test case is greater than a preset threshold, and the coverage target determined after the code is tested by using the candidate test case is not completely the same as the coverage target determined after the code is tested by using the preferred test case that has been determined, the candidate test case is taken as the preferred test case.

[0028] Optionally, the method further includes:

[0029] Each line of code in the preferred test case is traversed to identify the abnormal code contained in the preferred test case.

[0030] If the change amount of the comprehensive coverage obtained after the code is tested according to the other code in the preferred test case except the abnormal code is less than a preset change amount, the abnormal code is deleted.

[0031] The specification provides a code testing device, which includes:

[0032] A determination module is configured to determine, for each object under test, all member variables corresponding to the object under test and variable call statements corresponding to each member variable, and construct a method call statement sequence according to a call sequence corresponding to each method under test contained in the object under test.

[0033] A construction module is configured to construct an initial test case corresponding to the object under test according to the variable call statements and the method call statement sequence, wherein each initial test case corresponds to only one object under test.

[0034] A test module is configured to determine, according to each initial test case, a preferred test case corresponding to the code to be tested, and test the code to be tested by using the preferred test case.

[0035] Optionally, the determination module is specifically configured to determine, for each object under test, all member variables corresponding to the object under test and variable injection statements corresponding to each member variable, determine variable initialization statements corresponding to each member variable according to an initial value and a value range corresponding to each member variable, and combine the variable initialization statements and the variable injection statements to construct variable call statements corresponding to each member variable.

[0036] Optionally, the test module is specifically configured to reconstruct test cases according to the initial test cases to obtain candidate test cases; and determine the preferred test cases according to the candidate test cases.

[0037] Optionally, the test module is specifically configured to process method parameters corresponding to methods called in the initial test cases and member variable parameters corresponding to member variables called in the initial test cases to obtain processed method parameters and processed member variable parameters; and reconstruct test cases according to the processed method parameters and the processed member variable parameters and method call statements and variable call statements contained in the initial test cases to obtain candidate test cases.

[0038] Optionally, the test module is specifically configured to, for each initial test case, if the determined to-be-inserted measured method is not contained in the measured object corresponding to the initial test case, reconstruct test cases according to method call statements corresponding to the to-be-inserted measured method, method call statements and variable call statements contained in the initial test case to obtain candidate test cases.

[0039] Optionally, the test module is specifically configured to, for each candidate test case, determine, from the candidate test case, other method call statements except for method call statements contained in the initial test cases as target call statements.

[0040] According to a preset probability, at least part of the target call statements contained in the candidate test case are deleted to determine the preferred test cases according to the candidate test case from which the at least part of the target call statements are deleted.

[0041] Optionally, the test module is specifically configured to, for each candidate test case, if the comprehensive coverage corresponding to the candidate test case is greater than a preset threshold and the coverage target determined after code testing using the candidate test case is not completely the same as the coverage target determined after code testing using the already determined preferred test cases, take the candidate test case as a preferred test case.

[0042] Optionally, the test module is further configured to traverse each line of code in the preferred test case to identify abnormal code contained in the preferred test case; and if it is determined that the change amount of the comprehensive coverage obtained after code testing according to other code in the preferred test case except for the abnormal code is less than a preset change amount, delete the abnormal code.

[0043] The specification provides a computer-readable storage medium, the storage medium stores a computer program, the computer program is executed by a processor to implement the above-mentioned code testing method.

[0044] The specification provides an electronic device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor implements the method of the above code testing when executing the program.

[0045] The above at least one technical solution adopted by the specification can achieve the following beneficial effects:

[0046] In the method of code testing provided by the specification, the member variable corresponding to each test method contained in each test object can be determined first, and then the variable calling statement corresponding to all member variables can be determined, and the method calling statement sequence can be constructed according to the calling order of each method, so that the initial test case can be obtained according to the variable calling statement and the method calling statement sequence, and finally the preferred test case can be determined according to the initial test case corresponding to each test object to test the code to be tested.

[0047] As can be seen from the above method, only one test method will correspond to each test case in the specification, and the variable calling statement corresponding to all the member variables that need to be called will be determined in advance when the test case is constructed, and the calling order of each test method in the test object is considered. Compared with the test case constructed by the construction method of the prior art that a test case corresponds to multiple test objects and adopts a random statement sequence, the test case in the present scheme not only has more concise test case statements and more stable operation, but also has significantly improved test effect. BRIEF DESCRIPTION OF DRAWINGS

[0048] The drawings described herein are used to provide further understanding of the specification, and form a part of the specification. The illustrative embodiments of the specification and their descriptions are used to explain the specification, and do not constitute an improper limitation on the specification. In the drawings:

[0049] Figure 1 A flowchart of a method of code testing provided in the specification;

[0050] Figure 2 A schematic diagram of a preferred test case generation method provided by the specification;

[0051] Figure 3 A schematic diagram of a code testing device provided by the specification;

[0052] Figure 4 A schematic diagram of an electronic device corresponding to Figure 1 provided by the specification. DETAILED DESCRIPTION

[0053] For the purposes of the present description, the technical solutions and advantages thereof will be more apparent from the following detailed description of specific embodiments thereof, taken in conjunction with the accompanying drawings. Obviously, the described embodiments are only a part of the embodiments of the present description, rather than all the embodiments. Based on the embodiments in the present description, all other embodiments obtained by those of ordinary skill in the art without creative efforts fall within the scope of protection of the present description.

[0054] The technical solutions provided by the embodiments of the present description will be described in detail below with reference to the accompanying drawings.

[0055] Figure 1 For the flowchart of the method for testing code provided in the present description, the following steps are included:

[0056] S101: For each test object, determine all member variables corresponding to the test object and the variable call statements corresponding to each member variable, and construct a method call statement sequence according to the call order corresponding to each test method contained in the test object.

[0057] In the process of software development, the code of the software needs to be tested to ensure the normal operation of the software. In order to ensure that each function of the software can be successfully implemented during the development process, the objects contained in different classes in the software code can be tested independently, so that each test object can be tested independently without the influence of other objects it depends on. In this way, if there is a problem in the software code, the location of the problem can be easily found to process the problem code, so as to ensure that each class in the software code and the objects contained in each class can run smoothly, thereby ensuring the smooth operation of the software as a whole.

[0058] Based on this, the present description provides a method for testing code, wherein a test object needs to be obtained. The test object can be a group of codes in the code corresponding to a class for implementing a specific function. In other words, a group of codes in the code corresponding to the class for implementing a specific function can be used as a test object.

[0059] It should be noted that in the present description, only one test object will be included in an initial test case, that is, if a test object has been constructed in an initial test case, no other test object will be constructed.

[0060] In addition, in the present description, the execution subject of the method for testing code can be a notebook computer, a desktop computer, or a server. For the convenience of description, the present description only takes the terminal device as the execution subject to describe the method for testing code provided in the present description.

[0061] During the testing process, some objects relied on or called by the tested object (such as underlying services or underlying modules of the tested object) have complex logical structures, and if the objects are constructed in the initial test case, not only a large amount of system resources will be wasted, but also the constructed objects are not very stable during execution. Once the objects run abnormally or errors occur during construction, the test result will be affected.

[0062] Therefore, in order to focus on the tested object and exclude the influence of the objects relied on or called by the tested object on the testing process, the terminal device can use methods such as dependency service mock to simulate the objects relied on or called by the tested object, so as to directly obtain the return data of the objects. In this way, the return data of the objects can be obtained without constructing the processing logic of the objects, and the tested object can be tested by using the return data, thereby avoiding the influence of the objects relied on or called by the tested object on the test result.

[0063] Specifically, the terminal device can first analyze the tested object to determine all dependency objects relied on by the tested object, and then Mock the dependency objects to determine all member variables relied on by the tested object. The member variables can be return data corresponding to the dependency objects relied on or called by the current tested object, and during the testing process, the member variables can be used as input data of the tested object to perform testing on the tested object.

[0064] Of course, in actual application, some of the objects relied on or called by the tested object have relatively simple logical structures, and these objects will not affect the testing process even if they are constructed in the initial test case. Therefore, these objects with simple logical structures can not be Mocked, but can be directly constructed in the initial test case to obtain the member variables relied on by the tested object.

[0065] After the terminal device obtains all the member variables relied on by the tested object, the terminal device can inject the member variables into the tested object constructed in the initial test case by using a reflection injection algorithm, to obtain variable injection statements corresponding to the member variables.

[0066] After all the member variables are injected, the terminal device can assign values to the member variables according to initial values and value ranges of the member variables, to obtain variable initialization statements corresponding to the member variables. Then, the terminal device can combine the variable injection statements and the variable initialization statements to obtain variable call statements.

[0067] Specifically, the terminal device can first determine the variable injection statements corresponding to all the member variables, then determine the variable initialization statements corresponding to all the member variables, and then arrange the variable initialization statements after the variable injection statements to obtain the variable call statements. Of course, the terminal device can also arrange the variable initialization statement corresponding to each member variable after the variable injection statement corresponding to each member variable to obtain the variable call statement.

[0068] In addition, during the test process, the terminal device will also call the variable parameters corresponding to the member variables and the method parameters corresponding to each measured method, and therefore, the terminal device can construct the variable parameters corresponding to the member variables and the method parameters corresponding to each measured method in the initial test case through, for example, a Mock or New function, so as to obtain the corresponding parameter call statements and arrange the parameter call statements after the variable call statements.

[0069] The terminal device can also determine the measured methods included in the measured object and the calling order of the measured methods by performing static analysis on the measured object. The measured methods can be one or more functions included in the measured object, and the calling order of the measured methods is the same as the actual execution order of the measured methods when the measured object is actually executed.

[0070] For example, during the actual running of a measured object including an EXECUTE function, the INT function is executed first for assignment, and then the EXECUTE function is executed, so that the calling order of the measured methods included in the measured object in the test process of the measured object is the execution order of the above two functions, that is, the INT function is executed first, and then the EXECUTE function is executed.

[0071] The terminal device can construct the method call statement corresponding to each measured method, construct the method call statement sequence according to the calling order of the above measured methods, and arrange the method call statement sequence after the variable initialization statement.

[0072] In this specification, the terminal device can first determine all the member variables corresponding to the measured object and the variable call statements corresponding to each member variable, and then construct the method call statement sequence. The terminal device can also first construct the method call statement sequence, and then determine all the member variables corresponding to the measured object and the variable call statements corresponding to each member variable. Of course, both can be performed simultaneously, and this specification does not make specific limitations.

[0073] S102: Construct initial test cases corresponding to the measured object according to the variable call statements and the method call statement sequence, wherein each initial test case corresponds to only one measured object.

[0074] After determining the variable calling statement corresponding to the measured object, the terminal device can construct an initial test case statement according to the variable calling statement and the method calling statement, and the parameter calling statement.

[0075] Specifically, the terminal device can arrange the parameter calling statement between the variable calling statement and the method calling statement, so as to construct a complete initial test case in the order of the variable calling statement, the parameter calling statement, and the method calling statement, and each initial test case corresponds to only one measured object.

[0076] It should be noted that the test cases generated by different method parameter combinations cover different branches, paths, and selection conditions in the measured method, so one measured method can correspond to multiple initial test cases. It should be noted that although one measured object can correspond to multiple initial test cases, only one measured object corresponds to one initial test case.

[0077] S103: Determine a preferred test case according to each initial test case, and test the to-be-tested code through the preferred test case.

[0078] After determining each initial test case, the terminal device can reconstruct the test case based on the initial test cases, thereby obtaining each candidate test case.

[0079] Specifically, the terminal device can perform processing such as mutation (new parameters are generated by randomly changing part of parameter values) and crossover (parameters contained in different test cases are crossed to generate new parameters) on the method parameters corresponding to the called methods in the initial test case and the variable parameters corresponding to the called member variables, thereby obtaining processed method parameters and member variable parameters. Then, the terminal device can reconstruct the test case according to the processed method parameters and member variable parameters and the method calling statement and the variable calling statement contained in each initial test case, thereby obtaining a candidate test case.

[0080] It should be noted that the candidate test case mentioned in the present specification is a test case generated after reconstruction based on the initial test case, that is, the candidate test case can be a test case generated after any iteration.

[0081] The terminal device can thereby complete the first iteration of the initial test case and determine whether a preset condition is met. The preset condition can be that the comprehensive coverage rate corresponding to each candidate test case reaches a preset comprehensive coverage rate, or reaches a preset iteration number. The preset coverage rate and the preset iteration number can be set according to actual conditions, and the present specification does not make a specific limitation on this.

[0082] In the present specification, the coverage described above can be at least one of condition coverage, branch coverage, statement coverage, path coverage, etc. corresponding to each candidate test case, and the present specification does not make specific limitations thereto.

[0083] In actual application, the comprehensive coverage described above can also be represented by a fitness function, and the more the fitness function value approaches 0, the greater the comprehensive coverage, and the more the fitness function value deviates from 0, the smaller the comprehensive coverage. Of course, it can also be represented by the reciprocal of the fitness function value, and the greater the reciprocal of the fitness function, the greater the comprehensive coverage, and the smaller the reciprocal of the fitness function, the smaller the comprehensive coverage. The function value of the fitness function is greater than or equal to 0.

[0084] If the terminal device determines that the candidate test cases after the first iteration do not meet the preset condition, the method parameters corresponding to the called methods and the member variable parameters corresponding to the called member variables in these candidate test cases are processed, such as mutation, crossover, etc., to obtain the processed method parameters and the processed member variable parameters corresponding to each candidate test case.

[0085] Then, the terminal device can reconstruct the test cases according to the processed method parameters and the processed member variable parameters corresponding to each candidate test case obtained after the first iteration, and the method call statements and variable call statements contained in the candidate test cases, until the preset condition is met. Among the candidate test cases obtained after the preset condition is met, the preferred test case is determined.

[0086] In addition, the terminal device can also select some other methods related to the measured object from the measured methods contained in the initial test case, as the to-be-inserted measured methods. Among them, these to-be-inserted measured methods can be methods related to the measured object contained in other measured objects, can be methods contained in the class to which the other measured object belongs, and of course, can also be methods contained in the parent class to which the class corresponding to the measured object belongs, and the present specification does not make specific limitations thereto.

[0087] In order to avoid repeated construction of method call statements, for each to-be-inserted measured method, the terminal device can first determine whether the to-be-inserted measured method is contained in the current measured object.

[0088] If it is determined that the to-be-inserted method is not contained in the object under test, the terminal device can construct a method call statement corresponding to the to-be-inserted method in the candidate test case, insert the method call statement corresponding to the to-be-inserted method into the existing method call statement sequence in the candidate test case, obtain a new method call statement sequence, and then reconstruct the test case according to the new method call statement sequence and the variable call statement contained in the initial test case to obtain the candidate test case.

[0089] Of course, the terminal device can also directly obtain the method call statement corresponding to the to-be-inserted method, determine whether the method call statement is contained in the candidate test case, and if not, insert the method call statement into the existing method call statement sequence in the candidate test case and then reconstruct the test case.

[0090] In addition, since the candidate test case generated after each iteration contains some other method call statements in addition to the method call statements contained in the initial test case, the terminal device can randomly delete these method call statements to obtain a new candidate test case while retaining the original method call statements in the initial test case.

[0091] For each candidate test case, the terminal device can first determine the other method call statements in the candidate test case other than the method call statements contained in the initial test case as target call statements, and then delete at least part of the target call statements in the candidate test case according to a preset probability to determine the candidate test case after reconstruction in the current iteration according to the candidate test case after deletion of at least part of the target call statements, and then determine the preferred test case. The preset probability can be determined according to actual conditions, which is not limited in the specification.

[0092] It should be emphasized that for the method call statements, member variable statements, and parameter call statements contained in the initial test case, since these statements are necessary statements in the test process, once they are deleted, the normal operation of the test case and the accuracy of the test result cannot be guaranteed, and therefore these statements will not be deleted in each iteration.

[0093] In addition, in actual applications, the terminal device can use at least one of the reconstruction methods described above to reconstruct the test case until the preset condition is met, and determine the preferred test case according to the candidate test cases obtained after the preset condition is met.

[0094] Further, for each candidate test case obtained after satisfying the preset condition, the terminal device can determine a comprehensive coverage rate corresponding to the candidate test case and a coverage target determined after code testing by the candidate test case, where the coverage target can be a code line, a code branch, an execution path, a selection condition, etc. covered in the process of test execution by the test case, which is not specifically limited in the specification.

[0095] If the comprehensive coverage rate corresponding to the candidate test case is greater than a preset threshold, and the coverage target determined after code testing by the candidate test case is not completely the same as the coverage target determined after code testing by the preferred test case that has been determined, it indicates that the candidate test case covers new test targets, and the candidate test case can be used as the preferred test case.

[0096] If the coverage target determined after code testing by the candidate test case is the same as the coverage target determined after code testing by the preferred test case that has been determined, it indicates that the candidate test case does not cover new test targets, and the candidate test case can be deleted.

[0097] After determining the preferred test case corresponding to the tested object, the terminal device can traverse each line of code in each preferred test case, and identify abnormal codes contained in each preferred test case. In the actual testing process, these abnormal codes can be, for example, garbled codes, error codes, or other codes irrelevant to the tested method, etc.

[0098] If the change amount is greater than the preset change amount, it indicates that deleting the abnormal code will adversely affect the test result, and the abnormal code is not deleted.

[0099] Of course, in the specification, the terminal device can first delete the abnormal code, and then execute the test case to determine the change amount of the coverage rate corresponding to the test case. When the change amount of the coverage rate corresponding to the test case is greater than the preset change amount, the deleted abnormal code is restored.

[0100] The preset change amount can be set according to actual conditions, which is not specifically limited in the specification.

[0101] For ease of understanding, the present specification also provides a generation method schematic diagram of a preferred use case, as shown in Figure 2

[0102] Figure 2 A generation method schematic diagram of a preferred use case is provided for the present specification.

[0103] As can be seen from the figure, the generation process of the use case in the present specification is divided into three parts. In the use case initialization part, the server can first create the measured object, and then perform member variable analysis to determine all member variables corresponding to the measured object, and the variable call statements corresponding to these member variables. Then the server can determine the parameter call statements corresponding to the parameters contained in the measured object. For each measured method contained in the measured object, the server can analyze the execution order of the measured method contained in the measured object, and then determine the method call statement sequence. Finally, the server can determine the initial test case according to the variable call statement, the parameter call statement and the method call statement sequence.

[0104] In the use case iteration part, the server can perform use case iteration on the basis of the initial test case. For each candidate test case generated in the iteration process, the server can randomly insert the method call statements not contained in the test case according to a certain probability, and randomly delete other statements in the candidate test case except the method call statements, the parameter call statements and the variable call statements contained in the initial case according to a preset probability. In this way, the use case is reconstructed in each iteration process until the iteration target is met. Among the candidate test cases that meet the target, the candidate test cases that cover different targets are selected as preferred test cases.

[0105] In the use case simplification part, the server traverses each line of code in the preferred test case to identify abnormal codes and delete these abnormal codes. When deleting each abnormal code, the server also performs deletion verification, i.e., determines whether the coverage of the test case will decrease after deleting the abnormal code. If the coverage decreases, the deleted abnormal code is restored.

[0106] Then the terminal device can perform testing on the measured object through the determined preferred test case.

[0107] It should be noted that since each initial test case determined by the terminal device only corresponds to one measured object, each candidate test case and each preferred test case also only contains one measured object.

[0108] ​As can be seen from the above method, only one method to be tested corresponds to each test case in the specification, and all variable calling statements corresponding to the member variables to be called are determined in advance when the test case is constructed, and the calling sequence of each method to be tested in the tested object is considered. Compared with the test case constructed by the construction method of the prior art in which one test case corresponds to multiple tested objects and a random sequence of statements is used, the test case in the present scheme not only has more concise test case statements and more stable operation, but also has significantly improved test effect.

[0109] Moreover, in the process of reconstructing the test case, the present scheme does not delete the method calling statements contained in the initial test case, thereby ensuring the smooth execution of the test, and in addition, the present scheme does not take the candidate test case with the same coverage target as the preferred test case, thereby effectively reducing the number of test cases and further improving the comprehensive coverage rate of the test cases.

[0110] The above is the method for testing one or more embodiments of the present specification. Based on the same idea, the present specification also provides a corresponding code testing device, as shown in Figure 3 .

[0111] Figure 3 A schematic diagram of a code testing device provided by the present specification includes:

[0112] The determining module 301 is configured to determine, for each tested object, all member variables corresponding to the tested object and variable calling statements corresponding to each member variable, and construct a method calling statement sequence according to the calling sequence of each method to be tested contained in the tested object.

[0113] The constructing module 302 is configured to construct an initial test case corresponding to the tested object according to the variable calling statements and the method calling statement sequence, wherein each initial test case only corresponds to one tested object.

[0114] The testing module 303 is configured to determine a preferred test case according to each initial test case, and test the code to be tested through the preferred test case.

[0115] Optionally, the determining module 301 is specifically configured to determine, for each tested object, all member variables corresponding to the tested object and variable injection statements corresponding to each member variable; determine variable initialization statements corresponding to each member variable according to initial values and value ranges corresponding to each member variable; and combine the variable initialization statements and the variable injection statements to construct variable calling statements corresponding to each member variable.

[0116] Optionally, the test module 303 is specifically configured to reconstruct test cases according to the initial test cases to obtain candidate test cases; and determine the preferred test cases according to the candidate test cases.

[0117] Optionally, the test module 303 is specifically configured to process method parameters corresponding to the called methods and member variable parameters corresponding to the called member variables in the initial test cases to obtain processed method parameters and processed member variable parameters; and reconstruct test cases according to the processed method parameters and the processed member variable parameters, and the method calling statements and variable calling statements contained in the initial test cases to obtain candidate test cases.

[0118] Optionally, the test module 303 is specifically configured to, for each initial test case, if the determined to-be-inserted measured method is not contained in the measured object corresponding to the initial test case, reconstruct test cases according to the method calling statement corresponding to the to-be-inserted measured method, the method calling statements and variable calling statements contained in the initial test case to obtain candidate test cases.

[0119] Optionally, the test module 303 is specifically configured to, for each candidate test case, determine other method calling statements in the candidate test case except for the method calling statements contained in the initial test cases as target calling statements; and according to a preset probability, delete at least part of the target calling statements contained in the candidate test case to determine the preferred test cases according to the candidate test case after the at least part of the target calling statements are deleted.

[0120] Optionally, the test module 303 is specifically configured to, for each candidate test case, if the comprehensive coverage corresponding to the candidate test case is greater than a preset threshold, and the coverage target determined after code testing using the candidate test case is not completely same as the coverage target determined after code testing using the already determined preferred test cases, the candidate test case is taken as a preferred test case.

[0121] Optionally, the test module 303 is further configured to traverse each line of code in the preferred test case, identify the abnormal code contained in the preferred test case; and if it is determined that the change amount of the comprehensive coverage obtained after code testing according to other code in the preferred test case except for the abnormal code is less than a preset change amount, the abnormal code is deleted.

[0122] The specification also provides a computer readable storage medium, the storage medium stores a computer program, the computer program can be used to execute the above Figure 1 The provided method for code testing.

[0123] This instruction manual also provides Figure 4 One of the corresponding Figure 1 A schematic diagram of the structure of an electronic device. (e.g.) Figure 3 At the hardware level, the electronic device includes a processor, internal bus, network interface, memory, and non-volatile memory, and may also include other hardware required for the business operations. The processor reads the corresponding computer program from the non-volatile memory into memory and then runs it to achieve the above-mentioned functions. Figure 1 The code testing method described above. Of course, in addition to the software implementation method, this specification does not exclude other implementation methods, such as logic devices or a combination of hardware and software, etc. In other words, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.

[0124] In the 1990s, it was quite obvious to distinguish whether an improvement in a technology was in hardware (e.g., improvement in circuit structures of diodes, transistors, switches, etc.) or in software (improvement in method flow). However, as technology has evolved, many improvements in method flow today can be considered as direct improvements in hardware circuit structures. Designers almost always obtain the corresponding hardware circuit structures by programming the improved method flow into hardware circuits. Therefore, it cannot be said that an improvement in a method flow cannot be implemented by hardware entity modules. For example, a programmable logic device (PLD) (e.g., a field programmable gate array (FPGA)) is an integrated circuit whose logic function is determined by user programming of the device. A digital system is "integrated" on a PLD by the designer programming it, rather than by asking a chip manufacturer to design and fabricate a custom integrated circuit chip. Moreover, instead of manually fabricating integrated circuit chips, this programming is now mostly implemented by "logic compiler" software, which is similar to software compilers used in program development, and the original code to be compiled is written in a specific programming language, which is called a hardware description language (HDL), and there are many such languages, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc., and the most commonly used are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should be aware that, as long as the method flow is logically programmed in the above-mentioned hardware description languages and programmed into an integrated circuit, a hardware circuit implementing the logical method flow can be easily obtained.

[0125] The controller can be implemented in any suitable way, e.g. the controller can take the form of a microprocessor or processor and a computer readable medium storing computer readable program code, e.g. software or firmware, executable by the (micro)processor, logic gates, switches, an application specific integrated circuit (ASIC), a programmable logic controller and an embedded microcontroller, examples of controllers include but are not limited to the following microcontrollers: ARC 625D, Atmel AT91 SAM, Microchip PIC18F26K20 and Silicone Labs C8051F320, the memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art will also know that in addition to being implemented in pure computer readable program code form, the controller can perfectly well be implemented by means of logic programmed into logic gates, switches, application specific integrated circuits, programmable logic controllers and embedded microcontrollers, etc. to perform the same functions. The controller can thus be considered as a hardware component, and the means comprised therein for performing various functions can be considered as structures within the hardware component. Alternatively, or even, the means for performing various functions can be considered as both a software module implementing a method and a structure within a hardware component.

[0126] The systems, apparatuses, modules or units illustrated by the above embodiments can be implemented by computer chips or entities, or products with certain functions. A typical implementation device is a computer. Specifically, the computer can be a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.

[0127] For the sake of description, the above apparatuses are described in various units by functions respectively. Of course, the functions of each unit can be implemented in one or more software and / or hardware in the implementation of the present specification.

[0128] Those skilled in the art will understand that the embodiments of the present specification can be provided as a method, a system, or a computer program product. Therefore, the present specification can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present specification can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0129] The specification is presented with reference to flow diagrams and / or block diagrams of methods, apparatus (systems) and computer program products according to embodiments of the specification. It will be understood that each block of the flow diagrams and / or block diagrams, and combinations of blocks in the flow diagrams and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general purpose computer, special purpose computer, embedded processing element or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, create means for implementing the functions specified in the flow diagrams and / or block diagrams block or blocks. Figure 1 Figure 1 The flow diagrams and / or block diagrams in this specification can present a process and / or system and components of a process and / or system as boxes of the flow diagrams and / or block diagrams. Alternatively, and / or additionally, the flow diagrams and / or block diagrams can represent concurrent processes and / or systems, and / or concurrent processes and / or systems may

[0130] The computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flow diagrams and / or block diagrams block or blocks. Figure 1 Figure 1 The flow diagrams and / or block diagrams in this specification can present a process and / or system and components of a process and / or system as boxes of the flow diagrams and / or block diagrams. Alternatively, and / or additionally, the flow diagrams and / or block diagrams can represent concurrent processes and / or systems, and / or concurrent processes and / or systems may

[0131] The computer program instructions can also be loaded onto a computer or other programmable data processing apparatus to cause a series of operational steps to be performed on the computer or other programmable apparatus to produce a computer implemented process such that the instructions which execute on the computer or other programmable apparatus provide steps for implementing the functions specified in the flow diagrams and / or block diagrams block or blocks. Figure 1 ​ The flow diagrams and / or block diagrams in this specification can present a process and / or system and components of a process and / or system as boxes of the flow diagrams and / or block diagrams. Alternatively, and / or additionally, the flow diagrams and / or block diagrams can represent concurrent processes and / or systems, and / or concurrent processes and / or systems may

[0132] In a typical configuration, a computing device includes one or more processors (CPUs), input / output interfaces, network interfaces, and memory.

[0133] The memory can include non-persistent memory, Random Access Memory (RAM), and / or non-volatile memory, such as Read Only Memory (ROM) or flash memory, among others in a computer readable medium. The memory is an example of computer readable media.

[0134] ​​​Computer-readable media includes permanent and non-permanent, movable and non-movable media that can be implemented by any method or technology to store information. The information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible to a computing device. According to the definition herein, computer-readable media does not include transitory media such as modulated data signals and carriers.

[0135] It should also be noted that the terms "comprising", "comprising" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or apparatus that includes a list of elements not only includes those elements, but also includes other elements not explicitly listed or inherent to such process, method, article or apparatus. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, method, article or apparatus that includes the element.

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

[0137] The present specification can be described in the general context of computer-executable instructions, such as program modules, executed by computers. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The present specification can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in both local and remote computer storage media, including storage devices.

[0138] The various embodiments described in this specification are described using a numbering of embodiments approach: these are each individually integrated contributions pertaining to different aspects of the description. For each embodiment, the description focuses on the differences from the other embodiments. In particular, the description of the system embodiments is relatively brief, as the system embodiments are largely analogous to the method embodiments. The relevant parts of the description of the method embodiments are therefore referred to.

[0139] The above description is embodied in the form of examples of the description, and is not intended to limit the description. For those skilled in the art, the description can have various modifications and changes. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the description shall be included in the scope of claims of the description.

Claims

1. A method for code testing, comprising: For each object under test, determine all member variables corresponding to the object under test and the variable call statements corresponding to each member variable, and construct the method call statement sequence according to the call order of each method under test contained in the object under test; Based on the variable call statement and the method call statement sequence, construct the initial test cases corresponding to the object under test, wherein each initial test case corresponds to only one object under test; Based on each initial test case, optimal test cases are determined, and the code to be tested is tested using the optimal test cases. The optimal test cases are test cases with different coverage targets obtained after refactoring the initial test cases.

2. The method as described in claim 1, for each tested object, determines all member variables corresponding to the tested object and the variable calling statements corresponding to each member variable, specifically including: For each object under test, determine all member variables corresponding to that object under test, as well as the variable injection statements corresponding to each member variable; Based on the initial values ​​and ranges of each member variable, determine the variable initialization statements corresponding to each member variable; The variable initialization statement is combined with the variable injection statement to construct the variable call statement corresponding to each member variable.

3. The method as described in claim 1, wherein determining preferred test cases based on each initial test case specifically includes: Based on the initial test cases, the test cases are reconstructed to obtain candidate test cases; Based on the candidate test cases, the preferred test cases are determined.

4. The method as described in claim 3, wherein test cases are reconstructed based on the initial test cases to obtain candidate test cases, specifically including: The method parameters and member variable parameters corresponding to the methods called in each initial test case are processed to obtain the processed method parameters and processed member variable parameters. Based on the processed method parameters and processed member variable parameters, as well as the method call statements and variable call statements contained in each initial test case, the test cases are reconstructed to obtain candidate test cases.

5. The method as described in claim 3, wherein test cases are reconstructed based on the initial test cases to obtain candidate test cases, specifically including: For each initial test case, if the object under test corresponding to the initial test case does not contain the determined method to be inserted, then the test case is reconstructed based on the method call statement corresponding to the method to be inserted, the method call statement and variable call statement contained in the initial test case, to obtain candidate test cases.

6. The method as described in claim 3, wherein the preferred test cases are determined based on each candidate test case, specifically including: For each candidate test case, other method call statements besides those included in the initial test case are identified from that candidate test case and used as target call statements; Based on a preset probability, at least some of the target call statements contained in the candidate test case are deleted, so as to determine the preferred test case based on the candidate test case with the at least some target call statements deleted.

7. The method as described in claim 3, wherein the preferred test cases are determined based on each candidate test case, specifically including: For each candidate test case, if the overall coverage rate corresponding to the candidate test case is greater than the preset threshold, and the coverage target determined after testing the code using the candidate test case is not exactly the same as the coverage target determined after testing the code using the already determined preferred test cases, then the candidate test case is taken as the preferred test case.

8. The method of claim 1, further comprising: Iterate through each line of code in the preferred test cases and identify the abnormal code contained in the preferred test cases; If the change in overall coverage obtained after testing the code according to the preferred test cases (excluding the abnormal code) is less than the preset change, then the abnormal code is deleted.

9. An apparatus for code testing, comprising: The module determines all member variables and variable call statements corresponding to each member variable for each object under test, and constructs a method call statement sequence based on the call order of each method under test contained in the object under test. The construction module constructs initial test cases corresponding to the object under test based on the variable call statement and the method call statement sequence, wherein each initial test case corresponds to only one object under test; The testing module determines preferred test cases based on the initial test cases, and tests the code to be tested using the preferred test cases. The preferred test cases are test cases with different coverage targets obtained after refactoring the initial test cases.

10. The apparatus of claim 9, wherein the determining module is specifically configured to: for each tested object, determine all member variables corresponding to the tested object, and variable injection statements corresponding to each member variable; determine variable initialization statements corresponding to each member variable based on the initial values ​​and value ranges corresponding to each member variable; and combine the variable initialization statements with the variable injection statements to construct variable calling statements corresponding to each member variable.

11. The apparatus of claim 9, wherein the testing module is specifically configured to: reconstruct test cases based on each initial test case to obtain candidate test cases; and determine preferred test cases based on each candidate test case.

12. The apparatus of claim 11, wherein the testing module is specifically configured to process the method parameters corresponding to the methods called in each initial test case and the member variable parameters corresponding to the called member variables to obtain processed method parameters and processed member variable parameters; and reconstruct test cases based on the processed method parameters and processed member variable parameters, as well as the method call statements and variable call statements contained in each initial test case, to obtain candidate test cases.

13. The apparatus of claim 11, wherein the testing module is specifically configured to, for each initial test case, if the object under test corresponding to the initial test case does not contain the determined method to be inserted under test, then reconstruct the test case according to the method call statement corresponding to the method to be inserted under test, the method call statement and variable call statement contained in the initial test case, to obtain candidate test cases.

14. The apparatus of claim 11, wherein the testing module is specifically configured to, for each candidate test case, determine, from the candidate test cases, other than the method call statements contained in the initial test case, as target call statements; Based on a preset probability, at least some of the target call statements contained in the candidate test case are deleted, so as to determine the preferred test case based on the candidate test case with the at least some target call statements deleted.

15. The apparatus of claim 11, wherein the testing module is specifically configured to, for each candidate test case, if the overall coverage rate corresponding to the candidate test case is greater than a preset threshold, and the coverage target determined after using the candidate test case for code testing is not completely the same as the coverage target determined after using the already determined preferred test cases for code testing, then the candidate test case is used as the preferred test case.

16. The apparatus of claim 9, wherein the testing module is further configured to: traverse each line of code in the preferred test cases, identify abnormal code contained in the preferred test cases; and if it is determined that the change in overall coverage obtained after testing the code according to the other code in the preferred test cases excluding the abnormal code is less than a preset change, then delete the abnormal code.

17. A computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described in any one of claims 1 to 8.

18. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method described in any one of claims 1 to 8.

Citation Information

Patent Citations

  • Intelligent contract test case generation method applying improved genetic algorithm

    CN111797010A

  • Application program testing method and device, computer equipment and storage medium

    CN114064497A