Method and device for generating use cases of software code, storage medium, and electronic device
By detecting and filtering the test parameters of the reference use case collection, the target use case collection is generated, which solves the problem of low efficiency in software code test cases generation, and realizes automatic generation of efficient and highly adaptable test cases, improving testing efficiency and quality.
Patent Information
- Application Number
- CN202510728898.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-03
- Publication Date
- 2025-09-02
- Estimated Expiration
- 2045-06-03
AI Technical Summary
In the prior art, software code test cases are generated inefficiently, manual writing test cases are inefficient and easy to miss boundary situations. Traditional testing tools cannot analyze trigger conditions that do not cover code, resulting in difficult to optimize test cases, poor applicability, and difficulty in adapting to changes in different business scenarios and code patterns.
By detecting the test parameters of the reference use case set, candidate use cases that contribute greatly to the test coverage are selected, and reference use case sets are adjusted according to the test path of the candidate use case, target use case sets are generated, and test cases are generated using semantic understanding and automation to improve the effectiveness and generation efficiency of test cases.
It realizes automatic identification of effective test cases, optimizes the generation efficiency of test cases, improves the effectiveness and adaptability of test cases, overcomes the limitations of manual writing of test cases and traditional tools, and improves test efficiency and quality.
Smart Images

Figure CN120256316B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computers, and specifically, to a method and device for generating use cases of software code, a storage medium, and an electronic device. Background Art
[0002] In related technologies, when testing the integrity and validity of software code, test engineers usually manually design test cases based on the logic of the code to be tested, and then use testing tools (such as Sonar, JaCoCo, Istanbul, etc.) to count the code coverage of the code to be tested during the execution of the test cases, that is, the proportion of code covered by the code to be tested.
[0003] However, traditional testing methods have limitations. For one thing, manually written test cases are inefficient and prone to missing edge cases. Furthermore, when faced with large code bases, it's difficult for humans to identify complex logical branches and hidden execution paths within the code. Furthermore, traditional testing tools can only compile statistical coverage data, but are unable to conduct in-depth analysis of coverage results and proactively identify the reasons why test code is not covered. Furthermore, manually designed test cases have poor applicability and lack the ability to adapt to different business scenarios and code patterns. For example, for e-commerce and medical systems, two very different business scenarios, manually designed test cases are difficult to reuse directly and require extensive modification and redesign, extending the testing cycle. Therefore, in software code testing, the inefficiency of manually written test cases and the functional limitations of traditional testing tools hinder the test case optimization process, making it difficult to improve code coverage and, in turn, leading to low software code testing efficiency.
[0004] For technical problems such as low efficiency in test case generation in related technologies, no effective solutions have been proposed yet. Summary of the Invention
[0005] The embodiments of the present application provide a method and device for generating use cases of software code, a storage medium, and an electronic device, so as to at least solve the technical problems in related technologies such as low efficiency in generating test cases.
[0006] According to one embodiment of the present application, a method for generating a use case of software code is provided, comprising:
[0007] Detecting test parameters of a reference use case set on the software code, wherein each reference use case in the reference use case set is used to test a portion of a code path of the software code, and the test parameters are used to indicate the contribution of each reference use case to the test coverage of the software code;
[0008] Select candidate use cases whose test parameters meet the parameter conditions from the reference use case set, wherein the contribution of the use cases whose test parameters meet the parameter conditions to the test coverage is greater than the target contribution;
[0009] Get the target code path tested by the candidate use case;
[0010] The reference use case set is adjusted according to the target code path to obtain a target use case set, wherein the correlation between the code path tested by the target use case set and the target code path is greater than the correlation between the code path tested by the reference use case set and the target code path.
[0011] According to another embodiment of the present application, there is also provided a device for generating a use case of a software code, including:
[0012] a detection module, configured to detect test parameters of a reference use case set on a software code, wherein each reference use case in the reference use case set is used to test a portion of a code path of the software code, and the test parameters are used to indicate a contribution degree of each reference use case to the test coverage of the software code;
[0013] A screening module is used to screen candidate use cases whose test parameters meet parameter conditions from the reference use case set, wherein the use cases whose test parameters meet the parameter conditions contribute more to the test coverage than the target contribution;
[0014] The first acquisition module is used to obtain the target code path tested by the candidate use case;
[0015] An adjustment module is used to adjust the reference use case set according to the target code path to obtain a target use case set, wherein the correlation between the code path tested by the target use case set and the target code path is greater than the correlation between the code path tested by the reference use case set and the target code path.
[0016] The present application also provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of the method for generating a use case of any of the above-mentioned software codes when executing the computer program.
[0017] The present application also provides a computer-readable storage medium, in which a computer program is stored, wherein when the computer program is executed by a processor, the steps of the method for generating a use case of any of the above-mentioned software codes are implemented.
[0018] The present application also provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the method for generating a use case of any of the above-mentioned software codes.
[0019] Through this application, by detecting the test parameters of the reference case set for the software code, using the fact that different reference case sets in the reference case set test different code paths of the software code, resulting in different contributions to the test coverage of the software code by different reference case sets, candidate case sets whose contribution to the test coverage is greater than the target contribution are screened out from the reference case set, and then the target code paths tested by the candidate case sets are obtained. The reference case set is adjusted with the goal of increasing the correlation between the code paths tested by the test cases in the reference case set and the target code paths, thereby obtaining a target case set. That is, by screening candidate case sets with a greater contribution to the test coverage, and adjusting the reference case sets in the reference case set according to the target code paths tested by the candidate case sets, effective test cases are automatically detected and identified, and the execution paths of the effective test cases are used to optimize the test cases, thereby improving the effectiveness of the test cases. This overcomes the problem in the related art of relying on manually written test cases and traditional testing tools, which cannot analyze the trigger conditions of uncovered code, resulting in difficulty in targeted optimization of test cases. Therefore, the technical problem of low test case generation efficiency in the related art can be solved, and the technical effect of improving test case generation efficiency is achieved. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0021] Figure 1 It is a hardware structure block diagram of a computer device for a method for generating a use case of a software code according to an embodiment of the present application;
[0022] Figure 2 is a flowchart of a method for generating a use case of a software code according to an embodiment of the present application;
[0023] Figure 3 This is a flowchart of executing a test task according to a method for generating a use case of a software code according to an embodiment of the present application;
[0024] Figure 4 is a structural diagram of a test knowledge base according to an embodiment of the present application;
[0025] Figure 5 is a structural diagram of a use case generation model according to an embodiment of the present application;
[0026] Figure 6 is a flowchart of uncovered path analysis based on semantic understanding according to an embodiment of the present application;
[0027] Figure 7 is a flow chart of a supplementary test generator generating a use case according to an embodiment of the present application;
[0028] Figure 8 This is a structural block diagram of a device for generating a use case of a software code according to an embodiment of the present application;
[0029] Figure 9 is a schematic diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0030] The following will be combined with the accompanying drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.
[0031] It should be noted that, in the description of this application, the terms "comprises," "includes," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. The terms "first," "second," etc., in this application are used to distinguish similar objects, and are not used to describe a particular order or sequence.
[0032] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below with reference to the accompanying drawings and specific implementation methods.
[0033] The method embodiments provided in the embodiments of the present application can be executed in a server device or a similar computing device. Taking running on a server device as an example, Figure 1 This is a hardware structure block diagram of a computer device for a method of generating a software code use case in an embodiment of the present application. Figure 1 As shown, the server device may include one or more ( Figure 1 Only one is shown) a processor 102 (the processor 102 may include but is not limited to a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data. The server device may also include a transmission device 106 and an input / output device 108 for communication functions. It will be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above server device. Figure 1 More or fewer components than shown, or with Figure 1 Different configurations shown.
[0034] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the method for generating the use case of the software code in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, implementing the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely located relative to the processor 102, and these remote memories can be connected to the server device via a network. Examples of the above-mentioned network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.
[0035] Transmission device 106 is used to receive or transmit data via a network. A specific example of the aforementioned network may include a wireless network provided by a communication provider of the server device. In one embodiment, transmission device 106 includes a network interface controller (NIC), which can be connected to other network devices via a base station to enable communication with the Internet. In another embodiment, transmission device 106 may be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0036] This application proposes a method for generating use cases for software code. Before describing the optional embodiments of this application, to better understand the inventive concept and the inventiveness of this solution, we first explain the related art. For example, taking the code to be tested as online food ordering software, when the integrity and validity of the software code need to be tested, the related art method for generating use cases for software code typically involves: First, based on the requirements document and code logic of the online food ordering software code, the test engineer analyzes the main functional modules contained in the code, such as user registration and login, menu browsing and selection, order generation and payment, order status tracking, merchant order acceptance and processing, and other core functions. Then, a test case is written for each functional module. For the user registration and login function, for example, based on their own experience, the test engineer may consider test cases for normal login scenarios, such as entering the correct account and password, as well as test cases for abnormal scenarios such as entering an incorrect password or when the account does not exist. For the menu browsing and selection function, the test engineer may design test cases for routine operations, such as normal menu browsing and filtering by different categories. After completing the test case writing, the test engineer uses testing tools such as JaCoCo and Istanbul to collect code coverage data when the code executes the test cases. This data is used to evaluate the completeness and effectiveness of the online food ordering software code.
[0037] However, the use case generation method of software code in related technologies has the following technical problems:
[0038] 1) Manually writing test cases is inefficient and prone to missing edge cases. For example, when testing the menu browsing and selection functions, it is easy to overlook the situation where the menu inventory shows "0". This affects the testing quality and cycle time, making potential software defects difficult to detect.
[0039] 2) Traditional testing tools can only collect code coverage data but cannot analyze the triggering conditions of uncovered code. This leaves test engineers with a lack of data support when optimizing test cases, making it difficult to improve test cases in a targeted manner.
[0040] 3) Manually designed test cases have poor applicability. When business scenarios or code patterns change, the original test cases are difficult to directly apply to new scenarios and need to be redesigned and adjusted, which affects testing efficiency.
[0041] To solve the above problems, an embodiment of the present application provides a method for generating a use case of a software code. Figure 2 is a flow chart of a method for generating a use case of a software code according to an embodiment of the present application, such as Figure 2 As shown, the process includes the following steps:
[0042] Step S12: detecting test parameters of the reference use case set for the software code, wherein each reference use case in the reference use case set is used to test a portion of a code path of the software code, and the test parameters are used to indicate the contribution of each reference use case to the test coverage of the software code;
[0043] Step S14, screening out candidate use cases whose test parameters meet the parameter conditions from the reference use case set, wherein the contribution of the use cases whose test parameters meet the parameter conditions to the test coverage is greater than the target contribution;
[0044] Step S16, obtaining the target code path tested by the candidate use case;
[0045] Step S18, adjusting the reference use case set according to the target code path to obtain a target use case set, wherein the correlation between the code path tested by the target use case set and the target code path is greater than the correlation between the code path tested by the reference use case set and the target code path.
[0046] Optionally, in this embodiment, the software code can be, but is not limited to, the code of software that has test integrity and validity requirements, such as transaction processing system code, medical equipment control software code, and energy management system code. In this embodiment, the software code is a transaction processing system code as an example, but the type of software code is not limited. According to actual testing requirements, the software code can also be multiple codes.
[0047] Optionally, in this embodiment, Figure 3 This is a flowchart of a method for generating a use case of a software code according to an embodiment of the present application to perform a test task, such as Figure 3 As shown, the process of executing the test task of the method for generating use cases of software code may include but is not limited to:
[0048] Step S101: access the code library and obtain the software code;
[0049] Step S102: Apply semantic analysis to obtain code semantic understanding results of the software code;
[0050] Step S103, obtaining a code coverage test knowledge base, wherein the test knowledge base stores a variety of test cases;
[0051] Step S104: Obtain an initial set of test cases. This initial set of test cases covers the main functional paths and common boundary conditions of the software code. Based on the code semantic understanding results, historical test case templates similar to the current code structure or business domain are retrieved from the test knowledge base and parameterized to generate the initial set of test cases. The reference set of test cases includes the initial set of test cases.
[0052] Step S105: Execute the test, execute the initial test case, and obtain the coverage data of this test (i.e., test coverage);
[0053] Step S106, analyzing coverage data and performing uncovered path analysis based on semantic understanding;
[0054] Step S107, starting the intelligent feedback mechanism;
[0055] Step S108, generating supplementary test cases;
[0056] Step S109: Update the knowledge base, evaluate the effectiveness of newly generated test cases, convert effective test strategies into structured knowledge items and integrate them into the corresponding levels of the knowledge base, update the knowledge base, evaluate the effectiveness of existing knowledge items, and eliminate invalid knowledge items;
[0057] Step S110, detecting whether the coverage meets the requirement, if it meets the requirement, executing step S111, otherwise executing step S105;
[0058] Step S111, complete the test, and output the final test report and coverage analysis results (test coverage).
[0059] Optionally, in this embodiment, the code acquisition and analysis module can be used to acquire software code and perform preliminary analysis on the software code. The code acquisition and analysis module uses abstract syntax tree technology combined with static code analysis tools to parse the acquired software code, supports code analysis of multiple mainstream programming languages (Java, Python, C++, JavaScript, etc.), and can support new languages through plug-in expansion.
[0060] Optionally, in this embodiment, the code semantic understanding results may include, but are not limited to, the code logic, critical paths, and potential boundary conditions of the software code. The code semantic understanding results may be, but are not limited to, the output results obtained by inputting the software code into a semantic understanding model. The semantic understanding model may be, but is not limited to, a code semantic analysis model built based on Transformer. The model is trained by inputting code samples containing 10 million lines of open source code. It integrates a code-natural language bidirectional mapping model and a code execution flow graph neural network. It can understand the relationship between code comments and implementation, as well as capture the dynamic execution characteristics of the code. It has the ability to understand code logic, identify critical paths, and potential boundary conditions.
[0061] Optionally, in this embodiment, Figure 4 This is a structural diagram of a test knowledge base according to an embodiment of the present application, such as Figure 4As shown, the test knowledge base utilizes a multi-layered architecture to store a variety of test cases. This includes: a code syntax layer that stores programming language syntax rules, API features, and common usage patterns; a code semantic layer that stores function call relationships, data flow dependencies, and control flow graphs; a test pattern layer that stores effective test strategies and use case templates corresponding to different code structures; and a domain knowledge layer that stores testing experience and best practices for specific business domains. This test knowledge base updates its stored test cases through three mechanisms: expert experience encoding, automatic learning, and collaborative updating. It supports applications such as similarity retrieval, pattern matching, and knowledge reasoning. The storage layer utilizes a hierarchical storage structure with multi-dimensional cross-indexing based on a graph database. The indexing layer implements semantic-based knowledge retrieval and supports fuzzy matching and similarity search. The reasoning layer supports knowledge reasoning and uncertainty handling based on a rule engine and probabilistic graph models. The application layer provides a calling interface that allows other modules to access knowledge resources. Furthermore, the knowledge base supports incremental learning, continuously optimizing the quality of knowledge entries through feedback loops. It also implements a knowledge effectiveness evaluation mechanism to regularly eliminate low-value knowledge.
[0062] Optionally, in this embodiment, the reference use case set is a set of test cases to assist the current software testing. These test cases can come from different sources, including but not limited to: test cases of past similar software projects, industry standard test cases, related use cases in open source test case libraries, and test cases built based on the current software code, etc.
[0063] Optionally, in this embodiment, each reference use case in the reference use case set can be used to test part of the code path of the software code, and the test coverage of the software code by the reference use case set (i.e., code coverage) can be obtained by calculating the ratio of the code path tested by the reference use case set to all the code paths of the software code.
[0064] Optionally, in this embodiment, the step of obtaining the test coverage of the software code by the reference use case set includes: executing all reference use cases in the reference use case set; counting the partial execution paths in the software code triggered by each reference use case during the execution process (i.e., the code paths actually executed by the reference use case at runtime); and obtaining the test coverage of the software code by the reference use case set by calculating the ratio of the execution paths in the software code triggered during the execution of all reference use cases to the total execution paths of the software code.
[0065] Optionally, in this embodiment, the test parameters of the reference use case set for the software code can be detected in the following manner, but is not limited to: analyze the test coverage obtained after executing all reference use cases in the reference use case set, obtain the code path actually executed by each reference use case at runtime, and evaluate the contribution of each reference use case to the test coverage based on the actually executed code paths corresponding to all reference use cases, that is, quantify the extent to which each reference use case helps improve the code coverage, and obtain the test parameters.
[0066] Optionally, in this embodiment, the contribution degree of each reference use case can be obtained by calculating the ratio of the number of execution paths of the software code triggered by the reference use case to the total number of execution paths of the software code.
[0067] Optionally, in this embodiment, the target contribution level can be a preset fixed value or a dynamic value that is dynamically adjusted according to the current test coverage level, and is used to determine whether a test case is sufficiently effective for code coverage. Only when the contribution level of a test case is greater than this threshold is the test case considered to be sufficiently effective for code coverage, that is, a test case that is more capable of triggering test scenarios for uncovered paths. Because different reference use cases trigger different partial execution paths in the software code during execution, resulting in different contributions to test coverage, reference use cases with contributions greater than the target contribution level are screened out from the reference use case set and selected as candidate use cases.
[0068] Optionally, in this embodiment, the target code path is the actual executed code path corresponding to the candidate use case, that is, the path that is considered to have an important contribution to the code coverage after screening. The reference use cases in the reference use case set are adjusted according to the target code path. By adjusting the reference use case set, the partial execution path of the software code that can be triggered by the use cases in the set is made as close as possible to the target code path, and a new target use case set is obtained. This means that the contribution of the test cases in the adjusted target use case set to the test coverage is greater than the target contribution, and the test scenarios of uncovered paths can be triggered more effectively.
[0069] This application automatically detects and identifies valid test cases by screening candidate cases that contribute significantly to test coverage, and adjusts the reference cases in the reference case set based on the target code paths tested by the candidate cases. It then optimizes test cases using the execution paths of valid test cases, improving their effectiveness. This overcomes the problem in related technologies of relying on manually written test cases and traditional testing tools that are unable to analyze the triggering conditions of uncovered code, making it difficult to optimize test cases in a targeted manner. This achieves the technical effect of improving test case generation efficiency, thereby solving technical problems such as low test case generation efficiency.
[0070] As an optional solution, test the test parameters of the software code using a reference set of use cases, including:
[0071] S21, obtaining a path novelty parameter, a path complexity parameter, and a path weight parameter corresponding to the code path tested by each reference use case in the reference use case set, wherein the path novelty parameter is used to indicate the novelty of the corresponding code path during the software code testing process; the less frequently the code path is tested during the software code testing process, the higher the novelty; the path complexity parameter is used to indicate the complexity of the internal logic of the corresponding code path; and the path weight parameter is used to indicate the importance of the business function carried by the corresponding code path in the entire software code;
[0072] S22, generating test parameters according to the path novelty parameter, path complexity parameter, and path weight parameter of the code path.
[0073] Optionally, in this embodiment, the test parameters of the reference use case set for the software code can be detected according to the following steps, but are not limited to: first, obtain the test coverage of the reference use case set for the software code, convert the test coverage into a structured form, including the covered path set and the set of uncovered paths , where each execution path is represented as an ordered sequence of nodes , from the set of covered paths Get the code path actually executed by each reference use case at runtime, for example, the code path actually executed by the i-th reference use case at runtime is the covered path set Code path . Then, by evaluating the code path The frequency of testing during the software code testing process, the complexity of the internal logic and the importance of the business functions carried in the entire software code are used to obtain the code path Corresponding path novelty parameters , path complex parameters and path weight parameters , where the path novelty parameter The higher the value, the better the code path. The lower the frequency of testing during the software code testing process, the higher the novelty of the code path, and the higher the path complexity parameter. The higher the value, the better the code path. The more complex the internal logic is, the higher the path weight parameter The higher the value, the better the code path. The more important the business function it carries is in the entire software code; the test parameters are calculated based on the path novelty parameter, path complexity parameter and path weight parameter of the code path.
[0074] Optionally, in this embodiment, after obtaining the path novelty parameter, path complexity parameter, and path weight parameter corresponding to the code path tested by each reference case in the reference case set, test parameters can also be generated according to, but not limited to, the following steps: also obtaining the path length parameter and path dependency parameter corresponding to the code path tested by each reference case in the reference case set, wherein a larger path length parameter indicates more covered logical information, and the path dependency parameter is used to indicate the number of other execution paths or modules that the execution path depends on, and an execution path with a larger path dependency parameter affects more paths or modules. Test parameters are generated based on the path novelty parameter, path complexity parameter, path weight parameter, path length parameter, and path dependency parameter of the code path. Through the above-mentioned multiple execution path-related parameters, the contribution of each reference case to the test coverage can be more comprehensively evaluated, thereby more effectively screening out test cases that contribute more to the test coverage.
[0075] As an optional solution, test parameters are generated based on the path novelty parameter, path complexity parameter, and path weight parameter of the code path, including:
[0076] S31: When the reference use case set includes N reference use cases, and the i-th reference use case among the N reference use cases is used to test the j-th code path in the software code, generate the i-th test parameter of the i-th reference use case through the following steps, where N is an integer greater than 1, i is an integer greater than or equal to 1 and less than or equal to N, and j is an integer greater than or equal to 1:
[0077] S32, obtaining the jth path novelty parameter, the jth path complexity parameter, and the jth path weight parameter corresponding to the jth code path;
[0078] S33 , performing a multiplication operation on the j-th path novelty parameter, the j-th path complexity parameter, and the j-th path weight parameter to obtain the i-th test parameter.
[0079] Optionally, in this embodiment, assuming that the reference use case set includes N reference use cases, the code path actually executed by the i-th reference use case in the N reference use cases at runtime is the j-th code path in the software code. In the case of , the i-th test parameter of the i-th reference case can be generated according to, but not limited to, the following steps: Get the j-th path novel parameter corresponding to the j-th code path , j-th path complexity parameter and the jth path weight parameter , the i-th test parameter is obtained according to the following formula:
[0080] .
[0081] in, is the test parameter of the i-th reference case, indicating its contribution to the test coverage.
[0082] As an optional solution, candidate use cases whose test parameters meet the parameter conditions are screened out from the reference use case set, including:
[0083] S41, detecting whether there is a reference case whose test parameter is greater than a preset threshold in the reference case set;
[0084] S42, when detecting that a reference case with a test parameter greater than a preset threshold exists in the reference case set, determining the reference case with a test parameter greater than the preset threshold as a candidate case, wherein the magnitude of the test parameter is positively correlated with the contribution of each reference case to the test coverage of the software code;
[0085] S43 , when it is detected that there is no reference case with a test parameter greater than a preset threshold in the reference case set, a reference case with a maximum test parameter is selected from the reference case set as a candidate case.
[0086] Optionally, in this embodiment, after executing all reference use cases in the reference use case set, the test parameters corresponding to each reference use case are obtained based on the partial execution path in the software code triggered during the execution of each reference use case. Candidate use cases can be screened out from multiple reference use cases by, but not limited to, the following steps: obtaining a preset threshold, comparing the test parameters of each reference use case with the preset threshold, and when there is a reference use case with a test parameter greater than the preset threshold in the reference use case set, determining the reference use case with a test parameter greater than the preset threshold as a candidate use case. When there is no reference use case with a test parameter greater than the preset threshold in the reference use case set, screening out the reference use case with the largest test parameter from the reference use case set as a candidate use case.
[0087] By screening out candidate use cases that contribute more to test coverage, the test case screening efficiency is improved, unnecessary test cases are reduced, the effectiveness of test cases is ensured, and the test case generation strategy can dynamically adapt to different situations, thereby significantly improving the overall test efficiency and quality.
[0088] As an optional solution, before detecting the test parameters of the reference use case set on the software code, the method includes:
[0089] S51: Input the software code into a use case generation model, wherein the use case generation model includes a code input layer, a semantic understanding layer, a use case generation layer, and a use case output layer. The code input layer is connected to the use case generation layer through the semantic understanding layer, and the use case generation layer is also connected to the use case output layer. The semantic understanding layer is used to extract reference semantic information and reference logical information from the software code input by the code input layer. The reference semantic information is used to indicate the semantics expressed by the software code, and the reference logical information is used to indicate the code logic executed by the software code. The use case generation layer is used to obtain the reference semantic information and reference logical information extracted by the semantic understanding layer, and match one or more reference use cases corresponding to the reference semantic information and reference logical information from a use case database. The use case database records semantic information, logical information, and use cases with corresponding relationships. The use case output layer is used to output one or more reference use cases matched by the use case generation layer.
[0090] S52: Determine one or more reference use cases output by the use case generation model as a reference use case set.
[0091] Optionally, in this embodiment, Figure 5 This is a structural diagram of a use case generation model according to an embodiment of the present application, such as Figure 5 As shown, the use case generation model includes:
[0092] The code input layer can connect to the software code repository with test requirements, obtain the software code to be tested, and perform preliminary analysis on the obtained software code to extract the code structure, variable types, function call relationships and conditional branches in the software code;
[0093] The semantic understanding layer, which includes a semantic understanding model, can extract reference semantic information and reference logical information from the code input layer. Reference semantic information includes the semantics expressed by the software code, such as the function's function and the meaning of variables. Reference logical information includes the execution logic of the software code, such as conditional branches and loop structures.
[0094] The use case database, including the test knowledge base, records the corresponding semantic information, logical information, and test cases. A knowledge elimination and reinforcement mechanism is implemented regularly to ensure the efficiency and timeliness of the use cases in the use case database.
[0095] The use case generation layer, which includes a coverage analyzer, uncovered path analyzer, intelligent feedback analysis engine, and supplementary test generator, is used to match one or more reference use cases from the use case database based on the reference semantic information and reference logic information extracted by the semantic understanding layer;
[0096] Use case output layer: can output the reference use cases matched by the use case generation layer.
[0097] Optionally, in this embodiment, the use case database may include, but is not limited to: utility evaluation, definition of knowledge items The utility function of :
[0098] .
[0099] The factors represent the success rate, coverage gain and scope of application respectively; forgetting curve: the weight of low-utility knowledge items decays over time; knowledge distillation: regularly merge similar knowledge items to extract common rules; anomaly detection: identify abnormal items that conflict with mainstream knowledge and conduct manual review.
[0100] Optionally, in this embodiment, the use case generation layer can have, but is not limited to: extracting test constraints from code semantics and context; automatically determining test strategies based on code complexity and risk level; generating diverse test input data to cover common scenarios, boundary conditions, and abnormal situations; and building complete test scripts, including pre-test preparation, execution steps, and assertion verification.
[0101] Optionally, in this embodiment, the intelligent feedback analysis engine may have, but is not limited to: integrating mainstream coverage tools (such as JaCoCo, Istanbul, etc.) and unifying data formats; implementing incremental coverage calculation and contribution evaluation algorithms; dynamically adjusting test generation strategies based on a reinforcement learning framework; and using gradient boosting decision trees to predict coverage gains for specific test strategies.
[0102] Optionally, in this embodiment, the uncovered path analyzer can have, but is not limited to: converting code execution paths into multi-dimensional semantic feature vectors; clustering and prioritizing paths based on factors such as business criticality, execution complexity, and error propagation impact; identifying code paths that have implicit semantic associations but no direct calling relationships; and generating a structured uncovered path report by comprehensively considering input construction difficulty, environmental dependencies, and trigger condition complexity.
[0103] Optionally, in this embodiment, Figure 6 is a flowchart of uncovered path analysis based on semantic understanding according to an embodiment of the present application, such as Figure 6 As shown, the trigger condition identification of uncovered paths includes:
[0104] Step S601: Path semantic vectorization. Convert the code execution path into a semantic representation vector and introduce a multi-dimensional semantic feature space. , where each dimension represents a specific semantic attribute, such as business function relevance, execution risk, state transition complexity, etc.
[0105] Step S602: semantic cluster analysis. Intelligently cluster uncovered paths based on semantic similarity to construct a semantic cluster graph. ,in: Represents the set of uncovered paths; Represents the semantic association between paths; Represents the association strength weight matrix, which can identify a set of paths with different grammatical structures but similar semantic functions.
[0106] Step S603: critical path priority evaluation. Evaluate each uncovered path The business value and technical importance of the product are calculated as follows:
[0107]
[0108] in This function is a weight coefficient that is adaptively adjusted based on project characteristics, rather than a fixed value. It specifically focuses on state dependency, a factor that is often overlooked in traditional coverage analysis, and can effectively identify high-value test paths related to state transitions.
[0109] Step S604: semantic context association analysis. Constructing the semantic dependency chain of the path , identifies code snippets with implicit semantic associations but no direct calling relationships, and discovers "jump-type" execution paths that traditional static analysis cannot identify. It is suitable for complex interaction scenarios in microservice architectures and event-driven systems.
[0110] Step S605: Path measurability evaluation. Calculate the measurability index of uncovered paths. ,Comprehensively considering the input construction difficulty, environmental ,dependence and the complexity of trigger conditions, it can more ,accurately predict the test difficulty and avoid wasting resources on ,hard-to-test paths.
[0111] Step S606: Based on the above analysis results, a structured uncovered path report is generated, which includes path classification, priority sorting, and test suggestions, providing accurate guidance for subsequent generation of supplementary test cases.
[0112] Optionally, in this embodiment, Figure 7 This is a flow chart of a supplementary test generator generating a use case according to an embodiment of the present application, such as Figure 7As shown, the steps for generating use cases by the supplementary test generator may include, but are not limited to, the following: first, semantically guided test scenario construction is performed, taking as input an uncovered path analysis report containing path semantic features and priorities. The path semantic features are mapped to a predefined test scenario template library, and the output is an abstract scenario description containing the key operation sequence required to trigger the target path. Next, path activation conditions are reversed, extracting all branch conditional expressions from the target path. Hybrid symbolic execution techniques are used to solve the input value range that satisfies the conditions, paying special attention to boundary values and special values such as zero, maximum / minimum values, and overflow values. An improved combinatorial testing algorithm is then used to generate an efficient conditional combinatorial test set. Next, data dependencies are intelligently processed. A dependency graph between test data items is constructed through static and dynamic analysis. Constraints are propagated based on the dependency graph to ensure data consistency. A priority-based conflict resolution strategy is designed to handle data constraint conflicts. Specific test data that meets the requirements is generated based on the constraints. Context-aware test sequence generation is then performed. A system state transition model is constructed based on code analysis, the target path is mapped to state transition requirements, and the optimal operation sequence that triggers the required state transition chain is designed. The sequence validity is verified through lightweight simulation. Next is the intelligent configuration of environmental simulation, which identifies the environmental conditions that path execution depends on and automatically generates environment configuration scripts, including resource configuration, network settings, etc. It designs specific exception triggering mechanisms for exception handling paths and automatically designs thread scheduling strategies for concurrency-related paths. Then comes the incremental optimization mechanism, which evaluates coverage gain after each batch of test cases is generated, dynamically adjusts generation parameters based on the gain data, and maintains a test strategy effect database to support experience accumulation. Finally, test case generation is performed, and the generated test cases are converted into the specified test framework format, annotations and structured information are added to improve maintainability, and batch execution scripts are generated to support automated testing. Through these steps, the supplementary test generator can generate high-quality test cases, effectively improving test coverage and test efficiency.
[0113] Optionally, in this embodiment, taking the online payment system code as an example, a reference use case set can be obtained through the use case generation model through, but not limited to, the following steps: inputting the online payment system code into the code input layer; analyzing the code through the semantic understanding layer to extract reference semantic information (such as user login function, payment processing function and refund processing function) and reference logic information (such as conditional branches including payment success and payment failure; loop structure including multiple payment attempts; exception processing including network exceptions and fund freezes) in the code; the use case generation layer matches corresponding reference use cases from the use case database based on the extracted information, such as: use case 1 - testing user login function, use case 2 - testing normal payment success path, use case 3 - testing payment failure processing path, use case 4 - testing refund processing path under fund freeze state and use case 5 - testing payment processing path under specific network exceptions; the use case output layer outputs the matched reference use cases 1-5 to obtain a reference use case set.
[0114] The use case generation model can automatically extract semantic and logical information from the software code to be tested and generate corresponding test cases, significantly reducing the workload of manually writing test cases. With the help of the semantic understanding layer and the use case generation layer, the generated test cases can more accurately cover the key functions and logical paths of the software code, ensuring the comprehensiveness and effectiveness of the test. In addition, the use case generation model has dynamic adaptability and can generate new test cases in a timely manner based on changes in the software code, ensuring the timeliness and effectiveness of the test cases. Ultimately, the automatically generated test case collection can be directly applied to subsequent test parameter detection and screening, further improving the overall efficiency of the test.
[0115] As an optional solution, adjust the reference use case set according to the target code path to obtain the target use case set, including:
[0116] S61, obtaining a path weight parameter of each code path in the software code from the model parameters of the use case generation model, wherein the path weight parameter is used to indicate the degree of association between the use case generated by the use case generation model and the corresponding code path. The larger the path weight parameter, the higher the degree of association between the use case generated by the use case generation model and the corresponding code path.
[0117] S62, increasing the target path weight parameter corresponding to the target code path to obtain an adjusted target use case generation model;
[0118] S63, inputting the software code into the target use case generation model to obtain a target use case set output by the target use case generation model.
[0119] Optionally, in this embodiment, the target use case set may be, but is not limited to, a test case set that meets the test objectives after adjusting the reference use case set, and the use cases in the target use case set cover the execution path of the software code more comprehensively and effectively.
[0120] Optionally, in this embodiment, the target use case set can be obtained through, but is not limited to, the following steps: First, the path weight parameter of each code path in the software code is obtained from the model parameters of the use case generation model. For example, the weight parameter of the normal order path is 0.8, the weight parameter of the insufficient inventory handling path is 0.7, the weight parameter of the payment failure retry path is 0.6, and the weight parameter of the network interruption handling path is 0.5 (this is an uncovered path). Then, based on the priority feedback strategy, the network interruption handling path is found to be a significant uncovered path. Therefore, the weight parameter of the network interruption handling path is increased from 0.5 to 0.9 to increase the use case generation model's attention to this path. Finally, the software code is input into the adjusted target use case generation model to generate a new target use case set. Due to the increased weight parameter of the network interruption handling path, the model will generate more test cases for this path.
[0121] Optionally, in this embodiment, by analyzing the set of covered paths and the set of uncovered paths, the path weight parameter corresponding to each code path in the software code in the model parameters of the use case generation model is adjusted, thereby increasing the correlation between the use cases generated by the use case generation model and the corresponding code paths. This allows the test cases generated by the adjusted use case generation model to focus more on testing important code paths.
[0122] As an optional solution, after inputting the software code into the target use case generation model to obtain the target use case set output by the target use case generation model, the method further includes:
[0123] S71, extracting new use cases that are different between the target use case set and the reference use case set;
[0124] S72, obtaining new semantic information and new logical information corresponding to the new use case;
[0125] S73, adding the newly added semantic information, newly added logical information and newly added use cases with corresponding relationships to the use case database.
[0126] Optionally, in this embodiment, the use cases in the use case database may be updated by, but not limited to, the following steps: generating a target use case set based on the target use case generation model, calculating the corresponding covered path set of the target use case set; The set of covered paths corresponding to the set of reference use cases The difference between , obtain the new use cases that are different from the target use case set and the reference use case set; for each new use case, obtain the semantic information and logical information of the code path covered by each new use case; convert the new use cases and their corresponding semantic and logical information into structured knowledge entries and add them to the use case database (including the test knowledge base) for subsequent reuse.
[0127] By capturing the target code path, we can precisely identify test focus and ensure that test resources are concentrated on critical code paths. By adjusting the reference test case set based on the target code path and generating a new target test case set, we can dynamically adapt to code changes and complex logic, ensuring the timeliness and effectiveness of test cases. By adding test cases for uncovered paths, we can significantly improve code coverage.
[0128] Path Similarity Assessment: Using a variant of the edit distance, we calculate the similarity between sets of paths, identifying paths with similar structure but different semantics. For example, consider two payment paths: one under a normal network environment and the other under a weak network environment. Path similarity assessment can reveal that these paths are structurally similar but semantically different, allowing for a more accurate assessment of the impact of different network environments on payment path coverage.
[0129] Optionally, in this embodiment, in order to better understand the process of executing the test task by the method for generating the use case of the above-mentioned software code, the process of executing the test task by the method for generating the use case of the above-mentioned software code is described below in combination with the optional embodiment, but it is not used to limit the technical solution of the embodiment of this application.
[0130] In this embodiment, a method for generating a use case of software code is provided, which mainly includes the following steps:
[0131] Step 1: Access the code to be tested and configure parameters. The system integrates with code repositories (such as Git and SVN) and CI / CD pipelines (such as Jenkins and GitHub Actions). Configure test strategy parameters such as target coverage, priority rules, and resource limits. Import domain knowledge and historical test data to initialize the knowledge base. Based on the characteristics of the software code, select an appropriate semantic understanding model and test generation strategy.
[0132] Step 2: Incremental testing and continuous optimization. Intelligent testing is performed on code changes. Code version differences are compared to identify affected code areas. Dependency graph analysis is used to determine the scope of the change. Additional testing resources are allocated to high-risk areas to ensure that existing functionality is not affected by the new changes.
[0133] Step 3: To improve testing efficiency for large projects, the system supports a distributed architecture. The master node is responsible for task scheduling, strategy optimization, and result aggregation; worker nodes generate and execute test cases in parallel; a shared knowledge base enables cross-node knowledge synchronization; and an elastic scaling architecture supports on-demand adjustment of computing resources.
[0134] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method.
[0135] Based on this understanding, the technical solution of this application or the part that contributes to the existing technology can be embodied in the form of a software product. The computer software product is stored in a storage medium (such as ROM / RAM, disk, CD-ROM), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of this application.
[0136] In this embodiment, a device for generating a use case of software code is also provided. The device is used to implement the above-mentioned embodiments and preferred embodiments. The details that have been described will not be repeated here. As used below, the term "module" can refer to a combination of software and / or hardware that implements a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, implementation using hardware, or a combination of software and hardware, is also possible and contemplated.
[0137] Figure 8 is a structural block diagram of a device for generating a use case of a software code according to an embodiment of the present application; Figure 8 Shown, including:
[0138] A detection module 802 is configured to detect test parameters of a reference use case set on the software code, wherein each reference use case in the reference use case set is used to test a portion of a code path of the software code, and the test parameters are used to indicate the contribution of each reference use case to the test coverage of the software code;
[0139] A screening module 804 is configured to screen candidate use cases whose test parameters satisfy parameter conditions from the reference use case set, wherein the use cases whose test parameters satisfy the parameter conditions contribute more to the test coverage than the target contribution;
[0140] A first acquisition module 806 is used to acquire a target code path tested by a candidate use case;
[0141] The adjustment module 808 is used to adjust the reference use case set according to the target code path to obtain the target use case set, wherein the correlation between the code path tested by the target use case set and the target code path is greater than the correlation between the code path tested by the reference use case set and the target code path.
[0142] In an exemplary embodiment, the detection module includes:
[0143] a first acquisition unit, configured to acquire a path novelty parameter, a path complexity parameter, and a path weight parameter corresponding to a code path tested by each reference use case in the reference use case set, wherein the path novelty parameter is used to indicate the novelty of the corresponding code path during the software code testing process; the less frequently the code path is tested during the software code testing process, the higher the novelty; the path complexity parameter is used to indicate the complexity of the internal logic of the corresponding code path; and the path weight parameter is used to indicate the importance of the business function carried by the corresponding code path in the entire software code;
[0144] A generation unit is used to generate test parameters according to a path novelty parameter, a path complexity parameter, and a path weight parameter of the code path.
[0145] In an exemplary embodiment, the generating unit is further configured to:
[0146] When the reference case set includes N reference cases, and the i-th reference case among the N reference cases is used to test the j-th code path in the software code, the i-th test parameter of the i-th reference case is generated by the following steps, where N is an integer greater than 1, i is an integer greater than or equal to 1 and less than or equal to N, and j is an integer greater than or equal to 1:
[0147] Get the j-th path novelty parameter, j-th path complexity parameter, and j-th path weight parameter corresponding to the j-th code path;
[0148] The i-th test parameter is obtained by performing a multiplication operation on the j-th path novelty parameter, the j-th path complexity parameter, and the j-th path weight parameter.
[0149] In an exemplary embodiment, the screening module includes:
[0150] A detection unit, configured to detect whether there is a reference case whose test parameter is greater than a preset threshold in the reference case set;
[0151] a determining unit, configured to, upon detecting that a reference case with a test parameter greater than a preset threshold value exists in the reference case set, determine the reference case with the test parameter greater than the preset threshold value as a candidate case, wherein the magnitude of the test parameter is positively correlated with the contribution of each reference case to the test coverage of the software code;
[0152] The screening unit is used to screen out the reference case with the largest test parameter from the reference case set as a candidate case when it is detected that there is no reference case with a test parameter greater than a preset threshold in the reference case set.
[0153] In an exemplary embodiment, the apparatus further comprises:
[0154] An input module is used to input the software code into a use case generation model before detecting the test parameters of the software code against a reference use case set, wherein the use case generation model includes a code input layer, a semantic understanding layer, a use case generation layer, and a use case output layer. The code input layer is connected to the use case generation layer through the semantic understanding layer, and the use case generation layer is also connected to the use case output layer. The semantic understanding layer is used to extract reference semantic information and reference logical information from the software code input by the code input layer. The reference semantic information is used to indicate the semantics expressed by the software code, and the reference logical information is used to indicate the code logic executed by the software code. The use case generation layer is used to obtain the reference semantic information and reference logical information extracted by the semantic understanding layer, and match one or more reference use cases corresponding to the reference semantic information and reference logical information from the use case database. The use case database records semantic information, logical information, and use cases with corresponding relationships. The use case output layer is used to output one or more reference use cases matched by the use case generation layer.
[0155] The output module is used to determine one or more reference use cases output by the use case generation model as a reference use case set.
[0156] In an exemplary embodiment, the adjustment module includes:
[0157] a second obtaining unit, configured to obtain a path weight parameter of each code path in the software code from the model parameters of the use case generation model, wherein the path weight parameter is used to indicate the degree of association between the use case generated by the use case generation model and the corresponding code path, and a larger path weight parameter indicates a higher degree of association between the use case generated by the use case generation model and the corresponding code path;
[0158] An increasing unit, used to increase a target path weight parameter corresponding to a target code path to obtain an adjusted target use case generation model;
[0159] The input unit is used to input the software code into the target use case generation model to obtain the target use case set output by the target use case generation model.
[0160] In an exemplary embodiment, the apparatus further comprises:
[0161] An extraction module is used to extract new use cases that are different between the target use case set and the reference use case set after inputting the software code into the target use case generation model and obtaining the target use case set output by the target use case generation model;
[0162] The second acquisition module is used to obtain the newly added semantic information and newly added logical information corresponding to the newly added use case;
[0163] The adding module is used to add new semantic information, new logical information and new use cases with corresponding relationships to the use case database.
[0164] It should be noted that the above modules can be implemented through software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
[0165] For the description of the features in the embodiment corresponding to the device for generating a use case of software code, please refer to the relevant description of the embodiment corresponding to the method for generating a use case of software code, which will not be repeated here.
[0166] An embodiment of the present application further provides an electronic device, Figure 9 is a schematic diagram of an electronic device according to an embodiment of the present application, such as Figure 9 As shown, the electronic device includes a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in the embodiment of the method for generating any one of the above-mentioned software code use cases.
[0167] In an exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.
[0168] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail here.
[0169] An embodiment of the present application further provides a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps of the method for generating a use case of any of the above-mentioned software codes at runtime.
[0170] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.
[0171] An embodiment of the present application also provides a computer program product, including a computer program, which, when executed by a processor, implements the steps of the method described in each embodiment of the present application; the computer program product also includes a non-volatile computer-readable storage medium, which stores the computer program, which, when executed by a processor, implements the steps of the method for generating the use case of the software code described in each embodiment of the present application.
[0172] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0173] The above is a detailed introduction to a method for generating a use case of a software code provided by this application. Specific examples are used herein to illustrate the principles and implementation methods of this application. The description of the above embodiments is only used to help understand the method and core ideas of this application. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of this application, several improvements and modifications can be made to this application, and these improvements and modifications also fall within the scope of protection of the claims of this application.
Claims
1. A method for generating a use case of a software code, characterized in that: include: Detecting test parameters of a reference use case set on a software code, wherein each reference use case in the reference use case set is used to test a portion of a code path of the software code, and the test parameters are used to indicate a contribution degree of each reference use case to the test coverage of the software code; Screening out candidate use cases whose test parameters satisfy parameter conditions from the reference use case set, wherein the contribution of the use cases whose test parameters satisfy the parameter conditions to the test coverage is greater than a target contribution; Obtaining a target code path tested by the candidate use case; Adjusting the reference use case set according to the target code path to obtain a target use case set, wherein a correlation between a code path tested by the target use case set and the target code path is greater than a correlation between a code path tested by the reference use case set and the target code path; The test parameters of the software code detected by the reference use case set include: Obtaining a path novelty parameter, a path complexity parameter, and a path weight parameter corresponding to a code path tested by each reference use case in the reference use case set, wherein the path novelty parameter is used to indicate the novelty of the corresponding code path during the testing process of the software code, and the lower the frequency of the code path being tested during the testing process of the software code, the higher the novelty; the path complexity parameter is used to indicate the complexity of the internal logic of the corresponding code path; and the path weight parameter is used to indicate the importance of the business function carried by the corresponding code path in the entire software code; generating the test parameters according to the path novelty parameter, the path complexity parameter, and the path weight parameter of the code path; Alternatively, all the reference use cases in the reference use case set are executed; the code paths actually executed by each reference use case at runtime are counted; and the contribution of the corresponding reference use case to the test coverage is obtained by calculating the ratio of the number of execution paths actually executed by each reference use case at runtime to the total number of execution paths of the software code, thereby obtaining the test parameters.
2. The method according to claim 1, characterized in that Generating the test parameter according to the path novelty parameter, the path complexity parameter, and the path weight parameter of the code path includes: In a case where the reference use case set includes N reference use cases, and the i-th reference use case among the N reference use cases is used to test the j-th code path in the software code, the i-th test parameter of the i-th reference use case is generated by the following steps, where N is an integer greater than 1, i is an integer greater than or equal to 1 and less than or equal to N, and j is an integer greater than or equal to 1: Obtaining the jth path novelty parameter, the jth path complexity parameter, and the jth path weight parameter corresponding to the jth code path; A multiplication operation is performed on the j-th path novelty parameter, the j-th path complexity parameter, and the j-th path weight parameter to obtain the i-th test parameter.
3. The method according to claim 1, characterized in that The step of screening out candidate use cases whose test parameters satisfy parameter conditions from the reference use case set includes: Detecting whether there is a reference case in the reference case set whose test parameter is greater than a preset threshold; In a case where it is detected that the reference case whose test parameter is greater than the preset threshold exists in the reference case set, the reference case whose test parameter is greater than the preset threshold is determined as the candidate case, wherein the magnitude of the test parameter is positively correlated with the contribution of each reference case to the test coverage of the software code; When it is detected that the reference case set does not contain the reference case whose test parameter is greater than the preset threshold, the reference case with the largest test parameter is selected from the reference case set as the candidate case.
4. The method according to claim 1, wherein Before detecting the test parameters of the reference use case set on the software code, the method includes: Inputting the software code into a use case generation model, wherein the use case generation model includes a code input layer, a semantic understanding layer, a use case generation layer, and a use case output layer, wherein the code input layer is connected to the use case generation layer via the semantic understanding layer, and the use case generation layer is also connected to the use case output layer, wherein the semantic understanding layer is used to extract reference semantic information and reference logical information from the software code input by the code input layer, wherein the reference semantic information is used to indicate the semantics expressed by the software code, and the reference logical information is used to indicate the code logic executed by the software code, wherein the use case generation layer is used to obtain the reference semantic information and reference logical information extracted by the semantic understanding layer, and match one or more reference use cases corresponding to the reference semantic information and the reference logical information from a use case database, wherein the use case database records semantic information, logical information, and use cases having corresponding relationships, and wherein the use case output layer is used to output the one or more reference use cases matched by the use case generation layer; One or more reference use cases output by the use case generation model are determined as the reference use case set.
5. The method according to claim 4, characterized in that The adjusting the reference use case set according to the target code path to obtain a target use case set includes: Obtaining a path weight parameter of each code path in the software code from a model parameter of the use case generation model, wherein the path weight parameter is used to indicate a degree of association between a use case generated by the use case generation model and a corresponding code path, and a larger path weight parameter indicates a higher degree of association between the use case generated by the use case generation model and the corresponding code path; Increasing the target path weight parameter corresponding to the target code path to obtain an adjusted target use case generation model; The software code is input into the target use case generation model to obtain the target use case set output by the target use case generation model.
6. The method according to claim 5, characterized in that After inputting the software code into the target use case generation model to obtain the target use case set output by the target use case generation model, the method further includes: Extracting new use cases that are different between the target use case set and the reference use case set; Obtaining new semantic information and new logical information corresponding to the new use case; The newly added semantic information, the newly added logical information, and the newly added use case having corresponding relationships are added to the use case database.
7. A device for generating a use case of a software code, characterized in that: include: a detection module, configured to detect test parameters of a reference use case set on a software code, wherein each reference use case in the reference use case set is used to test a portion of a code path of the software code, and the test parameters are used to indicate a contribution degree of each reference use case to the test coverage of the software code; a screening module, configured to screen candidate use cases whose test parameters satisfy parameter conditions from the reference use case set, wherein the contribution of the use cases whose test parameters satisfy the parameter conditions to the test coverage is greater than a target contribution; A first acquisition module is used to obtain the target code path tested by the candidate use case; an adjustment module, configured to adjust the reference use case set according to the target code path to obtain a target use case set, wherein a correlation between the code path tested by the target use case set and the target code path is greater than a correlation between the code path tested by the reference use case set and the target code path; Wherein, the detection module is further used for: Obtaining a path novelty parameter, a path complexity parameter, and a path weight parameter corresponding to the code path tested by each reference use case in the reference use case set, wherein the path novelty parameter is used to indicate the novelty of the corresponding code path during the software code testing process; the lower the frequency of the code path being tested during the software code testing process, the higher the novelty; the path complexity parameter is used to indicate the complexity of the internal logic of the corresponding code path; and the path weight parameter is used to indicate the importance of the business function carried by the corresponding code path in the entire software code; generating test parameters based on the path novelty parameter, path complexity parameter, and path weight parameter of the code path; Alternatively, all the reference use cases in the reference use case set are executed; the code paths actually executed by each reference use case at runtime are counted; and the contribution of the corresponding reference use case to the test coverage is obtained by calculating the ratio of the number of execution paths actually executed by each reference use case at runtime to the total number of execution paths of the software code, thereby obtaining the test parameters.
8. An electronic device, characterized in that: include: memory for storing computer programs; A processor, configured to implement the steps of the method for generating a use case of the software code according to any one of claims 1 to 7 when executing the computer program.
9. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the method for generating a use case of the software code according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Test case completion method and system and electronic equipment
CN118916274A