Method, device and equipment for generating test case
By obtaining code unit variable information, using a large language model to generate test data that conforms to business semantic characteristics, and iteratively mutate through genetic search algorithms, the problem that test cases in the existing technology cannot accurately adapt to business logic is solved, and the reliability and accuracy of code unit tests are improved.
Patent Information
- Application Number
- CN202510541857.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-27
- Publication Date
- 2025-08-08
AI Technical Summary
The test cases generated by the existing technology cannot accurately adapt to business logic, resulting in poor code unit testing.
By obtaining variable information in the code unit, a large language model is used to generate test data that conforms to the business semantic characteristics of the variable reference value, and iterate through the genetic search algorithm to construct test cases.
The generated test cases are more in line with real business scenarios and improve the reliability and accuracy of code unit testing.
Smart Images

Figure CN120448269A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a method, apparatus, and device for generating test cases. Background Art
[0002] During software development, the correctness and stability of code directly impact the reliability of the software system. Unit testing verifies the correctness of code logic early in software development by designing test cases for code units (such as functions and classes). However, test cases generated using existing techniques cannot accurately adapt to business logic, resulting in poor testing results.
[0003] Based on this, how to provide a method for generating test cases to improve the accuracy of code unit testing has become a technical problem that needs to be solved urgently. 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 that the test cases generated by the existing methods for generating test cases cannot accurately test code units.
[0005] According to a first aspect of an embodiment of the present application, a method for generating a test case is provided, comprising:
[0006] Get the code unit to be tested;
[0007] Determining variable information of a variable in the code unit; the variable information includes a variable name and a variable reference value; the variable reference value is derived from historical business data of the variable;
[0008] Filling the variable information into a preset prompt word template to obtain a prompt word for input into a large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that meets the business semantic characteristics reflected by the variable reference value;
[0009] Calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference;
[0010] Constructing a seed test case based on the first seed test data;
[0011] A genetic search algorithm is used to iteratively mutate the seed test case to obtain a test case for testing the code unit.
[0012] According to a second aspect of an embodiment of the present application, there is provided an apparatus for generating a test case, comprising:
[0013] A code unit acquisition module is used to obtain the code unit to be tested;
[0014] A variable information determination module, configured to determine variable information of a variable in the code unit; the variable information includes a variable name and a variable reference value; the variable reference value is derived from historical business data of the variable;
[0015] a prompt word generation module, configured to fill the variable information into a preset prompt word template to obtain a prompt word for input into a large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that conforms to the business semantic characteristics reflected by the variable reference value;
[0016] a third acquiring unit, configured to call the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference;
[0017] A seed test data generating module, configured to construct a seed test case based on the first seed test data;
[0018] The seed test case construction module is used to iteratively mutate the seed test case using a genetic search algorithm to obtain a test case for testing the code unit.
[0019] According to a third aspect of an embodiment of the present application, a computing device is provided, comprising a memory, a processor, and computer instructions stored in the memory and executable on the processor, wherein the processor implements the steps of the test case generating method when executing the computer instructions.
[0020] One embodiment of the present specification can achieve at least the following beneficial effects: by obtaining a code unit to be tested; determining variable information of a variable in the code unit, where the variable information may include a variable name and a variable reference value; wherein the variable reference value is derived from historical business data of the variable; filling the variable information into a preset prompt word template to obtain a prompt word including task information indicating that the large language model outputs content that conforms to the business semantic characteristics reflected by the variable reference value; calling the large language model based on the prompt word to obtain first seed test data with the variable reference value as a reference; constructing a seed test case based on the first seed test data; iteratively mutating the seed test case to obtain a test case for testing the code unit; because the variable reference value is determined based on the actual data of the variable actually recorded, the first seed test data obtained using the large language model based on the variable reference value is more consistent with the real business scenario and real business semantics, thereby making the test case obtained based on the first seed test data more consistent with the real business scenario, and thus making the test result of the code unit tested using the test case better reflect the performance of the code unit in actual application, thereby enhancing the reliability of the test. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] In order to more clearly illustrate the embodiments of this specification or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. 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 any creative labor.
[0022] Figure 1 This is a schematic diagram of an application scenario of a method for generating test cases provided in an embodiment of this specification;
[0023] Figure 2 This is a flow chart of a method for generating a test case provided by an embodiment of this specification;
[0024] Figure 3 This is a schematic diagram of the overall process of generating a test case provided by an embodiment of this specification;
[0025] Figure 4 This is a schematic diagram of the structure of a device for generating test cases provided in an embodiment of this specification;
[0026] Figure 5 This is a structural block diagram of a device for generating test cases provided in an embodiment of this specification. DETAILED DESCRIPTION
[0027] The following description sets forth many specific details to facilitate a thorough 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 scope of the present application. Therefore, the present application is not limited to the specific implementations disclosed below.
[0028] The terms used in one or more embodiments of the present application are for the purpose of describing specific embodiments only and are not intended to limit one or more embodiments of the present application. The singular forms "a", "the" 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 otherwise. 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.
[0029] 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, such information should not be limited to these terms. These terms are only used to distinguish information of the same type 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".
[0030] First, the terms involved in one or more embodiments of the present application are explained.
[0031] Unit Testing: Unit testing focuses on verifying the behavior of the smallest testable part of a software program (typically a function, method, or module). The goal of unit testing is to ensure that each individual unit works as expected, independent of other parts. Through unit testing, developers can quickly identify and fix issues, while improving code quality and maintainability.
[0032] Test cases are an integral part of software testing. A test case is a documented set of conditions, input data, execution steps, and expected results used to execute specific test objectives or verify specific software functionality. Test cases help testers clarify the scope, approach, and expected outputs of testing to ensure that the software functions according to the requirements specification. Test cases help ensure comprehensiveness, repeatability, and traceability of testing, improving testing efficiency and software quality.
[0033] During software development, the correctness and stability of code directly impact the reliability of the software system. Traditional testing methods (such as manual testing or system-level testing) are typically performed late in development, delaying defect discovery and significantly increasing repair costs. Unit testing, by designing test cases for the smallest code units (such as functions and classes), can verify logical correctness early in development, covering normal, edge, and abnormal scenarios.
[0034] Existing Java unit test case generation methods based on traditional genetic search algorithms have achieved promising results in the industry, achieving 70% line coverage and excelling in automated test case generation. However, test cases generated using traditional genetic search algorithms suffer from poor readability. These problems include insufficient semantics in test data, meaningless test case names, and a lack of test case annotations to clarify test scenarios. These issues hinder developers' understanding of test cases and their maintenance efficiency.
[0035] In order to solve the defects in the related art, this solution provides the following embodiments.
[0036] The technical solutions provided by the embodiments of this specification are described in detail below with reference to the accompanying drawings.
[0037] Figure 1 A schematic diagram of an application scenario of a method for generating test cases provided in an embodiment of this specification.
[0038] like Figure 1 As shown, there are a server 101, a large language model 102, and a device 103 storing real data. The server 101 can be a server for generating test cases. The server 101 can obtain the code unit that needs to be tested, analyze the code unit and determine the variables in the code unit. In addition, the server 101 can also call the device 103 to determine the true value of the variable based on the historical business data of the variable stored in the device 103, so as to use the true value of the variable as the variable reference value. After the server 101 determines the variable reference value in the code unit, it can also fill the variable reference value, variable name and other data into the preset prompt word template to obtain the prompt word, and call the large language model 102 based on the obtained prompt word, and use the large language model to output the first seed test data with the variable reference value as a reference. After the large language model outputs the first seed test data, the server can also use the first seed test data to construct a seed test case, and then iteratively mutate the seed test case to obtain a test case for testing the code unit. In such a case Figure 1 In the illustrated scenario, the large language model 102 may be hosted on the server 101 or on another server in communication with the server 101. The device 103 storing real data may be a database or a business system, which is not specifically limited herein.
[0039] In such Figure 1 In the application scenario shown, the server may be connected to one or more terminal devices via a local area network connection, a wide area network connection, an Internet connection, or other types of data networks. Figure 1 The server may include but is not limited to any device, equipment, platform, equipment cluster, etc. with computing and processing capabilities.
[0040] 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.
[0041] Figure 2 A flowchart of a method for generating test cases provided in an embodiment of this specification.
[0042] From the program perspective, the execution body of the process can be a program installed on an application server or application terminal. It is understood that the method can be executed by any device, equipment, platform, or device cluster with computing and processing capabilities.
[0043] like Figure 2 As shown, the process may include the following steps:
[0044] Step 202: Obtain the code unit to be tested.
[0045] In the embodiments of this specification, a code unit can be a code snippet that needs to be tested during the software development process. A code unit can include objects that can be unit tested, such as functions, methods, classes, and modules to be tested.
[0046] In the embodiments of this specification, unit testing may refer to the process of checking and verifying the smallest testable unit (usually a function, method or module) in the software. Unit testing can make each independent unit work as expected without relying on other parts. Through unit testing, developers can quickly locate and fix problems for testing, while improving the quality and maintainability of the code. Among them, the unit in the unit test can refer to a function in C language, a class in Java, a window or a menu in graphical software, etc. In general, a unit can refer to the smallest functional module under test.
[0047] In the embodiments of this specification, a function or method to be tested may refer to a block of code used to perform a specific calculation or operation. A class to be tested may refer to a class with clear responsibilities in object-oriented programming. A module to be tested may be a fully functional subsystem or business component.
[0048] Step 204: Determine variable information of the variable in the code unit; the variable information includes a variable name and a variable reference value; the variable reference value is derived from historical business data of the variable.
[0049] In the embodiments of this specification, variables in a code unit can be identified by parsing the code unit. Variables can include inputs, outputs, and intermediate variables involved in the code unit. For example, in the calculate_order_total(price, quantity, discount) code unit, variables can include price, quantity, and discount.
[0050] In the embodiments of this specification, the variable reference value in the variable information may be the actual value of the variable extracted from the historical business data of the variable. Specifically, the variable reference value may include a typical value or representative value of the variable; or it may be any value of the variable extracted from the historical business data.
[0051] In the embodiments of this specification, variable reference values are determined based on the actual data of the variables that are actually recorded. This makes the test data constructed based on variable information more realistic to real business scenarios. This can, on the one hand, trigger edge cases and abnormal logic, thereby improving coverage. On the other hand, it can also make the test results more reflective of the performance of the code unit in actual applications, thereby enhancing the reliability of the test.
[0052] Step 206: Fill the variable information into a preset prompt word template to obtain a prompt word for input into the large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that meets the business semantic characteristics reflected by the variable reference value.
[0053] Large Language Models (LLMs), also known as large language models or large models, are natural language processing technologies based on deep learning. They can better understand natural language and generate high-quality text based on a given context. Examples include models like GPT-4, Claude, LLaMA, or Qwen.
[0054] In prompt engineering, a prompt is a piece of content that can be used to guide a large language model in generating a specific response. In the embodiments of this specification, when generating a prompt, a prompt template can be first obtained, and then variable information can be inserted into the prompt template to generate the prompt. The prompt template is a structured, reusable prompt design framework.
[0055] In the embodiments of this specification, business semantic features may specifically include business attribute features, data structure features, etc. Accordingly, the content that conforms to the business semantic features reflected by the variable reference value may specifically include content that is consistent with the business attributes, data structure, etc. represented by the historical business data.
[0056] In actual applications, task information may be information indicating a specific task that the user wants the large language model to complete. In the embodiment of this specification, task information may specifically be information indicating that the large language model outputs test data with reference to the variable reference value. Specifically, task information may be information indicating that the large language model outputs content that is consistent with the business attributes, data structure, etc. represented by the variable reference value. For example, task information may include "generate 5 suitable value data for each variable name of type string". For another example, task information may include "the 5 value data must have the following characteristics: 1. The business semantics are real, and real data related to the business scenario is used, with real business semantics; 2. Camel case naming is used, and the first letter is lowercase; 3. Desensitized data is prohibited;".
[0057] As an implementation method, the prompt word may also include role information, which is a key element in the prompt word composition. The role information may be information representing the role of the large language model. In the embodiment of this specification, the role information may refer to the occupation or identity that the user wants the large language model to play when generating variable values. The role information can enable the large language model to better simulate the behavior and language habits of a specific occupation or identity, thereby enhancing the professionalism and credibility of the content generated by the large language model. For example, in the embodiment of this specification, the role information may be "You are an experienced test engineer."
[0058] Step 208: Call the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference.
[0059] In the embodiments of this specification, prompt words are input into a large language model. Based on this large language model, a large amount of first seed test data that aligns with real business scenarios and reflects real business semantics can be obtained. This allows the test cases generated based on the first seed test data to better align with real business scenarios. Consequently, the test results of the code units tested using the test cases better reflect the performance of the code units in actual applications, thereby enhancing the reliability of the tests.
[0060] Step 210: Construct a seed test case based on the first seed test data.
[0061] In the embodiment of this specification, the first seed test data is an important content in the seed test case. Specifically, the seed test case can be generated based on the first seed test data, test steps and expected results.
[0062] Seed test cases can be used as original test cases to generate test cases for testing code units.
[0063] Step 212: Adopting a genetic search algorithm to iteratively mutate the seed test case to obtain a test case for testing the code unit.
[0064] In the embodiments of this specification, a test case can be a document containing conditions, input data, execution steps, and expected results used in software testing to execute specific test objectives or verify specific software functions. Test cases are an indispensable part of software testing. Test cases can 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 can help ensure the comprehensiveness, repeatability, and traceability of the test, thereby improving test efficiency and software quality.
[0065] In practical applications, iterative mutation is an automated technology that gradually generates more effective test cases by cyclically adjusting and optimizing initial input data. The essence of iterative mutation is to simulate the "mutation-selection-adaptation" process in biological evolution. In the embodiments of this specification, iterative mutation is performed on seed test cases, so that the test cases obtained through iterative mutation can expand the test coverage, such as boundary values and abnormal scenarios; expose potential defects, such as type errors and logical loopholes; and optimize test efficiency, such as reducing redundant test cases.
[0066] 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.
[0067] Figure 2 The method comprises obtaining a code unit to be tested; determining variable information of a variable in the code unit, wherein the variable information may include a variable name and a variable reference value; wherein the variable reference value is derived from historical business data of the variable; filling the variable information into a preset prompt word template to obtain a prompt word including task information indicating that the output of the large language model conforms to the business semantic characteristics reflected by the variable reference value; calling the large language model based on the prompt word to obtain first seed test data using the variable reference value as a reference; constructing a seed test case based on the first seed test data; and iteratively mutating the seed test case to obtain a test case for testing the code unit. Because the variable reference value is determined based on the actual data of the variable actually recorded, the first seed test data obtained using the large language model based on the variable reference value is more consistent with real business scenarios and real business semantics. As a result, the test case obtained based on the first seed test data is more consistent with real business scenarios, and the test results of the code unit tested using the test case can better reflect the performance of the code unit in actual applications, thereby enhancing the reliability of the test.
[0068] based on Figure 2 The present specification also provides some improved implementation methods of the method, which are described below.
[0069] In one or more embodiments of this specification, the code unit to be tested generally involves a variety of different types of variables, such as integers, floating-point types, Booleans, and strings. A large language model can be used to generate test data corresponding to string-type variables. String-type variables can include non-numeric string variables and numeric string variables. To improve the accuracy of the test data for string-type variables generated by the large language model, different methods can be used to generate test data for non-numeric string variables and numeric string variables.
[0070] Optionally, the variable specifically includes a non-numeric string variable; filling the variable information into a preset prompt word template to obtain a prompt word for input into the large language model may specifically include: filling the variable information into a first prompt word template for generating a variable value of a non-numeric string variable to obtain a first prompt word; the first prompt word contains first task information; the first task information is used to instruct the large language model to generate the variable value of the variable according to a first constraint condition; the first constraint condition is used to represent the characteristics that the variable value of the variable should conform to.
[0071] Calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference may specifically include: inputting the first prompt word into the large language model to obtain first seed test data of the variable generated by the large language model with reference to the variable reference value under the constraint of the first constraint condition.
[0072] In the embodiments of this specification, a non-numeric string variable may be a string containing at least one character other than a numeric character. As an implementation method, a non-numeric string variable may or may not contain numeric characters.
[0073] The first prompt word template can be a prompt word template for generating a variable value for a non-numeric string variable. A variable name (e.g., variableName) and a variable reference value (e.g., realValue) can be inserted into the first prompt word model to generate a first prompt word containing the variable name and the variable reference value.
[0074] The first task information, similar to the task information described above, may be information indicating a specific task that the user wishes the large language model to complete. In the embodiments of this specification, the first task information may be variable values indicating output variables of the large language model. Specifically, the first task information may be variable values indicating that the large language model generates variables according to the first constraint.
[0075] In the embodiment of this specification, the first constraint condition may include characteristics that the variable value of the variable should comply with.
[0076] In this embodiment, the variable value of the variable is generated by the large language model under the constraint of the first constraint condition with reference to the variable reference value. In practical applications, at least part of the variable value output by the large language model can be used as the first seed test data of the variable.
[0077] As an implementation method, the large language model may further output variable names, and the variable names output by the large language model may be the same as the variable names in the prompt words input to the large language model.
[0078] The variable values generated by the large language model using the reference variable values are more consistent with the business attributes and data structure reflected by the variables. This makes the test cases built based on the variable values more consistent with the business scenarios of the code units, thereby improving the accuracy of code unit testing.
[0079] For ease of understanding, the specific content of the first constraint condition is also provided in the embodiments of this specification.
[0080] Optionally, the first constraint condition includes at least one of a semantic authenticity condition, a naming rule condition and a non-massifying condition; the semantic authenticity condition is used to constrain the generated variable value to have a value that matches the business scenario; the naming rule condition is used to constrain the generated variable value to comply with a preset naming rule; the non-massifying condition is used to constrain the generated variable value to not be masked.
[0081] In the embodiments of this specification, the semantic authenticity condition may be a requirement that the variable values generated by the large language model must match the business scenario. For example, the semantic authenticity condition may be "using data related to the business scenario and having real business semantics."
[0082] The naming rule condition may be a requirement that the variable value output by the large language model conforms to a preset naming rule. Optionally, the naming rule may include a camel case naming rule. Among them, the camel case naming rule is a naming convention commonly used in programming, which enhances the readability of the code by mixing uppercase and lowercase letters. The camel case naming rule may specifically include lower camel case naming (Lower Camel Case or Upper Camel Case / Pascal Case). In the lower camel case naming method, the first letter of the first word is lowercase, the first letter of each subsequent word is uppercase, and the remaining letters are lowercase. No separators (such as spaces or underscores) are used between words, for example, myFirstName, calculateTotalPrice, etc. In the upper camel case naming method, the first letter of each word is uppercase (including the first word), and the remaining letters are lowercase. No separators (such as spaces or underscores) are used between words, for example, StudentRecord, DataProcessor, etc. In the embodiments of this specification, the camel case naming rule can be used to constrain the format of the generated non-numeric character string so that the generated non-numeric character string conforms to the actual business characteristics.
[0083] The non-masking condition can require that the variable values generated by the large language model are unmasked data to prevent masking, deformation, or other masking processes from rendering the data unusable for subsequent test cases. For example, a variable value can be id12345, but not id12**5.
[0084] In the embodiment of this specification, by using the first constraint condition to constrain the large language model, the variable values generated by the large language model can have real business semantics to meet the business scenarios of the code unit, thereby improving the accuracy of the results of the code unit test.
[0085] In order to further improve the accuracy of the variable values output by the large language model, the variable information can also include variable annotations.
[0086] Optionally, the variable information may also include variable annotations; calling the large language model based on the prompt word to obtain the first seed test data of the variable with the variable reference value as a reference may specifically include: inputting the first prompt word into the large language model, and obtaining the first seed test data of the variable generated by the large language model with reference to the variable reference value and the variable annotation under the constraint of the first constraint condition.
[0087] In the embodiments of this specification, variable annotations may be content that explains variables. In the Java programming language, comments are an important tool for adding descriptions and annotations to code. Specifically, variable annotations may include single-line comments, multi-line comments, Javadoc comments, and the like. Among them, single-line comments begin with double slashes ( / / ) and are used to annotate a single line of code or a variable. For example, / / This is a single-line comment int x=5. Another example, / / Here an integer variable x is declared with an initial value of 5. Multi-line comments begin with / * and end with * / and are used to annotate multiple lines of code or variables. For example, / *This is a multi-line comment, where an integer array is declared to store student grades* / . Javadoc comments begin with @ and are used to generate API documentation. They are usually used before the definition of classes, methods, fields, etc. to describe their functions, parameters, and other information. In the embodiments of this specification, variable annotation information can be provided to the large language model to constrain the generated non-numeric strings so that the generated non-numeric strings meet the requirements of actual business scenarios.
[0088] In the embodiments of this specification, the variable information includes variable annotations, so that the large language model can analyze the data structure of the variable reference value or the characters that the data should include, etc. in combination with the variable reference value itself and the variable annotations, thereby making the variable value output by the large language model more accurate.
[0089] In order to ensure that the variable values output by the large language model are the ones output by the large language model after careful consideration, and to improve the standardization of the output of the large language model, in the embodiments of this specification, the output format of the large language model can also be set.
[0090] Optionally, the first prompt word template may further include first output format information; the first output format information may be used to instruct the large language model to output at least one of variable semantic information and variable structure information; wherein, the variable semantic information may be used to represent the business semantics of the variable; the variable structure information may be used to represent the data structure of the variable.
[0091] In the embodiments of this specification, the variable semantic information may refer to the business semantics of the variable. Specifically, the variable semantic information may refer to the meaning represented by the variable. Suppose the variable information in the prompt word input to the large language model is as follows: "variableName: CP; realValue: Beijing A12345; comment: This variable represents the license plate number", where variableName represents the variable name, realValue represents the variable reference value, and comment represents the variable annotation. The variable semantic information output by the large language model for the variable "CP" based on the prompt word containing the above input content may be "license plate number".
[0092] In practical applications, the variable semantic information may be generated by the large language model based on the variable reference value. Further, if the prompt word includes variable annotations, the variable semantic information may also be generated by the large language model based on the variable reference value and variable annotations.
[0093] In the embodiments of this specification, the variable structure information may be information used to represent the data structure of the variable. Specifically, the variable structure information may be used to describe the data structure of the variable. Continuing with the example in the previous text for illustration, suppose the variable information in the prompt word input to the large language model is as follows: "variableName: CPH; realValue: Beijing A12345; comment: This variable represents the license plate number", and the variable semantic information output by the large language model based on the prompt word containing the above input content may be "The data includes the abbreviation of the province, and the abbreviation is 1 Chinese character; the code of the license-issuing unit, and the code of the license-issuing unit is 1 letter, and 5 random serial numbers".
[0094] As an implementation manner, the variable structure information may be generated by the large language model based on the variable reference value. Further, if the prompt word includes variable annotations, the variable structure information may also be generated by the large language model based on the variable reference value and variable annotations.
[0095] In the embodiments of this specification, variable values can be generated by a large language model based on variable semantic information and variable structure information. The variable values generated by the large language model must conform to the variable semantic information and variable structure information. In the process of generating the variable semantic information and variable structure information, the large language model considers variable reference values and variable annotations. Therefore, the variable values output by the large language model are also carefully considered by the large language model, further improving the accuracy of the variable values and making them more consistent with business scenarios.
[0096] To facilitate understanding, an example of a first prompt word template is provided in the embodiments of this specification, which may be specifically as follows:
[0097] You are an experienced test engineer, and your task is to generate 5 appropriate value data for each String type variable name.
[0098] The five value data must have the following characteristics:
[0099] 1. Authentic business semantics: using real data related to business scenarios and possessing authentic business semantics;
[0100] 2. Use camel case naming with the first letter lowercase;
[0101] 3. The use of desensitized data is prohibited;
[0102] Input data:
[0103]
[0104] Output format:
[0105]
[0106]
[0107] In the above prompt, "You are an experienced test engineer" can be the role information of the large language model. "Generate five appropriate values for each String variable name" can be the task information of the large language model. "The five values must have the following characteristics: 1. Business semantics are authentic, using real data related to the business scenario and having real business semantics; 2. Use camel case with the first letter lowercase; 3. Do not use desensitized data" can be the first constraint. In the input data, "variableName" can represent the variable name, "comment" is the variable comment, and "realValue" is the variable reference value. The specific contents of the variable name, variable comment, and variable reference value in the above input data can be modified according to actual needs.
[0108] "Output format" can be used to indicate the format of the output data of the large language model, where "variableName" can indicate the name of the variable that the large language model needs to output. "meaning" can be the variable semantic information mentioned above, indicating that the large language model needs to output variable semantic information. "Combining comment and realValue to explain the meaning of variableName" can be used to indicate that the large language model generates variable semantic information based on variable reference values and variable annotations. "composition" can be the variable structure information mentioned above, indicating that the large language model needs to output variable structure information. "Combining comment and realValue to explain how the value corresponding to variableName is usually constructed" can be used to indicate that the large language model generates composition based on variable reference values and variable annotations. "values" can indicate that the large language model needs to output variable values. "Based on meaning and composition, give an array of possible values" can be used to indicate that the large language model generates variable values based on variable semantic information and variable structure information.
[0109] In practical applications, compared to non-numeric strings (i.e., strings containing at least one character other than numbers), the semantic information carried or reflected by purely numeric strings is more difficult to understand. For example, when a large language model generates purely numeric strings based on the first prompt described above, it may generate strings such as "20220520123456789012, 202111111987654321098, and 20230303112233445566." These strings contain characters that are too random and lack semantic meaning.
[0110] Therefore, to further improve the accuracy and business semantics of generated pure numeric strings, this specification also provides an alternative processing method. Specifically, a large language model can be used to analyze the structure of the pure numeric string and output a string template. The template can then be filled in to obtain the specific pure numeric string, that is, the corresponding variable value.
[0111] Optionally, the variable specifically includes a numeric string variable; and filling the variable information into a preset prompt word template to obtain a prompt word for input into the large language model may specifically include: filling the variable information into a second prompt word template used to generate a format template for numeric string variables to obtain a second prompt word; the second prompt word includes second task information; the second task information is used to instruct the large language model to generate a format template that reflects the format of the variable according to a second constraint condition; the second constraint condition is used to indicate a feature that the format of the variable should conform to;
[0112] Calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference may specifically include: inputting the second prompt word into the large language model to obtain a format template of the variable generated by the large language model with reference to the variable reference value under the constraint of the second constraint condition; and generating the first seed test data of the variable based on the format template.
[0113] In the embodiment of this specification, a numeric string variable may be a string containing only numeric characters.
[0114] The second prompt word template can be a prompt word template used to generate a format template for a variable of the numeric string type. Variable information such as a variable name (e.g., variableName) and a variable reference value (e.g., realValue) can be inserted into the second prompt word model to generate a second prompt word containing the variable name and the variable reference value.
[0115] Among them, the second prompt word may also include second task information. The second task information has a similar meaning to the task information mentioned above, and may be information indicating a specific task that the user wants the large language model to complete. In the embodiment of this specification, the second task information may be a format template that instructs the large language model to output a format that reflects the variable. Specifically, the second task information may be a format template that instructs the large language model to output a format that can reflect the format according to the second constraint condition. That is to say, the large language model may not directly output the variable value, but output a format template, and then obtain the variable value based on the format template.
[0116] In the embodiment of this specification, the second constraint condition may be a feature that the format of the variable should conform to.
[0117] In the embodiments of this specification, the format template is generated by the large language model based on the variable reference value under the constraints of the second constraint condition. The variable value of the variable can be generated based on the format template. In practical applications, at least a portion of the variable value generated based on the format module can be used as the first seed test data of the variable.
[0118] As an implementation method, the large language model may further output variable names, and the variable names output by the large language model may be the same as the variable names in the prompt words input to the large language model.
[0119] As an implementation, after determining the variables in the code unit, the variable reference value may be analyzed to determine whether the variable is a numeric string variable or a non-numeric string variable, so as to perform different processing on different types of variables.
[0120] As a real-time method, the large language model for processing numeric string variables and the large language model for processing non-numeric string variables may be the same or different, and no specific limitation is made here.
[0121] The format template generated by the large language model using variable reference values is more consistent with the business attributes and data structure reflected by the variable. This makes the variable values obtained based on the format template more consistent with the business attributes and data structure reflected by the variable. This in turn makes the test cases constructed based on the variable values more consistent with the business scenarios of the code unit, improving the accuracy of code unit testing.
[0122] In the embodiments of this specification, considering that in actual applications, numeric character strings have unique characteristics and often contain time, date or random numbers, by designing the second prompt word as shown above, the large language model can output numeric characters that meet the requirements of the business scenario.
[0123] Optionally, the second constraint condition is used to constrain the generated format template to include at least one of a date data format, a time data format, and a random data format.
[0124] In an embodiment of the present specification, the second constraint may include the following: constraint 1: the format template may include a date data format, such as year-month-day [YYYYMMDD]; constraint 2: the format template may include a time data format, such as hour-minute-second [hhmmss]; constraint 3: the format template may include a random number, which may be represented as [random_number]. It is understandable that the second constraint may include one, two, or three of constraint 1, constraint 2, and constraint 3, and is not specifically limited here. In an embodiment of the present specification, by setting at least some of constraint 1, constraint 2, and constraint 3, the generated format template can be made to meet the expectations of the preset business scenario in terms of data structure.
[0125] As an implementation, the second constraint may also include the following: Constraint 4 always contains random numbers, and in rare cases, only contains random numbers. In fact, Constraint 4 describes the probability distribution of the value of a variable of a numeric string type in actual business scenarios. In the embodiments of this specification, by setting Constraint 4, the probability that the format template generated by the large language model will conform to the expected data structure of the preset business scenario can be increased.
[0126] The prompt word includes the second constraint condition, which can ensure that the format template generated by the large language model is generated by the large language model by analyzing the format of the variable. This makes the format template generated by the large language model more consistent with the business scenario of the code unit, thereby further ensuring the accuracy and business semantics of the variable value obtained based on the format module.
[0127] As an implementation method, in order to improve the analysis capability of the large language model and ensure the consistency between the format template generated by the large language model and the business scenario to which the code unit belongs, the variable information may further include variable annotations.
[0128] Optionally, the variable information may further include a variable annotation; inputting the second prompt word into the large language model to obtain a format template for the variable generated by the large language model with reference to the variable reference value under the constraint of the second constraint condition may specifically include:
[0129] The second prompt word is input into the large language model to obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition and referring to the variable reference value and the variable annotation.
[0130] In the embodiment of this specification, the variable comment may be content that explains the variable. For example, the variable comment for the variable name (variableName) "tradeNo" included in the variable information may be "@paramtradeNo".
[0131] In the embodiments of this specification, the variable information includes variable annotations, so that the large language model can analyze the data structure of the variable reference value or the characters that the data should include in combination with the variable reference value itself and the variable annotations, so that the format template output by the large language model can include content for generating the same data structure as the variable reference value, or the characters that the data should include, etc., thereby making the format template output by the large language model more accurate, and thus improving the accuracy of the variable value obtained based on the format template.
[0132] As an implementation method, in order to improve the analysis capability of the large language model and ensure the consistency between the format template generated by the large language model and the business scenario to which the code unit belongs, the variable information may further include a first variable length.
[0133] Optionally, the variable information may also include a first variable length; inputting the second prompt word into the large language model to obtain a format template for the variable generated by the large language model with reference to the variable reference value under the constraint of the second constraint condition may specifically include: inputting the second prompt word into the large language model to obtain a format template for the variable generated by the large language model with reference to the variable reference value, the variable annotation, and the first variable length under the constraint of the second constraint condition.
[0134] In the embodiment of this specification, the first variable length may be information about the length of the variable reference value. For example, the variable length of 2024091722001467681459953415 may be 28.
[0135] In the embodiment of this specification, the variable information includes the first variable length, so that the large language model can determine the data length in the generated format template in combination with the variable length information, thereby preventing the character string generated by the large language model from being too random and lacking semantics.
[0136] For ease of understanding, this specification continues to use the above example for the variable information contained in "variableName: tradeNo; realValue: 2024091722001467681459953415; comment: @paramtradeNo; length: 28," where variableName represents the variable name, realValue represents the variable reference value, comment represents the variable comment, and length represents the length of the first variable. Accordingly, the format module output by the large language model can be, for example, [YYYYMMDD][hhmmss][random_number]{14}.
[0137] In order to ensure that the variable values output by the large language model are the ones output by the large language model after careful consideration, and to improve the standardization of the output of the large language model, in the embodiments of this specification, the output format of the large language model can also be set.
[0138] Optionally, the second prompt word template also includes second output format information; the second output format information is used to indicate that the large language model outputs at least one of variable semantic information, variable structure information, and second variable length; wherein, the variable semantic information is used to represent the business semantics of the variable; the variable structure information is used to represent the data structure of the variable; and the second variable length is used to represent the data length of the variable.
[0139] In the embodiment of this specification, the variable semantic information and variable structure information in the second output format information of the second prompt word have the same meaning as the variable semantic information and variable structure information in the first output format information of the first prompt word mentioned above, so they are not repeated here.
[0140] In practical applications, variable semantic information can be generated by a large language model based on variable reference values. Furthermore, if the prompt word includes variable annotations, variable semantic information can also be generated by a large language model based on variable reference values and variable annotations.
[0141] As an implementation, the variable structure information may be generated by the large language model based on the variable reference value. Furthermore, if the prompt word includes a variable annotation, the variable structure information may also be generated by the large language model based on the variable reference value and the variable annotation.
[0142] The second variable length may be information indicating the length of the variable value contained in the format template generated by the large language model. The second variable length may be the same as the first variable length. In practical applications, if the variable information contains the first variable length, the large language model may determine the second variable length based on the first variable length. If the variable information does not contain the first variable length, the large language model may calculate the second variable length based on the variable reference value.
[0143] In the embodiment of this specification, the format template may be generated by the large language model based on the variable semantic information and the variable structure information. Further, the format template may be generated by the large language model based on the variable semantic information and the variable structure information and the second variable length.
[0144] The format template is generated by the large language model based on variable semantic information and variable structure information, or by the large language model based on variable semantic information and variable structure information and the second variable length, so that the format template must conform to the variable semantic information and variable structure information, or conform to the variable semantic information, variable structure information and the second variable length. In the process of generating variable semantic information and variable structure information, because the large language model takes into account variable reference values and variable annotations, the format template output by the large language model can be obtained after careful consideration by the large language model, thereby further improving the accuracy and business semantics of the format template, and further improving the accuracy and business semantics of the variable values obtained based on the format template, making the variable values more consistent with business scenarios.
[0145] In the embodiment of this specification, in order to further improve the comprehension ability of the large language model and to improve the accuracy of the format template output by the large language model, the second prompt word template in the embodiment of this specification may further include a reference case.
[0146] Optionally, the second prompt word template may further include reference case information; the reference case information includes case input information and case output information corresponding to the case input information.
[0147] In the embodiment of this specification, the reference case may include case input information and case output information, wherein the information structure of the case input information is consistent with the information structure of the variable information, and the information structure of the case output information is consistent with the information structure of the second output format information.
[0148] In practical applications, the reference examples may include one or more, which are not specifically limited here.
[0149] For ease of understanding, an example of a second prompt word template is provided in the embodiments of this specification, which may be specifically as follows:
[0150] You are an experienced test engineer and your task is to generate a format template for each numeric string variable.
[0151] A numeric string refers to a string consisting entirely of numbers.
[0152] A format template can only contain the following two elements:
[0153] 1. Time and date format
[0154] Year, month, day: [YYYYMMDD];
[0155] Hours, minutes, seconds: [hhmmss];
[0156] 2. Random number, for example, an 8-digit random number is represented as [random_number]{8};
[0157] Special note: Numeric strings always contain random numbers, and in rare cases only contain random numbers;
[0158] The input data is as follows:
[0159]
[0160] Your output will be in the following format:
[0161]
[0162] Example input 1:
[0163]
[0164] Sample output 1:
[0165]
[0166] In the prompt, "You are an experienced test engineer" can be the role information for a large language model. "Generate a format template for a numeric string variable" can be the task information for a large language model. "The format template can only contain the following two elements: 1. Date and time format, year, month, day: [YYYYMMDD]; hours, minutes, seconds: [hhmmss]; 2. Random numbers, for example, an 8-digit random number is represented as [random_number]{8}; Note: Numeric strings always contain random numbers, and rarely contain only random numbers" can be the second constraint. In the input data, "variableName" can represent the variable name, "comment" is the variable comment, "realValue" is the variable reference value, and "length" is the length of the first variable. The variable name, variable comment, variable reference value, and first variable length of the input data can be filled in or generated according to actual needs and are not specifically limited here.
[0167] The "output format" in the above prompt can be used to indicate the format of the output data of the large language model. "variableName" can indicate the name of the variable that the large language model needs to output. "meaning" can be the variable semantic information mentioned above, indicating that the large language model needs to output variable semantic information. "Combining comment and realValue to explain the meaning of variableName" can be used to instruct the large language model to generate variable semantic information based on the variable reference value and variable annotation.
[0168] As an implementation, in this embodiment, the prompt word may also include "relateUseId." "relateUseId" can indicate whether the variable is related to the userId. "Determine whether it is related to the userId. True indicates that the variable is related to the userId, and false indicates that the variable is not related to the userId." This can be used to instruct the large language model how to determine the value of "relateUseId." In this embodiment, the large language model determines whether the variable is related to the userId during the generation of the prompt word template. If the variable is related to the userId, the userId can be obtained through other means, thereby further ensuring the accuracy of the userId.
[0169] The "length" in the output format can be the length of the second variable. "Calculate the length of realValue. If realValue is empty, you need to give the length that variableName should correspond to based on experience" can be used to instruct the large language model how to generate the length of the second variable. "composition" can be the variable structure information mentioned above, indicating that the large language model needs to output variable structure information. "If relateuseId is true, the output is empty; if relatelseId is false, describe what positions x through x are composed of" can instruct the large language model to output the content of the variable structure information. "template" format template is used to indicate that the large language model needs to output a format template.
[0170] Among them, input example 1 and output example 1 are reference examples and will not be repeated here.
[0171] As an implementation method, the embodiments of this specification also provide another method for obtaining test data.
[0172] Optionally, determining variable information of variables in the code unit may specifically include: parsing the code unit to determine a class object loaded in the code unit, a method called in the class object, and variables used; and calling a test data interface to obtain service traffic data based on the class name of the class object, the method name of the method, and the variable name of the variable.
[0173] After determining the variable information of the variables in the code unit, the method may further include: generating second seed test data based on the business traffic data.
[0174] In the embodiments of this specification, parsing a code unit may include performing static analysis or dynamic analysis on the code unit, or a combination of static analysis and dynamic analysis. Static analysis may be performed by reading the code unit to identify its structure, call relationships, etc., without running the program. Dynamic analysis may be performed by running the program to observe its behavior, such as by tracing the execution path of the code unit through a debugger or logging.
[0175] In the embodiments of this specification, by parsing a code unit, the class objects loaded in the code unit can be determined, and all classes instantiated or referenced in the code can be found. It is also possible to determine which methods are called by the class objects. It is also possible to identify all variables (local variables, member variables, global variables, etc.) used in the code unit. The class objects loaded in the code unit are the classes to be tested that the code unit needs to test. The methods called by the class objects are the methods to be tested that need to be tested in the code unit.
[0176] In the embodiments of this specification, a call request to a test data interface can be constructed based on the determined class, method, and variable to be tested. The class, method, and variable to be tested can be used as query parameters or filtering conditions to obtain business traffic data related to the tested code from the test data interface.
[0177] In the embodiments of this specification, the test data interface can be used to obtain traffic information of the simulation environment.
[0178] In practical applications, at least part of the acquired service traffic data may be used as second seed test data of the variable.
[0179] In the embodiments of this specification, real test data is obtained by obtaining real business traffic data from the test data interface. Generating test cases based on real test data can enhance the business semantics of the test cases, thereby improving the user's understanding of the test cases. For example, the userId of 20889863412 is meaningful, while 123456789 is meaningless. The business semantics of the test cases generated based on the test data of 20889863412 are relatively clear and can be understood by the user, while the business semantics of the test cases generated based on 123456789 are unclear and cannot be understood by the user.
[0180] For ease of understanding, the embodiments of this specification also provide a specific method for parsing code units.
[0181] Optionally, the code unit is parsed to determine the class object loaded in the code unit, the method called in the class object, and the variables used, which may specifically include: using a bytecode analysis tool to determine the bytecode instructions in the code unit; identifying the member variables of the class to be tested to which the method to be tested belongs according to the bytecode instructions; associating the class name of the class to be tested and the member variables of the string type in the class to be tested and storing them as the first information; identifying the string type input parameters of the method to be tested according to the bytecode instructions; associating the method name of the method to be tested, the string type input parameters of the method to be tested, and the parameter order information of the string type input parameters and storing them as the second information; identifying the complex objects in the method to be tested according to the bytecode instructions; the complex objects are objects obtained by the method to be tested by calling the external services that the method to be tested depends on; associating the method name of the method to be tested, the fully qualified class name of the complex object, and the get method name called in the method to be tested and storing them as the third information.
[0182] Calling the test data interface to obtain business traffic data based on the class name of the class object, the method name of the method, and the variable name of the variable may specifically include: calling the test data interface to obtain business traffic data based on the first information, the second information, and the third information.
[0183] Bytecode analysis is a method for gaining a deep understanding of Java program behavior. As the intermediate representation between Java source code and the Java Virtual Machine (JVM), bytecode plays a crucial role in Java execution. Java bytecode is the JVM's instruction set, reducing inter-system dependencies and supporting Java's cross-platform nature. By analyzing bytecode instructions, we can identify the classes, methods, and variables to be tested within a code unit.
[0184] The ASM (Abstract Syntax Tree Method) framework is a widely used bytecode manipulation and analysis framework in Java, providing a powerful API for analyzing and manipulating bytecode. ASM uses the visitor pattern to traverse and modify bytecode, allowing for the insertion, deletion, or modification of existing bytecode instructions, thereby dynamically adjusting class behavior. In the embodiments of this specification, the ASM framework can be used to parse and process Java bytecode, i.e., perform ASM bytecode analysis.
[0185] A get method can be a method used to obtain data or values from an object, data structure, or API.
[0186] For ease of understanding, the following is an explanation with reference to specific code units, which may be as follows:
[0187] class A{
[0188] private String value;
[0189] private Service service; / / declare dependency
[0190] public void T getParam(String str1, String str2){
[0191] Obj obj = service.getObj(str1); / / Call the getObj method of service and pass in str1
[0192] }
[0193] public void T getMethod(Obj obj){
[0194] String name = obj.getName(); / / Call obj's getName method to get the name name
[0195] }
[0196] public void T getMethod1(){
[0197] Obj1 obj=service.getObj(); / / Call the getObj method of service to get the Obj1 object
[0198] String name = obj.getName1(); / / Call obj's getName method to get the name name
[0199] }
[0200] }.
[0201] In the embodiments of this specification, bytecode analysis can be used to determine the tested class and member variables of type string (String) in the aforementioned code unit; the tested method and the object Obj used in the tested method, as well as the get method call of Obj; the tested method and the input parameter names of the tested method of type String.
[0202] More specifically, based on the solution of the embodiment of this specification, the information of the class to be tested can be analyzed through bytecode to obtain all member variables of the class to be tested. In actual applications, member variables (Member Variables) are a concept in object-oriented programming (OOP), which can refer to variables defined inside a class (Class) but outside a method (Methods), which are used to store the state or properties of an object. Furthermore, variables of string type can also be filtered out from all member variables. Furthermore, the class name of the class to be tested and the member variables of string type in the class to be tested can be associated and stored as the first information storage, and in actual applications, they can be stored in memory. As in the above example, the identified class to be tested is classA, and the member variables of string type in this class include value, so that the first information can be stored as class A->value.
[0203] Based on the solution of the embodiment of this specification, the information of the method to be tested can be analyzed through bytecode to obtain the input parameters of the method to be tested. Furthermore, string-type input parameters can be filtered out from the input parameters. In actual application, the parameter order of each input parameter can also be analyzed. Afterwards, the method name of the method to be tested, the string-type input parameters of the method to be tested, and the parameter order information of each string-type input parameter can be associated and stored as the second information. In actual application, it can be stored in the memory. As in the above example, for the method to be tested getMethod, its corresponding string-type input parameters can include str1 and str2, and str1 is the first of the input parameters of getMethod, and str2 is the second of the input parameters of getMethod. Therefore, the second information can be stored as getParam->(1,str1),(2,str2).
[0204] Based on the solution of the embodiment of this specification, the information of the method to be tested can be analyzed through bytecode. Specifically, the get method involving complex objects in the method to be tested can be analyzed. In the above example, the complex object can be used to represent the object obtained by calling the dependent service in the current class class A. For example, the object obj obtained by calling the getObj method of the service in the getParam method; another example is the object obj1 obtained by calling the getObj method of the service in the getMethod1 method. Furthermore, in the above example, the input parameter of the getMethod method is the complex object obj, and the getName method of obj is called in the getMethod method. Therefore, the method to be tested getMethod, the fully qualified class name of the complex object obj involved, and the getName method called can be associated and stored as the third information. Assuming that the fully qualified name of the object obj is com.alipay.xx.Obj, the third information can be stored as getMethod->(com.alipay.xx.Obj,getName). In addition, in the above example, when the getMethod1 method is called, the getObj method of service is first called to obtain the complex object Obj1, and then the getName1 method of obj is called. Therefore, the tested method getMethod1, the fully qualified class name of the complex object Obj1 involved, and the called getName1 method can be associated and stored as the third information. Assuming that the fully qualified name of the object obj is com.alipay.xx.Obj1, the third information can be stored as getMethod1->(com.alipay.xx.Obj1,getName1).
[0205] As in the above example, after obtaining information such as class A->value, getParam->(1,str1),(2,str2), getMethod->(com.alipay.xx.Obj,getName), getMethod1->(com.alipay.xx.Obj1,getName1) by analyzing the bytecode, a data acquisition request can be constructed based on this information, and the real business values corresponding to the variables involved in the aforementioned code unit to be tested can be obtained by sending the aforementioned data acquisition request to the test data interface.
[0206] As an implementation method, another method of constructing test data is also provided in the examples of this specification.
[0207] Optionally, after obtaining the code unit to be tested, the following steps may also be performed:
[0208] The code unit is analyzed using a static analysis tool to obtain constant data in the code unit.
[0209] Based on the constant data, third seed test data is generated.
[0210] In the embodiments of this specification, a static analysis tool may be a type of tool used to check for code defects and vulnerabilities without running the program. It may identify potential errors, coding style issues, non-standard practices, and security issues in a program by analyzing source code, binary code, or bytecode.
[0211] In the embodiments of this specification, the code unit can be analyzed by using a static analysis tool available in the relevant technology to obtain constant data in the code unit. Specifically, the static analysis tool may include Soot, JavaParser, ASM, etc.
[0212] In the embodiments of this specification, constant data may include numerical constants, character constants, string constants, enumeration constants, etc. Constant data may include: public static final constants, such as MAX_RETRY=3; enumeration type constants (enum), such as Status{PENDING,SUCCESS}; default values in annotations, such as @Timeout(value=5000); literals, such as "default", -1, true. Constants usually represent key values of business rules (such as boundary values, status codes) and are high-value inputs to test cases. Covering these values can quickly trigger abnormal branches, avoid generating meaningless parameters, focus on values with a high probability of triggering defects, and improve test coverage.
[0213] In traditional algorithms, generated test case names typically follow a fixed format, such as test_tested method name_subscript index. Multiple test cases are typically generated for a single class under test, with subscript indexes starting at 0. This naming scheme avoids compilation failures caused by duplicate test case names. However, this naming scheme results in test case names that fail to reflect their specific purpose and meaning, leaving them unclear to users. Furthermore, traditional algorithms fail to generate meaningful comments for test cases, which are crucial for users to understand the specific scenarios of test cases.
[0214] Based on this, optionally, after the genetic search algorithm is used to iteratively mutate the seed test case to obtain the test case for testing the code unit, the method may further include: obtaining a third prompt word template for semantically enhancing the test case; the third prompt word template includes third task information; the third task information is used to instruct the large language model to generate semantic enhancement information for the test case according to preset thought chain information; the semantic enhancement information includes at least one of use case annotation information and a new use case name. The third prompt word template and the test case are input into the large language model to obtain the semantic enhancement information generated by the large language model. The test case is modified based on the semantic enhancement information to obtain a semantically enhanced test case.
[0215] In the embodiment of this specification, the third prompt word template may be a prompt word template used to guide the large language model to generate at least one of use case annotation information and a new use case name.
[0216] The third task information, similar to the task information described above, may be information indicating a specific task that the user wishes the large language model to complete. In the embodiments of this specification, the third task information may be information instructing the large language model to generate semantically enhanced information for a test case. Furthermore, the third task information may be used to instruct the large language model to generate semantically enhanced information for a test case according to a preset thought chain.
[0217] In one embodiment, the third prompt word template and the test cases requiring semantic enhancement can be input into the large language model as two separate files. In another embodiment, the test cases requiring semantic enhancement can be inserted into the third prompt word template to obtain the third prompt word, which is then input into the large language model. Furthermore, the third prompt word template can include one or more test cases requiring semantic enhancement.
[0218] As an implementation method, the large language model in the embodiment of this specification can output semantic enhancement information of the test case, and the server can modify the test case based on the semantic enhancement information to obtain a semantically enhanced test case to ensure the accuracy of the enhanced test case.
[0219] As another implementation, the large language model in the embodiment of this specification can also further modify the test case based on the semantic enhancement information after determining the semantic enhancement information of the test case, so that the large language model directly outputs the semantically enhanced test case to improve convenience.
[0220] For ease of understanding, in the embodiments of this specification, an example is provided for the third prompt word template as follows:
[0221] You are a senior Java testing expert focused on generating concise comments and new method names for unit tests.
[0222] To complete the task, you should follow these structured steps:
[0223] 1. Understand the provided unit test code.
[0224] 2. Identify key functions or concerns.
[0225] 3. Generate clear comments and new method names that conform to CamelCase.
[0226] Your output is a JSON array with the following format:
[0227]
[0228] Among them, "You are a senior Java testing expert" is the role information of the large language model. "Focus on generating concise comments and new method names for unit tests" is the third task information of the large language model. "To complete the task, you should follow the following structured steps: 1. Understand the provided unit test code. 2. Identify key functions or concerns. 3. Generate clear comments and new method names that conform to the camel case naming convention. Note: Enum names in use cases are prohibited from being translated into Chinese when they appear in comments." is the thought chain information, which can be used to inform the thinking process of the large language model. The thought chain information can improve the accuracy of the semantic enhancement information generated by the large language model. "Your output is a JSON array with the following format" is used to indicate the output format of the large language model.
[0229] Assume that the test case is as follows:
[0230] @Test(timeout=5000)
[0231] public void test_doXX_0()throws Throwable{
[0232] / / Call method:doXX
[0233] String string=xxImpl.doXX("","");
[0234] assertEquals("S",string);
[0235] }.
[0236] The semantic enhancement information output by the large language model includes:
[0237] {"caseName":test_doXX_0,
[0238] "comment": [Single test case] Test scenario: Test the return value of the doXX method when inputting 'zhixxbao' and 'add_funds',
[0239] "newCaseName":testdoXXWithZhixxbaoAndAddFunds
[0240] },
[0241] Among them, "test_doXX_0" is the name of the test case method input into the large language model; "[Single test case] Test scenario: Test the return value of the doXX method when 'zhixxbao' and 'add_funds' are input" is a line of comments generated by the large language model for the test case; "testdoXXWithZhixxbaoAndAddFunds" is the new test case method name generated by the large language model based on the comments.
[0242] Furthermore, the test cases are modified based on the semantic enhancement information output by the large language model. The semantically enhanced test cases obtained can be as follows:
[0243] / **
[0244] *[Single test case] Test scenario: Test the return value of the doXX method when inputting 'zhixxbao' and 'add_funds'
[0245] * /
[0246] @Test(timeout=5000)
[0247] public void testdoXXWithZhixxbaoAndAddFunds()throws Throwable{
[0248] / / Call method:doXX
[0249] String string=xxImpl.doXX("zhixxbao","add_funds");
[0250] assertEquals("S",string);
[0251] }.
[0252] As shown in the above example, by using the method of the embodiments of this specification, first, the variable values zhixxbao and add_funds that conform to the camelCase naming convention are generated, and doXX("","") is modified to doXX("zhixxbao","add_funds"); then, based on doXX("zhixxbao","add_funds"), a semantically intelligent annotation "[Single test case] Test scenario: Test the return value of the doXX method when 'zhixxbao' and 'add_funds' are input" and a new case name "testdoXXWithZhixxbaoAndAddFunds" that conforms to the camelCase naming convention is generated for the test case. As a result, the business semantics and readability of the test case can be greatly improved.
[0253] The various technical features in the above embodiments can be arbitrarily combined 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, the arbitrary combination of the various technical features in the above embodiments also falls within the scope of disclosure of this specification.
[0254] In order to more clearly illustrate a method for generating a test case provided in an embodiment of this specification, Figure 3 This is a schematic diagram of the overall process of generating a test case provided by an embodiment of this specification. The process of the method for generating a test case can be executed by an application server or an application terminal.
[0255] like Figure 3 As shown in the figure, the process of generating test cases includes the following steps:
[0256] Step 302: Obtain the code unit to be tested.
[0257] A code unit is a piece of code that needs to be tested during software development. It can include functions, methods, classes, modules, and other objects that can be unit tested.
[0258] Step 304: Population initialization.
[0259] The population initialization can be done by building a seed pool. Specifically, the seed pool can be built in three ways: building a data pool through program analysis, building a data pool based on business traffic data, and building a data pool through a large language model.
[0260] For ease of understanding, the following three methods of constructing seed pools are explained one by one:
[0261] To build a data pool through program analysis, the following steps may be included:
[0262] Step 3402: Use a static analysis tool to analyze the code unit to obtain constant data in the code unit.
[0263] Step 3404: Generate third seed test data based on the constant data.
[0264] The constant data in the code unit can be obtained by analyzing the code unit using static analysis tools available in the relevant technology. Specifically, static analysis tools may include Soot, JavaParser, ASM, etc.
[0265] Constant data can include numeric constants, character constants, string constants, enumeration constants, etc. Constant data can include: public static final constants, such as MAX_RETRY=3; enumeration type constants (enum), such as Status{PENDING,SUCCESS}; default values in annotations, such as @Timeout(value=5000); and literals, such as "default", -1, true.
[0266] To build a data pool based on business traffic data, the following steps may be included:
[0267] Step 3502: Parse the code unit to determine the class objects loaded in the code unit, the methods called in the class objects, and the variables used.
[0268] Step 3502 may specifically include:
[0269] Using a bytecode analysis tool, bytecode instructions in the code unit are determined.
[0270] According to the bytecode instructions, member variables of the class to be tested to which the method to be tested belongs are identified.
[0271] The class name of the class to be tested and the member variable of the string type in the class to be tested are associated and stored as the first information.
[0272] According to the bytecode instruction, an input parameter of a string type of the method to be tested is identified.
[0273] The method name of the method to be tested, the string type input parameter of the method to be tested, and the parameter sequence information of the string type input parameter are associated and stored as the second information.
[0274] According to the bytecode instructions, a complex object in the method to be tested is identified; the complex object is an object obtained by the method to be tested by calling an external service on which it depends.
[0275] The method name of the method to be tested, the fully qualified class name of the complex object, and the name of the get method called in the method to be tested are associated and stored as the third information.
[0276] Since the relevant contents of the above content have been described in detail in the previous article, they will not be repeated here.
[0277] Step 3504: Based on the class name of the class object, the method name of the method, and the variable name of the variable, call the test data interface to obtain business traffic data.
[0278] In the embodiments of this specification, a test data interface can be called based on the first information, the second information, and the third information to obtain service flow data. Specifically, query information can be constructed based on the first information, the second information, and the third information to obtain service flow data related to the code under test from the test data interface.
[0279] Step 3506: Generate second seed test data based on the business traffic data.
[0280] In practical applications, at least part of the acquired service traffic data may be used as second seed test data of the variable.
[0281] By obtaining real business traffic data from the test data interface, you can obtain real test data. Generating test cases based on real test data can improve the business semantics of the test cases and thus enhance user understanding of the test cases.
[0282] To build a data pool using a large language model, you can specifically include the following steps:
[0283] Step 3602: Determine variable information of the variable in the code unit; the variable information includes a variable name, a variable reference value, and a variable comment; the variable reference value is derived from historical business data of the variable;
[0284] By parsing a code unit, you can identify variables in the code unit. Variables can be inputs, outputs, and intermediate variables involved in the code unit.
[0285] The variable reference value in the variable information may be an actual value of the variable extracted from the historical business data of the variable. Specifically, the variable reference value may include a typical value or a representative value of the variable.
[0286] Variable annotations can be content that explains variables.
[0287] Step 3604: Determine the type of the variable. If the variable is a non-numeric string variable, execute steps 3606 to 3608; if the variable is a numeric string variable, execute steps 3610 to 3614.
[0288] A non-numeric string variable can contain at least one character other than a numeric character. A numeric string variable can contain only numeric characters.
[0289] Step 3606: Fill the variable information into a first prompt word template used to generate a variable value of a non-numeric string variable to obtain a first prompt word; the first prompt word contains first task information; the first task information is used to instruct the large language model to generate the variable value of the variable according to the first constraint condition; the first constraint condition is used to represent the characteristics that the variable value of the variable should meet.
[0290] The first prompt word template may be a prompt word template used to generate a variable value of a non-numeric string variable.
[0291] As an embodiment, the first prompt word template may also include first output format information; the first output format information may be used to indicate that the large language model outputs at least one of variable semantic information and variable structure information; wherein the variable semantic information may be used to represent the business semantics of the variable; and the variable structure information may be used to represent the data structure of the variable.
[0292] The variable semantic information may be generated by the large language model based on the variable reference value. Furthermore, if the prompt word includes a variable annotation, the variable semantic information may also be generated by the large language model based on the variable reference value and the variable annotation.
[0293] The variable structure information may be generated by the large language model based on the variable reference value. Furthermore, if the prompt word includes a variable annotation, the variable structure information may also be generated by the large language model based on the variable reference value and the variable annotation.
[0294] The first task information may be a variable value indicating an output variable of the large language model. Specifically, the first task information may be a variable value indicating that the large language model generates a variable according to the first constraint condition.
[0295] The first constraint condition may include characteristics that the variable value of the variable should conform to. Specifically, the first constraint condition may include at least one of a semantic authenticity condition, a naming rule condition, and a non-masking condition; the semantic authenticity condition may be used to constrain the generated variable value to have a value that matches the business scenario; the naming rule condition may be used to constrain the generated variable value to conform to a preset naming rule; and the non-masking condition may be used to constrain the generated variable value to not be masked.
[0296] Step 3608: Input the first prompt word into the large language model to obtain first seed test data of the variable generated by the large language model under the constraint of the first constraint condition, with reference to the variable reference value and the variable annotation.
[0297] In the embodiments of this specification, the first prompt word is input into the large language model. The large language model can generate variable semantic information and variable structure information based on the variable reference value and variable annotation, and then generate the variable value based on the variable semantic information and variable structure information. This allows the large language model to consider the variable reference value and variable annotation when outputting the variable value, thereby improving the accuracy of the variable value and making the variable value more suitable for the business scenario.
[0298] Step 3610: Fill the variable information into the second prompt word template of the format template used to generate a numeric string variable to obtain a second prompt word; the second prompt word contains second task information; the second task information is used to instruct the large language model to generate a format template that reflects the format of the variable according to the second constraint condition; the second constraint condition is used to indicate the characteristics that the format of the variable should conform to.
[0299] In the embodiment of this specification, the variable information may further include a first variable length. The first variable length may be information about the length of the variable reference value. For example, the variable length of 2024091722001467681459953415 may be 28.
[0300] In the embodiment of this specification, the variable information includes the first variable length, so that the large language model can determine the data length in the generated format template in combination with the variable length information, thereby preventing the character string generated by the large language model from being too random and lacking semantics.
[0301] The second prompt word template may be a prompt word template that is a format template for generating a variable of the type of a numeric character string variable.
[0302] As an implementation, the second prompt word template may further include second output format information and a reference case. The second output format information may be used to indicate at least one of variable semantic information, variable structure information, and a second variable length for the large language model output. The variable semantic information may be used to represent the business semantics of the variable; the variable structure information may be used to represent the data structure of the variable; and the second variable length may be used to represent the data length of the variable. The reference case information includes case input information and case output information corresponding to the case input information.
[0303] The second output format information and reference cases have been described in detail in the previous article and will not be repeated here.
[0304] The second task information may instruct the large language model to output a format template that reflects the format according to the second constraint condition. In other words, the large language model may not directly output the variable value, but instead output a format template, and obtain the variable value based on the format template.
[0305] The second constraint condition may be used to constrain the generated format template to include at least one of a date data format, a time data format, and a random data format.
[0306] Step 3612: Input the second prompt word into the large language model to obtain a format template of the variable generated by the large language model with reference to the variable reference value under the constraint of the second constraint condition.
[0307] Step 3614: Generate first seed test data of the variable based on the format template.
[0308] A seed test case may be constructed based on one or more of the first seed test data, the second seed test data, and the third seed test data.
[0309] Step 306: Use case iteration and variation.
[0310] Specifically, a genetic search algorithm may be used to iteratively mutate the seed test case to obtain a test case for testing the code unit.
[0311] Step 308: Use case post-processing.
[0312] The use case post-processing can specifically include the following steps:
[0313] Step 3702: Obtain a third prompt word template for semantically enhancing the test case; the third prompt word template includes third task information; the third task information is used to instruct the large language model to generate semantic enhancement information for the test case according to preset thought chain information; the semantic enhancement information includes at least one of use case annotation information and a new use case name.
[0314] The third prompt word template may be a prompt word template for guiding the large language model to generate at least one of use case annotation information and a new use case name.
[0315] The third task information may be semantic enhancement information for instructing the large language model to generate test cases according to preset thought chain information.
[0316] Step 3704: Input the third prompt word template and the test case into the large language model to obtain semantic enhancement information generated by the large language model.
[0317] In the embodiment of this specification, the large language model for generating the semantic enhancement information and the large language model for generating the first seed test data described above may be different large language models.
[0318] Step 3706: Modify the test case based on the semantic enhancement information to obtain a semantically enhanced test case.
[0319] Step 310: Compile and execute the generated semantically enhanced test case.
[0320] Based on the same idea, the embodiments of this specification also provide a device corresponding to the above method.
[0321] Figure 4 A schematic diagram of the structure of a device for generating test cases provided in an embodiment of this specification.
[0322] like Figure 4 As shown, the device may include:
[0323] The code unit acquisition module 402 is used to acquire the code unit to be tested.
[0324] The variable information determination module 404 is used to determine the variable information of the variable in the code unit; the variable information includes a variable name and a variable reference value; the variable reference value is derived from the historical business data of the variable.
[0325] The prompt word generation module 406 is used to fill the variable information into a preset prompt word template to obtain a prompt word for input into the large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that conforms to the business semantic characteristics reflected by the variable reference value.
[0326] The seed test data generating module 408 is configured to call the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference.
[0327] The seed test case construction module 410 is configured to construct a seed test case based on the first seed test data.
[0328] The test case generation module 412 is configured to iteratively mutate the seed test case using a genetic search algorithm to obtain a test case for testing the code unit.
[0329] based on Figure 4 The present specification also provides some specific implementation plans of the method, which are described below.
[0330] Optionally, the variable specifically includes a non-numeric string variable, and the prompt word generation module 406 can be specifically used to:
[0331] Filling the variable information into a first prompt word template for generating a variable value of a non-numeric string variable to obtain a first prompt word; the first prompt word includes first task information; the first task information is used to instruct the large language model to generate a variable value of the variable according to a first constraint condition; the first constraint condition is used to indicate a feature that the variable value of the variable should meet;
[0332] The seed test data generating module 408 may be specifically used to:
[0333] The first prompt word is input into the large language model to obtain first seed test data of the variable generated by the large language model with reference to the variable reference value under the constraint of the first constraint condition.
[0334] Optionally, the first constraint condition includes at least one of a semantic authenticity condition, a naming rule condition and a non-massifying condition; the semantic authenticity condition is used to constrain the generated variable value to have a value that matches the business scenario; the naming rule condition is used to constrain the generated variable value to comply with a preset naming rule; the non-massifying condition is used to constrain the generated variable value to not be masked.
[0335] Optionally, the variable information also includes variable annotations; the seed test data generation module 408 can be specifically used to:
[0336] The first prompt word is input into the large language model to obtain first seed test data of the variable generated by the large language model under the constraint of the first constraint condition and referring to the variable reference value and the variable annotation.
[0337] Optionally, the first prompt word template also includes first output format information; the first output format information is used to indicate that the large language model outputs at least one of variable semantic information and variable structure information; wherein, the variable semantic information is used to represent the business semantics of the variable; and the variable structure information is used to represent the data structure of the variable.
[0338] Optionally, the variable specifically includes a numeric string variable, and the prompt word generation module 406 can be specifically used to:
[0339] Filling the variable information into a second prompt word template of a format template for generating a numeric string variable to obtain a second prompt word; the second prompt word includes second task information; the second task information is used to instruct the large language model to generate a format template for reflecting the format of the variable according to a second constraint condition; the second constraint condition is used to indicate a feature that the format of the variable should conform to;
[0340] The seed test data generating module 408 may specifically include:
[0341] a format template acquiring unit, configured to input the second prompt word into the large language model, and obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition and with reference to the variable reference value;
[0342] The first seed test data generating unit is configured to generate first seed test data for the variable based on the format template.
[0343] Optionally, the second constraint condition is used to constrain the generated format template to include at least one of a date data format, a time data format, and a random data format.
[0344] Optionally, the variable information also includes variable annotations; the format template acquisition unit can be specifically used to:
[0345] The second prompt word is input into the large language model to obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition and referring to the variable reference value and the variable annotation.
[0346] Optionally, the variable information also includes a first variable length; the format template acquisition unit can be specifically used for.
[0347] The second prompt word is input into the large language model to obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition with reference to the variable reference value, the variable annotation and the first variable length.
[0348] Optionally, the second prompt word template also includes second output format information; the second output format information is used to indicate that the large language model outputs at least one of variable semantic information, variable structure information, and second variable length; wherein, the variable semantic information is used to represent the business semantics of the variable; the variable structure information is used to represent the data structure of the variable; and the second variable length is used to represent the data length of the variable.
[0349] Optionally, the second prompt word template also includes reference case information; the reference case information includes case input information and case output information corresponding to the case input information.
[0350] Optionally, the variable information determination module 404 may specifically include:
[0351] The parsing unit is used to parse the code unit to determine the class objects loaded in the code unit, the methods called in the class objects, and the variables used.
[0352] The business flow data acquisition unit is used to call the test data interface to acquire business flow data according to the class name of the class object, the method name of the method and the variable name of the variable.
[0353] Optional, Figure 4 The device may further include:
[0354] The second seed test data generating module is used to generate second seed test data based on the service flow data.
[0355] Optionally, the parsing unit may be specifically used to:
[0356] Using a bytecode analysis tool, bytecode instructions in the code unit are determined.
[0357] According to the bytecode instructions, member variables of the class to be tested to which the method to be tested belongs are identified.
[0358] The class name of the class to be tested and the member variable of the string type in the class to be tested are associated and stored as the first information.
[0359] According to the bytecode instruction, an input parameter of a string type of the method to be tested is identified.
[0360] The method name of the method to be tested, the string type input parameter of the method to be tested, and the parameter sequence information of the string type input parameter are associated and stored as the second information.
[0361] According to the bytecode instructions, a complex object in the method to be tested is identified; the complex object is an object obtained by the method to be tested by calling an external service on which it depends.
[0362] The method name of the method to be tested, the fully qualified class name of the complex object, and the name of the get method called in the method to be tested are associated and stored as the third information.
[0363] The service flow data acquisition unit can be used to:
[0364] According to the first information, the second information and the third information, a test data interface is called to obtain service flow data.
[0365] Optional, Figure 4 The device may further include:
[0366] The analysis unit is used to analyze the code unit using a static analysis tool to obtain constant data in the code unit.
[0367] The third seed test data generating module is configured to generate third seed test data based on the constant data.
[0368] Optional, Figure 4 The device may further include:
[0369] A third prompt word template acquisition module is used to obtain a third prompt word template for semantically enhancing the test case; the third prompt word template includes third task information; the third task information is used to instruct the large language model to generate semantic enhancement information for the test case according to preset thought chain information; the semantic enhancement information includes at least one of use case annotation information and a new use case name.
[0370] The semantic enhancement information acquisition module is used to input the third prompt word template and the test case into the large language model to obtain the semantic enhancement information generated by the large language model.
[0371] The semantic enhancement test case acquisition module is used to modify the test case based on the semantic enhancement information to obtain the semantically enhanced test case.
[0372] It is understood that the above modules refer to computer programs or program segments for performing 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.
[0373] The above is a schematic diagram of a device for generating test cases according to this embodiment. It should be noted that the technical solution of the device for generating test cases and the technical solution of the method for generating test cases described above are based on the same concept. For details not described in detail in the technical solution of the device for generating test cases, please refer to the description of the technical solution of the method for generating test cases described above.
[0374] Based on the same idea, the embodiments of this specification also provide devices corresponding to the above methods.
[0375] Figure 5 This is a schematic diagram of the structure of a device for generating test cases provided in an embodiment of this specification. Figure 5 As shown, the device 500 may include:
[0376] at least one processor 510; and,
[0377] A memory 530 in communication with the at least one processor; wherein,
[0378] The memory 530 stores instructions 520 executable by the at least one processor 510. The instructions are executed by the at least one processor 510 to enable the at least one processor 510 to:
[0379] Get the code unit to be tested;
[0380] Variable information of a variable in the code unit is determined; the variable information includes a variable name and a variable reference value; the variable reference value is derived from historical business data of the variable.
[0381] The variable information is filled into a preset prompt word template to obtain a prompt word for input into a large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that conforms to the business semantic characteristics reflected by the variable reference value.
[0382] The large language model is called based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference.
[0383] A seed test case is constructed based on the first seed test data.
[0384] A genetic search algorithm is used to iteratively mutate the seed test case to obtain a test case for testing the code unit.
[0385] 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 devices, equipment, and 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.
[0386] The foregoing description describes specific embodiments of the present application. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. Furthermore, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0387] In the 1990s, technological improvements could be clearly distinguished as either hardware improvements (for example, improvements to circuit structures such as diodes, transistors, and switches) or software improvements (improvements to process flows). However, with the advancement of technology, many process flow improvements today can now be considered direct improvements to hardware circuit structures. Designers almost always create the corresponding hardware circuit structure by programming the improved process flow into the hardware circuit. Therefore, it cannot be said that a process flow improvement cannot be implemented using hardware modules. For example, a programmable logic device (PLD) (such as a field programmable gate array (FPGA)) is an integrated circuit whose logical function is determined by user programming. Designers can "integrate" a digital system onto a PLD through their own programming, without having to hire a chip manufacturer to design and manufacture a dedicated integrated circuit chip. Moreover, nowadays, instead of manually fabricating integrated circuit chips, this programming is mostly done using "logic compiler" software. This is similar to the software compiler used when developing programs. Before compilation, the original code must also be written in a specific programming language, called a hardware description language (HDL). There is not just one HDL, but many, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. The most commonly used ones are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art will also understand that by simply programming the method flow in one of these hardware description languages and then programming it into an integrated circuit, a hardware circuit that implements the logic method flow can be easily obtained.
[0388] The controller can be implemented in any suitable manner. For example, the controller can take the form of a microprocessor or processor and a computer-readable medium storing computer-readable program code (e.g., software or firmware) executable by the (micro)processor, logic gates, switches, application-specific integrated circuits (ASICs), programmable logic controllers, and embedded microcontrollers. 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 will also know that in addition to implementing the controller in a purely computer-readable program code format, the controller can be implemented in the form of logic gates, switches, application-specific integrated circuits, programmable logic controllers, and embedded microcontrollers by logically programming the method steps. Therefore, such a controller can be considered a hardware component, and the devices included therein for implementing various functions can also be considered as structures within the hardware component. Or even, the devices for implementing various functions can be considered as both software modules that implement the method and structures within the hardware component.
[0389] The systems, devices, modules, or units described in the above embodiments may be implemented by computer chips or entities, or by products having certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smartphone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0390] For the convenience of description, the above devices are described as being divided into various units according to their functions. Of course, when implementing this application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0391] It will be understood by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Thus, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, 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 magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0392] 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 flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, 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 flowcharts and / or block diagrams. 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.
[0393] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.
[0394] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 The steps for the function specified in one or more boxes.
[0395] In a typical configuration, a computing device includes one or more processors (CPUs), input / output interfaces, network interfaces, and memory.
[0396] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.
[0397] 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. The information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.
[0398] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.
[0399] 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 communications network. In a distributed computing environment, program modules may be located in local and remote computer storage media, including storage devices.
[0400] The foregoing is merely 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 modifications, equivalent replacements, improvements, etc. made within the spirit and principles of the present application should all be included within the scope of the claims of the present application.
Claims
1. A method for generating a test case, comprising: Get the code unit to be tested; Determining variable information of variables in the code unit; The variable information includes a variable name and a variable reference value; The variable reference value is derived from the historical business data of the variable; Filling the variable information into a preset prompt word template to obtain a prompt word for input into a large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that meets the business semantic characteristics reflected by the variable reference value; Calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference; Constructing a seed test case based on the first seed test data; A genetic search algorithm is used to iteratively mutate the seed test case to obtain a test case for testing the code unit.
2. The method of claim 1, wherein the variable specifically comprises a non-numeric string variable; and wherein the step of filling the variable information into a preset prompt word template to obtain a prompt word for input into the large language model specifically comprises: Filling the variable information into a first prompt word template for generating a variable value of a non-numeric string variable to obtain a first prompt word; The first prompt word includes first task information; The first task information is used to instruct the large language model to generate the variable value of the variable according to the first constraint condition; the first constraint condition is used to represent the characteristics that the variable value of the variable should conform to; The step of calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference specifically includes: The first prompt word is input into the large language model to obtain first seed test data of the variable generated by the large language model with reference to the variable reference value under the constraint of the first constraint condition.
3. The method according to claim 2, wherein: The first constraint condition includes at least one of a semantic authenticity condition, a naming rule condition, and a non-masking condition; the semantic authenticity condition is used to constrain the generated variable value to have a value that matches the business scenario; The naming rule condition is used to constrain the generated variable value to comply with the preset naming rule; the non-massification condition is used to constrain the generated variable value to not be masked.
4. The method of claim 2, wherein the variable information further includes variable annotations; wherein the step of calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference comprises: The first prompt word is input into the large language model to obtain first seed test data of the variable generated by the large language model under the constraint of the first constraint condition and referring to the variable reference value and the variable annotation.
5. The method according to claim 2, wherein the first prompt word template further includes first output format information; the first output format information is used to instruct the large language model to output at least one of variable semantic information and variable structure information; wherein, The variable semantic information is used to represent the business semantics of the variable; the variable structure information is used to represent the data structure of the variable.
6. The method of claim 1 , wherein the variable specifically comprises a numeric string variable; and wherein the step of filling the variable information into a preset prompt word template to obtain a prompt word for input into the large language model specifically comprises: Filling the variable information into a second prompt word template of a format template for generating a digital character string variable to obtain a second prompt word; The second prompt word includes second task information; The second task information is used to instruct the large language model to generate a format template reflecting the format of the variable according to the second constraint condition; The second constraint condition is used to indicate the characteristics that the format of the variable should comply with; The step of calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference specifically includes: Inputting the second prompt word into the large language model to obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition and referring to the variable reference value; Based on the format template, first seed test data of the variable is generated.
7. The method according to claim 6, wherein: The second constraint condition is used to constrain the generated format template to include at least one of a date data format, a time data format, and a random data format.
8. The method of claim 6, wherein the variable information further includes a variable annotation; wherein inputting the second prompt word into the large language model to obtain a format template for the variable generated by the large language model with reference to the variable reference value under the constraint of the second constraint condition specifically comprises: The second prompt word is input into the large language model to obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition and referring to the variable reference value and the variable annotation.
9. The method of claim 8, wherein the variable information further includes a first variable length; wherein inputting the second prompt word into the large language model to obtain a format template for the variable generated by the large language model with reference to the variable reference value under the second constraint condition specifically comprises: The second prompt word is input into the large language model to obtain a format template of the variable generated by the large language model under the constraint of the second constraint condition with reference to the variable reference value, the variable annotation and the first variable length.
10. The method of claim 6, wherein: The second prompt word template also includes second output format information; the second output format information is used to indicate that the large language model outputs at least one of variable semantic information, variable structure information, and second variable length; wherein the variable semantic information is used to represent the business semantics of the variable; the variable structure information is used to represent the data structure of the variable; and the second variable length is used to represent the data length of the variable.
11. The method according to claim 6, wherein: The second prompt word template also includes reference case information; the reference case information includes case input information and case output information corresponding to the case input information.
12. The method according to claim 1, wherein determining the variable information of the variable in the code unit specifically comprises: Parsing the code unit to determine the class objects loaded in the code unit, the methods called in the class objects, and the variables used; According to the class name of the class object, the method name of the method and the variable name of the variable, calling the test data interface to obtain business traffic data; After determining the variable information of the variables in the code unit, the method further includes: generating second seed test data based on the business traffic data.
13. The method according to claim 12, wherein parsing the code unit to determine the class objects loaded in the code unit, the methods called in the class objects, and the variables used, specifically comprises: Using a bytecode analysis tool, determining the bytecode instructions in the code unit; Identifying, according to the bytecode instructions, member variables of the class to be tested to which the method to be tested belongs; Associating the class name of the class to be tested and the member variable of the string type in the class to be tested as first information; According to the bytecode instruction, identifying the string type input parameter of the method to be tested; Associating the method name of the method to be tested, the string-type input parameter of the method to be tested, and the parameter sequence information of the string-type input parameter, and storing the result as the second information; Identify, based on the bytecode instructions, complex objects in the method to be tested; the complex objects are objects obtained by the method to be tested by calling an external service that it depends on; The method name of the method to be tested, the fully qualified class name of the complex object, and the name of the get method called in the method to be tested are associated and stored as the third information; The step of calling a test data interface to obtain service flow data according to the class name of the class object, the method name of the method, and the variable name of the variable specifically includes: According to the first information, the second information and the third information, a test data interface is called to obtain service flow data.
14. The method according to claim 1, further comprising, after obtaining the code unit to be tested: Analyzing the code unit using a static analysis tool to obtain constant data in the code unit; Based on the constant data, third seed test data is generated.
15. The method according to claim 1, further comprising: Obtaining a third prompt word template for performing semantic enhancement processing on the test case; The third prompt word template includes third task information; The third task information is used to instruct the large language model to generate semantic enhancement information of the test case according to the preset thought chain information; The semantic enhancement information includes at least one of use case annotation information and a new use case name; Inputting the third prompt word template and the test case into the large language model to obtain semantic enhancement information generated by the large language model; The test case is modified based on the semantic enhancement information to obtain a semantically enhanced test case.
16. A device for generating a test case, comprising: A code unit acquisition module is used to obtain the code unit to be tested; A variable information determination module, configured to determine variable information of variables in the code unit; The variable information includes a variable name and a variable reference value; the variable reference value is derived from the historical business data of the variable; a prompt word generation module, configured to fill the variable information into a preset prompt word template to obtain a prompt word for input into a large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that conforms to the business semantic characteristics reflected by the variable reference value; a seed test data generating module, configured to call the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference; A seed test case construction module, configured to construct a seed test case based on the first seed test data; The test case generation module is used to iteratively mutate the seed test case using a genetic search algorithm to obtain a test case for testing the code unit.
17. A device for generating test cases, 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 code unit to be tested; Determining variable information of a variable in the code unit; the variable information includes a variable name and a variable reference value; the variable reference value is derived from historical business data of the variable; Filling the variable information into a preset prompt word template to obtain a prompt word for input into a large language model; the prompt word includes task information; the task information is used to instruct the large language model to output content that meets the business semantic characteristics reflected by the variable reference value; Calling the large language model based on the prompt word to obtain first seed test data of the variable with the variable reference value as a reference; Constructing a seed test case based on the first seed test data; A genetic search algorithm is used to iteratively mutate the seed test case to obtain a test case for testing the code unit.