Method, device and equipment for generating test case
By analyzing the call relationship information of the method to be tested and the allowable state of the target variable, avoiding the assignment of null values in the test cases, solving the problem of false positive NPE in the automatic generation of test cases, and improving the accuracy and code quality of the test cases.
Patent Information
- Application Number
- CN202411479040.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-22
- Publication Date
- 2025-01-10
AI Technical Summary
There is a problem of falsely positive null pointer exception (NPE) in the existing automated test case generation method, which leads to inaccurate test cases.
By obtaining the call relationship information of the method to be tested, determine whether the target variable is allowed to be null, and avoid assigning null values to target parameters that should not be null when generating test cases, ensuring the accuracy of the test cases.
Reduced the situation of false positive NPE use cases, improved the accuracy and code quality of test cases, and significantly reduced the false positive rate by about 20%.
Smart Images

Figure CN119292940A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of software testing technology, and more particularly to a method for generating a test case. The present invention also relates to an apparatus for generating a test case and a device for generating a test case. Background Art
[0002] Test cases are an integral part of software testing. A test case is a document that contains a set of conditions, input data, execution steps, and expected results used in software testing to execute specific test objectives or verify specific software functions.
[0003] In the process of generating test cases, in order to maximize the potential triggering of NullPointer Exception (NPE), all relevant variables are assigned null values during the process of automatically generating test cases. However, in practice, it is found that there are test cases that falsely report NPE. Summary of the invention
[0004] In view of this, embodiments of the present application provide a method, apparatus, and device for generating test cases to solve the problem of false positive NPE in existing methods for automatically generating test cases.
[0005] According to a first aspect of an embodiment of the present application, a method for generating a test case is provided, comprising: obtaining calling relationship information of a method to be tested; determining, based on the calling relationship information, a target variable in an upstream method that calls the method to be tested; the value of the target variable is used to assign a target parameter in the method to be tested; determining whether the target variable is allowed to be a null value, and obtaining a determination result; if the determination result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value.
[0006] According to the second aspect of an embodiment of the present application, a device for generating test cases is provided, comprising: a call relationship information acquisition module, used to acquire call relationship information of a method to be tested; a target variable determination module, used to determine, based on the call relationship information, a target variable in an upstream method that calls the method to be tested; the value of the target variable is used to assign a target parameter in the method to be tested; a judgment module, used to judge whether the target variable is allowed to be a null value, and obtain a judgment result; a test case generation module, used to generate a test case for testing the method to be tested, which contains test data that sets the target parameter to a non-null value, if the judgment result indicates that the target variable is not allowed to be a null value.
[0007] According to a third aspect of an embodiment of the present application, a device for generating test cases is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can: obtain calling relationship information of a method to be tested; determine a target variable in an upstream method that calls the method to be tested based on the calling relationship information; the value of the target variable is used to assign a target parameter in the method to be tested; determine whether the target variable is allowed to be a null value to obtain a judgment result; if the judgment result indicates that the target variable is not allowed to be a null value, generate a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value.
[0008] One embodiment of the present specification can achieve at least the following beneficial effects: by determining, for the target parameter in the method to be tested, the target variable in the upstream method of the method to be tested that is used to assign a value to the target parameter, and then analyzing whether the target variable is allowed to be a null value, and if the target variable is not allowed to be a null value, setting the test data of the target parameter to a non-null value when generating a test case for testing the method to be tested, thereby avoiding assigning a null value to a target parameter that should not be a null value in the test case, thereby avoiding false positive NPE reports. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the embodiments of this specification or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0010] Figure 1 A flowchart of a method for generating a test case provided in an embodiment of this specification;
[0011] Figure 2 The embodiments of this specification provide corresponding to Figure 1 A schematic diagram of the structure of a device for generating test cases;
[0012] Figure 3 The embodiments of this specification provide corresponding to Figure 1 A schematic diagram of the structure of a device for generating test cases. DETAILED DESCRIPTION
[0013] Many specific details are described in the following description to facilitate a full understanding of the present application. However, the present application can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present application, so the present application is not limited by the specific implementation disclosed below.
[0014] The terms used in one or more embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present application. The singular forms of "a", "said" and "the" used in one or more embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of the present application refers to and includes any or all possible combinations of one or more associated listed items.
[0015] It should be understood that, although the terms first, second, etc. may be used to describe various information in one or more embodiments of the present application, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of the present application, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0016] First, the terms involved in one or more embodiments of the present application are explained.
[0017] Unit Testing: The smallest test unit in software testing. Unit testing focuses on verifying the behavior of the smallest testable part of the software (usually a function, method, or module). The goal of unit testing is to ensure that each independent unit works as expected without relying on other parts. Through unit testing, developers can quickly locate and fix problems while improving the quality and maintainability of the code.
[0018] Test case: An integral part of software testing. A test case is a document of a set of conditions, input data, execution steps, and expected results used in software testing to execute specific test objectives or verify specific software functions. Test cases help testers clarify the scope, methods, and expected outputs of the test to ensure that the software works according to the requirements specification. Test cases help ensure the comprehensiveness, repeatability, and traceability of the test, improving test efficiency and software quality.
[0019] NPE: Null Pointer Exception. In Java, NPE is a runtime exception that occurs when trying to access or operate a null reference.
[0020] NPE use case: NPE occurs when a use case is executed. This use case is called an NPE use case.
[0021] Formal parameters are variables declared in the method (function) definition to receive data passed by the caller. Formal parameters are used inside the method to perform the operations required by the method. Formal parameters are not initialized before the method is called. They are just placeholders to receive actual data when the method is called.
[0022] Actual Parameters, or actual parameters for short, are actual values or variables passed to a method (function) when it is called, and are used to replace the formal parameters in the method (function) definition. Actual parameters can be constants, variables, expressions, or the return value of another method. When a method is called, the values of the actual parameters are passed to the corresponding formal parameters, and the method uses these values internally to perform operations.
[0023] In the process of generating test cases for unit testing, in order to maximize the potential triggering of NPE, all relevant variables will be assigned null values during the test case generation process. Specifically, in the solution of automatically generating test cases, a set of random assignment strategies can be designed in advance. Based on the genetic search algorithm, all relevant variables (including method parameters, local variables, and class variables, etc.) are randomly assigned null values during the test case generation process to generate test cases that can reveal NPE.
[0024] However, in practice, it is found that there are some false positive NPE cases. The inventor of the present application has found through analysis that in the code logic of some actual units under test, some parameters will never be empty. In the process of automatically generating test cases, if a null value is mistakenly assigned to a parameter that should not be empty, it will cause a false positive NPE case.
[0025] In view of this, in an embodiment of the present specification, a solution for generating test cases is provided, which analyzes the upstream link of the unit under test in depth, analyzes whether the input parameters of the unit under test may be assigned null values in a specific context, and regenerates test cases based on the analysis results, avoiding assigning null values to variables that should not be null values in the generated test cases. In this way, the situation of false positive NPE cases can be reduced.
[0026] The technical solutions provided by the embodiments of this specification are described in detail below in conjunction with the accompanying drawings.
[0027] In the present application, a method for generating a test case is provided. The present application also relates to an apparatus for generating a test case and a device for generating a test case, which are described in detail one by one in the following embodiments.
[0028] Figure 1 A flowchart of a method for generating a test case provided in an embodiment of this specification.
[0029] From the program perspective, the execution subject of the process can be a program installed on the application server. It can be understood that the method can be executed by any device, equipment, platform, or device cluster with computing and processing capabilities.
[0030] like Figure 1 As shown, the process may include the following steps:
[0031] Step 102: Obtain the call relationship information of the method to be tested.
[0032] The method to be tested may represent a specific object of the unit test, and may specifically be an independent code unit to be tested. In practical applications, the method to be tested may also include other code units to be tested named as a function to be tested or a module to be tested.
[0033] In actual application, the call relationship information may be obtained by processing the application program including the method to be tested using a static analysis tool. In step 102, the acquisition of the call relationship information of the method to be tested may mean that the application program including the method to be tested is processed using a static analysis tool to obtain the call relationship information; or may mean that predetermined call relationship information is acquired, and the call relationship information may be obtained by processing the application program including the method to be tested using a static analysis tool.
[0034] Among them, static analysis tools can be a type of software used to check code defects and vulnerabilities without running the program, and can identify potential errors, code style issues, non-standard practices, and security issues in the program by analyzing source code, binary code, or bytecode. In the embodiments of this specification, the application can be analyzed by using static analysis tools provided in the relevant technology to analyze the data flow between the methods in the application and determine the call relationship information between the methods. For example, common static analysis tools may include Soot, etc.
[0035] In actual application, the process of using a static analysis tool to process an application program containing a method to be tested to obtain call relationship information may include: first, the compiled bytecode of the application program containing the method to be tested may be input into the static analysis tool to obtain a JSON file output by the static analysis tool, and the JSON file is used to reflect the call relationship between various methods in the application program including the method to be tested; then, the JSON file may be parsed to determine the call path of the method to be tested, and obtain call relationship information containing at least one call path.
[0036] Step 104: Determine the target variable in the upstream method that calls the method to be tested according to the calling relationship information; the value of the target variable is used to assign a value to a target parameter in the method to be tested.
[0037] In the embodiment of the present specification, the call relationship information may include one, two or more call paths for the method to be tested. Therefore, from the perspective of different call paths, the upstream method that calls the method to be tested may specifically include upstream methods corresponding to different call paths.
[0038] In addition, in any calling path of the method to be tested, the first-layer upstream method that calls the method to be tested may be included; optionally, the second-layer upstream method that calls the first-layer upstream method may also be included; further optionally, the third-layer upstream method that calls the second-layer upstream method may also be included; and so on, the fourth-layer upstream method may also be included. Therefore, from the perspective of directly or indirectly calling the method to be tested, the upstream method that calls the method to be tested may include the first-layer upstream method that directly calls the method to be tested; optionally, the second-layer upstream method for calling the first-layer upstream method may also be included. By analogy, the upstream method may also include the third-layer upstream method for calling the second-layer upstream method, etc.
[0039] In actual application, the statistical probability of variable values in upstream methods at different levels being passed to the method to be tested is comprehensively considered in practice. In the process of determining the target variable according to the calling relationship information, only one layer of calling relationship may be analyzed (i.e., only the first layer of upstream methods are analyzed), or two layers of calling relationship may be analyzed (i.e., only the first layer of upstream methods and the second layer of upstream methods are analyzed), so as to improve the accuracy of the analysis results while consuming fewer computing resources.
[0040] In each layer of upstream methods, one or more upstream methods may be included. For example, one or more upstream methods may be included in the first layer of upstream methods; one or more upstream methods may be included in the second layer of upstream methods, and specifically, for each upstream method in the first layer of upstream methods, one or more upstream methods for calling the upstream method may be included in the second layer of upstream methods.
[0041] As an example, suppose that two layers of call relationships are analyzed in the process of determining the target variable according to the call relationship information. For the method A to be tested, the analysis shows that the first layer upstream methods B1 and B2 of the method A to be tested are directly called; and the analysis shows that the second layer upstream methods C1 and C2 of the first layer upstream method B1 are called, and the second layer upstream method C3 of the first layer upstream method B2 is called. Therefore, in this example, the call path of the method A to be tested includes A→B1→C1, A→B1→C2, and A→B2→C3.
[0042] Assume that the calling path of the method A to be tested includes the first calling path A→B1→C1. Then, for the method A to be tested
[0043] The target parameter P in the first call path can be determined by analyzing the upstream method B1 (optionally, the upstream method C1 can be further analyzed) to determine the target variable V assigned to the target parameter P in the first call path (optionally, the upper target variable V′ assigned to the target variable V can also be determined). In subsequent steps 106 and 108, it can be judged whether the target variable V is allowed to be a null value (optionally, it can also be judged whether the upper target variable V′ is allowed to be a null value), and then the test data for the target parameter P in the test case is generated based on the judgment result.
[0044] Another assumption is that the calling path of the method A to be tested includes a first calling path A→B1 and a second calling path A→B2. Then, for the target parameter P in the method A to be tested, not only can the target variable V1 assigned to the target parameter P in the first calling path be determined by analyzing the upstream method B1, but also the target variable V2 assigned to the target parameter P in the second calling path can be determined by analyzing the upstream method B2. Then, in step 106 and step 108, it can be judged whether the target variables V1 and V2 are allowed to be null values, and then the test data for the target parameter P in the test case is generated based on the judgment result.
[0045] Step 106: Determine whether the target variable is allowed to be a null value, and obtain a determination result.
[0046] In the embodiments of this specification, a variable is not allowed to be a null value, which can mean that the value of the variable cannot be a null value, or that the variable cannot be in a state where no object or value is assigned. In programming, null can usually be used to indicate that a variable does not point to any object or value.
[0047] Step 108: If the judgment result indicates that the target variable is not allowed to be a null value, a test case for testing the method to be tested is generated, which includes test data for setting the target parameter to a non-null value.
[0048] Specifically, if the judgment result indicates that the target variable is not allowed to be a null value, the test data used to be assigned to the target parameter in the test case of the method to be tested is set to a non-null value. If the judgment result indicates that the target variable is allowed to be a null value, the test data used to be assigned to the target parameter in the test case of the method to be tested can be set to include a null value.
[0049] It should be understood that in the methods described in one or more embodiments of this specification, the order of some steps can be adjusted according to actual needs, or some steps can be omitted.
[0050] Figure 1 The method in the embodiment of the present invention determines, for a target parameter in a method to be tested, a target variable in an upstream method of the method to be tested that is used to assign a value to the target parameter, and then analyzes whether the target variable is allowed to be a null value. If the target variable is not allowed to be a null value, the test data of the target parameter is set to a non-null value when generating a test case for testing the method to be tested. In this way, it is possible to avoid assigning a null value to a target parameter that should not be a null value in a test case, thereby avoiding a false positive NPE.
[0051] In practice, the products that automatically generate unit test cases can be improved based on the solutions of the embodiments of this specification to reduce the number of NPE cases that are falsely reported by these products that automatically generate unit test cases, and significantly improve the accuracy of test case recommendations. Experiments show that by using the solutions provided in the embodiments of this specification to improve the products that automatically generate unit test cases, false positives can be reduced by about 20%.
[0052] based on Figure 1 The present specification also provides some improved implementation methods of the method, which are described below.
[0053] In one or more embodiments of the present specification, the calling relationship information of the method to be tested can reflect the calling relationship between various methods, including the calling relationship between the method to be tested and other methods, and more specifically, can reflect the calling relationship between other methods and the method to be tested. When generating a test case for the method to be tested, in order to determine whether the target parameter in the method to be tested can be assigned a null value, it is necessary to know whether the target variable assigned to the target parameter in the upstream method of the method to be tested is allowed to be a null value. Therefore, it is necessary to first determine the target variable that has a mapping relationship with the target parameter in the method to be tested. Specifically, the parameter mapping relationship information between methods can be determined based on the calling relationship information between various methods. In one or more embodiments of the present specification, a method for determining the parameter mapping relationship information between the target parameter in the method to be tested and the target variable in the upstream method that calls the method to be tested is provided, that is, a method for determining the parameter assignment link is provided.
[0054] Specifically, in step 104, determining the target variable in the upstream method that calls the method to be tested based on the calling relationship information may specifically include: determining the upstream method that calls the method to be tested based on the calling relationship information; analyzing the upstream method to determine the target variable in the upstream method that is assigned to the target parameter in the method to be tested.
[0055] In one or more embodiments of the present specification, the calling relationship information provided by the static analysis tool can be identified, for example, by parsing the JSON file output by the static analysis tool to obtain the upstream method that calls the method to be tested.
[0056] In actual application, a Java virtual machine (JVM) can be used to implement the parsing and subsequent analysis of JSON files. Since static analysis tools usually do not use standard JVM signatures, during the parsing process, the signature format of the JSON file output by the static analysis tool needs to be converted into a standard JVM signature.
[0057] Specifically, determining the upstream method that calls the method to be tested based on the calling relationship information may include: first using a preset format conversion method to convert the method signature information in the calling relationship information to obtain a method signature in a standard JVM format after format conversion; and then, determining the upstream method that calls the method to be tested based on the method signature in the standard JVM format.
[0058] In one or more embodiments of the present specification, considering that the call relationship information is constructed based on the bytecode after the program is compiled, the method directly pointed to by the method to be tested may not be the real upstream method that calls the method to be tested. For example, there are anonymous inner classes, Lambda expressions, etc. In these cases, in order to provide an accurate basis for the subsequent analysis of the parameter assignment link, it is necessary to further analyze the situation where these methods to be tested directly point to methods of a specific form to obtain an accurate parameter call relationship.
[0059] In Java, there is a special local inner class, called an anonymous inner class, which is an object created for one-time use only in a specific context. During compilation, the anonymous inner class will be converted into an independent .class file, which is the same as a standard class file. In actual application, since the call relationship information is constructed based on the compiled bytecode, the calling method of the method to be tested determined according to the call relationship information may point to the method in the anonymous inner class. In the embodiments of the present specification, in order to accurately determine the parameter mapping relationship information, the use of anonymous inner classes can be identified, and the external classes and corresponding external methods of these anonymous inner classes can be traced to build a more accurate parameter assignment link.
[0060] Specifically, determining the upstream method that calls the method to be tested based on the calling relationship information may include: determining the calling method of the method to be tested based on the calling relationship information; determining whether the class where the calling method is located is an anonymous inner class, and obtaining an anonymous inner class judgment result; if the anonymous inner class judgment result indicates that the class where the calling method is located is an anonymous inner class, determining the main class corresponding to the anonymous inner class; and determining a method that uses the anonymous inner class in at least one method corresponding to the main class as the upstream method that calls the method to be tested.
[0061] In an embodiment of the present specification, the calling method of the method to be tested refers to the method for calling the method to be tested displayed by the calling relationship information. For example, when the calling relationship information is a graph data containing nodes and edges, the calling method and the method to be tested can be two adjacent nodes connected by an edge. The upstream method that calls the method to be tested refers to the method that actually calls the method to be tested obtained by further analyzing the calling relationship information. Subsequently, the upstream method variables can be matched with the parameters in the method to be tested, or in other words, the target variables for assigning the target parameters of the method to be tested can be found from the upstream method. In actual application, usually, the calling method can be directly used as the upstream method, but in some special cases, for example, when the class of the method to be tested is an anonymous inner class or when the method to be tested is represented by a Lambda expression, it is necessary to further determine the upstream method that actually calls the method to be tested based on the calling method.
[0062] Among them, judging whether the class where the calling method is located is an anonymous inner class can specifically include, first, determining the class where the calling method is located; and then judging whether the class name of the class where the calling method is located conforms to the class name format of the anonymous inner class. More specifically, it can be judged whether the class name of the class where the calling method is located contains specific characters. For example, it can be judged whether the class name of the class where the calling method is located contains a $ sign. In actual application, if the class name of the class where the calling method is located contains a $ sign, it can be determined that the class where the calling method is located is an anonymous inner class.
[0063] Correspondingly, when the class where the calling method is located is an anonymous inner class, determining the main class corresponding to the anonymous inner class can specifically include determining the character string before specific characters in the class name of the anonymous inner class as the class name of the main class corresponding to the anonymous inner class, thereby determining the main class corresponding to the anonymous inner class.
[0064] For ease of understanding, the following examples are given:
[0065] public class AAA{
[0066] public void delete(String id){
[0067] TransactionTemplate.execute(new TransactionCallback(){
[0068] @Override
[0069] protected void doInTransaction(){
[0070] methodB(id)
[0071] }
[0072] });
[0073] }
[0074] }.
[0075] In this example, according to the calling relationship information, it can be known that the calling method of the method to be tested methodB is doInTransaction, and the class where the calling method doInTransaction belongs is AAA$0.
[0076] Based on the method of the embodiment of this specification, it can be determined whether the class AAA$0 where the calling method doInTransaction is located is an anonymous inner class. Specifically, by determining whether AAA$0 contains a $ symbol, it can be known that the class AAA$0 is an anonymous inner class.
[0077] Then, it can be determined that the main class corresponding to the anonymous inner class AAA$0 is AAA. Further, the methods corresponding to the main class AAA can be analyzed to determine that the method using the anonymous inner class AAA$0 in the methods corresponding to the main class AAA is the delete method, and the delete method is determined as the upstream method that actually calls the method to be tested methodB.
[0078] In Java, Lambda expressions provide a concise way to implement single-use functional code blocks. During compilation, each Lambda expression will be encapsulated in a private static method of the class to which it belongs. In actual application, since the call relationship information is constructed based on the compiled bytecode, the calling method of the method to be tested determined according to the call relationship information may point to the method represented by the Lambda expression. In the embodiments of the present specification, in order to accurately determine the parameter mapping relationship information, the use of Lambda expressions can be identified, and the external methods corresponding to these Lambda expressions can be traced back to build a more accurate parameter assignment link.
[0079] Specifically, determining the upstream method that calls the method to be tested based on the calling relationship information specifically includes: determining the calling method of the method to be tested based on the calling relationship information; judging whether the calling method is a method represented by a Lambda expression, and obtaining a Lambda expression judgment result; if the Lambda expression judgment result indicates that the calling method is a method represented by a Lambda expression, determining the upstream method that calls the method to be tested based on the Lambda expression.
[0080] Wherein, determining whether the calling method is a method represented by a Lambda expression may specifically include determining whether the calling method conforms to a preset Lambda expression format. For example, it may be determined whether the calling method conforms to a format such as "lambda$real method name$number". If so, it may be determined that it is a Lambda expression.
[0081] Correspondingly, determining the upstream method that calls the method to be tested according to the Lambda expression may specifically include extracting the real method name in the Lambda expression, that is, extracting the string between the first specific character and the second specific character in the Lambda expression, wherein the first specific character may be a $ sign, and the second specific character may be a $ sign.
[0082] For ease of understanding, the following examples are given:
[0083] public String insert(ElementRequest request){
[0084] return TransactionTemplate.execute(transactionStatus->{
[0085] checkExist(request.getElementCode());
[0086] return convert2Model(request);
[0087] })
[0088] }.
[0089] For this example, in the call relationship information obtained by analyzing the program code using a static analysis tool, the call method of the test method convert2Model is displayed as lambda$insert$2, for example. This call method meets the naming characteristics of "lambda$real method name$number" and is a lambda expression. Therefore, according to the lambda expression lambda$insert$2, it can be determined that the upstream method that calls the test method convert2Model is the insert method.
[0090] In one or more embodiments of the present specification, the target variable in the upstream method is assigned to the target parameter in the method to be tested, which can be used to indicate that the target parameter is assigned with an assignment statement containing the target variable. Specifically, it can include using the value of the target variable as the value of the target parameter, or determining the value of the target parameter based on the value of the target variable. Therefore, in fact, the process of determining the target variable in the upstream method to be assigned to the target parameter in the method to be tested can be a process of first determining a parameter assignment link, and then determining the target variable that plays a key role in the target parameter assignment from the parameter assignment link.
[0091] Specifically, analyzing the upstream method to determine the target variable in the upstream method that is assigned to the target parameter in the method to be tested may include: determining a parameter assignment link in the upstream method that is assigned to the target parameter in the method to be tested by analyzing instructions in the upstream method; and determining, from the parameter assignment link, a target variable used to establish a parameter mapping relationship with the target variable according to a preset target variable determination rule.
[0092] In practical applications, considering that in a parameter assignment link, when the upstream variable is not empty, the downstream variable that depends on the upstream variable is not empty, therefore, the initial variable in the parameter assignment link can be determined as the target variable, and whether the target parameter can be assigned a null value can be determined based on whether the target variable can be assigned a null value, without having to judge whether each variable on the parameter assignment link can be assigned a null value, so as to save computing resources.
[0093] For ease of understanding, the following examples are given:
[0094] public void upstreamMethod(int a,String b){
[0095] int aa=2*a;
[0096] String bb=b.toUpperCase();
[0097] int cc=someMethodThatReturnsAnInt();
[0098] downstreamMethod(aa,bb,cc);
[0099] }
[0100] public void downstreamMethod(int x,String y,int z){
[0101] System.out.println("x:"+x+",y:"+y+",z:"+z);
[0102] }.
[0103] In this example, the upstream method upstreamMethod calls the method to be tested downstreamMethod. For the target parameter int x in the method to be tested downstreamMethod, by analyzing the upstream method upstreamMethod, we can know that the parameter assignment link is x→aa→a, that is, the value of x comes from aa, and the value of aa comes from a. In this parameter assignment link, a is the target variable. We can determine whether the target parameter x can be assigned a null value by identifying whether the target variable can be assigned a null value.
[0104] In addition, in actual application, the parameter assignment link may include the formal parameters of the upstream method, or may include the internal variables of the upstream method, or may include both the formal parameters of the upstream method and the internal variables of the upstream method. In this case, in order to simplify the verification process, the formal parameters of the upstream method involved in the parameter assignment link may be prioritized for determining whether they can be assigned null values.
[0105] Optionally, determining the target variable for establishing a parameter mapping relationship with the target variable from the parameter assignment link according to a preset target variable determination rule may include: if the parameter assignment link contains formal parameters of the upstream method, determining the formal parameters as the target variable for establishing a parameter mapping relationship with the target variable; if the parameter assignment link does not contain the formal parameters of the upstream method, determining the internal variables of the upstream method contained in the parameter assignment link as the target variable for establishing a parameter mapping relationship with the target variable.
[0106] For example, if the parameter assignment link only contains the formal parameters of the upstream method, the formal parameters of the upstream method can be determined as the target variables; for another example, if the parameter assignment link contains both the formal parameters of the upstream method and the internal variables of the upstream method, the formal parameters of the upstream method can be determined as the target variables; for another example, if the parameter assignment link does not contain the formal parameters of the upstream method but only contains the internal variables of the upstream method, the internal variables can be determined as the target variables.
[0107] The above is only an example of determining the target variable from the parameter assignment link. By determining whether the target variable in the parameter assignment link can be assigned a null value, instead of determining whether all variables in the parameter assignment link can be assigned a null value, computing resources can be saved. When the determined target variable is determined to be not assigned a null value, it can be determined that the assigned target parameter corresponding to the parameter assignment link cannot be assigned a null value.
[0108] Optionally, in order to improve the accuracy of the judgment result of whether the target parameter can be assigned a null value, when the determined target variable is judged to be able to be assigned a null value, another target variable that has no dependency relationship with the previously determined target variable can be further determined from the parameter assignment link, and a judgment can be made as to whether the other target variable can be assigned a null value. If the other target variable cannot be assigned a null value, the assigned target parameter cannot be assigned a null value.
[0109] Continuing with the example of the upstream method upstreamMethod calling the method to be tested downstreamMethod in the above text, if the statement downstreamMethod(aa,bb,cc) is changed to downstreamMethod(aa,bb,dd), and the previously added statement int dd=aa+cc; then, the parameter assignment link for the target parameter int z in the method to be tested downstreamMethod can include: z→dd→aa→a and z→dd→cc. Assume that, for the target variable a determined for the first time, if it is identified that the target variable a cannot be assigned a null value, then it can be determined that the target parameter z cannot be assigned a null value; if it is not identified that the target variable a cannot be assigned a null value, then, optionally, another target variable cc can be further determined, and it can be determined whether cc can be assigned a null value, so as to improve the accuracy of the judgment result on whether the target parameter z can be assigned a null value.
[0110] In one or more embodiments of the present specification, calling the upstream method of the method to be tested may specifically include calling the first-layer upstream method of the method to be tested and calling the second-layer upstream method of the first-layer upstream method. Of course, optionally, it may also include calling the third-layer upstream method of the second-layer upstream method, and so on, and may also include more layers of upstream methods. In actual application, considering the consumption of computing resources and the improvement of the accuracy of the judgment result of whether the parameter can be assigned a null value, the parameter assignment link of the two-layer upstream method may be considered.
[0111] Specifically, the analyzing the upstream method to determine the target variable in the upstream method that is assigned to the target parameter in the method to be tested may include: analyzing the first-layer upstream method to determine the first-layer target variable in the first-layer upstream method that is assigned to the target parameter in the method to be tested; analyzing the second-layer upstream method to determine the second-layer target variable in the second-layer upstream method that is assigned to the first-layer target variable in the first-layer upstream method.
[0112] Among them, the analyzing the first-layer upstream method to determine the first-layer target variable in the first-layer upstream method that is assigned to the target parameter in the method to be tested may specifically include: determining the first-layer assignment link in the first-layer upstream method that is assigned to the target parameter in the method to be tested by analyzing the instructions in the first-layer upstream method; then, according to the preset target variable determination rule, determining the first-layer target variable used to establish a parameter mapping relationship with the target variable from the first-layer assignment link. Further, if the first-layer assignment link contains the formal parameters of the first-layer upstream method, the formal parameters of the first-layer upstream method are determined as the first-layer target variables used to establish a parameter mapping relationship with the target variables; if the first-layer assignment link does not contain the formal parameters of the first-layer upstream method, the internal variables of the first-layer upstream method contained in the parameter assignment link are determined as the first-layer target variables used to establish a parameter mapping relationship with the target variables.
[0113] Similarly, the analysis of the second-layer upstream method to determine the second-layer target variable in the second-layer upstream method that is assigned to the first-layer target variable in the first-layer upstream method may specifically include: determining the second-layer assignment link in the second-layer upstream method that is assigned to the first-layer target variable in the first-layer upstream method by analyzing the instructions in the second-layer upstream method; and then, according to the preset target variable determination rule, determining the second-layer target variable used to establish a parameter mapping relationship with the first-layer target variable from the second-layer assignment link. Further, if the second-layer assignment link contains the formal parameters of the second-layer upstream method, the formal parameters of the second-layer upstream method are determined as the second-layer target variables used to establish a parameter mapping relationship with the first-layer target variables; if the second-layer assignment link does not contain the formal parameters of the second-layer upstream method, the internal variables of the second-layer upstream method contained in the second-layer parameter assignment link are determined as the second-layer target variables used to establish a parameter mapping relationship with the first-layer target variables.
[0114] In one or more embodiments of the present specification, considering the parameter assignment link of the two-layer upstream method, the determining whether the target variable is allowed to be a null value and obtaining a determination result may specifically include: determining whether the first-layer target variable is allowed to be a null value and obtaining a first-layer determination result.
[0115] Then, if the judgment result indicates that the target variable is not allowed to be a null value, a test case is generated for testing the method to be tested, which contains test data that sets the target parameter to a non-null value. Specifically, it may include: if the first-layer judgment result indicates that the first-layer target variable is not allowed to be a null value, a test case is generated for testing the method to be tested, which contains test data that sets the target parameter to a non-null value.
[0116] And optionally, if the judgment result indicates that the target variable is not allowed to be a null value, then a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value is generated, which may specifically include: if the first-layer judgment result indicates that the first-layer target variable is allowed to be a null value, then a judgment is made as to whether the second-layer target variable is allowed to be a null value to obtain a second-layer judgment result; if the second-layer judgment result indicates that the second-layer target variable is not allowed to be a null value, then a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value is generated.
[0117] In one or more embodiments of the present specification, after determining the target variable in the upstream method for assigning a value to the target parameter in the method to be tested based on the call relationship information, parameter mapping relationship information between the target variable in the method to be tested and the target parameter in the upstream method can be established. When establishing the parameter mapping relationship information, for the sake of accuracy, readability, and small storage space, the mapping relationship between the index information of the parameters can be used to represent the mapping relationship between the parameters.
[0118] Specifically, according to the calling relationship information, the target variable in the upstream method that calls the method to be tested is determined; after the value of the target variable is used to assign the target parameter in the method to be tested, it can include: establishing index mapping relationship information between the index information of the target parameter and the index information of the target variable.
[0119] Continuing with the example of the upstream method upstreamMethod calling the method to be tested downstreamMethod in the above text, in this example, in the upstream method upstreamMethod, the first target variable having a mapping relationship with the first target parameter int x of the downstream method downstreamMethod is int a; the second target variable having a mapping relationship with the second target parameter String y of the downstream method downstreamMethod is String b; and the third target variable having a mapping relationship with the third target parameter int z of the downstream method downstreamMethod is int cc.
[0120] In actual application, when establishing mapping relationship information, it can be represented by the index of each parameter or variable. Specifically, in the downstream method downstreamMethod, the indexes of the formal parameters int x, String y and int z are 0, 1 and 2 respectively; in the upstream method upstreamMethod, the indexes of the formal parameters int a and String b are 0 and 1 respectively, and the index of the internal variable can be set to -1. In the embodiment of the present specification, the index of the variable is -1, which means that the variable is not a formal parameter of the method, but an internal variable of the method. Therefore, for each formal parameter in the downstream method downstreamMethod, the index mapping relationship information established between the upstream method upstreamMethod can be: 0→0, 1→1 and 2→-1.
[0121] In the embodiments of this specification, in order to facilitate query, when establishing index mapping relationship information, the former of the mapping relationship can be set to represent the parameters in the method to be tested, and the latter can be set to represent the variables (including formal parameters or internal variables) in the upstream method. Of course, the former can also be set to the variables (including formal parameters or internal variables) in the upstream method, and the latter can be set to the parameters in the method to be tested, and the format rules can be agreed in advance.
[0122] In actual application, another device can determine the target variables in the upstream method corresponding to each target parameter in the method to be tested in advance according to the call relationship information, and establish index mapping relationship information. As a result, on the one hand, when it is necessary to generate a test case, the pre-generated index mapping relationship information can be called to quickly determine the target variable, thereby improving the efficiency of generating test cases; on the other hand, the established index mapping relationship information can be reused when establishing test cases in various test scenarios, which improves information utilization and is equivalent to saving computing resources.
[0123] In addition, in actual applications, when using pre-established index mapping relationship information to determine the target variable, if the target variable is a formal parameter of the upstream method, the specific formal parameter can be directly located according to the index mapping relationship information; if the target variable is a local variable in the upstream method, the specific local variable can be determined by further analyzing the method body of the upstream method.
[0124] In one or more embodiments of the present specification, when the method to be tested has only one calling path, it can be analyzed whether the calling path allows the value of the target parameter in the method to be tested to be set to a null value. If the calling path does not allow the value of the target parameter in the method to be tested to be set to a null value, then when generating a test case for testing the method to be tested, it is necessary to set the test value of the target parameter to a non-null value to avoid false positives of NPE cases.
[0125] In one or more embodiments of the present specification, when the method to be tested has two or more different calling paths, it can be analyzed separately whether the value of the target parameter in the method to be tested is allowed to be set to a null value in each calling path. If the value of the target parameter in the method to be tested is not allowed to be set to a null value in each calling path, then when generating a test case for testing the method to be tested, it is necessary to set the test value of the target parameter to a non-null value to avoid false positives of NPE use cases. If at least one calling path in each calling path allows the value of the target parameter in the method to be tested to be set to a null value, then when generating a test case for testing the method to be tested, the test value of the target parameter can be set to include a null value to improve the hit rate of the NPE use case.
[0126] Specifically, the upstream method specifically includes upstream methods corresponding to at least two call paths of the method to be tested. The judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether the target variable corresponding to each call path in the at least two call paths is allowed to be a null value; if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, which may specifically include: if the target variables corresponding to each call path in the at least two call paths are not allowed to be null values, then generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value.
[0127] Optionally, if the method to be tested has two call paths: the upstream method may include a first path upstream method in the first call path of the method to be tested and a second path upstream method in the second call path of the method to be tested, and the target variable may include a first path target variable in the first path upstream method and a second path target variable in the second path upstream method. Then, the step of judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether the first path target variable is allowed to be a null value and obtaining a first path judgment result; and judging whether the second path target variable is allowed to be a null value and obtaining a second path judgment result; accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value may specifically include: if the first path judgment result indicates that the first path target variable is not allowed to be a null value, and the second path judgment result indicates that the second path target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value.
[0128] As an example, it is assumed that the method A1 to be tested includes a target parameter Pa1, and the method A1 to be tested has a first call path B1-A1 and a second call path B2-A1. The first path upstream method B1 of the method A1 to be tested includes a first path target variable Pb1 for assigning a value to the target parameter Pa1, and the second path upstream method B2 of the method A1 to be tested includes a second path target variable Pb2 for assigning a value to the target parameter Pa1. Therefore, it is possible to analyze whether the first path target variable Pb1 and the second path target variable Pb2 are allowed to be null values. If both the first path target variable Pb1 and the second path target variable Pb2 are not allowed to be null values, it indicates that the target parameter Pa1 cannot be null values in all call paths (the first call path B1-A1 and the second call path B2-A1). Therefore, when generating a test case for the method A1 to be tested, the test data of the target parameter Pa1 can be set to a non-null value to avoid false positives when generating NPE cases. If at least one of the first path target variable Pb1 and the second path target variable Pb2 is allowed to be a null value, it indicates that the target parameter Pa1 may be a null value in at least part of the calling path (at least one of the first calling path B1-A1 and the second calling path B2-A1). Therefore, when generating a test case for the method A1 to be tested, the test data of the target parameter Pa1 can be set to include a null value so as to trigger potential NPF problems as much as possible.
[0129] Optionally, if the method to be tested has three calling paths: the upstream method not only includes the first path upstream method in the first calling path of the method to be tested and the second path upstream method in the second calling path of the method to be tested, but may also include the third path upstream method in the third calling path of the method to be tested; the target variable may include the first path target variable in the first path upstream method and the second path target variable in the second path upstream method, and may also include the third path target variable in the third path upstream method. Then, the judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether the first path target variable is allowed to be a null value and obtaining a first path judgment result; and judging whether the second path target variable is allowed to be a null value and obtaining a second path judgment result; and judging whether the third path target variable is allowed to be a null value and obtaining a third path judgment result; accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value may specifically include: if the first path judgment result indicates that the first path target variable is not allowed to be a null value, and the second path judgment result indicates that the second path target variable is not allowed to be a null value, and the third path judgment result indicates that the third path target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value.
[0130] For the case involving more call paths, the method for determining whether the value of the target parameter in the method to be tested is allowed to be set to a null value can refer to the above cases of two call paths and three call paths, which will not be repeated here.
[0131] In one or more embodiments of the present specification, when the upstream method calls the method to be tested, two parameter passing situations may be included. In one situation, the formal parameters of the upstream method are passed as the actual parameters of the downstream method to be tested, that is, the target variables are the formal parameters of the upstream method; in another situation, the local variables defined in the upstream method are passed as the actual parameters of the downstream method to be tested, that is, the target variables are the local variables of the upstream method.
[0132] The example of the upstream method upstreamMethod calling the method to be tested downstreamMethod in the above text is used. In this example, the target variable int a that has a mapping relationship with the target parameter int x in the method to be tested downstreamMethod is a formal parameter of the upstream method upstreamMethod; the target variable String b that has a mapping relationship with the target parameter String y in the method to be tested downstreamMethod is a formal parameter of the upstream method upstreamMethod; the target variable int cc that has a mapping relationship with the target parameter int z in the method to be tested downstreamMethod is a local variable of the upstream method upstreamMethod.
[0133] The following specifically introduces the method for determining whether a target variable can be assigned a null value in these two situations provided by the embodiments of this specification.
[0134] In an optional embodiment of the present specification, the target variable may include a formal parameter of the upstream method, and the value of the formal parameter may be passed to the method to be tested. Specifically, the value of the formal parameter may be used as an actual parameter for calling the method to be tested.
[0135] Specifically, the target variable may be a target formal parameter in the upstream method. Then, judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether the upstream method satisfies at least one of a first condition and a second condition and obtaining a parameter judgment result; the first condition includes that there is a specified annotation information for the target formal parameter in the upstream method, and the specified annotation information is used to indicate that the target formal parameter is not allowed to be a null value; the second condition includes that there is a first verification instruction before the target call instruction for calling the method to be tested, and the first verification instruction is used to verify that the value of the target formal parameter is not a null value. Accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value may specifically include: if the parameter judgment result indicates that the upstream method satisfies at least one of the first condition and the second condition, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value. More specifically, if the formal parameter judgment result is yes, it indicates that the target formal parameter is not allowed to be a null value, and the target parameter is also not allowed to be a null value. Therefore, when generating a test case for testing the method to be tested, the test data of the target parameter should be set to a non-null value to reduce the probability of false positives of NPE cases.
[0136] In actual application, the first condition and the second condition can be judged in sequence. Optionally, it is possible to first judge whether the first condition is met. If the first condition is met, there is no need to judge the second condition again, so as to save computing resources; if the first condition is not met, the second condition is judged. Of course, it is also possible to first judge whether the second condition is met. If the second condition is met, there is no need to judge the first condition again, so as to save computing resources; if the second condition is not met, the first condition is judged again.
[0137] In some optional scenarios, there may be specified annotation information for the target formal parameter in the upstream method.
[0138] Specifically, the target variable may be a target formal parameter in the upstream method. The step of judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether there is specified annotation information for the target formal parameter in the upstream method, and obtaining a first judgment result; the specified annotation information is used to indicate that the target formal parameter is not allowed to be a null value. Accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value may specifically include: if the first judgment result indicates that there is specified annotation information for the target formal parameter, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value.
[0139] Among them, annotation is a special mark applied to elements such as classes, methods, parameters, variables, constructors and packages. These marks can be read at compile time, load time and runtime, and corresponding processing is performed. The specified annotation information can specifically include at least one of @NonNull, @NotNull, @NotBlank, @Valid and @Validated. When there is one of the above annotation information for the target formal parameter, it indicates that the target formal parameter will not be null. Therefore, when calling the downstream method to be tested, the target parameter will not be null.
[0140] In actual application, first, the ASM framework can be used to parse the bytecode of the upstream method and identify the annotation information of the formal parameters. The identified annotation information includes runtime annotation information and compile-time annotation information. Then, it can be determined whether the annotation information contains the specified annotation information of the target variable that has a mapping relationship with the target parameter. For example, the specified annotation information may include @NonNull, @NotNull, @NotBlank, @Valid or @Validated, etc. The specified annotation information can be used to indicate that the relevant parameters should not be null values.
[0141] In some other optional scenarios, the upstream method may perform autonomous verification on the target formal parameters.
[0142] Specifically, the target variable may be a target formal parameter in the upstream method. The step of judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether, in the upstream method, a first verification instruction exists before the target call instruction for calling the method to be tested, and obtaining a second judgment result; the first verification instruction is used to verify that the value of the target formal parameter is not a null value. Accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value may specifically include: if the second judgment result indicates that a first verification instruction exists before the target call instruction for calling the method to be tested, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value.
[0143] Among them, the first verification instruction is used to verify that the value of the formal parameter is a non-null value, which can specifically mean that when the value of the formal parameter is not a null value, the verification is successful, and subsequent instructions can be executed, for example, the method to be tested can be called; when the value of the formal parameter is a null value, the verification fails, and subsequent instructions are not executed, for example, the method to be tested cannot be called.
[0144] In actual application, in order to identify this scenario, first, you can analyze the calling instructions in the method one by one to check whether there is a keyword match, such as whether there is a calling instruction of a verification function such as validate or assertNotNull; then, you can determine whether the verification function verifies the target formal parameter; if so, and the verification rule does not allow the target formal parameter to be empty, then the target formal parameter cannot be assigned a null value in the current path, and the target parameter to be tested cannot be assigned a null value.
[0145] For example, methodA(param){
[0146] ValidationUtill.validate(param)
[0147] In this example, the first validation instruction ValidationUtil1.validate(param) implements the non-empty validation of the formal parameter param.
[0148] In an optional embodiment of the present specification, the target variable may include a target local variable defined in the upstream method, and the value of the target local variable may be passed to the method to be tested. Specifically, the value of the target local variable may be used as an actual parameter for calling the method to be tested.
[0149] Specifically, the target variable is the target local variable defined in the upstream method. Then, the determination of whether the target variable is allowed to be a null value and obtaining a determination result may specifically include: determining whether there is a specified instruction before the target call instruction for calling the method to be tested in the upstream method, and obtaining a local variable determination result; the specified instruction includes at least one of an assignment instruction and a second verification instruction; the assignment instruction includes an initialization instruction or a target method call instruction; the initialization instruction is used to indicate that an initialization operation is performed on the target local variable; the target method call instruction is used to assign the return value of the called method to the target local variable and the return value does not contain a null value; the second verification instruction is used to verify that the value of the target local variable is not a null value. Accordingly, if the determination result indicates that the target variable is not allowed to be a null value, then a test case containing test data for setting the target parameter to a non-null value is generated for testing the method to be tested, which may specifically include: if the local variable determination result indicates that there is a specified instruction before the target call instruction for calling the method to be tested, then a test case containing test data for setting the target parameter to a non-null value is generated for testing the method to be tested. More specifically, if the local variable judgment result is yes, it can be determined that the target variable is not allowed to be a null value, and the target parameter is also not allowed to be a null value. Therefore, when generating a test case for testing the method to be tested, the test data of the target parameter should be set to a non-null value to reduce the probability of false alarm of the NPE case.
[0150] In actual application, if it is assumed that there is an initialization instruction before the target call instruction for calling the method to be tested as the third condition, there is a target method call instruction before the target call instruction for calling the method to be tested as the fourth condition, and there is a second verification instruction before the target call instruction for calling the method to be tested as the fifth condition, then any first condition among the third condition, the fourth condition and the fifth condition can be judged first. If it is not hit, the other conditions among the third condition, the fourth condition and the fifth condition are judged in turn until any condition is hit, or until it is judged that any condition is not satisfied. In this way, the judgment result of whether the test data of the target parameter should be set to a non-null value can be determined with less computing resources.
[0151] In some optional scenarios, before calling the method to be tested, the upstream method can initialize the target local variables needed to call the method to be tested.
[0152] Specifically, the target variable may be a target local variable defined in the upstream method. The step of judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether, in the upstream method, there is an initialization instruction before the target call instruction for calling the method to be tested, and obtaining a third judgment result; the initialization instruction is used to instruct to perform an initialization operation on the target local variable. Accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value may specifically include: if the third judgment result indicates that there is an initialization instruction before the target call instruction for calling the method to be tested, then generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value.
[0153] More specifically, the determination of whether there is an initialization instruction before the target call instruction for calling the method to be tested in the upstream method may include: identifying the initialization instruction before the target call instruction for calling the method to be tested in the upstream method; determining the initialization local variables corresponding to the initialization instruction; and determining whether the target local variables are included in the initialization local variables. If the target local variables are included in the initialization local variables, it indicates that the initialization operation has been performed on the target local variables; if the target local variables are not included in the initialization local variables, it indicates that the initialization operation has not been performed on the target local variables.
[0154] In actual application, when the local variables of the upstream method have been explicitly initialized before being passed to the downstream method, the initialization instructions can be analyzed to identify whether the target local variables are non-null. If the local variables have been initialized to non-null values when assigned, it is considered that the parameters in this path should not be null values.
[0155] For example, methodA(){
[0156] param = new Param();
[0157] methodB(param);
[0158] In this example, the upstream method methodA calls the method to be tested methodB, and the local variable param defined in the upstream method methodA is used as the actual parameter passed when calling the method to be tested methodB. Among them, param=newParam() is an initialization instruction for initializing the parameter param.
[0159] In some other optional scenarios, the local variables in the upstream method are the return values of calling other methods. In this case, if the called method does not return a null value, the actual parameters of the downstream method call will not be null values.
[0160] Specifically, the target variable may be a target local variable defined in the upstream method. The step of judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether in the upstream method, a target method calling instruction exists before the target calling instruction used to call the method to be tested, and obtaining a fourth judgment result; the target method calling instruction is used to assign the return value of the called method to the target local variable and the return value does not contain a null value. Accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then a test case containing test data that sets the target parameter to a non-null value is generated for testing the method to be tested, which may specifically include: if the fourth judgment result indicates that a target method calling instruction exists before the target calling instruction used to call the method to be tested, then a test case containing test data that sets the target parameter to a non-null value is generated for testing the method to be tested.
[0161] More specifically, the determination of whether a target method calling instruction exists before a target calling instruction for calling the method to be tested in the upstream method to obtain a fourth determination result may specifically include: identifying a method calling instruction before a target calling instruction for calling the method to be tested in the upstream method; the method calling instruction is used to assign the return value of the called method to the target local variable; and then, determining whether the return value of the called method contains a null value to obtain a return value determination result. Accordingly, if the fourth determination result indicates that a target method calling instruction exists before a target calling instruction for calling the method to be tested, then generating a test case for testing the method to be tested that contains test data for setting the target parameter to a non-null value specifically includes: if the return value determination result indicates that the return value of the called method does not contain a null value, then generating a test case for testing the method to be tested that contains test data for setting the target parameter to a non-null value.
[0162] In actual application, for local variables that are returned by other methods and are clearly not null, the calling methods assigned to these variables can be checked. Specifically, it can be mainly checked whether these calling methods have the characteristic of non-null return. For example, this can usually be achieved by analyzing the method ret instruction (return statement). If the calling method is guaranteed not to return a null value, the corresponding parameters in the current path cannot be assigned null values.
[0163] For example, methodA(){
[0164] param = buildParam();
[0165] methodB(param);
[0166] };
[0167] buildParam(){
[0168] xxx=new XXX();
[0169] Return xxx;
[0170] }. In this example, the upstream method methodA calls the method to be tested methodB, and the local variable param of the upstream method methodA is the actual parameter passed when calling the method to be tested methodB, and the local variable param is the return value of the buildParam method. In actual application, the method body of buildParam can be analyzed to confirm whether the return value of buildParam can be null, thereby confirming whether the local variable param can be null.
[0171] In some other optional scenarios, the upstream method can independently check the target local variable to be used in calling the method to be tested before calling the method to be tested to ensure that the variable is not a null value.
[0172] Specifically, the target variable may be a target local variable defined in the upstream method. The step of judging whether the target variable is allowed to be a null value and obtaining a judgment result may specifically include: judging whether, in the upstream method, a second verification instruction exists before the target call instruction used to call the method to be tested, and obtaining a fifth judgment result; the second verification instruction is used to verify that the value of the target local variable is not a null value. Accordingly, if the judgment result indicates that the target variable is not allowed to be a null value, then a test case containing test data setting the target parameter to a non-null value is generated for testing the method to be tested, which may specifically include: if the fifth judgment result indicates that a second verification instruction exists before the target call instruction used to call the method to be tested, then a test case containing test data setting the target parameter to a non-null value is generated for testing the method to be tested.
[0173] Among them, the second verification instruction is used to verify that the value of the target local variable is not a null value. Specifically, it can be indicated that when the value of the target local variable is not a null value, the verification is successful, and subsequent instructions can be continued to be executed, for example, the method to be tested can be called; when the value of the target local variable is a null value, the verification fails, and subsequent instructions will not be executed, for example, the method to be tested cannot be called.
[0174] In actual application, in order to identify this scenario, first, you can analyze the calling instructions in the method one by one to check whether there is a keyword match, such as whether there is a calling instruction of a verification function such as validate or assertNotNull; then, you can determine whether the verification function has verified the target local variable; if so, and the verification rule does not allow the target local variable to be empty, then the target local variable cannot be assigned a null value in the current path, and the target parameter to be tested cannot be assigned a null value.
[0175] For example, methodA(){
[0176] param.validate();
[0177] methodB(param);
[0178] In this example, the upstream method methodA calls the method to be tested methodB, and the local variable param of the upstream method methodA is used as the actual parameter passed when calling the method to be tested methodB. Among them, the non-empty check of the local variable param is implemented through the first check instruction param.validate().
[0179] Based on one or more of the above embodiments of this specification, through the code static analysis method, the structure and execution logic of the code are considered to ensure that false positives are reduced in the process of automated test case generation, and the test accuracy and code quality are improved. In addition, when determining whether a variable can be assigned a null value, the identification of various scenarios not only helps to improve the generation of test cases, but also enhances the developer's understanding of code behavior, thereby improving the overall reliability and maintainability of the software.
[0180] The various technical features in the above embodiments can be combined arbitrarily as long as there is no conflict or contradiction between the combinations of features. However, due to space limitations, they are not described one by one. Therefore, any combination of the various technical features in the above embodiments also falls within the scope of this specification.
[0181] Based on the same idea, the embodiments of this specification also provide a device corresponding to the above method.
[0182] Figure 2 The embodiments of this specification provide corresponding to Figure 1 A structural schematic diagram of a device for generating test cases.
[0183] like Figure 2 As shown, the device may include:
[0184] The calling relationship information acquisition module 202 is used to acquire the calling relationship information of the method to be tested;
[0185] A target variable determination module 204 is used to determine a target variable in an upstream method that calls the method to be tested according to the call relationship information; the value of the target variable is used to assign a target parameter in the method to be tested;
[0186] A judgment module 206 is used to judge whether the target variable is allowed to be a null value and obtain a judgment result;
[0187] The test case generation module 208 is used to generate a test case for testing the method to be tested, which contains test data that sets the target parameter to a non-null value, if the judgment result indicates that the target variable is not allowed to be a null value.
[0188] based on Figure 2 The present specification also provides some specific implementation schemes of the method, which are described below.
[0189] Optionally, the target variable determination module 204 specifically includes: an upstream method determination unit, used to determine the upstream method that calls the method to be tested based on the calling relationship information; a target variable determination unit, used to analyze the upstream method and determine the target variable in the upstream method that is assigned to the target parameter in the method to be tested.
[0190] Optionally, the upstream method determination unit is specifically used to: determine the calling method of the method to be tested based on the calling relationship information; determine whether the class where the calling method is located is an anonymous inner class, and obtain an anonymous inner class judgment result; if the anonymous inner class judgment result indicates that the class where the calling method is located is an anonymous inner class, determine the main class corresponding to the anonymous inner class; and determine the method that uses the anonymous inner class in at least one method corresponding to the main class as the upstream method that calls the method to be tested.
[0191] Optionally, the upstream method determination unit is specifically used to: determine the calling method of the method to be tested based on the calling relationship information; determine whether the calling method is a method represented by a Lambda expression, and obtain a Lambda expression judgment result; if the Lambda expression judgment result indicates that the calling method is a method represented by a Lambda expression, then determine the upstream method that calls the method to be tested based on the Lambda expression.
[0192] Optionally, the target variable determination unit is specifically used to: determine the parameter assignment link in the upstream method that is assigned to the target parameter in the method to be tested by analyzing the instructions in the upstream method; and determine the target variable used to establish a parameter mapping relationship with the target variable from the parameter assignment link according to a preset target variable determination rule. Optionally, determining the target variable used to establish a parameter mapping relationship with the target variable from the parameter assignment link according to a preset target variable determination rule specifically includes: if the parameter assignment link contains the formal parameters of the upstream method, determining the formal parameters as the target variables used to establish a parameter mapping relationship with the target variables; if the parameter assignment link does not contain the formal parameters of the upstream method, determining the internal variables of the upstream method contained in the parameter assignment link as the target variables used to establish a parameter mapping relationship with the target variables.
[0193] Optionally, the upstream method includes a first-level upstream method that calls the method to be tested and a second-level upstream method that calls the first-level upstream method; the target variable determination unit is specifically used to: analyze the first-level upstream method to determine the first-level target variable in the first-level upstream method that is assigned to the target parameter in the method to be tested; analyze the second-level upstream method to determine the second-level target variable in the second-level upstream method that is assigned to the first-level target variable in the first-level upstream method.
[0194] Optionally, the judgment module 206 is specifically used to: judge whether the first-layer target variable is allowed to be a null value, and obtain a first-layer judgment result; the test case generation module 208 is specifically used to: if the first-layer judgment result indicates that the first-layer target variable is not allowed to be a null value, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value.
[0195] Optionally, the test case generation module 208 is specifically used to: if the first-layer judgment result indicates that the first-layer target variable is allowed to be a null value, then judge whether the second-layer target variable is allowed to be a null value to obtain a second-layer judgment result; if the second-layer judgment result indicates that the second-layer target variable is not allowed to be a null value, then generate a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value.
[0196] Optionally, the upstream method specifically includes upstream methods corresponding to at least two calling paths of the method to be tested; the judgment module 206 is specifically used to: judge whether the target variable corresponding to each calling path in the at least two calling paths is allowed to be a null value; the test case generation module 208 is specifically used to: if the target variable corresponding to each calling path in the at least two calling paths is not allowed to be a null value, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value.
[0197] Optionally, the target variable is a target formal parameter in the upstream method; the judgment module 206 is specifically used to: judge whether there is specified annotation information for the target formal parameter in the upstream method, and obtain a first judgment result; the specified annotation information is used to indicate that the target formal parameter is not allowed to be a null value; the test case generation module 208 is specifically used to: if the first judgment result indicates that there is specified annotation information for the target formal parameter, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value.
[0198] Optionally, the specified annotation information includes at least one of @NonNull, @NotNull, @NotBlank, @Valid and @Validated.
[0199] Optionally, the target variable is a target formal parameter in the upstream method; the judgment module 206 is specifically used to: judge whether in the upstream method, a first verification instruction exists before the target calling instruction used to call the method to be tested, and obtain a second judgment result; the first verification instruction is used to verify that the value of the target formal parameter is not an empty value; the test case generation module 208 is specifically used to: if the second judgment result indicates that there is a first verification instruction before the target calling instruction used to call the method to be tested, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-empty value.
[0200] Optionally, the target variable is a target local variable defined in the upstream method; the judgment module 206 is specifically used to: judge whether there is an initialization instruction in the upstream method before the target call instruction used to call the method to be tested, and obtain a third judgment result; the initialization instruction is used to indicate that an initialization operation is performed on the target local variable; the test case generation module 208 is specifically used to: if the third judgment result indicates that there is an initialization instruction before the target call instruction used to call the method to be tested, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value.
[0201] Optionally, the judgment module 206 is specifically used to: identify the initialization instruction before the target call instruction for calling the method to be tested in the upstream method; determine the initialized local variables corresponding to the initialization instruction; and determine whether the target local variables are included in the initialized local variables.
[0202] Optionally, the target variable is a target local variable defined in the upstream method; the judgment module 206 is specifically used to: judge whether in the upstream method, there is a target method calling instruction before the target calling instruction used to call the method to be tested, to obtain a fourth judgment result; the target method calling instruction is used to assign the return value of the called method to the target local variable and the return value does not contain a null value; the test case generation module 208 is specifically used to: if the fourth judgment result indicates that there is a target method calling instruction before the target calling instruction used to call the method to be tested, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-null value.
[0203] Optionally, the judgment module 206 is specifically used to: identify the method calling instruction in the upstream method before the target calling instruction for calling the method to be tested; the method calling instruction is used to assign the return value of the called method to the target local variable; the test case generation module 208 is specifically used to: if the fourth judgment result indicates that there is a target method calling instruction before the target calling instruction for calling the method to be tested, then generate a test case for testing the method to be tested that includes test data setting the target parameter to a non-null value, specifically including: if the return value judgment result indicates that the return value of the called method does not contain a null value, then generate a test case for testing the method to be tested that includes test data setting the target parameter to a non-null value.
[0204] Optionally, the target variable is a target local variable defined in the upstream method; the judgment module 206 is specifically used to: judge whether in the upstream method, a second verification instruction exists before the target calling instruction used to call the method to be tested, and obtain a fifth judgment result; the second verification instruction is used to verify that the value of the target local variable is not an empty value; the test case generation module 208 is specifically used to: if the fifth judgment result indicates that there is a second verification instruction before the target calling instruction used to call the method to be tested, then generate a test case for testing the method to be tested that contains test data that sets the target parameter to a non-empty value.
[0205] It is understood that the above modules refer to computer programs or program segments for executing one or more specific functions. In addition, the distinction between the above modules does not mean that the actual program codes must also be separated.
[0206] The above is a schematic scheme of a device for generating test cases in this embodiment. It should be noted that the technical scheme of the device for generating test cases and the technical scheme of the method for generating test cases described above belong to the same concept, and the details not described in detail in the technical scheme of the device for generating test cases can be found in the description of the technical scheme of the method for generating test cases described above.
[0207] Based on the same idea, the embodiments of this specification also provide a device corresponding to the above method.
[0208] Figure 3 The embodiments of this specification provide corresponding to Figure 1 A schematic diagram of the structure of a device for generating test cases.
[0209] like Figure 3 As shown, the device 300 may include:
[0210] at least one processor 310; and,
[0211] A memory 330 is communicatively connected to the at least one processor; wherein,
[0212] The memory 330 stores instructions 320 executable by the at least one processor 310. The instructions are executed by the at least one processor 310 to enable the at least one processor 310 to:
[0213] Get the calling relationship information of the method to be tested;
[0214] Determine, according to the call relationship information, a target variable in an upstream method that calls the method to be tested; the value of the target variable is used to assign a value to a target parameter in the method to be tested;
[0215] Determine whether the target variable is allowed to be a null value, and obtain a determination result;
[0216] If the judgment result indicates that the target variable is not allowed to be a null value, a test case for testing the method to be tested is generated, which includes test data for setting the target parameter to a non-null value.
[0217] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical scheme of the method for generating test cases described above are of the same concept, and the details not described in detail in the technical scheme of the computing device can be found in the description of the technical scheme of the method for generating test cases described above.
[0218] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other. Each embodiment focuses on the differences from other embodiments. In particular, for the device and equipment embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments. The devices, equipment and methods provided in the embodiments of this specification correspond to each other, so the devices and equipment also have beneficial technical effects similar to the corresponding methods. Since the beneficial technical effects of the methods have been described in detail above, the beneficial technical effects of the corresponding devices and equipment will not be repeated here.
[0219] The above describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0220] In the 1990s, improvements to a technology could be clearly distinguished as hardware improvements (for example, improvements to the circuit structure of diodes, transistors, switches, etc.) or software improvements (improvements to the method flow). However, with the development of technology, many improvements to the method flow today can be regarded as direct improvements to the hardware circuit structure. Designers almost always obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that an improvement in a method flow cannot be implemented using a hardware entity module. For example, a programmable logic device (PLD) (such as a field programmable gate array (FPGA)) is such an integrated circuit whose logical function is determined by the user's programming of the device. Designers can "integrate" a digital system on a PLD by programming it themselves, without having to ask a chip manufacturer to design and produce a dedicated integrated circuit chip. Moreover, nowadays, instead of manually making integrated circuit chips, this kind of programming is mostly implemented by "logic compiler" software, which is similar to the software compiler used when developing and writing programs, and the original code before compilation must also be written in a specific programming language, which is called hardware description language (HDL). There is not only one HDL, but many kinds, 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 should also know that it is only necessary to program the method flow slightly in the above-mentioned hardware description languages and program it into the integrated circuit, and then it is easy to obtain the hardware circuit that implements the logic method flow.
[0221] The controller can be implemented in any appropriate manner, for example, the controller can take the form of a microprocessor or processor and a computer-readable medium storing a computer-readable program code (such as software or firmware) that can be executed by the (micro)processor, a logic gate, a switch, an application-specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller. Examples of controllers include, but are not limited to, the following microcontrollers: ARC 625D, Atmel 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 also know that in addition to implementing the controller in a purely computer-readable program code manner, the controller can be implemented in the form of a logic gate, a switch, an application-specific integrated circuit, a programmable logic controller, and an embedded microcontroller by logically programming the method steps. Therefore, this controller can be considered as a hardware component, and the devices included therein for implementing various functions can also be regarded as structures within the hardware component. Or even, the devices for implementing various functions can be regarded as both software modules for implementing the method and structures within the hardware component.
[0222] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with 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 smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0223] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0224] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0225] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0226] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0227] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0228] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0229] The 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. The memory is an example of a computer-readable medium.
[0230] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. 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 disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, 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 temporary computer readable media (transitory media), such as modulated data signals and carrier waves.
[0231] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0232] The present application may be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present application may also be practiced in distributed computing environments where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.
[0233] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.
Claims
1. A method for generating a test case, comprising: Get the calling relationship information of the method to be tested; Determine, according to the call relationship information, a target variable in an upstream method that calls the method to be tested; The value of the target variable is used to assign a value to a target parameter in the method to be tested; Determine whether the target variable is allowed to be a null value, and obtain a determination result; If the judgment result indicates that the target variable is not allowed to be a null value, a test case for testing the method to be tested is generated, which includes test data for setting the target parameter to a non-null value.
2. The method according to claim 1, wherein determining the target variable in the upstream method that calls the method to be tested according to the call relationship information specifically comprises: Determine, according to the calling relationship information, an upstream method that calls the method to be tested; The upstream method is analyzed to determine a target variable in the upstream method that is assigned to a target parameter in the method to be tested.
3. The method according to claim 2, wherein determining the upstream method that calls the method to be tested according to the call relationship information specifically comprises: Determining the calling method of the method to be tested according to the calling relationship information; Determine whether the class where the calling method is located is an anonymous inner class, and obtain an anonymous inner class determination result; If the anonymous inner class judgment result indicates that the class where the calling method is located is an anonymous inner class, then determining the main class corresponding to the anonymous inner class; A method using the anonymous inner class in at least one method corresponding to the main class is determined as an upstream method that calls the method to be tested.
4. The method according to claim 2, wherein determining the upstream method that calls the method to be tested according to the call relationship information specifically comprises: Determining the calling method of the method to be tested according to the calling relationship information; Determine whether the calling method is a method represented by a Lambda expression, and obtain a Lambda expression determination result; If the Lambda expression judgment result indicates that the calling method is a method represented by a Lambda expression, an upstream method for calling the method to be tested is determined according to the Lambda expression.
5. The method according to claim 2, wherein analyzing the upstream method to determine the target variable in the upstream method assigned to the target parameter in the method to be tested specifically comprises: Determine, by analyzing instructions in the upstream method, a parameter assignment link in the upstream method that is assigned to a target parameter in the method to be tested; According to a preset target variable determination rule, a target variable for establishing a parameter mapping relationship with the target variable is determined from the parameter assignment link.
6. The method according to claim 5, wherein determining the target variable for establishing a parameter mapping relationship with the target variable from the parameter assignment link according to a preset target variable determination rule specifically comprises: If the parameter assignment link includes a formal parameter of the upstream method, the formal parameter is determined as a target variable for establishing a parameter mapping relationship with the target variable; If the parameter assignment link does not contain the formal parameters of the upstream method, the internal variables of the upstream method contained in the parameter assignment link are determined as target variables for establishing a parameter mapping relationship with the target variables.
7. The method of claim 2, wherein: The upstream method includes calling a first-layer upstream method of the method to be tested and calling a second-layer upstream method of the first-layer upstream method; analyzing the upstream method to determine a target variable in the upstream method that is assigned to a target parameter in the method to be tested specifically includes: Analyze the first-layer upstream method to determine a first-layer target variable in the first-layer upstream method that is assigned to a target parameter in the method to be tested; The second-layer upstream method is analyzed to determine a second-layer target variable in the second-layer upstream method that is assigned to a first-layer target variable in the first-layer upstream method.
8. The method according to claim 7, wherein the step of determining whether the target variable is allowed to be a null value and obtaining a determination result specifically comprises: Determine whether the first-layer target variable is allowed to be a null value, and obtain a first-layer determination result; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the first-layer judgment result indicates that the first-layer target variable is not allowed to be a null value, a test case for testing the method to be tested is generated, which contains test data that sets the target parameter to a non-null value.
9. The method according to claim 8, wherein if the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprises: If the first-layer judgment result indicates that the first-layer target variable is allowed to be a null value, then judging whether the second-layer target variable is allowed to be a null value, and obtaining a second-layer judgment result; If the second-layer judgment result indicates that the second-layer target variable is not allowed to be a null value, a test case for testing the method to be tested is generated, which contains test data that sets the target parameter to a non-null value.
10. The method according to claim 1, wherein the upstream method specifically comprises upstream methods corresponding to at least two calling paths of the method to be tested; The determining whether the target variable is allowed to be a null value and obtaining a determination result specifically includes: Determine whether the target variable corresponding to each calling path in the at least two calling paths is allowed to be a null value; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the target variables corresponding to each of the at least two calling paths are not allowed to be null values, a test case for testing the method to be tested is generated, which includes test data for setting the target parameters to non-null values.
11. The method of claim 1, wherein the target variable is a target formal parameter in the upstream method; The determining whether the target variable is allowed to be a null value and obtaining a determination result specifically includes: Determine whether there is specified annotation information for the target formal parameter in the upstream method, and obtain a first determination result; The specified annotation information is used to indicate that the target formal parameter is not allowed to be a null value; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the first judgment result indicates that there is specified annotation information for the target formal parameter, a test case for testing the method to be tested is generated, which contains test data for setting the target parameter to a non-null value.
12. The method of claim 11, wherein: The specified annotation information includes at least one of @NonNull, @NotNull, @NotBlank, @Valid and @Validated.
13. The method of claim 1, wherein the target variable is a target formal parameter in the upstream method; The determining whether the target variable is allowed to be a null value and obtaining a determination result specifically includes: Determine whether, in the upstream method, a first verification instruction exists before a target call instruction for calling the method to be tested, to obtain a second determination result; The first verification instruction is used to verify that the value of the target formal parameter is not a null value; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the second judgment result indicates that a first verification instruction exists before the target calling instruction for calling the method to be tested, a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value is generated.
14. The method of claim 1, wherein the target variable is a target local variable defined in the upstream method; The determining whether the target variable is allowed to be a null value and obtaining a determination result specifically includes: Determine whether, in the upstream method, an initialization instruction exists before a target call instruction for calling the method to be tested, to obtain a third determination result; the initialization instruction is used to instruct to perform an initialization operation on the target local variable; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the third judgment result indicates that an initialization instruction exists before the target calling instruction for calling the method to be tested, a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value is generated.
15. The method according to claim 14, wherein the determining whether, in the upstream method, an initialization instruction exists before a target call instruction for calling the method to be tested specifically comprises: Identifying an initialization instruction in the upstream method before a target call instruction for calling the method under test; Determine the initialization local variable corresponding to the initialization instruction; It is determined whether the initialized local variables include the target local variable.
16. The method of claim 1, wherein the target variable is a target local variable defined in the upstream method; The determining whether the target variable is allowed to be a null value and obtaining a determination result specifically includes: Determine whether, in the upstream method, there is a target method calling instruction before the target calling instruction for calling the method to be tested, to obtain a fourth judgment result; the target method calling instruction is used to assign the return value of the called method to the target local variable and the return value does not contain a null value; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the fourth judgment result indicates that a target method calling instruction exists before the target calling instruction for calling the method to be tested, a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value is generated.
17. The method according to claim 16, wherein the determining, in the upstream method, whether there is a target method calling instruction before the target calling instruction for calling the method to be tested, to obtain a fourth determination result specifically comprises: identifying a method call instruction in the upstream method that precedes a target call instruction for calling the method under test; The method call instruction is used to assign the return value of the called method to the target local variable; Determine whether the return value of the called method contains a null value, and obtain a return value determination result; If the fourth judgment result indicates that there is a target method calling instruction before the target calling instruction for calling the method to be tested, generating a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value, specifically comprising: If the return value judgment result indicates that the return value of the called method does not include a null value, a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value is generated.
18. The method of claim 1, wherein the target variable is a target local variable defined in the upstream method; The determining whether the target variable is allowed to be a null value and obtaining a determination result specifically includes: Determine whether, in the upstream method, a second verification instruction exists before a target call instruction for calling the method to be tested, to obtain a fifth determination result; The second verification instruction is used to verify that the value of the target local variable is not a null value; If the judgment result indicates that the target variable is not allowed to be a null value, generating a test case for testing the method to be tested that includes test data that sets the target parameter to a non-null value, specifically comprising: If the fifth judgment result indicates that a second verification instruction exists before the target calling instruction for calling the method to be tested, a test case for testing the method to be tested that includes test data for setting the target parameter to a non-null value is generated.
19. A device for generating a test case, comprising: A call relationship information acquisition module is used to obtain the call relationship information of the method to be tested; A target variable determination module, used to determine the target variable in the upstream method that calls the method to be tested according to the call relationship information; The value of the target variable is used to assign a value to a target parameter in the method to be tested; A judgment module is used to judge whether the target variable is allowed to be a null value and obtain a judgment result; A test case generation module is used to generate a test case for testing the method to be tested, which contains test data that sets the target parameter to a non-null value if the judgment result indicates that the target variable is not allowed to be a null value.
20. A device for generating a test case, comprising: at least one processor; as well as, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to: Get the calling relationship information of the method to be tested; Determine, according to the call relationship information, a target variable in an upstream method that calls the method to be tested; the value of the target variable is used to assign a value to a target parameter in the method to be tested; Determine whether the target variable is allowed to be a null value, and obtain a determination result; If the judgment result indicates that the target variable is not allowed to be a null value, a test case for testing the method to be tested is generated, which includes test data for setting the target parameter to a non-null value.