A method and device for code testing and test case generation

By obtaining the parameter type and bytecode analysis of the code to be tested, determining the actual return value type, and generating a Mock method for testing, the problem of difficulty in testing the method to be tested separately in software testing is solved, and the test accuracy and coverage are improved.

CN114840427BActive Publication Date: 2025-09-19ALIPAY (HANGZHOU) INFORMATION TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202210508851.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-05-10
Publication Date
2025-09-19
Estimated Expiration
2042-05-10

AI Technical Summary

Technical Problem

In software testing, the high dependency between the method under test and other methods makes it difficult for testers to test the method under test independently, hindering the testing process. Furthermore, the existing technology uses mock services to randomly determine the return value type, reducing the accuracy and coverage of test cases.

Method used

By obtaining the parameter types of the methods called by the code under test and analyzing the bytecode, the actual return value type is determined, and then a mock method is generated for testing. This method includes determining the actual return value type of generic methods and forced type conversion methods and generating a mock method based on this.

Benefits of technology

It improves the accuracy of test results, enhances the coverage of test cases, and ensures effective testing of the code to be tested.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114840427B_ABST
    Figure CN114840427B_ABST
Patent Text Reader

Abstract

This specification discloses a method and device for code testing and test case generation, which can accurately determine the actual return value types of generic methods called by the code to be tested and methods that undergo forced type conversion during the execution of the code to be tested, so that the code to be tested can be tested based on the determined actual return value type, thereby improving the accuracy of the test results.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This specification relates to the field of computer technology, and in particular to a method and device for code testing and test case generation. Background Art

[0002] During the software testing process, since the method to be tested may have a high degree of dependency on other methods, the tester may not be able to test the method to be tested alone, which in turn affects the progress of the testing work. Therefore, in order to solve this problem, software testers usually use Mock services (i.e., dependency service substitutes) to replace other methods associated with the method to be tested to test the method to be tested.

[0003] However, when using the Mock service, it is necessary to determine the return value type of the method called by the method to be tested. For ordinary methods, the return value type of the method can be determined directly, while for some methods whose return value needs to be determined according to the actual runtime, a type is often randomly determined as the return value type of the method, which reduces the accuracy of the generated test cases and affects the coverage of the test cases.

[0004] Therefore, how to effectively determine the return value type of the method to be tested during software testing, and thereby improve the accuracy of the test results of the method to be tested, is a problem that needs to be solved urgently. Summary of the Invention

[0005] This specification provides a method and apparatus for code testing and test case generation to partially solve the problem in the prior art of low test result accuracy caused by returning an erroneous return value.

[0006] This manual adopts the following technical solutions:

[0007] This specification provides a code testing method, including:

[0008] Get the code to be tested;

[0009] Determining, based on the types of parameters required by the method called by the code to be tested and / or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, the actual return value type corresponding to the method called when the code to be tested is tested, where the method type of the method called when the code to be tested is tested includes at least one of a generic method and a forced type conversion;

[0010] A test result for the code to be tested is obtained according to the actual return value type.

[0011] Optionally, determining the actual return value type corresponding to the method called when testing the code to be tested based on the type of parameters required by the method called by the code to be tested and / or the analysis result obtained after analyzing the bytecode corresponding to the code to be tested specifically includes:

[0012] Get the code of the method called by the code to be tested;

[0013] Determine, based on the obtained code of the method called by the code to be tested, the association between the actual return value of the method called when the code to be tested is tested and the parameter type required by the method called by the code to be tested;

[0014] According to the association relationship and the types of parameters required by the method called by the code to be tested, the actual return value type corresponding to the method called when the code to be tested is tested is determined.

[0015] Optionally, obtaining a test result for the code to be tested according to the actual return value type specifically includes:

[0016] According to the association relationship, a method data structure MethodDescripter structure is created, wherein the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, and the input parameters of the calling method;

[0017] Generate a mock method corresponding to the method called by the code to be tested according to the MethodDescripter structure and the actual return value type;

[0018] The mock method is used to obtain the test result for the code to be tested.

[0019] Optionally, determining the actual return value type corresponding to the method called when testing the code to be tested based on the type of parameters required by the method called by the code to be tested and / or the analysis result obtained after analyzing the bytecode corresponding to the code to be tested specifically includes:

[0020] By analyzing the bytecode corresponding to the code to be tested, determining, from the methods called by the code to be tested, a method whose method type is a forced type conversion, as the target method;

[0021] When testing the code to be tested, the actual return value type corresponding to the method called by the code to be tested is determined according to the target method.

[0022] Optionally, when testing the code to be tested, before determining, according to the target method, an actual return value type corresponding to the method called by the code to be tested, the method further includes:

[0023] Save the corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type of the return value of the target method after forced type conversion;

[0024] When testing the code to be tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method specifically includes:

[0025] If, during the testing process of the code to be tested, the code line number of the method called by the code to be tested is determined to be a method of forced type conversion, the code line number is used as the target line number;

