CN114840427A - Code testing and test case generating method and device - Google Patents

Code testing and test case generating method and device Download PDF

Info

Publication number
CN114840427A
CN114840427A CN202210508851.1A CN202210508851A CN114840427A CN 114840427 A CN114840427 A CN 114840427A CN 202210508851 A CN202210508851 A CN 202210508851A CN 114840427 A CN114840427 A CN 114840427A
Authority
CN
China
Prior art keywords
code
tested
test
return value
test case
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202210508851.1A
Other languages
Chinese (zh)
Inventor
张剑飞
周海莲
周智泉
赵红兵
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Alipay Hangzhou Information Technology Co Ltd
Original Assignee
Alipay Hangzhou Information Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Alipay Hangzhou Information Technology Co Ltd filed Critical Alipay Hangzhou Information Technology Co Ltd
Priority to CN202210508851.1A priority Critical patent/CN114840427A/en
Publication of CN114840427A publication Critical patent/CN114840427A/en
Priority to PCT/CN2023/092939 priority patent/WO2023217118A1/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3684Test management for test design, e.g. generating new test cases
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3676Test management for coverage analysis
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F11/00Error detection; Error correction; Monitoring
    • G06F11/36Preventing errors by testing or debugging software
    • G06F11/3668Software testing
    • G06F11/3672Test management
    • G06F11/3692Test management for test results analysis

Abstract

The specification discloses a method and a device for code testing and test case generation, which can accurately determine the actual return value types of a generic method and a method for generating forced type conversion, which are called in the running process of a code to be tested, when the code to be tested is tested, so that the code to be tested can be tested based on the determined actual return value types, and the accuracy of a test result is further improved.

Description

