Test case generation method and device, electronic equipment and storage medium
Patent Information
- Application Number
- CN202611455262.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-18
- Publication Date
- 2026-10-30
AI Technical Summary
[0003]传统方法主要基于预定义的测试脚本或简单功能规约生成测试用例,缺乏对业务场景的深度理解,导致生成的测试用例往往与实际业务逻辑脱节,难以验证软件在真实应用环境中的行为符合性
[0015]本申请实施例提出的测试用例生成方法、测试用例生成装置、电子设备及计算机可读存储介质,获取被测系统的源代码和测试需求文本,以根据源代码确定被测系统的底层运行逻辑,根据测试需求文本确定测试需求。被测系统的程序逻辑封装在源代码内,为准确理解代码逻辑,对源代码进行代码分析,得到测试对象、测试对象的对象属性以及测试对象之间的对象依赖关系,并根据测试对象、对象属性和对象依赖关系构建被测系统的测试场景图谱。为生成贴合真实业务场景的测试用例,从测试需求文本提取测试场景和测试约束条件,根据测试场景和测试约束条件对测试场景图谱进行图谱查询,以完整挖掘所有代码执行路径和数据依赖,得到测试路径。为提升测试用例覆盖率,引入强化学习技术主动探索新路径,并对测试路径进行优化。基于强化学习技术将测试路径拆分为至少两个业务状态节点,从预设操作空间选取业务状态节点的业务操作,并通过强化学习网络对业务操作进行操作评估,得到操作评估数据,根据操作评估数据对业务操作进行筛选,得到业务状态节点的目标操作,对每个业务状态节点的目标操作进行拼接,得到贴合真实业务场景的目标测试用例,从而提高了测试用例生成的准确性。
Smart Images