[0026] According to the target line number, the actual return value type corresponding to the method called by the code to be tested is queried from the pre-saved corresponding relationship.

[0027] Optionally, obtaining a test result for the code to be tested according to the actual return value type specifically includes:

[0028] Generate a mock method corresponding to the method called by the code to be tested according to the actual return value type corresponding to the method called by the code to be tested in the pre-saved corresponding relationship;

[0029] The mock method is used to obtain the test result for the code to be tested.

[0030] This specification provides a method for generating test cases, including:

[0031] Get each test case;

[0032] For each test case, test the set code using the above code testing method to obtain the test result corresponding to the test case;

[0033] Determine whether the preset test conditions are met based on the test results corresponding to each test case;

[0034] If it is determined that the test condition is not met, each test case is regenerated, and the setting code is tested according to each regenerated test case until the test condition is met, thereby obtaining a test case generated when the test condition is met.

[0035] Optionally, regenerate each test case, including:

[0036] Select at least some of the test cases from each test case and combine them in pairs to obtain test case combinations;

[0037] For each test case combination, the same type of parameters in the two test cases included in the test case combination are weighted to obtain a regenerated test case.

[0038] Optionally, regenerate each test case, including:

[0039] selecting at least some of the test cases from each of the test cases;

[0040] For each selected test case, at least part of the parameters included in the test case are numerically adjusted, and / or the code to be tested included in the test case is adjusted to obtain a regenerated test case.

[0041] This specification provides a code testing device, including:

[0042] Get module, used to get the code to be tested;

[0043] a determination module, configured to determine, based on the types of parameters required by the method called by the code to be tested and / or an analysis result obtained after analyzing the bytecode corresponding to the code to be tested, an actual return value type corresponding to the method called when the code to be tested is tested, wherein the method type of the method called when the code to be tested is tested includes at least one of a generic method and a forced type conversion;

[0044] The test module is used to obtain a test result for the code to be tested according to the actual return value type.

[0045] Optionally, the determination module is specifically used to obtain the code of the method called by the code to be tested; determine the actual return value of the method called when the code to be tested is tested, and the association relationship with the parameter type required by the method called by the code to be tested based on the obtained code of the method called by the code to be tested; determine the actual return value type corresponding to the method called when the code to be tested is tested based on the association relationship and the type of parameters required by the method called by the code to be tested.

[0046] Optionally, the test module is specifically used to create a method data structure MethodDescripter structure based on the association relationship, and the MethodDescripter structure includes: the actual return value type of the called method, the number of times the MethodDescripter structure is called, the method name of the called method, the class name of the called method, and the incoming parameters of the called method; based on the MethodDescripter structure and the actual return value type, generate a mock method corresponding to the method called by the code to be tested; and obtain the test results for the code to be tested through the mock method.

[0047] Optionally, the determination module is specifically used to determine, by analyzing the bytecode corresponding to the code to be tested, a method whose method type is a forced type conversion from the methods called by the code to be tested, as a target method; when testing the code to be tested, determine the actual return value type corresponding to the method called in the code to be tested according to the target method.

[0048] Optionally, the device further comprises:

[0049] An analysis module is used to save the correspondence between the code line number corresponding to the target method in the code to be tested and the actual return value type of the return value of the target method after forced type conversion;

[0050] The determination module is specifically used to, if during the testing process of the code to be tested, determine the code line number of the method called by the code to be tested as a forced type conversion method, as the target line number; according to the target line number, query the actual return value type corresponding to the method called by the code to be tested from a pre-saved correspondence relationship.

[0051] Optionally, the test module is specifically used to generate a mock method corresponding to the method called by the code to be tested based on the actual return value type corresponding to the method called by the code to be tested in a pre-saved correspondence relationship; and obtain the test result for the code to be tested through the mock method.

[0052] This specification provides a device for generating test cases, including:

[0053] Use case acquisition module, used to obtain each test case;

[0054] The use case testing module is used to test the set code for each test case using the above code testing method to obtain the test result corresponding to the test case;

[0055] The judgment module is used to determine whether the preset test conditions are met based on the test results corresponding to each test case;

[0056] The generation module is used to regenerate each test case when it is determined that the test condition is not met, and test the setting code according to the regenerated test cases until the test condition is met, thereby obtaining the test case generated when the test condition is met.

[0057] Optionally, the generation module is specifically used to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, weight the same type of parameters in the two test cases contained in the test case combination to obtain a regenerated test case.

[0058] Optionally, the generation module is specifically used to select at least part of the test cases from each test case; for each selected test case, numerically adjust at least part of the parameters contained in the test case, and / or adjust the code to be tested contained in the test case to obtain a regenerated test case.

[0059] This specification provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the above-mentioned code testing and test case generation methods.

[0060] This specification provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the above-mentioned code testing and test case generation methods are implemented.

[0061] At least one of the above technical solutions adopted in this specification can achieve the following beneficial effects:

[0062] In the code testing method provided in this specification, the code to be tested is first obtained, and then the actual return value type corresponding to the method called when the code to be tested is tested is determined based on the type of parameters required by the method called by the code to be tested and / or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested. The method type of the method called when the code to be tested is tested includes: calling a generic method and at least one of forced type conversion, and the test result for the code to be tested is obtained based on the actual return value type.

[0063] It can be seen from the above method that when testing the code to be tested, the actual return value types of the generic methods called during the running of the code to be tested and the methods where forced type conversion occurs can be accurately determined, so that the code to be tested can be tested based on the determined actual return value types, thereby improving the accuracy of the test results. BRIEF DESCRIPTION OF THE DRAWINGS

[0064] The drawings described herein are used to provide a further understanding of this specification and constitute a part of this specification. The exemplary embodiments and descriptions of this specification are used to explain this specification and do not constitute an improper limitation of this specification. In the drawings:

[0065] Figure 1 A flowchart of a code testing method provided in this specification;

[0066] Figure 2 A schematic diagram of a method for determining the actual return value type of a generic method provided in this specification;

[0067] Figure 3 A flowchart of a test case generation method provided in this specification;

[0068] Figure 4 A schematic diagram of a code testing device provided in this specification;

[0069] Figure 5 A schematic diagram of a device for generating a test case provided in this specification;

[0070] Figure 6 This manual provides a corresponding Figure 1 Schematic diagram of the electronic device. DETAILED DESCRIPTION

[0071] To make the objectives, technical solutions, and advantages of this specification more clear, the following will clearly and completely describe the technical solutions of this specification in conjunction with the specific embodiments of this specification and the corresponding drawings. Obviously, the embodiments described are only part of the embodiments of this specification, not all of the embodiments. Based on the embodiments in this specification, all other embodiments obtained by ordinary technicians in this field without making any creative efforts are within the scope of protection of this specification.

[0072] The technical solutions provided by the embodiments of this specification are described in detail below with reference to the accompanying drawings.

[0073] Figure 1 This is a flowchart of a code testing method provided in this specification, which includes the following steps:

[0074] S101: Obtain the code to be tested.

[0075] In this specification, the tester can test the code of the software to be tested based on the test requirements. Specifically, the tester can first determine a part of the code from the code of the software to be tested as the code to be tested, and then test the code to be tested to obtain the test results for the code to be tested.

[0076] Among them, the code to be tested can refer to a collection of codes that are part of the code of the software to be tested. For example, the code to be tested can be the code corresponding to one or more classes to be tested (the class here refers to a custom reference data type, which encapsulates several properties and methods) contained in the code of the software to be tested. For another example, the code to be tested can also be the code corresponding to one or more methods (that is, a collection of code statements, which together execute a function, and the method is contained in a class or an object of the class) contained in the code of the software to be tested.

[0077] In this specification, the execution subject of the method for implementing code testing may refer to a designated device such as a terminal device set up on a business platform, or may refer to a terminal device such as a desktop computer, a laptop computer, etc. For the sake of ease of description, the following only takes the terminal device as the execution subject as an example to illustrate the code testing method provided in this specification.

[0078] Therefore, after receiving the test instruction sent by the tester, the terminal device can first obtain the code to be tested, and then test the code to be tested to obtain the test result of the code to be tested.

[0079] S102: Determine the actual return value type corresponding to the method called when the code to be tested is tested based on the type of parameters required by the method called by the code to be tested and / or the analysis result obtained after analyzing the bytecode corresponding to the code to be tested, where the method type of the method called when the code to be tested is tested includes: at least one of a generic method and a forced type conversion.

[0080] When the code to be tested is running, the terminal device can intercept a call instruction of a method called by the code to be tested during the running process, and determine the method type of the method called by the code to be tested during the running process according to the intercepted call instruction.

[0081] Among them, the method type of the method called by the code to be tested during execution can be at least one of a generic method (i.e., a method in which the type of the input parameter of the method is uncertain) and a forced type conversion (i.e., a method in which a forced type conversion occurs during the calling process, wherein the forced type conversion occurs by converting the original return value type of the method into another data type).

[0082] It should be noted that the interception mentioned above is not literal interception, but a method similar to interception effect achieved through dynamic proxy by monitoring the call instructions of the method called by the code to be tested during its execution.

[0083] Based on the introduction of the above method types, this specification mainly determines the actual return value types of the above two method types, namely, generic methods and forced type conversions. Therefore, the following will explain in detail how to determine the actual return value types of the two method types.

[0084] For generic methods, after intercepting the calling instruction of the generic method in the code to be tested, the terminal device can obtain the code of the generic method called in the code to be tested, and thus determine the actual return value type of the method called when the code to be tested is tested, and the association relationship with the parameter type required by the method called by the code to be tested based on the obtained code of the generic method called by the code to be tested. Then, based on the determined association relationship, the actual return value type corresponding to the generic method called when the code to be tested is tested can be determined, such as Figure 2 shown.

[0085] Figure 2 This is a schematic diagram of a method for determining the actual return value type of a generic method provided in this specification.

[0086] Combine Figure 2 It can be seen that the terminal device can monitor the calling instructions of the method called by the code to be tested during the execution of the code to be tested, and then can obtain the code of the generic method called in the code to be tested before the called method is executed, so as to perform generic analysis on the obtained code of the generic method called by the code to be tested, and determine the various actual return value types of the generic method called when the code to be tested is tested, and the association between the various parameter types required by the generic method called by the code to be tested (that is, the parameter type that needs to be passed in when the code to be tested calls the method)

[0087] Furthermore, the terminal device can create each method data structure MethodDescripter structure based on the determined association relationship (for each incoming parameter type of the generic method, the combination of the incoming parameter type and the actual return value type corresponding to the incoming parameter type corresponds to a MethodDescripter structure), and then determine the actual incoming parameters when the code to be tested calls the generic method based on the intercepted calling instructions, and based on the incoming parameters, query the MethodDescripter structure that matches the incoming parameters in each MethodDescripter structure, and then determine the actual return value type of the generic method called when the code to be tested is tested.

[0088] The MethodDescripter structure includes: the actual return value type of the called method, the number of times the MethodDescripter structure is called, the method name of the called method, the class name of the called method, the input parameters of the called method, and the unique identification ID of the MethodDescripter structure.

[0089] Furthermore, the terminal device can generate a mock method corresponding to the method called by the code to be tested based on the MethodDescripter structure that matches the input parameters, and then simulate the return value of the method called by the code to be tested through the mock method to obtain the test result for the code to be tested.

[0090] It should be noted that since the same generic method may be called multiple times in the code to be tested, when generating the mock method corresponding to the generic method, it is necessary to assemble the mock statements in the order of calling the MethodDescripter structure according to the number of times the MethodDescripter structure is called to generate the mock method.

[0091] For methods that require forced type conversion, the terminal device can, before testing the code to be tested, analyze the bytecode corresponding to the code to be tested in advance, determine the method whose method type is forced type conversion from the methods called by the code to be tested, and save the corresponding code line number of the target method in the code to be tested and the corresponding relationship between the actual return value type of the target method after the forced type conversion (considering the easy-to-read feature of data saved in the hash table, the corresponding relationship here can be saved in the hash table).

[0092] After completing the storage of the above correspondence, the terminal device can determine the actual return value type based on the above correspondence during the code testing process. Specifically, after intercepting the call instruction of the method called by the forced type conversion in the code to be tested, the method information of the calling method can be obtained. The method information here includes: the code of the calling method, and the context information of the calling method, wherein the context information of the calling method includes: the variable object defined by the calling method, the scope chain of the calling method (that is, the sequential linked list for searching variables in the calling method), the calling code of the caller (that is, the code of the method that calls the generic method or the forced conversion in the code to be tested), etc.

[0093] Furthermore, the terminal device can determine the code line number of the method called by the code to be tested where the forced type conversion occurs based on the obtained method information of the calling method, as the target line number, and then, based on the target line number, query the actual return value type corresponding to the method called by the code to be tested where the forced type conversion occurs from the correspondence between the pre-saved code line number corresponding to the method where the forced type conversion occurs in the code to be tested and the actual return value type of the return value of the method where the forced type conversion occurs after the forced type conversion.

[0094] Because in actual applications, when performing bytecode analysis on the code to be tested, not all the methods that require forced conversion contained in the code to be tested may be identified. This will result in the inability to query the actual return value type corresponding to the method that requires forced type conversion called by the code to be tested from the pre-saved correspondence based on the code line number of the method that requires forced conversion in the code to be tested during the actual code testing process.

[0095] For this situation, in this specification, the terminal device can arbitrarily select a data type from a preset type pool as a candidate return value type, and then use the candidate return value type as the actual return value type of the method that is called when the code to be tested and undergoes forced type conversion, and obtain a test result based on the candidate return value type. Then, based on the test result, it can be determined whether the candidate return value type is the actual return value type of the method that is called when the code to be tested and undergoes forced type conversion; if not, a return value type is selected from the type pool again as the candidate return value type, until it is determined that the candidate return value type is the actual return value type of the method that is called when the code to be tested and undergoes forced type conversion.

[0096] The data types in the type pool described above may be pre-determined by the terminal device before the code under test is executed. Specifically, the terminal device may analyze the bytecode corresponding to the code under test to determine all data types contained in the code under test, and then save the determined data types contained in the code under test to the pre-determined type pool.

[0097] From the above content, it can be seen that when testing the code to be tested, the terminal device can accurately determine the actual return value types of the generic methods called during the operation of the code to be tested and the methods that undergo forced type conversion. Therefore, the code to be tested can be tested based on the determined actual return value type, thereby improving the accuracy of the test results.

[0098] In practical applications, the above code testing method can not only be used to test the code to be tested, but also to generate test cases. That is, through the above code testing method, some preferred test cases that can achieve multiple test goals (the goal here can refer to the various coverage rates of the code to be tested reaching preset thresholds) are constructed. The method of generating test cases is described in detail below. Figure 3 shown.

[0099] Figure 3 A flow chart of a test case generation method provided in this specification includes the following steps:

[0100] S301: Obtain each test case.

[0101] In this specification, the terminal device can obtain code association information of the code to be tested before testing the code to be tested, and then generate various test cases based on the code to be tested and the association information of the code to be tested, wherein the code association information can be, for example: document information of the code to be tested, context information of the code to be tested, etc.

[0102] S302: For each test case, the code to be tested is tested using the above code testing method to obtain a test result corresponding to the test case.

[0103] For each test case, the terminal device can run the test case through the above-mentioned code testing method to test the setting code contained in the test case and obtain the test result corresponding to the test case, wherein the setting code can refer to the code to be tested, or it can refer to any piece of code (for example: the code that has been tested can also be used as the setting code, and then by using the means of testing the setting code, some preferred test cases are finally obtained.) The test results here refer to the test effect of the code to be tested, such as: the line coverage of the code to be tested, the method coverage of the code to be tested, the exception coverage of the code to be tested, etc.

[0104] The initial test cases generated by the terminal device usually cannot achieve the multiple pre-set test objectives well. Therefore, it is necessary to iterate based on these initial test cases to optimize each initial test case and ultimately achieve the test objectives of the test code. The specific process of iterating for each test case is achieved by the following steps.

[0105] S303: Determine whether the preset test conditions are met based on the test results corresponding to each test case.

[0106] S304: If it is determined that the test condition is not met, regenerate each test case, and test the code to be tested according to each regenerated test case until the test condition is met, thereby obtaining the test case generated when the test condition is met.

[0107] The terminal device determines whether the preset test conditions are met based on the test results of each test case. If it is determined that the test conditions are not met, the terminal device regenerates each test case and tests the test code based on the regenerated test cases until the test conditions are met, and obtains the test cases generated when the test conditions are met.

[0108] The preset test conditions may be formulated according to the test requirements, for example, the line coverage of the code to be tested reaches a preset threshold, or the number of iterations reaches a preset threshold.

[0109] In the above content, the method for regenerating each test case can be to sort each test case according to the test results of each test case (for example, sort according to the line coverage of the code to be tested), and filter out each basic test case based on the sorted test cases.

[0110] Furthermore, the terminal device can select at least some basic test cases from each basic test case and combine them in pairs to obtain each test case combination, and for each test case combination, weight the same type of parameters contained in the two basic test cases in the test case combination to obtain a regenerated test case.

[0111] In addition, the terminal device can also select at least some basic test cases from the basic test cases, and for each selected basic test case, adjust the parameters contained in the basic test case numerically, and / or adjust the code to be tested contained in the basic test case to regenerate each test case.

[0112] In addition, since during the running of the test case, the generic method called by the code to be tested usually has multiple types of parameters that can be passed in, the terminal device can also regenerate each test case based on the MethodDescripter structure generated during the running of the test case, which contains: the actual return value type of the called method, the number of times the MethodDescripter structure is called, the method name of the called method, the class name of the called method, and the type of the parameters passed in to the called method, so as to test the various input parameter types and the actual return value type of the generic method called by the code to be tested.

[0113] It should be noted that the above three methods can be used individually to regenerate each test case, or can be used together to regenerate the test case.

[0114] From the above content, it can be seen that the terminal device can generate initial test cases for the setting code, and in the process of testing the setting code through each initial test case, according to the test results of each initial test case, optimize each initial test case in an iterative manner, so that the final generated test case can comprehensively test the setting code, thereby improving the test effect.

[0115] The above is a code testing method provided by one or more embodiments of this specification. Based on the same idea, this specification also provides a corresponding code testing device and a test case generation device such as Figure 4 、 Figure 5 shown.

[0116] Figure 4 A schematic diagram of a code testing device provided in this specification includes:

[0117] Acquisition module 401, used to obtain the code to be tested;

[0118] Determination module 402 is configured to determine the actual return value type corresponding to the method called when the code to be tested is tested based on the type of parameters required by the method called by the code to be tested and / or the analysis result obtained after analyzing the bytecode corresponding to the code to be tested, where the method type called when the code to be tested is tested includes at least one of a generic method and a forced type conversion.

[0119] The testing module 403 is used to obtain a test result for the code to be tested according to the actual return value type.

[0120] Optionally, the determination module 402 is specifically used to obtain the code of the method called by the code to be tested; determine the actual return value of the method called when the code to be tested is tested, and the association relationship with the parameter type required by the method called by the code to be tested based on the obtained code of the method called by the code to be tested; determine the actual return value type corresponding to the method called when the code to be tested is tested based on the association relationship and the type of parameters required by the method called by the code to be tested.