Code testing and test case generating method and device
Technical Field
The present disclosure relates to the field of computer technologies, and in particular, to a method and an apparatus for code testing and test case generation.
Background
In the process of software testing, because the method to be tested may have a higher dependency on other methods, a tester cannot test the method to be tested alone, and further the performance of the testing work is affected, therefore, to solve this problem, the tester of the software usually performs the testing work of the method to be tested by using a Mock service (i.e., relying on a service to replace another method associated with the method to be tested).
However, when the Mock service is used, the return value type of the method called by the method to be tested needs to be determined, for a common method, the return value type of the method can be directly determined, and for methods in which some return values need to be actually run to determine a specific type, a type is often randomly determined as the return value type of the method, so that the accuracy of the generated test case is reduced, and the coverage rate of the test case is influenced.
Therefore, how to effectively determine the return value type of the method to be tested during the software testing work, and further improve the accuracy of the test result of the method to be tested, is a problem to be solved urgently.
Disclosure of Invention
The present specification provides a method and an apparatus for code testing and test case generation, so as to partially solve the problem of low accuracy of test results caused by returning a return value of an error type in the prior art.
The technical scheme adopted by the specification is as follows:
the present specification provides a method of code testing, comprising:
acquiring a code to be tested;
determining an actual return value type corresponding to a method called when the code to be tested is tested according to the type of the parameter 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, wherein the method type of the method called when the code to be tested is tested comprises: at least one of a generic method and a forced type conversion;
and obtaining a test result aiming at the code to be tested according to the actual return value type.
Optionally, determining an actual return value type corresponding to the method called when the code to be tested is tested according to the type of the parameter 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, and specifically includes:
acquiring a code of a method called by the code to be tested;
determining an incidence relation between an actual return value of the method called when the code to be tested is tested and a parameter type required by the method called by the code to be tested according to the obtained code of the method called by the code to be tested;
and determining the actual return value type corresponding to the method called when the code to be tested is tested according to the incidence relation and the type of the parameter required by the method called by the code to be tested.
Optionally, obtaining a test result for the code to be tested according to the actual return value type, specifically including:
according to the incidence relation, a method data structure is created, and the method descriptor structure comprises the following steps: the method comprises the steps of calling an actual return value type of a method, the number of times of calling a MethodDescriptor structure, a method name of the calling method, a class name of the calling method and an incoming parameter of the calling method;
generating a mock method corresponding to the method called by the code to be tested according to the method Descriptor structure and the actual return value type;
and obtaining a test result aiming at the code to be tested by the mock method.
Optionally, determining an actual return value type corresponding to the method called when the code to be tested is tested according to the type of the parameter 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, and specifically includes:
determining a method type determined from methods called by the code to be tested as a method for forced type conversion as a target method by analyzing the byte code corresponding to the code to be tested;
and when the code to be tested is tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method.
Optionally, when the code to be tested is tested, before determining, according to the target method, an actual return value type corresponding to a method called by the code to be tested, the method further includes:
storing the corresponding relation between the code line number of the target method corresponding to the code to be tested and the actual return value type of the return value of the target method after forced type conversion;
when the code to be tested is tested, determining an actual return value type corresponding to a method called by the code to be tested according to the target method, specifically comprising:
if the code line number of the method called by the code to be tested is determined to be a code line number of a method for forced type conversion in the test process of the code to be tested, the code line number is used as a target line number;
and inquiring the actual return value type corresponding to the method called by the code to be tested from the pre-stored corresponding relation according to the target line number.
Optionally, obtaining a test result for the code to be tested according to the actual return value type includes:
generating 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, which is inquired from the pre-stored corresponding relation;
and obtaining a test result aiming at the code to be tested by the mock method.
The present specification provides a method for generating a test case, including:
obtaining each test case;
testing the set code by the code testing method aiming at each test case to obtain a test result corresponding to the test case;
determining whether a preset test condition is met or not according to a test result corresponding to each test case;
and if the test condition is determined not to be met, regenerating each test case, testing the set code according to each regenerated test case until the test condition is met, and obtaining the test case generated when the test condition is met.
Optionally, regenerating each test case specifically includes:
selecting at least part of test cases from each test case to be combined in pairs to obtain each test case combination;
and weighting the same type parameters in the two test cases contained in each test case combination to obtain the regenerated test case.
Optionally, regenerating each test case specifically includes:
selecting at least part of test cases from the test cases;
and aiming at each selected test case, performing numerical value adjustment on at least part of parameters contained in the test case, and/or adjusting a code to be tested contained in the test case to obtain a regenerated test case.
This specification provides an apparatus for code testing, comprising:
the acquisition module is used for acquiring a code to be tested;
a determining module, configured to determine, according to a type of a parameter required by the method called by the code to be tested and/or an analysis result obtained after analyzing a 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, 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;
and the testing module is used for obtaining a testing result aiming at the code to be tested according to the actual return value type.
Optionally, the determining module is specifically configured to obtain a code of a method called by the code to be tested; determining an incidence relation between an actual return value of the method called when the code to be tested is tested and a parameter type required by the method called by the code to be tested according to the obtained code of the method called by the code to be tested; and determining the actual return value type corresponding to the method called when the code to be tested is tested according to the incidence relation and the type of the parameter required by the method called by the code to be tested.
Optionally, the test module is specifically configured to create a method data structure according to the association relationship, where the method data descriptor structure includes: the method comprises the steps of calling an actual return value type of a method, the number of times of calling a MethodDescriptor structure, a method name of the calling method, a class name of the calling method and an incoming parameter of the calling method; generating a mock method corresponding to the method called by the code to be tested according to the method Descriptor structure and the actual return value type; and obtaining a test result aiming at the code to be tested by the mock method.
Optionally, the determining module is specifically configured to determine, through analysis of the bytecode corresponding to the code to be tested, that the method type determined from the methods called by the code to be tested is a method for forced type conversion, and use the method as a target method; and when the code to be tested is tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method.
Optionally, the apparatus further comprises:
the analysis module is used for storing the corresponding relation 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 the forced type conversion;
the determining module is specifically configured to determine, if the method called by the code to be tested is a code line number of a method for forced type conversion in the test process of the code to be tested, as a target line number; and inquiring the actual return value type corresponding to the method called by the code to be tested from the pre-stored corresponding relation according to the target line number.
Optionally, the test module is specifically configured to, according to an actual return value type corresponding to the method called by the code to be tested, which is queried from a pre-stored correspondence relationship, generate a mock method corresponding to the method called by the code to be tested; and obtaining a test result aiming at the code to be tested by the mock method.
This specification provides a device for test case generation, including:
the case acquisition module is used for acquiring each test case;
the case testing module is used for testing the set code by the code testing method aiming at each test case to obtain a testing result corresponding to the test case;
the judging module is used for determining whether preset test conditions are met or not according to the test result corresponding to each test case;
and the generating module is used for regenerating each test case when the test condition is determined not to be met, testing the set code according to each regenerated test case until the test condition is met, and obtaining the test case generated when the test condition is met.
Optionally, the generating module is specifically configured to select at least a part of the test cases from the test cases to perform pairwise combination, so as to obtain each test case combination; and weighting the same type parameters in the two test cases contained in each test case combination to obtain the regenerated test case.
Optionally, the generating module is specifically configured to select at least part of the test cases from the test cases; and aiming at each selected test case, performing numerical value adjustment on at least part of parameters contained in the test case, and/or adjusting a code to be tested contained in the test case to obtain a regenerated test case.
The present specification provides a computer-readable storage medium storing a computer program which, when executed by a processor, implements the above-described methods of code testing, test case generation.
The present specification provides an electronic device, which includes a memory, a processor, and a computer program stored in the memory and executable on the processor, and when the processor executes the program, the method for code testing and test case generation described above is implemented.
The technical scheme adopted by the specification can achieve the following beneficial effects:
in the code testing method provided in this specification, a code to be tested is first obtained, and then an actual return value type corresponding to a method called when the code to be tested is determined according to a type of a parameter required by the method called by the code to be tested and/or an analysis result obtained by analyzing a 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: and calling at least one of a generic method and forced type conversion, and obtaining a test result aiming at the code to be tested according to the actual return value type.
According to the method, the actual return value types of the generic method and the method for generating the forced type conversion, which are called in the running process of the code to be tested, can be accurately determined when the code to be tested is tested, so that the code to be tested can be tested based on the determined actual return value types, and the accuracy of the test result is improved.
Drawings
The accompanying drawings, which are included to provide a further understanding of the specification and are incorporated in and constitute a part of this specification, illustrate embodiments of the specification and together with the description serve to explain the principles of the specification and not to limit the specification in a limiting sense. In the drawings:
FIG. 1 is a flow chart illustrating a method of code testing provided herein;
FIG. 2 is a schematic diagram of a method for determining an actual return value type for a generic method provided in this specification;
FIG. 3 is a flowchart illustrating a method for test case generation provided in the present specification;
FIG. 4 is a schematic diagram of an apparatus for code testing provided herein;
FIG. 5 is a schematic diagram of an apparatus for test case generation provided in the present specification;
fig. 6 is a schematic diagram of an electronic device corresponding to fig. 1 provided in the present specification.
Detailed Description
In order to make the objects, technical solutions and advantages of the present disclosure more clear, the technical solutions of the present disclosure will be clearly and completely described below with reference to the specific embodiments of the present disclosure and the accompanying drawings. It is to be understood that the embodiments described are only a few embodiments of the present disclosure, and not all embodiments. All other embodiments obtained by a person of ordinary skill in the art based on the embodiments in the present specification without any creative effort belong to the protection scope of the present specification.
The technical solutions provided by the embodiments of the present description are described in detail below with reference to the accompanying drawings.
Fig. 1 is a schematic flow chart of a method for testing code provided in this specification, including the following steps:
s101: and acquiring a code to be tested.
In this specification, a tester may test a code of software to be tested based on a test requirement, and specifically, the tester may determine a part of the code from the code of the software to be tested, and use the part of the code as a code to be tested, and then may test the code to be tested, so as to obtain a test result for the code to be tested.
The code to be tested may refer to a set of codes that are part of the code of the software to be tested, for example: the code to be tested may be a code corresponding to one or more classes to be tested (where a class refers to a self-defined reference data type, and several attributes and methods are packaged in the class) included in the code of the software to be tested, and for example: the code to be tested may also be the code corresponding to one or more methods contained in the code of the software to be tested (i.e., a collection of code statements that together perform a function, the methods being contained in a class or object of a class).
In this specification, the execution subject of the method for implementing the code test may refer to a specific device such as a terminal device installed on the service platform, or may refer to a terminal device such as a desktop computer or a notebook computer.
Therefore, after receiving the test instruction sent by the tester, the terminal device can obtain the code to be tested first, and then test the code to be tested to obtain the test result of the code to be tested.
S102: determining an actual return value type corresponding to a method called when the code to be tested is tested according to the type of the parameter 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, wherein the method type of the method called when the code to be tested is tested comprises: a generic method and a forced type conversion.
When the code to be tested runs, the terminal equipment can intercept a calling instruction of the method called by the code to be tested in the running process, and determine the method type of the method called by the code to be tested in the running process according to the intercepted calling instruction.
The method type of the method called by the code to be tested in the running process can be at least one of a generic method (namely, a method with uncertain type of the incoming parameter of the method) and a forced type conversion (namely, a method with the forced type conversion in the calling process, wherein the forced type conversion is the type of the original return value of the method and is converted into another data type).
It should be noted that the interception in the above contents is not literal interception, but is a method similar to the interception effect implemented by monitoring a call instruction of a method called by a code to be tested in the running process of the code to be tested and by a dynamic proxy manner.
Based on the introduction of the above method types, the present specification mainly determines the actual return value types of the above two method types, i.e., the generic method and the mandatory type conversion, and therefore, the following will describe in detail how to determine the actual return value types of the two method types.
For the generic method, the terminal device may obtain a code of the generic method called in the code to be tested after intercepting a call instruction of the generic method in the code to be tested, so as to determine an actual return value type of the method called in the test of the code to be tested according to the obtained code of the generic method called by the code to be tested, and an association relationship between the actual return value type and a parameter type required by the method called by the code to be tested, and further determine an actual return value type corresponding to the generic method called in the test of the code to be tested based on the determined association relationship, as shown in fig. 2.
FIG. 2 is a schematic diagram of a method for determining an actual return value type of a generic method provided in this specification.
As can be seen from fig. 2, the terminal device may monitor, in the running process of the code to be tested, a call instruction of the code to be tested calling the method in the running process, and may further obtain, before the called method is executed, a code of the generic method called in the code to be tested, so as to perform generic analysis on the obtained code of the generic method called by the code to be tested, and determine an association relationship between various actual return value types of the generic method called when the code to be tested is tested and various parameter types required by the generic method called by the code to be tested (i.e., parameter types required to be introduced when the code to be tested calls the method).
Further, the terminal device may create a method descriptor structure for each method data structure (for each incoming parameter type of the generic method, a combination of the incoming parameter type and an actual return value type corresponding to the incoming parameter type corresponds to one method descriptor structure) according to the determined association relationship, may further determine an actual incoming parameter when the code to be tested calls the generic method according to the intercepted call instruction, and may query, according to the incoming parameter, the method descriptor structure matching the incoming parameter in each method descriptor structure, and may further determine the actual return value type of the generic method called when the code to be tested is tested.
The method Descriptor structure comprises the following steps: the method comprises the steps of calling the actual return value type of the method, the number of times of calling the method Descriptor structure, the method name of the calling method, the class name of the calling method, the incoming parameter of the calling method and the unique identification ID of the method Descriptor structure.
Further, the terminal device may generate a mock method corresponding to the method called by the code to be tested based on the method descriptor structure matched with the incoming parameter, and may further generate a return value of the method called by the code to be tested through the mock method in a simulation manner, so as to obtain a test result for the code to be tested.
It should be noted that, since the same generic method may be called in the code to be tested for multiple times, when generating the mock method corresponding to the generic method, the mock statements need to be assembled according to the called times of the method descriptor structure and according to the sequence of calling the method descriptor structure, so as to generate the mock method.
For the method of generating the forced type conversion, before testing the code to be tested, the terminal device may determine, in advance, through analysis of the bytecode corresponding to the code to be tested, that the method type determined from the methods called by the code to be tested is the forced type conversion method, and use the method as the target method, and store a correspondence between a code line number corresponding to the target method in the code to be tested and an actual return value type of the return value of the target method after the forced type conversion (considering the characteristic that data stored in the hash table is easy to read, the correspondence here may be stored in the hash table).
After the storage of the corresponding relation is completed, the terminal device can determine the actual return value type based on the corresponding relation in the code testing process. Specifically, after intercepting a call instruction of a method for forced type conversion called in a code to be tested, method information of the called method may be obtained, where the method information includes: the method comprises the following steps of calling code of the method and context information of the calling method, wherein the context information of the calling method comprises the following steps: the variable objects defined by the calling method, the scope chain of the calling method (i.e. the sequence chain table for searching variables in the calling method), the calling code of the caller (i.e. the code for calling the generic method or the method for generating the forced conversion in the code to be tested), etc.
Further, the terminal device may determine, according to the obtained method information of the calling method, a code line number of the method called by the code to be tested and having the forced type conversion, as a target line number, and further query, according to the target line number, an actual return value type corresponding to the method called by the code to be tested and having the forced type conversion from a correspondence between a pre-stored code line number corresponding to the method having the forced type conversion in the code to be tested and an actual return value type after the return value of the method having the forced type conversion is subjected to the forced type conversion.
In practical application, when bytecode analysis is performed on a code to be tested, all methods which are included in the code to be tested and are subjected to forced conversion may not be identified, so that in an actual code testing process, an actual return value type corresponding to a method which is called by the code to be tested and is subjected to forced type conversion may not be queried from a pre-stored corresponding relationship according to a code line number of the method which is subjected to forced conversion in the code to be tested.
For such a situation, in this specification, the terminal device may arbitrarily select one data type from a preset type pool as a candidate return value type, and may further use the candidate return value type as an actual return value type of a method for generating forced type conversion, which is called when a code to be tested is tested, and obtain a test result based on the candidate return value type, and then may determine, according to the test result, whether the candidate return value type is the actual return value type of the method for generating forced type conversion, which is called when the code to be tested is tested; if not, selecting a return value type from the type pool again to serve as a candidate return value type until the candidate return value type is determined to be the actual return value type of the method which is called to generate the forced type conversion when the code to be tested is tested.
The data type in the type pool in the above content may be predetermined by the terminal device before the code to be tested runs. Specifically, the terminal device may determine all data types included in the code to be tested by analyzing the bytecode corresponding to the code to be tested, and then may store the determined data types included in the code to be tested in a preset type pool.
From the above, it can be seen that the terminal device can accurately determine the actual return value types of the generic method and the method for generating the forced type conversion, which are called in the running process of the code to be tested, when the code to be tested is tested, so that the code to be tested can be tested based on the determined actual return value types, and the accuracy of the test result is further improved.
In practical applications, the code testing method may be used not only for testing a code to be tested, but also for generating test cases, that is, some preferred test cases capable of achieving multiple testing targets (the target may be that various coverage rates of the code to be tested reach a preset threshold value) are constructed by the code testing method, and the method for generating the test cases is described in detail below, as shown in fig. 3.
Fig. 3 is a flowchart of a method for generating a test case provided in this specification, and includes the following steps:
s301: and obtaining each test case.
In this specification, the terminal device may obtain code association information of the code to be tested before testing the code to be tested, and then generate each test case according to the code to be tested and the association information of the code to be tested, where the code association information may be, for example: document information of the code to be tested, context information of the code to be tested, and the like.
S302: and aiming at each test case, testing the code to be tested by the code testing method to obtain a test result corresponding to the test case.
For each test case, the terminal device may run the test case by the code testing method described above to test the set code included in the test case to obtain a test result corresponding to the test case, where the set code may refer to a code to be tested, or may refer to any one section of code (for example, a code that has been tested may also be used as the set code, and further, by using a means for testing the set code, some preferred test cases are finally obtained), where the test result refers to a test effect of the code to be tested, such as: line coverage of the code to be tested, method coverage of the code to be tested, abnormal coverage of the code to be tested, and the like.
The initial test cases generated by the terminal device usually cannot well achieve a plurality of preset test targets, and therefore, iteration needs to be performed based on the initial test cases to optimize each initial test case and finally achieve the test target of the code to be tested, wherein a specific process of performing iteration on each test case is achieved by the following steps.
S303: and determining whether the preset test conditions are met or not according to the test result corresponding to each test case.
S304: and if the test condition is determined not to be met, regenerating each test case, testing the code to be tested according to each regenerated test case until the test condition is met, and obtaining the test case generated when the test condition is met.
And the terminal equipment determines whether the preset test conditions are met or not according to the test result of each test case, regenerates each test case if the test conditions are not met, tests the code to be tested according to the regenerated test cases until the test conditions are met, and obtains the test case generated when the test conditions are met.
Wherein, preset test conditions can be formulated according to the test requirements, for example: and (3) when the line coverage rate of the code to be tested reaches a preset threshold value, for example: the number of iterations reaches a preset threshold, and the like.
In the above, the method for regenerating each test case may be to sort each test case according to the test result of each test case (for example, sort according to the row coverage of the code to be tested), and to screen each basic test case according to the sorted test cases.
Further, the terminal device may select at least part of the basic test cases from the basic test cases to perform pairwise combination to obtain each test case combination, and perform weighting according to the same type of parameters included in the two basic test cases in the test case combination to obtain a regenerated test case for each test case combination.
In addition, the terminal device may select at least part of the basic test cases from the basic test cases, and for each selected basic test case, perform numerical adjustment on parameters included in the basic test case, and/or perform adjustment on a code to be tested included in the basic test case, so as to regenerate each test case.
In addition, since the generic method called for the code to be tested generally has a plurality of types of parameters that can be introduced during the running process of the test case, the terminal device may further include, according to the following in the method descriptor structure generated during the running process of the test case: the method comprises the steps of calling an actual return value type of a method, the number of times that a method Descriptor structure is called, the method name of the calling method, the class name of the calling method and the type of incoming parameters of the calling method, and regenerating test cases so as to test the incoming parameter types and the actual return value types of the generic method called by a code to be tested.
It should be noted that the three methods described above may be used separately to regenerate each test case, or may be used together to regenerate the test cases.
According to the above, the terminal device can generate the initial test cases for the set codes, and optimize each initial test case in an iterative manner according to the test result of each initial test case in the process of testing the set codes through each initial test case, so that the finally generated test cases can comprehensively test the set codes, and the test effect is improved.
Based on the same idea, the code testing method provided in one or more embodiments of the present specification further provides a corresponding code testing apparatus, and a test case generating apparatus as shown in fig. 4 and 5.
Fig. 4 is a schematic diagram of a code testing apparatus provided in the present specification, including:
an obtaining module 401, configured to obtain a code to be tested;
a determining module 402, configured to determine, according to a type of a parameter required by the method called by the code to be tested and/or an analysis result obtained after analyzing a 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, 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;
the testing module 403 is configured to obtain a testing result for the code to be tested according to the actual return value type.
Optionally, the determining module 402 is specifically configured to obtain a code of a method called by the code to be tested; determining an incidence relation between an actual return value of the method called when the code to be tested is tested and a parameter type required by the method called by the code to be tested according to the obtained code of the method called by the code to be tested; and determining the actual return value type corresponding to the method called when the code to be tested is tested according to the incidence relation and the type of the parameter required by the method called by the code to be tested.
Optionally, the test module 403 is specifically configured to create a method data structure according to the association relationship, where the method data descriptor structure includes: the method comprises the steps of calling an actual return value type of a method, the number of times of calling a MethodDescriptor structure, a method name of the calling method, a class name of the calling method and an incoming parameter of the calling method; generating a mock method corresponding to the method called by the code to be tested according to the method Descriptor structure and the actual return value type; and obtaining a test result aiming at the code to be tested by the mock method.
Optionally, the determining module 402 is specifically configured to determine, through analysis of the bytecode corresponding to the code to be tested, that the method type determined from the methods called by the code to be tested is a method for forced type conversion, and use the method as a target method; and when the code to be tested is tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method.
Optionally, the apparatus further comprises:
an analysis module 404, configured to store a corresponding relationship between a code line number corresponding to the target method in the code to be tested and an actual return value type of the return value of the target method after forced type conversion;
the determining module 402 is specifically configured to determine, as a target line number, a code line number of a method for forced type conversion, where the method called by the code to be tested is used in a test process of the code to be tested;
and inquiring the actual return value type corresponding to the method called by the code to be tested from the pre-stored corresponding relation according to the target line number.
Optionally, the testing module 403 is specifically configured to, according to an actual return value type corresponding to the method called by the code to be tested, which is queried from a pre-stored correspondence relationship, generate a mock method corresponding to the method called by the code to be tested; and obtaining a test result aiming at the code to be tested by the mock method.
Fig. 5 is a schematic diagram of an apparatus for generating test cases provided in this specification, including:
a case acquiring module 501, configured to acquire each test case;
a case testing module 502, configured to test the set code by using the code testing method for each test case to obtain a testing result corresponding to the test case;
the judging module 503 is configured to determine whether a preset test condition is met according to a test result corresponding to each test case;
the generating module 504 is configured to regenerate each test case when it is determined that the test condition is not satisfied, and test the set code according to each regenerated test case until the test condition is satisfied, so as to obtain the test case generated when the test condition is satisfied.
Optionally, the generating module 504 is specifically configured to select at least a part of the test cases from the test cases to perform pairwise combination, so as to obtain each test case combination; and aiming at each test case combination, weighting the parameters of the same type in the two test cases contained in the test case combination to obtain the regenerated test case.
Optionally, the generating module 504 is specifically configured to select at least part of the test cases from the test cases; and aiming at each selected test case, performing numerical adjustment on at least part of parameters contained in the test case, and/or performing adjustment on a code to be tested contained in the test case to obtain a regenerated test case.
The present specification also provides a computer-readable storage medium, which stores a computer program, and the computer program can be used to execute a method for code testing and test case generation provided in fig. 1.
This specification also provides a schematic block diagram of an electronic device corresponding to that of figure 1, shown in figure 6. As shown in fig. 6, at the hardware level, the electronic device includes a processor, an internal bus, a network interface, a memory, and a non-volatile memory, but may also include hardware required for other services. The processor reads a corresponding computer program from the nonvolatile memory into the memory and then runs the computer program to implement the method for code testing and test case generation described in fig. 1. Of course, besides the software implementation, the present specification does not exclude other implementations, such as logic devices or a combination of software and hardware, and the like, that is, the execution subject of the following processing flow is not limited to each logic unit, and may be hardware or logic devices.
In the 90 s of the 20 th century, improvements in a technology could clearly distinguish between improvements in hardware (e.g., improvements in circuit structures such as diodes, transistors, switches, etc.) and improvements in software (improvements in process flow). However, as technology advances, many of today's process flow improvements have been seen as direct improvements in hardware circuit architecture. Designers almost always obtain the corresponding hardware circuit structure by programming an improved method flow into the hardware circuit. Thus, it cannot be said that an improvement in the process flow cannot be realized by hardware physical modules. For example, a Programmable Logic Device (PLD), such as a Field Programmable Gate Array (FPGA), is an integrated circuit whose Logic functions are determined by programming the Device by a user. A digital system is "integrated" on a PLD by the designer's own programming without requiring the chip manufacturer to design and fabricate application-specific integrated circuit chips. Furthermore, nowadays, instead of manually making an Integrated Circuit chip, such Programming is often implemented by "logic compiler" software, which is similar to a software compiler used in program development and writing, but the original code before compiling is also written by a specific Programming Language, which is called Hardware Description Language (HDL), and HDL is not only one but many, such as abel (advanced Boolean Expression Language), ahdl (alternate Hardware Description Language), traffic, pl (core universal Programming Language), HDCal (jhdware Description Language), lang, Lola, HDL, laspam, hardward Description Language (vhr Description Language), vhal (Hardware Description Language), and vhigh-Language, which are currently used in most common. It will also be apparent to those skilled in the art that hardware circuitry that implements the logical method flows can be readily obtained by merely slightly programming the method flows into an integrated circuit using the hardware description languages described above.
The controller may be implemented in any suitable manner, for example, the controller may take the form of, for example, 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 which include, but are not limited to, the following microcontrollers: the ARC625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320, the memory controller may also be implemented as part of the control logic for the memory. Those skilled in the art will also appreciate that, in addition to implementing the controller as pure computer readable program code, the same functionality can be implemented by logically programming method steps such that the controller is in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers and the like. Such a controller may thus be considered a hardware component, and the means included therein for performing the various functions may also be considered as a structure within the hardware component. Or even means for performing the functions may be regarded as being both a software module for performing the method and a structure within a hardware component.
The systems, devices, modules or units illustrated in the above embodiments may be implemented by a computer chip or an entity, or by a product with certain functions. One typical implementation device is a computer. In particular, the computer may be, for example, a personal computer, a laptop computer, a cellular telephone, 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.
For convenience of description, the above devices are described as being divided into various units by function, and are described separately. Of course, the functions of the various elements may be implemented in the same one or more software and/or hardware implementations of the present description.
As will be appreciated by one skilled in the art, embodiments of the present description may be provided as a method, system, or computer program product. Accordingly, the description may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and so forth) having computer-usable program code embodied therein.
The description has been presented with reference to flowchart illustrations and/or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the description. It will be understood that each flow and/or block of the flow diagrams and/or block diagrams, and combinations of flows and/or blocks in the flow diagrams and/or block diagrams, can be implemented by computer program instructions. These computer program instructions may be provided to a processor of a general purpose computer, special purpose computer, embedded processor, 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 flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to function in a particular manner, such that the instructions stored in the computer-readable memory produce an article of manufacture including instruction means which implement the function specified in the flowchart flow or flows and/or block diagram block or blocks.
These computer program instructions may 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 flowchart flow or flows and/or block diagram block or blocks.
In a typical configuration, a computing device includes one or more processors (CPUs), input/output interfaces, network interfaces, and memory.
The memory may include forms of volatile memory in a computer readable medium, Random Access Memory (RAM) and/or non-volatile memory, such as Read Only Memory (ROM) or flash memory (flash RAM). Memory is an example of a computer-readable medium.
Computer-readable media, including both non-transitory and non-transitory, removable and non-removable media, may implement information storage by any method or technology. The information may be computer readable instructions, data structures, modules of a program, 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 Discs (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information that can be accessed by a computing device. As defined herein, a computer readable medium does not include a transitory computer readable medium such as a modulated data signal and a carrier wave.
It should also be noted that the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising an … …" does not exclude the presence of other like elements in a process, method, article, or apparatus that comprises the element.
As will be appreciated by one skilled in the art, embodiments of the present description may be provided as a method, system, or computer program product. Accordingly, the description may take the form of an entirely hardware embodiment, an entirely software embodiment or an embodiment combining software and hardware aspects. Furthermore, the description may take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, and the like) having computer-usable program code embodied therein.
This description may be described in the general context of computer-executable instructions, such as program modules, being executed by a computer. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform particular tasks or implement particular abstract data types. The specification may also be practiced in distributed computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed computing environment, program modules may be located in both local and remote computer storage media including memory storage devices.
The embodiments in the present specification are described in a progressive manner, and the same and similar parts among the embodiments are referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, for the system embodiment, since it is substantially similar to the method embodiment, the description is simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only an example of the present specification, and is not intended to limit the present specification. Various modifications and alterations to this description will become apparent to those skilled in the art. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present specification should be included in the scope of the claims of the present specification.