Figure CN123011733A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, and is applied to the fields of fintech and healthcare, and particularly to a test case generation method, device, electronic device, and storage medium. Background Technology
[0002] In related technologies, test cases are used to verify whether software functions as expected, ensuring its quality and reliability. Taking fintech scenarios as an example, financial trading systems need to handle massive amounts of financial transactions. Test cases simulating concurrent transactions can be used to stress test these systems, verifying their ability to handle concurrent transaction events. In healthcare scenarios, patient medical data is considered private data. Test cases simulating unauthorized access can be used to test the security of healthcare systems, verifying their authorization and access control capabilities.
[0003] Traditional methods primarily generate test cases based on predefined test scripts or simple functional specifications, lacking a deep understanding of the business scenario. This often results in generated test cases that are disconnected from actual business logic, making it difficult to verify the software's behavioral conformity in real-world application environments. Therefore, improving the accuracy of test case generation has become an urgent problem to be solved. Summary of the Invention
[0004] The main objective of this application is to provide a test case generation method, apparatus, electronic device, and storage medium, aiming to improve the accuracy of test case generation.
[0005] To achieve the above objectives, a first aspect of this application proposes a test case generation method, the method comprising: Obtain the source code and test requirement text of the system under test; The source code is analyzed to obtain the test object, the object attributes of the test object, and the object dependencies between the test objects. A test scenario graph of the system under test is then constructed based on the test object, the object attributes, and the object dependencies. Extract the test scenarios and test constraints from the test requirements text; Based on the test scenario and the test constraints, a graph query is performed on the test scenario graph to obtain the test path; The test path is split into at least two business status nodes. Business operations of the business status nodes are selected from a preset operation space. The business operations are evaluated through a reinforcement learning network to obtain operation evaluation data. The business operations are filtered based on the operation evaluation data to obtain the target operations for the business status nodes; The target operation for each of the aforementioned business status nodes is concatenated to obtain the target test cases.
[0006] In some embodiments, the reinforcement learning network is trained according to the following steps: Initialize the test cases to obtain the initial test cases; Execute the initial test cases to obtain the code coverage, defect detection rate, and execution time of the initial test cases; Based on the code coverage, the defect detection rate, and the execution time, the initial test cases are optimized to obtain candidate test cases; The reinforcement learning network is obtained by training a model on the preset network based on the candidate test cases.
[0007] In some embodiments, optimizing the initial test cases based on the code coverage, the defect detection rate, and the execution time to obtain candidate test cases includes: The initial test cases are hierarchically divided based on the code coverage, the defect detection rate, and the execution time to obtain the test case hierarchy; The initial test cases are selected based on the code coverage, the defect detection rate, the execution time, and the test case hierarchy to obtain the first test cases; The first test case is cross-referenced to obtain the second test case; The second test case is mutated according to a preset mutation probability to obtain the third test case; The candidate test cases are obtained based on the first test case, the second test case, and the third test case.
[0008] In some embodiments, selecting the initial test cases based on the code coverage, the defect detection rate, the execution time, and the test case hierarchy to obtain the first test cases includes: The initial test case is mapped to a point in a preset three-dimensional space based on the code coverage, defect detection rate, and execution time to obtain a test point; wherein, the preset three-dimensional space is a three-dimensional space constructed with the code coverage, defect detection rate, and execution time as dimensions, and the preset three-dimensional space includes uniformly distributed reference points; Calculate the distance between the test point and the reference point; The initial test cases are bound to the reference point based on the distance, and the number of test cases bound to the reference point for the initial test cases is obtained. The initial test cases are selected based on the test case hierarchy and the number of test cases to obtain the first test cases.
[0009] In some embodiments, selecting the initial test cases based on the use case hierarchy and the number of use cases to obtain the first test cases includes: The initial test cases are selected based on the test case hierarchy. Based on the number of test cases, a second selection is made from the first selected initial test cases to obtain the first test cases.
[0010] In some embodiments, the preset network includes a first network and a second network, and the candidate test cases include a first test state, a first test operation, reference reward data for executing the first test operation, and a second test state switched from the first test state to execute the first test operation. The step of training the preset network based on the candidate test cases to obtain the reinforcement learning network includes: The first network is used to evaluate the first test state and the first test operation to obtain predictive evaluation data. The second test operation in the second test state is selected through the second network, and the operation of the second test state and the second test operation is evaluated to obtain initial evaluation data; Calculate the target evaluation data based on the initial evaluation data and the reference reward data; Calculate the target loss data based on the predicted evaluation data and the target evaluation data; The network parameters of the first network are adjusted based on the target loss data to obtain the reinforcement learning network.
[0011] In some embodiments, after concatenating the target operations for each of the service state nodes to obtain target test cases, the method further includes: Cluster the target test cases to obtain test patterns; Execute the target test cases according to the test mode to obtain test case execution data; The effectiveness of the target test cases is evaluated based on the test case execution data to obtain test case evaluation data. The target test cases are updated based on the test case evaluation data.
[0012] To achieve the above objectives, a second aspect of this application provides a test case generation apparatus, the apparatus comprising: The acquisition module is used to acquire the source code and test requirement text of the system under test. The code analysis module is used to perform code analysis on the source code to obtain the test object, the object attributes of the test object, and the object dependencies between the test objects, and to construct a test scenario map of the system under test based on the test object, the object attributes, and the object dependencies. The extraction module is used to extract test scenarios and test constraints from the test requirement text; The query module is used to perform a graph query on the test scenario graph based on the test scenario and the test constraints to obtain the test path; The evaluation module is used to split the test path into at least two business status nodes, select the business operations of the business status nodes from the preset operation space, and evaluate the business operations through a reinforcement learning network to obtain operation evaluation data. The filtering module is used to filter the business operations based on the operation evaluation data to obtain the target operation of the business status node; The splicing module is used to splice the target operations of each of the business status nodes to obtain target test cases.
[0013] To achieve the above objectives, a third aspect of this application provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the method described in the first aspect.
[0014] To achieve the above objectives, a fourth aspect of the present application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described in the first aspect.
[0015] The test case generation method, device, electronic device, and computer-readable storage medium proposed in this application acquire the source code and test requirement text of the system under test. Based on the source code, the underlying operational logic of the system under test is determined, and based on the test requirement text, test requirements are determined. The program logic of the system under test is encapsulated within the source code. To accurately understand the code logic, code analysis is performed on the source code to obtain test objects, their object attributes, and object dependencies. A test scenario graph of the system under test is constructed based on the test objects, their attributes, and their dependencies. To generate test cases that closely match real business scenarios, test scenarios and test constraints are extracted from the test requirement text. The test scenario graph is then queried based on the test scenarios and constraints to fully uncover all code execution paths and data dependencies, thus obtaining the test paths. To improve test case coverage, reinforcement learning technology is introduced to actively explore new paths and optimize the test paths. Based on reinforcement learning technology, the test path is split into at least two business state nodes. Business operations of the business state nodes are selected from the preset operation space, and the business operations are evaluated by the reinforcement learning network to obtain operation evaluation data. Based on the operation evaluation data, the business operations are filtered to obtain the target operations of the business state nodes. The target operations of each business state node are concatenated to obtain target test cases that fit the real business scenario, thereby improving the accuracy of test case generation. Attached Figure Description
[0016] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 This is a schematic diagram of the application environment for the test case generation method provided in the embodiments of this application; Figure 2 This is a flowchart of the test case generation method provided in the embodiments of this application; Figure 3 This is a flowchart of the training process of the reinforcement learning network provided in the embodiments of this application; Figure 4 yes Figure 3 The flowchart of step S330 in the text; Figure 5 yes Figure 4 The flowchart of step S420 in the middle; Figure 6 yes Figure 5 The flowchart of step S540 in the text; Figure 7 yes Figure 3 The flowchart of step S340 in the text; Figure 8 This is another flowchart of the test case generation method provided in the embodiments of this application; Figure 9 This is a schematic diagram of the test case generation device provided in the embodiments of this application; Figure 10 This is a schematic diagram of the hardware structure of the electronic device provided in the embodiments of this application. Detailed Implementation
[0018] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.
[0019] It should be noted that although functional modules are divided in the device schematic diagram and a logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first," "second," etc., in the specification, claims, and the aforementioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.
[0020] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.
[0021] In related technologies, test cases are used to verify whether software functions as expected, ensuring its quality and reliability. Taking fintech scenarios as an example, financial trading systems need to handle massive amounts of financial transactions. Test cases simulating concurrent transactions can be used to stress test these systems, verifying their ability to handle concurrent transaction events. In healthcare scenarios, patient medical data is considered private data. Test cases simulating unauthorized access can be used to test the security of healthcare systems, verifying their authorization and access control capabilities.
[0022] Traditional methods primarily generate test cases based on predefined test scripts or simple functional specifications, lacking a deep understanding of the business scenario. This often results in generated test cases that are disconnected from actual business logic, making it difficult to verify the software's behavioral conformity in real-world application environments. Therefore, improving the accuracy of test case generation has become an urgent problem to be solved.
[0023] Based on this, embodiments of this application provide a test case generation method, a test case generation device, an electronic device, and a computer-readable storage medium, aiming to improve the accuracy of test case generation.
[0024] The test case generation method, test case generation device, electronic device, and computer-readable storage medium provided in this application are specifically described through the following embodiments. First, the test case generation method in this application is described.
[0025] The test case generation method provided in this application relates to the field of artificial intelligence technology. The test case generation method provided in this application can be applied to a terminal, a server, or software running on either a terminal or a server. In some embodiments, the terminal can be a smartphone, tablet, laptop, desktop computer, etc.; the server can be configured as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application implementing the test case generation method, but is not limited to the above forms.
[0026] This application can be used in a wide variety of general-purpose or special-purpose computer system environments or configurations. Examples include: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, and distributed computing environments including any of the above systems or devices. This application can 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. This application can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.
[0027] The test case generation method provided in this application can be applied to, for example... Figure 1In this application environment, the client communicates with the server via a network. The server can obtain the source code and test requirement text of the system under test from the client, perform code analysis on the source code to obtain the test objects, object attributes of the test objects, and object dependencies between the test objects, and construct a test scenario graph of the system under test based on the test objects, object attributes, and object dependencies; extract test scenarios and test constraints from the test requirement text; perform graph lookup on the test scenario graph based on the test scenarios and test constraints to obtain test paths; split the test paths into at least two business state nodes, select business operations for the business state nodes from a preset operation space, and evaluate the business operations through a reinforcement learning network to obtain operation evaluation data; filter the business operations based on the operation evaluation data to obtain the target operations for the business state nodes; and concatenate the target operations for each business state node to obtain the target test cases. In this application, code analysis is performed on the source code of the system under test to accurately understand the underlying code operation logic, obtain the test objects, object attributes of the test objects, and object dependencies between the test objects, and construct a test scenario graph of the system under test based on the test objects, object attributes, and object dependencies. Test scenarios and constraints are extracted from the test requirements text. Based on these scenarios and constraints, a test scenario graph is queried to fully uncover all code execution paths and data dependencies, resulting in test paths. To generate test cases that closely match real-world business scenarios and improve test case coverage, reinforcement learning technology is introduced to actively explore new paths and optimize them, thereby obtaining target test cases and improving the accuracy of test case generation. The client can be, but is not limited to, various personal computers, laptops, smartphones, tablets, and portable wearable devices. The server can be implemented using a standalone server or a server cluster consisting of multiple servers. The following detailed description of specific embodiments further illustrates this application.
[0028] Figure 2 This is an optional flowchart of the test case generation method provided in the embodiments of this application. Figure 2 The method may include, but is not limited to, steps S210 to S270.
[0029] Step S210: Obtain the source code and test requirement text of the system under test; Step S220: Analyze the source code to obtain the test object, the object attributes of the test object, and the object dependencies between the test objects, and construct the test scenario map of the system under test based on the test object, object attributes, and object dependencies. Step S230: Extract test scenarios and test constraints from the test requirement text; Step S240: Perform a graph query on the test scenario graph based on the test scenario and test constraints to obtain the test path; Step S250: The test path is split into at least two business status nodes. Business operations of the business status nodes are selected from the preset operation space, and the business operations are evaluated through a reinforcement learning network to obtain operation evaluation data. Step S260: Filter business operations based on operation evaluation data to obtain the target operations for business status nodes; Step S270: Concatenate the target operations for each business status node to obtain the target test cases.
[0030] Steps S210 to S270, as illustrated in this embodiment, involve code analysis of the source code of the system under test and the construction of a test scenario graph. This allows for a more accurate understanding of the underlying operational logic of the system's code and enhances the depth of understanding of real-world business scenarios. To ensure that test cases meet testing requirements and align with real-world business scenarios, the test scenario graph is queried based on the test scenarios and constraints to obtain test paths. To improve test case coverage, reinforcement learning techniques are introduced to actively explore new paths and optimize these paths, thereby obtaining target test cases and improving the accuracy of test case generation.
[0031] In step S210 of some embodiments, the source code and test requirement text of the system under test are obtained. The system under test is a business program or software service to be tested, such as a financial transaction system or a medical consultation platform. The source code is the program code that implements the system's business logic. For example, the source code of a financial transaction system is used to implement financial transaction business functions such as transfer, deduction, and reconciliation, while the source code of a medical consultation platform is used to implement medical consultation business functions such as online consultation, patient registration, and doctor order taking.
[0032] In step S220 of some embodiments, the source code is converted into a tree structure by a parser to obtain an abstract syntax tree (AST). The AST is traversed to extract three types of target data: test objects, object attributes of test objects, and object dependencies between test objects. Test objects are entities in the business logic that can be tested independently, including entity objects and behavioral objects. Entity objects can be classes, interfaces, enumerations, database entities, etc., while behavioral objects can be service classes, utility classes, controllers, business methods, etc. Object attributes describe the test objects and can be all member variables and semantically defined access fields within the test objects. Object dependencies are the associations arising from mutual references between test objects.
[0033] Construct a relational graph with test objects as graph nodes, object attributes as internal features of nodes, and object dependencies as directed edges to obtain the test scenario graph.
[0034] In step S230 of some embodiments, test scenarios and test constraints are extracted from the test requirement text using a large model. A test scenario is an independently executable, complete business test branch; one test scenario corresponds to an independent test process, such as an order payment scenario under insufficient balance conditions or a patient's online appointment for an outpatient appointment. Test scenarios can be categorized into normal processes, abnormal processes, boundary scenarios, concurrency scenarios, and permission-related scenarios. Test constraints are conditions that limit the execution of the test scenario, such as preconditions, parameter ranges, permission rules, performance indicators, and data validation rules.
[0035] In step S240 of some embodiments, the test objects in the test scenario graph are first filtered according to the test scenario, and the node attributes of the first-filtered test nodes are matched according to the test constraints to obtain the target test objects. The test paths are then determined based on the object dependencies between the target test objects.
[0036] In step S250 of some embodiments, the test path is split into at least two business status nodes according to the splitting rules. The splitting rules can be set according to the actual situation, such as generating a business status node for each new test object; or segmenting the test path according to the business execution stage, with each business process corresponding to a business status node. For example, the financial test path can be divided into an account status node, a business service status node, and an order generation status node.
[0037] The preset operation space includes multiple business operations, and executing these operations will cause changes in the business status. For example, executing the business operation of "subscribing" will cause the business status node to jump from the account status node to the business service status node. A business operation is selected from the preset operation space, and each business status node has a business status. The business status and business operation are input into a reinforcement learning network for operation evaluation, and the Q-value output by the reinforcement learning network is used as the operation evaluation data.
[0038] In step S260 of some embodiments, the business operation with the largest operation evaluation data is selected to obtain the target operation of the business status node.
[0039] In step S270 of some embodiments, the target operations of each business status node are concatenated in the order of their arrangement in the test path to obtain the target test cases.
[0040] Please see Figure 3 In some embodiments, the training process of the reinforcement learning network may include, but is not limited to, steps S310 to S340: Step S310: Initialize test cases to obtain initial test cases; Step S320: Execute the initial test cases to obtain the code coverage, defect detection rate, and execution time of the initial test cases; Step S330: Optimize the initial test cases based on code coverage, defect detection rate, and execution time to obtain candidate test cases; Step S340: Train the model of the preset network according to the candidate test cases to obtain the reinforcement learning network.
[0041] In step S310 of some embodiments, test cases are randomly initialized to obtain initial test cases.
[0042] In step S320 of some embodiments, initial test cases are executed to obtain the code coverage, defect detection rate, and execution time of the initial test cases. Code coverage is the proportion of source code executed by the initial test cases; defect detection rate is the proportion of defects in the source code detected by the initial test cases to the total number of defects; and execution time is the time consumed by the initial test cases from start to finish.
[0043] In step S330 of some embodiments, initial test cases are selected based on code coverage, defect detection rate, and execution time to obtain first test cases. The first test cases include business operations. The business operations in any two first test cases are swapped to obtain a second test case. The second test cases are then mutated according to a preset mutation probability, that is, at least one business operation in the second test cases is replaced with another business operation to obtain a third test case. The first, second, and third test cases are then used as candidate test cases.
[0044] In step S340 of some embodiments, the preset network includes a first network and a second network. Candidate test cases include a first test state, a first test operation, reference reward data for executing the first test operation, and a second test state switched from the first test state by executing the first test operation. The preset network is trained using the candidate test cases to obtain a reinforcement learning network. The first test state indicates the program test state of the source code before executing the first test operation, and includes code coverage bitmaps, interface metadata (such as parameter types, value range encodings, etc.), and historical test operation sequences. The second test state indicates the program test state of the source code after executing the first test operation. The first and second test operations are test operations applied to the source code. The reference reward data is a pre-set reward given for executing the test operation.
[0045] Through the above steps S310 to S340, a reinforcement learning network can be obtained, which can be used to filter business operations.
[0046] Please see Figure 4 In some embodiments, step S330 may include, but is not limited to, steps S410 to S450: Step S410: Based on code coverage, defect detection rate, and execution time, the initial test cases are hierarchically divided to obtain the test case hierarchy; Step S420: Select initial test cases based on code coverage, defect detection rate, execution time, and test case hierarchy to obtain the first test case; Step S430: Cross-validate the first test case to obtain the second test case; Step S440: Mutate the second test case according to the preset mutation probability to obtain the third test case; Step S450: Based on the first test case, the second test case, and the third test case, candidate test cases are obtained.
[0047] In step S410 of some embodiments, initial test cases are mapped to a point in a preset three-dimensional space based on code coverage, defect detection rate, and execution time, thus obtaining a test point. The preset three-dimensional space is horizontally divided into multiple levels, and the level to which the test point belongs is used as the test case level. The closer to the top of the preset three-dimensional space, the larger the test case level.
[0048] In step S420 of some embodiments, the distance between the test point and the reference point is calculated, and initial test cases are bound to the reference point based on the distance. The number of test cases bound to the reference point is then obtained. Initial test cases are first selected based on the test case hierarchy, and second selections are made from the first selected initial test cases based on the number of test cases, resulting in the first test cases.
[0049] In step S430 of some embodiments, the business operations of any two first test cases are interleaved to obtain a second test case.
[0050] In step S440 of some embodiments, each business state node of the second test case is traversed, a random decimal between 0 and 1 is generated, and the random decimal is compared with a preset mutation probability. If the random decimal is less than or equal to the preset mutation probability, mutation is triggered. If the random decimal is greater than the preset mutation probability, the business operation of the current business state node remains unchanged. For the business state node that has triggered mutation, a new business operation is selected to replace the original business operation. This process continues until the last business state node is traversed, resulting in the third test case.
[0051] In step S450 of some embodiments, the first test case, the second test case, and the third test case are selected as candidate test cases.
[0052] Through the above steps S410 to S450, candidate test cases can be obtained, and network training can be performed based on the candidate test cases.
[0053] Please see Figure 5 In some embodiments, step S420 may include, but is not limited to, steps S510 to S540: Step S510: Map the initial test case to a point in a preset three-dimensional space based on code coverage, defect detection rate, and execution time to obtain the test point; wherein, the preset three-dimensional space is a three-dimensional space constructed with code coverage, defect detection rate, and execution time as dimensions, and the preset three-dimensional space includes uniformly distributed reference points; Step S520: Calculate the distance between the test point and the reference point; Step S530: Bind the initial test cases and the reference point according to the distance, and obtain the number of test cases bound to the reference point. Step S540: Select initial test cases based on the test case hierarchy and the number of test cases to obtain the first test case.
[0054] In step S510 of some embodiments, an initial test case is mapped to a point in a preset three-dimensional space based on code coverage, defect detection rate, and execution time, thus obtaining a test point. The preset three-dimensional space is a three-dimensional space constructed with code coverage, defect detection rate, and execution time as dimensions, and includes uniformly distributed reference points.
[0055] In step S520 of some embodiments, the distance between the test point and the reference point is calculated. This distance can be Euclidean distance, Mahalanobis distance, etc.
[0056] In step S530 of some embodiments, the initial test case corresponding to the test point with the smallest distance is bound to the reference point, and the number of test cases bound to the reference point is obtained.
[0057] In step S540 of some embodiments, initial test cases are first selected according to the use case hierarchy, and the first selected initial test cases are second selected according to the number of use cases to obtain the first test cases.
[0058] Through the above steps S510 to S540, a first test case can be obtained, and network training can be performed based on the first test case.
[0059] Please see Figure 6 In some embodiments, step S540 may include, but is not limited to, steps S610 to S620: Step S610: Select the initial test cases according to the test case hierarchy; Step S620: Based on the number of test cases, perform a second selection on the first selected initial test cases to obtain the first test cases.
[0060] In step S610 of some embodiments, a preset number of initial test cases are selected from the use case hierarchy in descending order to perform the first selection, where the preset number is an integer greater than 1.
[0061] In step S620 of some embodiments, in order to ensure the sparsity of test cases, the initial test case with the smallest number of test cases is selected from the initial test cases obtained by the first selection, and then a second selection is made to obtain the first test case. This makes the test cases as widely distributed as possible in different program test state spaces, covering more program behaviors with fewer test cases, thereby improving the probability of defect detection.
[0062] Through the above steps S610 to S620, the diversity of test case generation can be guaranteed.
[0063] Please see Figure 7 In some embodiments, step S340 may include, but is not limited to, steps S710 to S750: Step S710: The first test state and the first test operation are evaluated through the first network to obtain predictive evaluation data; Step S720: Select the second test operation of the second test state through the second network, and evaluate the operation of the second test state and the second test operation to obtain initial evaluation data; Step S730: Calculate the target evaluation data based on the initial evaluation data and the reference reward data; Step S740: Calculate the target loss data based on the prediction assessment data and the target assessment data; Step S750: Adjust the network parameters of the first network according to the target loss data to obtain the reinforcement learning network.
[0064] In step S710 of some embodiments, the first test state and the first test operation are input to the first network for operation evaluation to obtain predictive evaluation data.
[0065] In step S720 of some embodiments, a service operation in a second test state is selected from a preset operation space. The second test state and the service operation are input to the second network for operation evaluation. The service operation with the largest operation evaluation data is selected as the second test operation. The second test state and the second test operation are input to the second network for operation evaluation to obtain initial evaluation data.
[0066] In step S730 of some embodiments, reference reward data for performing the first test operation is obtained, and the initial evaluation data and the reference reward data are summed to obtain the target evaluation data.
[0067] In step S740 of some embodiments, the mean square error between the predicted evaluation data and the target evaluation data is calculated to obtain the target loss data.
[0068] In step S750 of some embodiments, the network parameters of the first network are adjusted to minimize the target loss data, resulting in a reinforcement learning network. The network parameters of the first network are then synchronized to the second network according to a preset period.
[0069] Through the above steps S710 to S750, a reinforcement learning network can be obtained, which can be used to filter business operations and generate accurate test cases.
[0070] Please see Figure 8 In some embodiments, after step S270, the test case generation method may also include, but is not limited to, steps S810 to S840: Step S810: Cluster the target test cases to obtain the test pattern; Step S820: Execute the target test cases according to the test mode to obtain test case execution data; Step S830: Evaluate the effectiveness of the target test cases based on the test case execution data to obtain test case evaluation data; Step S840: Update the target test cases based on the test case evaluation data.
[0071] In step S810 of some embodiments, a clustering algorithm such as K-Means clustering is used to cluster the target test cases, dividing them into different categories to obtain test patterns. Test patterns indicate the type of software testing performed using the target test cases, such as SQL injection test pattern or smoke test pattern.
[0072] In step S820 of some embodiments, different test cases are used under different test modes. The corresponding target test cases are executed according to the test mode to obtain test case execution data. The test case execution data is the result data generated by executing the target test cases, and includes code coverage, defect detection rate, execution time, execution failure rate, etc.
[0073] In step S830 of some embodiments, the test case execution data of the target test case is input into the evaluation model. The evaluation model evaluates the effectiveness of the target test case to obtain test case evaluation data. The evaluation model can be a rule-based model, a machine learning model such as a classification model, etc. The test case evaluation data is a category obtained based on the defect detection effectiveness of the test case, used to indicate whether the target test case is a valid test case, a redundant test case, or an inefficient test case.
[0074] In step S840 of some embodiments, if the test case evaluation data indicates that the target test case is a redundant or inefficient test case, then the target test case is updated according to steps S240 to S270, or the target test case is sent to the program terminal for manual updating by the programmer on the program terminal. If the test case evaluation data indicates that the target test case is a valid test case, then the target test case is stored in the test case library so that it can be reused when performing software testing with the same test mode.
[0075] By following steps S810 to S840, the accuracy of test case generation can be guaranteed.
[0076] Please see Figure 9 This application also provides a test case generation apparatus that can implement the above-described test case generation method. The test case generation apparatus includes: Module 910 is used to obtain the source code and test requirement text of the system under test. The code analysis module 920 is used to perform code analysis on the source code, obtain the test objects, the object attributes of the test objects, and the object dependencies between the test objects, and construct the test scenario map of the system under test based on the test objects, object attributes, and object dependencies. Extraction module 930 is used to extract test scenarios and test constraints from test requirement text; The query module 940 is used to perform a graph query on the test scenario graph based on the test scenario and test constraints to obtain the test path; The evaluation module 950 is used to split the test path into at least two business status nodes, select business operations of business status nodes from the preset operation space, and evaluate the business operations through a reinforcement learning network to obtain operation evaluation data. The filtering module 960 is used to filter business operations based on operation evaluation data to obtain the target operations of business status nodes. The splicing module 970 is used to splice the target operations of each business status node to obtain the target test cases.
[0077] This application also provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the above-described test case generation method. This electronic device can be any smart terminal, including tablet computers, in-vehicle computers, etc.
[0078] Please see Figure 10 , Figure 10 The hardware structure of an electronic device according to another embodiment is illustrated. The electronic device includes: The processor 1010 can be implemented using a general-purpose central processing unit (CPU), microprocessor, application specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application. The memory 1020 can be implemented as a read-only memory (ROM), static storage device, dynamic storage device, or random access memory (RAM). The memory 1020 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 1020 and is called and executed by the processor 1010 using the test case generation method of the embodiments of this application. The input / output interface 1030 is used to implement information input and output; The communication interface 1040 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.). Bus 1050 transmits information between various components of the device (e.g., processor 1010, memory 1020, input / output interface 1030, and communication interface 1040); The processor 1010, memory 1020, input / output interface 1030 and communication interface 1040 are connected to each other within the device via bus 1050.
[0079] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described test case generation method.
[0080] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.
[0081] The test case generation method, device, electronic device, and computer storage medium provided in this application analyze the source code of the system under test and construct a test scenario graph. This allows for a more accurate understanding of the underlying code logic of the system under test and enhances the understanding of real-world business scenarios. To ensure test cases meet testing requirements and align with real-world business scenarios, the test scenario graph is queried based on the test scenarios and constraints to obtain test paths. To improve test case coverage, reinforcement learning technology is introduced to actively explore new paths and optimize these paths, thereby obtaining target test cases and improving the accuracy of test case generation.
[0082] The embodiments described in this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided by the embodiments of this application. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of this application are also applicable to similar technical problems.
[0083] Those skilled in the art will understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of this application, and may include more or fewer steps than shown, or combine certain steps, or different steps.
[0084] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0085] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.
[0086] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.
[0087] It should be understood that in this application, "at least one (item)" means one or more, and "more than" means two or more. "And / or" is used to describe the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one (item) of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one (item) of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.
[0088] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.
[0089] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0090] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.
[0091] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes multiple instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing programs, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0092] It should be noted that any AI models, software tools, or components not belonging to this company appearing in the embodiments of this application are merely illustrative examples and do not represent actual use. All user personal information involved in the embodiments of this application has been authorized (with the knowledge and consent) by the relevant parties or has been fully authorized by all parties, and the executing entity may obtain it through various legal and compliant means. The collection, storage, use, processing, transmission, provision, and disclosure of the information, data, and signals involved all comply with relevant laws and regulations and do not violate public order and good morals.
[0093] The preferred embodiments of the present application have been described above with reference to the accompanying drawings, but this does not limit the scope of the claims of the present application. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and substance of the embodiments of the present application shall be within the scope of the claims of the present application.
Claims
1. A test case generation method, characterized in that, The method includes: Obtain the source code and test requirement text of the system under test; The source code is analyzed to obtain the test object, the object attributes of the test object, and the object dependencies between the test objects. A test scenario graph of the system under test is then constructed based on the test object, the object attributes, and the object dependencies. Extract the test scenarios and test constraints from the test requirements text; Based on the test scenario and the test constraints, a graph query is performed on the test scenario graph to obtain the test path; The test path is split into at least two business status nodes. Business operations of the business status nodes are selected from a preset operation space. The business operations are evaluated through a reinforcement learning network to obtain operation evaluation data. The business operations are filtered based on the operation evaluation data to obtain the target operations for the business status nodes; The target operation for each of the aforementioned business status nodes is concatenated to obtain the target test cases.
2. The method according to claim 1, characterized in that, The reinforcement learning network is trained according to the following steps: Initialize the test cases to obtain the initial test cases; Execute the initial test cases to obtain the code coverage, defect detection rate, and execution time of the initial test cases; Based on the code coverage, the defect detection rate, and the execution time, the initial test cases are optimized to obtain candidate test cases; The reinforcement learning network is obtained by training a model on the preset network based on the candidate test cases.
3. The method according to claim 2, characterized in that, The step of optimizing the initial test cases based on the code coverage, the defect detection rate, and the execution time to obtain candidate test cases includes: The initial test cases are hierarchically divided based on the code coverage, the defect detection rate, and the execution time to obtain the test case hierarchy; The initial test cases are selected based on the code coverage, the defect detection rate, the execution time, and the test case hierarchy to obtain the first test cases; The first test case is cross-referenced to obtain the second test case; The second test case is mutated according to a preset mutation probability to obtain the third test case; The candidate test cases are obtained based on the first test case, the second test case, and the third test case.
4. The method according to claim 3, characterized in that, The process of selecting initial test cases based on code coverage, defect detection rate, execution time, and test case hierarchy to obtain first test cases includes: The initial test cases are mapped to a point in a preset three-dimensional space based on the code coverage, defect detection rate, and execution time, thus obtaining a test point; wherein, the preset three-dimensional space is a three-dimensional space constructed with the code coverage, defect detection rate, and execution time as dimensions, and the preset three-dimensional space includes uniformly distributed reference points; Calculate the distance between the test point and the reference point; The initial test cases are bound to the reference point based on the distance, and the number of test cases bound to the reference point for the initial test cases is obtained. The initial test cases are selected based on the test case hierarchy and the number of test cases to obtain the first test cases.
5. The method according to claim 4, characterized in that, The step of selecting the initial test cases based on the test case hierarchy and the number of test cases to obtain the first test cases includes: The initial test cases are selected based on the test case hierarchy. Based on the number of test cases, a second selection is made from the first selected initial test cases to obtain the first test cases.
6. The method according to claim 2, characterized in that, The preset network includes a first network and a second network. The candidate test cases include a first test state, a first test operation, reference reward data for executing the first test operation, and a second test state switched from the first test state to execute the first test operation. The step of training the preset network based on the candidate test cases to obtain the reinforcement learning network includes: The first network is used to evaluate the first test state and the first test operation to obtain predictive evaluation data. The second test operation in the second test state is selected through the second network, and the operation of the second test state and the second test operation is evaluated to obtain initial evaluation data; Calculate the target evaluation data based on the initial evaluation data and the reference reward data; Calculate the target loss data based on the predicted evaluation data and the target evaluation data; The network parameters of the first network are adjusted based on the target loss data to obtain the reinforcement learning network.
7. The method according to any one of claims 1 to 6, characterized in that, After concatenating the target operations for each of the business state nodes to obtain the target test cases, the method further includes: Cluster the target test cases to obtain test patterns; Execute the target test cases according to the test mode to obtain test case execution data; The effectiveness of the target test cases is evaluated based on the test case execution data to obtain test case evaluation data. The target test cases are updated based on the test case evaluation data.
8. A test case generation device, characterized in that, The device includes: The acquisition module is used to acquire the source code and test requirement text of the system under test. The code analysis module is used to perform code analysis on the source code to obtain the test object, the object attributes of the test object, and the object dependencies between the test objects, and to construct a test scenario map of the system under test based on the test object, the object attributes, and the object dependencies. The extraction module is used to extract test scenarios and test constraints from the test requirement text; The query module is used to perform a graph query on the test scenario graph based on the test scenario and the test constraints to obtain the test path; The evaluation module is used to split the test path into at least two business status nodes, select the business operations of the business status nodes from the preset operation space, and evaluate the business operations through a reinforcement learning network to obtain operation evaluation data. The filtering module is used to filter the business operations based on the operation evaluation data to obtain the target operation of the business status node; The splicing module is used to splice the target operations of each of the business status nodes to obtain target test cases.
9. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the method described in any one of claims 1 to 7.