[0121] Optionally, the test module 403 is specifically used to create a method data structure MethodDescripter structure based on the association relationship, and the MethodDescripter structure includes: the actual return value type of the called method, the number of times the MethodDescripter structure is called, the method name of the called method, the class name of the called method, and the incoming parameters of the called method; based on the MethodDescripter structure and the actual return value type, generate a mock method corresponding to the method called by the code to be tested; and obtain the test results for the code to be tested through the mock method.

[0122] Optionally, the determination module 402 is specifically used to determine, by analyzing the bytecode corresponding to the code to be tested, a method whose method type is a forced type conversion from the methods called by the code to be tested, as a target method; when testing the code to be tested, determine the actual return value type corresponding to the method called in the code to be tested according to the target method.

[0123] Optionally, the device further comprises:

[0124] An analysis module 404 is configured to store a correspondence between a line number of the target method in the code to be tested and an actual return value type of the target method after the return value is converted to a mandatory type.

[0125] The determining module 402 is specifically configured to, if during the testing process of the code to be tested, determine the code line number of the method called by the code to be tested as a method of forced type conversion, and use it as the target line number;

[0126] According to the target line number, the actual return value type corresponding to the method called by the code to be tested is queried from the pre-saved corresponding relationship.

[0127] Optionally, the testing module 403 is specifically used to generate a mock method corresponding to the method called by the code to be tested based on the actual return value type corresponding to the method called by the code to be tested in a pre-saved correspondence relationship; and obtain the test result for the code to be tested through the mock method.

[0128] Figure 5 A schematic diagram of a device for generating test cases provided in this specification, including:

[0129] A test case acquisition module 501 is used to acquire each test case;

[0130] The use case testing module 502 is used to test the set code for each test case using the above code testing method to obtain the test result corresponding to the test case;

[0131] The judgment module 503 is used to determine whether the preset test conditions are met according to the test results corresponding to each test case;

[0132] The generation module 504 is used to regenerate each test case when it is determined that the test condition is not met, and test the setting code according to each regenerated test case until the test condition is met, thereby obtaining the test case generated when the test condition is met.

[0133] Optionally, the generation module 504 is specifically used to select at least some test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, weight the same type of parameters in the two test cases contained in the test case combination to obtain a regenerated test case.

[0134] Optionally, the generation module 504 is specifically used to select at least part of the test cases from each test case; for each selected test case, numerically adjust at least part of the parameters contained in the test case, and / or adjust the code to be tested contained in the test case to obtain a regenerated test case.

[0135] This specification also provides a computer-readable storage medium, which stores a computer program that can be used to execute the above Figure 1 Provides a method for code testing and test case generation.

[0136] This manual also provides Figure 6 The one shown corresponds to Figure 1 Schematic diagram of the electronic equipment. Figure 6As mentioned above, at the hardware level, the electronic device includes a processor, an internal bus, a network interface, a memory and a non-volatile memory, and may also include other hardware required for the business. The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it to achieve the above Figure 1 The code testing and test case generation methods described above. Of course, in addition to software implementation, this specification does not exclude other implementation methods, such as logic devices or a combination of software and hardware, 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.

[0137] In the 1990s, technological improvements could be clearly distinguished as either hardware improvements (for example, improvements to circuit structures like diodes, transistors, and switches) or software improvements (improvements to process flows). However, with the advancement of technology, many process flow improvements today can now be considered direct improvements to hardware circuit structures. Designers almost always create the corresponding hardware circuit structure by programming the improved process flow into the hardware circuit. Therefore, it cannot be said that a process flow improvement cannot be implemented using hardware modules. For example, a programmable logic device (PLD), such as a field programmable gate array (FPGA), is an integrated circuit whose logical function is determined by user programming. Designers can "integrate" a digital system on a PLD through their own programming, without having to hire a chip manufacturer to design and manufacture a dedicated integrated circuit chip. Moreover, nowadays, instead of manually fabricating integrated circuit chips, this programming is mostly done using "logic compiler" software. This is similar to the software compiler used when developing programs. Before compilation, the original code must also be written in a specific programming language, called a hardware description language (HDL). There is not just one HDL, but many, 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. The most commonly used ones are VHDL (Very-High-Speed ​​Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art will also understand that by simply programming the method flow in one of these hardware description languages ​​and then programming it into an integrated circuit, a hardware circuit that implements the logic method flow can be easily obtained.

[0138] The controller can be implemented in any suitable manner. For example, 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: ARC625D, Atmel AT91SAM, 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 implementing the controller in a purely computer-readable program code format, the controller can be implemented in the form of logic gates, switches, an application-specific integrated circuit, a programmable logic controller, and an embedded microcontroller by logically programming the method steps. Therefore, such a controller can be considered a hardware component, and the means for implementing various functions included therein can also be considered as structures within the hardware component. Or even, the means for implementing various functions can be considered as both a software module implementing the method and a structure within the hardware component.

[0139] The systems, devices, modules, or units described in the above embodiments may be implemented by computer chips or entities, or by products having certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smartphone, 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.

[0140] For the convenience of description, the above devices are described as being divided into various units according to their functions. Of course, when implementing this specification, the functions of each unit can be implemented in the same or multiple software and / or hardware.

[0141] Those skilled in the art will appreciate that the embodiments of this specification may be provided as methods, systems, or computer program products. Therefore, this specification may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Furthermore, this specification may 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.

[0142] This specification is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of this specification. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of processes and / or boxes 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 that can direct a computer or other programmable data processing device to work 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 The 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 operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 The steps for the function specified in one or more boxes.

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

[0146] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.

[0147] Computer-readable media includes permanent and non-permanent, removable and non-removable 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 technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic tape, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory computer-readable media (transitory media), such as modulated data signals and carrier waves.

[0148] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.

[0149] Those skilled in the art will appreciate that the embodiments of this specification may be provided as methods, systems, or computer program products. Thus, this specification may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, this specification may 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.

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

[0151] The various embodiments in this specification are described in a progressive manner. Similar parts between the various embodiments can be referred to in conjunction with each other. Each embodiment focuses on the differences between the other embodiments. In particular, the system embodiments are generally similar to the method embodiments, so the description is relatively simple. For relevant parts, refer to the description of the method embodiments.

[0152] The foregoing is merely an example of the present invention and is not intended to limit the present invention. Various modifications and variations are possible for those skilled in the art. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention are intended to be included within the scope of the claims of the present invention.

Claims

1. A code testing method, comprising: Get the code to be tested; Determining, based on the types of parameters required by the method called by the code to be tested and / or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, the actual return value type corresponding to the method called when the code to be tested is tested, where the method type of the method called when the code to be tested is tested includes at least one of a generic method and a forced type conversion; Obtaining a test result for the code to be tested according to the actual return value type; Among them, when the called method is a generic method, the actual return value type is determined based on a pre-saved association relationship and the types of parameters required by the called method; the association relationship is obtained by parsing the code of the called method; when the called method is a forced type conversion, the actual return value type is determined based on the code line number of the called method and a pre-saved correspondence relationship; the correspondence relationship is obtained by parsing the code of the called method; the bytecode corresponding to the code to be tested is analyzed to identify the method type as a forced type conversion method.

2. The method according to claim 1, determining the actual return value type corresponding to the method called when testing the code to be tested based on the type of parameters required by the method called by the code to be tested and / or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, specifically comprising: Get the code of the method called by the code to be tested; Determine, based on the obtained code of the method called by the code to be tested, the actual return value type of the method called when the code to be tested is tested, and its association with the parameter types required by the method called by the code to be tested; According to the association relationship and the types of parameters required by the method called by the code to be tested, the actual return value type corresponding to the method called when the code to be tested is tested is determined.

3. The method according to claim 2, obtaining a test result for the code to be tested according to the actual return value type, specifically comprising: According to the association relationship, a method data structure MethodDescripter structure is created, wherein the MethodDescripter structure includes: the actual return value type of the calling method, the number of times the MethodDescripter structure is called, the method name of the calling method, the class name of the calling method, and the input parameters of the calling method; Generate a mock method corresponding to the method called by the code to be tested according to the MethodDescripter structure and the actual return value type; The mock method is used to obtain the test result for the code to be tested.

4. The method according to claim 1, determining the actual return value type corresponding to the method called when testing the code to be tested based on the type of parameters required by the method called by the code to be tested and / or the analysis results obtained after analyzing the bytecode corresponding to the code to be tested, specifically comprising: By analyzing the bytecode corresponding to the code to be tested, determining, from the methods called by the code to be tested, a method whose method type is a forced type conversion, as the target method; When testing the code to be tested, the actual return value type corresponding to the method called by the code to be tested is determined according to the target method.

5. The method according to claim 4, wherein, when testing the code to be tested, before determining the actual return value type corresponding to the method called by the code to be tested according to the target method, the method further comprises: Save the corresponding relationship between the code line number corresponding to the target method in the code to be tested and the actual return value type of the return value of the target method after forced type conversion; When testing the code to be tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method specifically includes: During the testing process of the code to be tested, determining the code line number of the method called by the code to be tested as a method of forced type conversion as the target line number; According to the target line number, the actual return value type corresponding to the method called by the code to be tested is queried from the pre-saved corresponding relationship.

6. The method according to claim 5, obtaining a test result for the code to be tested according to the actual return value type, specifically comprising: Generate a mock method corresponding to the method called by the code to be tested according to the actual return value type corresponding to the method called by the code to be tested in the pre-saved corresponding relationship; The mock method is used to obtain the test result for the code to be tested.

7. A method for generating a test case, comprising: Get each test case; For each test case, the setting code is tested by the method described in any one of claims 1 to 6 to obtain a test result corresponding to the test case; Determine whether the preset test conditions are met based on the test results corresponding to each test case; If it is determined that the test condition is not met, each test case is regenerated, and the setting code is tested according to each regenerated test case until the test condition is met, thereby obtaining a test case generated when the test condition is met.

8. The method according to claim 7, wherein regenerating each test case comprises: Select at least some of the test cases from each test case and combine them in pairs to obtain test case combinations; For each test case combination, the same type of parameters in the two test cases included in the test case combination are weighted to obtain a regenerated test case.

9. The method according to claim 7, wherein regenerating each test case comprises: selecting at least some of the test cases from each of the test cases; For each selected test case, at least part of the parameters included in the test case are numerically adjusted, and / or the code to be tested included in the test case is adjusted to obtain a regenerated test case.

10. A code testing device, comprising: Get module, used to get the code to be tested; a determination module, configured to determine, based on the types of parameters required by the method called by the code to be tested and / or an analysis result obtained after analyzing the bytecode corresponding to the code to be tested, an actual return value type corresponding to the method called when the code to be tested is tested, wherein the method type of the method called when the code to be tested is tested includes at least one of a generic method and a forced type conversion; A testing module, configured to obtain a test result for the code to be tested according to the actual return value type; Among them, when the called method is a generic method, the actual return value type is determined based on a pre-saved association relationship and the types of parameters required by the called method; the association relationship is obtained by parsing the code of the called method; when the called method is a forced type conversion, the actual return value type is determined based on the code line number of the called method and a pre-saved correspondence relationship; the correspondence relationship is obtained by parsing the code of the called method; the bytecode corresponding to the code to be tested is analyzed to identify the method type as a forced type conversion method.

11. The device as described in claim 10, wherein the determination module is specifically used to obtain the code of the method called by the code to be tested; determine the actual return value of the method called when the code to be tested is tested, and the association relationship with the parameter type required by the method called by the code to be tested based on the obtained code of the method called by the code to be tested; and determine the actual return value type corresponding to the method called when the code to be tested is tested based on the association relationship and the type of the parameter required by the method called by the code to be tested.

12. The apparatus according to claim 11, wherein the test module is specifically configured to create a method data structure MethodDescripter structure according to the association relationship, wherein the MethodDescripter structure includes: The actual return value type of the called method, the number of times the MethodDescripter structure is called, the method name of the called method, the class name of the called method, and the incoming parameters of the called method; based on the MethodDescripter structure and the actual return value type, a mock method corresponding to the method called by the code to be tested is generated; and a test result for the code to be tested is obtained through the mock method.

13. The device as described in claim 10, wherein the determination module is specifically used to determine, by analyzing the bytecode corresponding to the code to be tested, a method whose method type is a forced type conversion from among the methods called by the code to be tested, as a target method; when testing the code to be tested, determine the actual return value type corresponding to the method called by the code to be tested based on the target method.

14. The apparatus of claim 13, further comprising: An analysis module is used to save the correspondence between the code line number corresponding to the target method in the code to be tested and the actual return value type of the return value of the target method after forced type conversion; The determination module is specifically used to determine, during the testing process of the code to be tested, the code line number of the method called by the code to be tested as the forced type conversion method, as the target line number; and according to the target line number, query the actual return value type corresponding to the method called by the code to be tested from a pre-saved correspondence relationship.

15. The device as described in claim 14, wherein the testing module is specifically used to generate a mock method corresponding to the method called by the code to be tested based on the actual return value type corresponding to the method called by the code to be tested queried from a pre-saved correspondence relationship; and obtain a test result for the code to be tested through the mock method.

16. A device for generating test cases, comprising: Use case acquisition module, used to obtain each test case; A use case testing module, configured to test the setting code for each test case by using the method described in any one of claims 1 to 6 above, and obtain a test result corresponding to the test case; The judgment module is used to determine whether the preset test conditions are met based on the test results corresponding to each test case; The generation module is used to regenerate each test case when it is determined that the test condition is not met, and test the setting code according to the regenerated test cases until the test condition is met, thereby obtaining the test case generated when the test condition is met.

17. The device as described in claim 16, wherein the generation module is specifically used to select at least part of the test cases from each test case and combine them in pairs to obtain each test case combination; for each test case combination, weight the same type of parameters in the two test cases contained in the test case combination to obtain a regenerated test case.

18. In the device as described in claim 16, the generation module is specifically used to select at least part of the test cases from each test case; for each selected test case, numerically adjust at least part of the parameters contained in the test case, and / or adjust the code to be tested contained in the test case to obtain a regenerated test case.

19. A computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the method according to any one of claims 1 to 9.

20. 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 method according to any one of claims 1 to 9 when executing the program.

Citation Information

Patent Citations

  • Method and device for automatic mock of external dependency

    CN105335281A

  • Test case generation method and system and computer system

    CN109992498A

  • Code testing method and device, equipment and storage medium

    CN113778849A