Claims (20)

1. A method of code testing, comprising:
acquiring a code to be tested;
determining an actual return value type corresponding to a method called when the code to be tested is tested according to the type of the parameter 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, wherein the method type of the method called when the code to be tested is tested comprises: at least one of a generic method and a forced type conversion;
and obtaining a test result aiming at the code to be tested according to the actual return value type.
2. The method of claim 1, wherein the determining an actual return value type corresponding to the method called when the code to be tested is tested according to a type of a parameter required by the method called by the code to be tested and/or an analysis result obtained by analyzing a bytecode corresponding to the code to be tested specifically includes:
acquiring a code of a method called by the code to be tested;
determining an actual return value type of the method called when the code to be tested is tested and an incidence relation between the actual return value type and a parameter type required by the method called by the code to be tested according to the obtained code of the method called by the code to be tested;
and determining the actual return value type corresponding to the method called when the code to be tested is tested according to the incidence relation and the type of the parameter required by the method called by the code to be tested.
3. The method of claim 2, wherein obtaining the test result for the code to be tested according to the actual return value type specifically comprises:
according to the incidence relation, a method data structure is created, and the method descriptor structure comprises the following steps: the method comprises the steps of calling an actual return value type of a method, the number of times of calling a MethodDescriptor structure, a method name of the calling method, a class name of the calling method and an incoming parameter of the calling method;
generating a mock method corresponding to the method called by the code to be tested according to the method Descriptor structure and the actual return value type;
and obtaining a test result aiming at the code to be tested by the mock method.
4. The method of claim 1, wherein the determining an actual return value type corresponding to the method called when the code to be tested is tested according to a type of a parameter required by the method called by the code to be tested and/or an analysis result obtained by analyzing a bytecode corresponding to the code to be tested specifically includes:
determining a method type determined from methods called by the code to be tested as a method for forced type conversion as a target method by analyzing the byte code corresponding to the code to be tested;
and when the code to be tested is tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method.
5. The method of claim 4, when testing the code to be tested, before determining an actual return value type corresponding to a method called by the code to be tested according to the target method, the method further comprises:
storing the corresponding relation between the code line number of the target method corresponding to the code to be tested and the actual return value type of the return value of the target method after forced type conversion;
when the code to be tested is tested, determining an actual return value type corresponding to a method called by the code to be tested according to the target method, specifically comprising:
in the test process of the code to be tested, determining a code line number of a method called by the code to be tested as a method for forced type conversion as a target line number;
and inquiring the actual return value type corresponding to the method called by the code to be tested from the pre-stored corresponding relation according to the target line number.
6. The method of claim 5, obtaining a test result for the code to be tested according to the actual return value type, specifically comprising:
generating 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, which is inquired from the pre-stored corresponding relation;
and obtaining a test result aiming at the code to be tested by the mock method.
7. A method of test case generation, comprising:
obtaining each test case;
for each test case, testing the set code by the method of any one of claims 1 to 6 to obtain a test result corresponding to the test case;
determining whether a preset test condition is met or not according to a test result corresponding to each test case;
and if the test condition is determined not to be met, regenerating each test case, testing the set code according to each regenerated test case until the test condition is met, and obtaining the test case generated when the test condition is met.
8. The method according to claim 7, wherein regenerating each test case specifically comprises:
selecting at least part of test cases from each test case to be combined in pairs to obtain each test case combination;
and weighting the same type parameters in the two test cases contained in each test case combination to obtain the regenerated test case.
9. The method according to claim 7, wherein regenerating each test case specifically comprises:
selecting at least part of test cases from the test cases;
and aiming at each selected test case, performing numerical value adjustment on at least part of parameters contained in the test case, and/or adjusting a code to be tested contained in the test case to obtain a regenerated test case.
10. An apparatus for code testing, comprising:
the acquisition module is used for acquiring a code to be tested;
a determining module, configured to determine, according to a type of a parameter required by the method called by the code to be tested and/or an analysis result obtained after analyzing a 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, 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;
and the testing module is used for obtaining a testing result aiming at the code to be tested according to the actual return value type.
11. The apparatus of claim 10, wherein the determining module is specifically configured to obtain a code of a method called by the code to be tested; determining an incidence relation between an actual return value of the method called when the code to be tested is tested and a parameter type required by the method called by the code to be tested according to the obtained code of the method called by the code to be tested; and determining the actual return value type corresponding to the method called when the code to be tested is tested according to the incidence relation and the type of the parameter required by the method called by the code to be tested.
12. The apparatus of claim 11, wherein the test module is specifically configured to create a method data structure according to the association relationship, and the method descriptor structure includes: the method comprises the steps of calling an actual return value type of a method, the number of times of calling a MethodDescriptor structure, a method name of the calling method, a class name of the calling method and an incoming parameter of the calling method; generating a mock method corresponding to the method called by the code to be tested according to the method Descriptor structure and the actual return value type; and obtaining a test result aiming at the code to be tested by the mock method.
13. The apparatus according to claim 10, wherein the determining module is specifically configured to determine, as the target method, a method whose method type is determined to be a method of forced type conversion from among methods called by the code to be tested, through analysis of a bytecode corresponding to the code to be tested; and when the code to be tested is tested, determining the actual return value type corresponding to the method called by the code to be tested according to the target method.
14. The apparatus of claim 13, the apparatus further comprising:
the analysis module is used for storing the corresponding relation 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 the forced type conversion;
the determining module is specifically configured to determine, in a test process of the code to be tested, a code line number of a method called by the code to be tested as a method for forced type conversion, as a target line number; and inquiring the actual return value type corresponding to the method called by the code to be tested from the pre-stored corresponding relation according to the target line number.
15. The apparatus according to claim 14, wherein the testing module is specifically configured to, according to an actual return value type corresponding to the method called by the code to be tested, which is found out from a pre-stored correspondence, generate a mock method corresponding to the method called by the code to be tested; and obtaining a test result aiming at the code to be tested by the mock method.
16. An apparatus for test case generation, comprising:
the case acquisition module is used for acquiring each test case;
a case testing module, configured to test the set code by using the method according to any one of claims 1 to 6 for each test case, so as to obtain a test result corresponding to the test case;
the judging module is used for determining whether the preset test conditions are met or not according to the test result corresponding to each test case;
and the generating module is used for regenerating each test case when the test condition is determined not to be met, testing the set code according to each regenerated test case until the test condition is met, and obtaining the test case generated when the test condition is met.
17. The apparatus according to claim 16, wherein the generating module is specifically configured to select at least some test cases from the test cases to combine two by two, so as to obtain each test case combination; and weighting the same type parameters in the two test cases contained in each test case combination to obtain the regenerated test case.
18. The apparatus according to claim 16, wherein the generating module is specifically configured to select at least a part of the test cases from the test cases; and aiming at each selected test case, performing numerical value adjustment on at least part of parameters contained in the test case, and/or adjusting a 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 which, when executed by a processor, implements the method of any of claims 1-9.
20. An electronic device comprising a memory, a processor and a computer program stored on the memory and executable on the processor, the processor implementing the method of any of claims 1 to 9 when executing the program.
CN202210508851.1A 2022-05-10 2022-05-10 Code testing and test case generating method and device Pending CN114840427A (en)

Priority Applications (2)

Application Number Priority Date Filing Date Title
CN202210508851.1A CN114840427A (en) 2022-05-10 2022-05-10 Code testing and test case generating method and device
PCT/CN2023/092939 WO2023217118A1 (en) 2022-05-10 2023-05-09 Code test method and apparatus, and test case generation method and apparatus

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202210508851.1A CN114840427A (en) 2022-05-10 2022-05-10 Code testing and test case generating method and device

Publications (1)

Publication Number Publication Date
CN114840427A true CN114840427A (en) 2022-08-02

Family

ID=82569467

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202210508851.1A Pending CN114840427A (en) 2022-05-10 2022-05-10 Code testing and test case generating method and device

Country Status (2)

Country Link
CN (1) CN114840427A (en)
WO (1) WO2023217118A1 (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023217118A1 (en) * 2022-05-10 2023-11-16 支付宝(杭州)信息技术有限公司 Code test method and apparatus, and test case generation method and apparatus

Family Cites Families (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107133174A (en) * 2017-05-04 2017-09-05 浙江路港互通信息技术有限公司 Test case code automatically generating device and method
US11481311B2 (en) * 2020-06-10 2022-10-25 Sap Se Automatic evaluation of test code quality
CN113778849A (en) * 2021-03-15 2021-12-10 北京沃东天骏信息技术有限公司 Method, apparatus, device and storage medium for testing code
CN114840427A (en) * 2022-05-10 2022-08-02 支付宝(杭州)信息技术有限公司 Code testing and test case generating method and device

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023217118A1 (en) * 2022-05-10 2023-11-16 支付宝(杭州)信息技术有限公司 Code test method and apparatus, and test case generation method and apparatus

Also Published As

Publication number Publication date
WO2023217118A1 (en) 2023-11-16

Similar Documents

Publication Publication Date Title
US10296447B2 (en) Automated software program repair
CN109032825B (en) Fault injection method, device and equipment
US10394694B2 (en) Unexplored branch search in hybrid fuzz testing of software binaries
CN110635962B (en) Abnormity analysis method and device for distributed system
CN114547024A (en) SQL statement risk detection method, device, equipment and medium
CN111124870A (en) Interface testing method and device
CN111897711A (en) Method and device for positioning bug in code, electronic equipment and readable storage medium
CN108304313B (en) Device, client and method for data testing
CN111639011A (en) Data monitoring method, device and equipment
WO2023217118A1 (en) Code test method and apparatus, and test case generation method and apparatus
CN113704117A (en) Algorithm testing system, method and device
CN111694992B (en) Data processing method and device
CN111078435A (en) Service processing method and device and electronic equipment
CN109976769B (en) Method, device and equipment for determining server identification corresponding to application installation package
CN114840426A (en) Code testing method and device
US9489284B2 (en) Debugging method and computer program product
CN111488569B (en) Authority determining and managing method, device, equipment and medium
CN108733564B (en) Browser performance testing method, device and equipment
CN110688430B (en) Method and device for obtaining data bypass and electronic equipment
CN109903165B (en) Model merging method and device
CN107451050B (en) Function acquisition method and device and server
CN111796864A (en) Data verification method and device
CN111242195A (en) Model, insurance wind control model training method and device and electronic equipment
CN111352825B (en) Data interface testing method and device and server
CN109324966B (en) Test script generation and execution method and device

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination