Test case generation method and device, computer equipment and storage medium
By building target test case templates and combining historical test data and code analysis, test cases for complex and dynamically changing business rules are generated, which solves the problem that existing technology cannot cope with complex business rules and achieves the comprehensiveness and accuracy of code testing.
Patent Information
- Application Number
- CN202411722089.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-28
- Publication Date
- 2025-02-11
AI Technical Summary
The prior art is difficult to deal with complex and dynamically changing business rules when generating test cases, and cannot meet the needs of code testing.
By obtaining the historical test data of the code to be tested, building the target test case template, combining static and dynamic analysis of the code, improving the initial test case template, and generating test cases for the covered and uncovered code paths.
The generated test cases can effectively deal with complex and dynamic business rules, ensure the comprehensiveness and accuracy of code testing, and meet the needs of current application scenarios.
Smart Images

Figure CN119415437A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of code testing technology, and in particular to a test case generation method, apparatus, computer equipment and storage medium. Background Art
[0002] In order to ensure the continuous and stable operation of the system code, or to verify the execution stability of a newly launched business, it is necessary to generate test cases to test the code through the test cases, thus ensuring the abnormal detection operation of the system code.
[0003] Existing technologies often use random generation in the process of generating test cases, which is difficult to cope with complex and dynamically changing business rules and cannot meet existing code testing needs. Summary of the invention
[0004] Based on this, it is necessary to provide a method, apparatus, computer device and storage medium for generating test cases that can cope with complex and dynamically changing business rules in response to the above technical problems.
[0005] In a first aspect, the present application provides a method for generating a test case. The method comprises:
[0006] Obtain at least one historical test data for the code to be tested;
[0007] Constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0008] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
[0009] In one embodiment, constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested includes:
[0010] Extracting requirement rules for the application scenario of the code to be tested to obtain functional requirements and business rules that meet the application scenario;
[0011] According to the functional requirements, construct an initial test case template for the code to be tested;
[0012] The initial test case template is improved according to the business rules to obtain a target test case template.
[0013] In one embodiment, the step of improving the initial test case template according to the business rule to obtain a target test case template includes:
[0014] Performing logic analysis on the code to be tested to obtain a logic analysis result of the code to be tested;
[0015] The initial test case template is improved according to the logic analysis result and the business rules to obtain a target test case template.
[0016] In one embodiment, the logic analysis process includes code static analysis and code dynamic analysis, and the logic analysis process of the code to be tested is performed to obtain the logic analysis result of the code to be tested, including:
[0017] Performing code static analysis on the code to be tested to obtain a first analysis result for the code to be tested;
[0018] Performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested;
[0019] The first analysis result and the second analysis result are used as logic analysis results of the code to be tested.
[0020] In one of the embodiments, generating code test cases for covered code paths and uncovered code paths of the code to be tested respectively according to the target test case template includes:
[0021] Performing pattern mining on the code to be tested according to the historical test data to obtain test pattern parameters for the code to be tested;
[0022] According to the target test case template and the test mode parameters, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
[0023] In one embodiment, the method further comprises:
[0024] Determining the test priority of each code path according to the problem risk level of each code path of the code to be tested;
[0025] According to the test priority of each code path, the test priority of the code test case corresponding to each code path is determined.
[0026] In a second aspect, the present application also provides a test case generation device. The device comprises:
[0027] An acquisition module, used to acquire at least one historical test data for the code to be tested;
[0028] A construction module, used to construct a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0029] A generation module is used to generate code test cases for covered code paths and uncovered code paths of the code to be tested according to the target test case template.
[0030] In a third aspect, the present application further provides a computer device. The computer device includes a memory and a processor, the memory stores a computer program, and the processor implements the following steps when executing the computer program:
[0031] Obtain at least one historical test data for the code to be tested;
[0032] Constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0033] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
[0034] In a fourth aspect, the present application further provides a computer-readable storage medium. The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0035] Obtain at least one historical test data for the code to be tested;
[0036] Constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0037] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
[0038] In a fifth aspect, the present application further provides a computer program product. The computer program product includes a computer program, and when the computer program is executed by a processor, the following steps are implemented:
[0039] Obtain at least one historical test data for the code to be tested;
[0040] Constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0041] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
[0042] The above-mentioned test case generation method, device, computer equipment and storage medium obtain at least one historical test data for the code to be tested to realize the functional requirements and business rules corresponding to the application scenario of the code to be tested, and construct a target test case template for the code to be tested. Then, according to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively. According to the above content, it can be seen that in the process of generating code test cases, the present application does not adopt a random generation form, but constructs a target test case template according to the functional requirements and business rules corresponding to the application scenario of the code to be tested; then, at least one historical test data of the code to be tested is collected according to the target test case template to generate code test cases; therefore, in the process of generating code test cases, the present application refers to the functional requirements and business rules of the application scenario, so that the code test cases can cope with the complex and changeable requirements and business rules under the current application scenario, and ensures that in the process of testing the code to be tested according to the code test case, the test results for the code to be tested are accurately obtained, so that the test results meet the current application scenario of the code to be tested; and the generated code test cases include code test cases for covered code paths of the code to be tested and code test cases for uncovered code paths, so as to achieve sufficient and effective detection of the code to be tested. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] FIG1 is an application environment diagram of a method for generating a test case provided in an embodiment of the present application;
[0044] FIG2 is a schematic diagram of a flow chart of a method for generating a first test case provided in an embodiment of the present application;
[0045] FIG3 is a schematic diagram of a flow chart of a second test case generation method provided in an embodiment of the present application;
[0046] FIG4 is a schematic diagram of a flow chart of a third test case generation method provided in an embodiment of the present application;
[0047] FIG5 is a schematic diagram of a flow chart of a fourth test case generation method provided in an embodiment of the present application;
[0048] FIG6 is a structural block diagram of a test case generation device provided in an embodiment of the present application;
[0049] FIG. 7 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0051] The method for generating test cases provided in the embodiment of the present application can be applied in the application environment shown in Figure 1. Among them, the terminal 102 communicates with the server 104 via the network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or it can be placed on the cloud or other network servers. By obtaining at least one historical test data for the code to be tested, in order to realize the functional requirements and business rules corresponding to the application scenario of the code to be tested, a target test case template for the code to be tested is constructed, and then, according to the target test case template, a code test case is generated for the covered code path and the uncovered code path of the code to be tested. Among them, the terminal 102 can be, but is not limited to, various personal computers, laptops, smart phones, tablet computers, Internet of Things devices and portable wearable devices, and the Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart car-mounted devices, etc. Portable wearable devices can be smart watches, smart bracelets, head-mounted devices, etc. The server 104 can be implemented with an independent server or a server cluster consisting of multiple servers.
[0052] In one embodiment, as shown in FIG. 2 , a method for generating a test case is provided, which is described by taking the method applied to the server 104 in FIG. 1 as an example, and includes the following steps:
[0053] S201, obtaining at least one historical test data for the code to be tested.
[0054] The historical test data may include, but is not limited to, passed and failed test cases, test input and output data, execution time, code coverage, and error logs.
[0055] It should be noted that in order to ensure that code test cases can be effectively determined based on historical test data in the future, data preprocessing may be performed on the historical test data to ensure the validity of the historical test data.
[0056] Specifically, after obtaining at least one item of historical test data for the code to be tested, the historical test data can be preprocessed, wherein the preprocessing includes operations such as cleaning up invalid data, removing redundant information, and standardizing input and output formats, thereby ensuring the quality and consistency of the historical test data and providing a reliable data foundation for the subsequent generation of code test cases for covered code paths and uncovered code paths of the code to be tested.
[0057] S202: construct a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested.
[0058] It should be noted that the functional requirements and business rules that meet the application scenarios can be obtained by extracting the requirement rules for the application scenarios of the code to be tested; specifically, in the process of extracting the functional requirements, the functional requirements of the insurance business process can be parsed through the big model, including underwriting rules, claim conditions, policy management rules, premium calculation and other specific requirements. In the process of extracting business rules, the big model can be used to extract business rules from historical data, such as order conditions, claim limits, premium calculation logic, etc., and manual confirmation can be performed after extraction to ensure that the test cases meet the current business rules and insurance policies.
[0059] As an example, functional requirements may include policy issuance conditions and claim limits, and business rules may include premium calculation logic; where policy issuance conditions are used to characterize and analyze the policy issuance standards involved in historical data, and extract general conditions and restrictions under special scenarios. Claim limits are used to characterize the claim limits of different insurance types and policies mined through data analysis, ensuring that the generated test cases cover the boundary conditions of the maximum claim limit. Premium calculation logic is used to characterize and identify premium calculation formulas and related factors (such as age, insurance type, insured amount, etc.), ensuring that test cases cover different calculation logics and boundary values.
[0060] To further explain, in order to ensure that the constructed target test case template can meet the functional requirements and business rules of the application scenario, an initial test case template for the code to be tested can be constructed in advance according to the functional requirements; then the initial test case template can be improved according to the business rules to obtain the target test case template.
[0061] S203, generating code test cases for covered code paths and uncovered code paths of the code to be tested according to the target test case template.
[0062] It should be noted that for the covered code paths of the code to be tested, code test cases similar to historical test cases can be generated through the target test case template to achieve repeated verification of the covered code paths and similar situations. For the uncovered code paths of the code to be tested, the test coverage is automatically supplemented through the triggered generated boundary value test, random input test or extreme input test to achieve the generation of multiple types of code test cases, ensuring that new problems in the code to be tested can be effectively discovered in the process of detecting the code to be tested according to the code test cases.
[0063] Further explanation: after determining the code test case, the test priority of each code path can be determined according to the risk level of each code path of the code to be tested; then, according to the test priority of each code path, the test priority of the code test case corresponding to each code path can be determined. In this way, the test operation for the code to be tested can be implemented by using the corresponding code test case according to the test priority.
[0064] In one embodiment of the present application, the code test cases where problems occur can also be recorded based on the test execution results; and the subsequent process of generating code test cases for the covered code paths and uncovered code paths of the code to be tested can be optimized based on the code test cases where problems occur.
[0065] The above-mentioned test case generation method obtains at least one historical test data for the code to be tested to realize the functional requirements and business rules corresponding to the application scenario of the code to be tested, and constructs a target test case template for the code to be tested. Then, according to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively. According to the above content, it can be seen that in the process of generating code test cases, the present application does not adopt a random generation form, but constructs a target test case template according to the functional requirements and business rules corresponding to the application scenario of the code to be tested; then, at least one historical test data of the code to be tested is collected according to the target test case template to generate code test cases; therefore, in the process of generating code test cases, the present application refers to the functional requirements and business rules of the application scenario, so that the code test cases can cope with the complex and changeable requirements and business rules under the current application scenario, and ensures that in the process of testing the code to be tested according to the code test case, the test results for the code to be tested are accurately obtained, so that the test results meet the current application scenario of the code to be tested; and the generated code test cases include code test cases for covered code paths of the code to be tested and code test cases for uncovered code paths, so as to achieve sufficient and effective detection of the code to be tested.
[0066] In one embodiment, as shown in FIG3 , when it is necessary to construct a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested, the following contents may be specifically included:
[0067] S301, extracting requirement rules for the application scenario of the code to be tested, and obtaining functional requirements and business rules that meet the application scenario.
[0068] In one embodiment of the present application, when it is necessary to extract requirement rules for the application scenario of the code to be tested, an analysis operation can be performed on the business process corresponding to the application scenario of the code to be tested and the process processing results corresponding to the business process to extract functional requirements that meet the application scenario.
[0069] In another embodiment of the present application, when it is necessary to extract requirement rules for the application scenario of the code to be tested, the operation logic contained in each historical processing record corresponding to the application scenario of the code to be tested can be extracted and summarized, and business rules that meet the application scenario can be obtained based on the operation logic summary.
[0070] S302: construct an initial test case template for the code to be tested according to functional requirements.
[0071] In one embodiment of the present application, when it is necessary to construct an initial test case template for the code to be tested, the functional requirements can be parsed in natural language to identify key business steps and conditions, and then the key business steps and conditions can be converted into logically executable test steps, and an initial test case template for the code to be tested can be constructed based on the test steps.
[0072] S303, perfecting the initial test case template according to the business rules to obtain a target test case template.
[0073] It should be noted that when it is necessary to improve the initial test case template according to business rules to obtain the target test case template, it may include the following contents: performing logical analysis on the code to be tested to obtain the logical analysis results of the code to be tested; improving the initial test case template according to the logical analysis results and business rules to obtain the target test case template.
[0074] In one embodiment of the present application, when the initial test case template is improved according to the logical analysis results and business rules, it can be verified whether there is any content in the initial test case template that violates the logical analysis results and business rules. If so, the initial test case template is modified according to the logical analysis results and business rules to obtain the target test case template.
[0075] In another embodiment of the present application, when the initial test case template is improved according to the logical analysis results and business rules, it is also possible to verify whether there is content in the logical analysis results and business rules that is not reflected in the initial test case template. If so, the initial test case template is supplemented according to the logical analysis results and business rules to obtain the target test case template.
[0076] Further explanation, the logic analysis result of the code to be tested may include the structure of the code, variable relationship, function call chain, conditional branch, exception handling, etc. Specifically, the logic analysis process includes code static analysis and code dynamic analysis. Therefore, when it is necessary to perform logic analysis on the code to be tested and obtain the logic analysis result of the code to be tested, the following contents may be included: performing code static analysis on the code to be tested to obtain a first analysis result for the code to be tested; performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested; and using the first analysis result and the second analysis result as the logic analysis result of the code to be tested.
[0077] Specifically, the code static analysis and dynamic analysis methods may include the following analysis methods: (1) Abstract syntax tree analysis, wherein the abstract syntax tree analysis may include an AST generation step and an AST analysis process, wherein the AST generation step includes the following two processes: a. Lexical analysis: First, the source code is lexically analyzed to split the code into the smallest grammatical units (tokens), such as keywords, variable names, operators, etc. b. Parsing: These tokens are organized into a hierarchical abstract syntax tree through grammatical rules (usually based on context-free grammars), reflecting the grammatical structure of the code without paying attention to the specific text representation. (2) Data flow analysis method: Specifically, the control flow graph (CFG) generation: First, the code is converted into a control flow graph, which represents the control flow relationship between basic blocks in the program. Each basic block contains a series of statements executed sequentially. Data flow equation establishment: Define the input and output data information for each basic block. By analyzing the definition and use of variables, a data flow equation is established to determine the state of the variables at each program point. Equation solving: Solve data flow equations through iterative algorithms or data flow analysis frameworks to derive the definition and usage relationship of variables in the program.
[0078] The above-mentioned test case generation method obtains functional requirements and business rules that meet the application scenario, so that in the process of generating code test cases, the functional requirements and business rules of the application scenario are referred to, thereby ensuring that in the process of testing the code to be tested according to the code test case, the test results for the code to be tested are accurately obtained, so that the test results meet the current application scenario of the code to be tested.
[0079] In one embodiment, as shown in FIG. 4 , when it is necessary to generate code test cases for covered code paths and uncovered code paths of the code to be tested respectively according to the target test case template, the following contents may be specifically included:
[0080] S401, performing pattern mining for the code to be tested based on historical test data to obtain test pattern parameters for the code to be tested.
[0081] It should be noted that based on historical test data, frequent pattern mining technology is used to analyze common patterns of test failures, boundary conditions and recurring errors to obtain test pattern parameters for the code to be tested; this ensures that corresponding code test cases can be generated based on the code weaknesses indicated in the test pattern parameters, ensuring wider test coverage and greater effectiveness.
[0082] In one embodiment of the present application, the mining methods used for pattern mining of the code to be tested may include: pattern mining algorithm, association rule mining and sequence pattern mining; specifically, the pattern mining algorithm (Frequent Pattern Mining) is a classic pattern mining method that aims to identify patterns that appear frequently in test data; including (1) Apriori algorithm: by iteratively calculating frequent item sets, common input and output patterns, failure case patterns, etc. in test data can be discovered. For example, certain specific input parameter combinations always cause test failures, which can be identified as high-risk combinations. (2) FP-growth algorithm: similar to Apriori, but with higher efficiency in calculating frequent item sets, especially suitable for large-scale test data mining. By building and mining FP-tree (frequent pattern tree), frequent patterns in the test can be quickly discovered. Association rule mining (Association Rule Mining) is used to discover implicit relationships between input conditions and output results. For example, under specific input conditions, the probability of test failure is high, then the system can generate association rules and generate new test cases for targeted verification. Sequence pattern mining can discover patterns in the execution order of test cases. For example, some errors may only occur in a specific execution order. Specifically, this may include (1) Sequential pattern recognition: The system identifies common failure modes by analyzing the execution order and results of historical tests, such as a certain functional test always failing after a certain test scenario, or the system crashing after a series of specific operations. (2) Time constraints: The system can consider the time sequence and interval time of test case execution to better deduce the cause of potential problems.
[0083] To further illustrate, the logic analysis results of the code to be tested obtained by code static analysis and code dynamic analysis can be used as input data for pattern mining; specifically, there can be many types of input data, which are not limited here. As an example, the input data for pattern mining can also include: (1) Historical test data: Contains detailed information of all test cases that have been executed, such as input parameters, expected output, actual output, whether passed, etc. (2) Test execution data: Execution information of each test case, including the number of executions, execution time, execution order, execution environment, etc. (3) Code coverage data: Code coverage data recorded after each test execution, including branch coverage, function call coverage, line coverage, etc. (4) Test logs and error logs: Logs generated during the test process, including running logs of successful tests and detailed error information when errors fail. (5) System performance data: Reflects the performance of the system in different test scenarios, such as memory usage, CPU usage, response time, etc. (6) Business rules or functional requirements documents: System business logic or functional description, which helps pattern mining identify important boundary conditions or test scenarios.
[0084] Accordingly, the output test pattern parameters may include but are not limited to: (1) See pattern recognition results: Frequent patterns: input combinations with high frequency, common error patterns in specific scenarios, etc. Boundary condition patterns: failure cases when input or output is close to certain limit values. (2) Generated new test cases: test cases generated based on pattern mining, covering untested code paths, untested boundary conditions or frequently failed functional modules. (3) Boundary value analysis results: targeted boundary value test results automatically generated by the system to help discover potential problems in code under boundary conditions. (4) Regression test case set: regression test cases generated based on historical error patterns, used to verify whether code modifications introduce new errors or cause existing problems to recur. (5) Test priority sorting recommendations: based on test risks, historical execution data and coverage analysis, the generated test cases are prioritized to ensure that high-risk scenarios are tested first. (6) Pattern mining result analysis report: a comprehensive analysis report that lists common problems, coverage blind spots, potential high-risk areas, etc. discovered by the system through pattern mining.
[0085] S402 , generating code test cases for covered code paths and uncovered code paths of the code to be tested respectively according to the target test case template and the test mode parameters.
[0086] The above-mentioned test case generation method determines the test mode parameters to generate code test cases for the covered code paths and uncovered code paths of the code to be tested according to the target test case template and the test mode parameters, thereby ensuring that the generated code test cases are more in line with the actual situation of the code segment to be tested and the test mode.
[0087] In one embodiment, as shown in FIG. 5 , when it is necessary to generate code test cases for covered code paths and uncovered code paths of the code to be tested, the following contents may be included:
[0088] S501, obtaining at least one historical test data for the code to be tested.
[0089] S502, extracting requirement rules for the application scenario of the code to be tested, and obtaining functional requirements and business rules that meet the application scenario.
[0090] S503: construct an initial test case template for the code to be tested according to the functional requirements.
[0091] S504: Perform code static analysis on the code to be tested to obtain a first analysis result for the code to be tested.
[0092] S505: Perform a dynamic code analysis on the code to be tested to obtain a second analysis result for the code to be tested.
[0093] S506: Use the first analysis result and the second analysis result as logic analysis results of the code to be tested.
[0094] S507, improving the initial test case template according to the logic analysis result and the business rules to obtain the target test case template.
[0095] S508, performing pattern mining for the code to be tested based on historical test data to obtain test pattern parameters for the code to be tested.
[0096] S509 , generating code test cases for covered code paths and uncovered code paths of the code to be tested respectively according to the target test case template and the test mode parameters.
[0097] In one embodiment of the present application, the specific functional requirements and business rules for the insurance industry may include the following: (1) Functional requirements: Policy Issuance; Business rules: The policy issuance process involves a variety of rules, such as customer qualifications, product requirements, insured amount calculation, additional terms, etc. Code test case generation may include: generating test cases based on policy issuance conditions to simulate policy issuance scenarios under different conditions. For example, boundary condition tests are set for parameters such as insured amount, customer age, and health status to verify whether the policy issuance standards are met under different scenarios. Custom boundary value tests: special boundary values are defined according to different insurance products and customer types, such as policy restrictions for high-risk customers, to ensure that test cases cover extreme cases. (2) Functional requirements: Claims Processing; Business rules: The complexity of claims processing lies in the claim limit, accident type, and loss calculation method, and also involves a variety of verification processes. Code test case generation may include: the generated test cases must cover various claim rules, including the claim limit and compensation ratio of each insurance type. Special test conditions are set for different insurance types, such as the claim limit of auto insurance and the claim verification process of critical illness insurance. Boundary value and restriction condition testing: Generate boundary value test cases through dynamic generation and historical data extraction to ensure that the claims process works properly under key conditions such as maximum claim limit and deductible. (3) Functional requirements: Premium calculation (Premium Calculation) Business rules: Premium calculation needs to consider factors such as customer age, gender, insurance type, insured amount, additional insurance, etc., and each factor may affect the premium result in different combinations. Code test case generation may include: using rule-based generation methods to generate corresponding premium calculation test cases for different customer information and insurance combinations. Especially when the insurance type combination is more complex, the system needs to cover all possible premium calculation paths. Dynamic adjustment and risk level testing: For example, for elderly customers or high-value policies, automatically generate test cases to verify whether the risk factor adjustment in the premium calculation is correct. (4) Policy Renewal; Business rules: The renewal process usually includes original policy evaluation, rate update, health status review, etc., and it is necessary to ensure that the terms and premium calculation after renewal comply with the current business rules. Code test case generation may include: Test cases generated during the renewal process need to simulate different original policy states and customer changes to ensure that the system responds correctly to renewal rules under different conditions. Clause update testing: The test generation method needs to include simulation of policy terms or rate changes, and the test cases cover the complete process of rate recalculation and customer information update.
[0098] The above-mentioned test case generation method obtains at least one historical test data for the code to be tested to realize the functional requirements and business rules corresponding to the application scenario of the code to be tested, and constructs a target test case template for the code to be tested. Then, according to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively. According to the above content, it can be seen that in the process of generating code test cases, the present application does not adopt a random generation form, but constructs a target test case template according to the functional requirements and business rules corresponding to the application scenario of the code to be tested; then, at least one historical test data of the code to be tested is collected according to the target test case template to generate code test cases; therefore, in the process of generating code test cases, the present application refers to the functional requirements and business rules of the application scenario, so that the code test cases can cope with the complex and changeable requirements and business rules under the current application scenario, and ensures that in the process of testing the code to be tested according to the code test case, the test results for the code to be tested are accurately obtained, so that the test results meet the current application scenario of the code to be tested; and the generated code test cases include code test cases for covered code paths of the code to be tested and code test cases for uncovered code paths, so as to achieve sufficient and effective detection of the code to be tested.
[0099] It should be understood that, although the steps in the flowcharts involved in the above embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above embodiments may include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.
[0100] Based on the same inventive concept, the embodiment of the present application also provides a test case generation device for implementing the test case generation method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in the embodiments of the device for generating one or more test cases provided below can refer to the limitations of the test case generation method above, and will not be repeated here.
[0101] In one embodiment, as shown in FIG6 , a test case generation device is provided, comprising: an acquisition module 10, a construction module 20 and a generation module 30, wherein:
[0102] The acquisition module 10 is used to acquire at least one historical test data for the code to be tested.
[0103] The construction module 20 is used to construct a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested.
[0104] The generating module 30 is used to generate code test cases for covered code paths and uncovered code paths of the code to be tested according to the target test case template.
[0105] In one embodiment, requirement rules are extracted for the application scenario of the code to be tested to obtain functional requirements and business rules that meet the application scenario;
[0106] Build initial test case templates for the code to be tested based on functional requirements;
[0107] The initial test case template is improved according to business rules to obtain the target test case template.
[0108] Performing logic analysis on the code to be tested to obtain the logic analysis result of the code to be tested;
[0109] The initial test case template is improved according to the logic analysis results and business rules to obtain the target test case template.
[0110] In one embodiment, a code static analysis is performed on the code to be tested to obtain a first analysis result for the code to be tested;
[0111] Performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested;
[0112] The first analysis result and the second analysis result are used as the logic analysis result of the code to be tested.
[0113] Perform pattern mining on the code to be tested based on historical test data to obtain test pattern parameters for the code to be tested;
[0114] According to the target test case template and the test mode parameters, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0115] Determine the testing priority of each code path based on the risk level of each code path of the code to be tested;
[0116] According to the test priority of each code path, the test priority of the code test case corresponding to each code path is determined.
[0117] The above-mentioned test case generation device obtains at least one historical test data for the code to be tested to realize the functional requirements and business rules corresponding to the application scenario of the code to be tested, and constructs a target test case template for the code to be tested. Then, according to the target test case template, it generates code test cases for the covered code paths and uncovered code paths of the code to be tested respectively. According to the above content, it can be seen that in the process of generating code test cases, the present application does not adopt a random generation form, but constructs a target test case template according to the functional requirements and business rules corresponding to the application scenario of the code to be tested; then, at least one historical test data of the code to be tested is collected according to the target test case template to generate code test cases; therefore, in the process of generating code test cases, the present application refers to the functional requirements and business rules of the application scenario, so that the code test cases can cope with the complex and changeable requirements and business rules under the current application scenario, and ensures that in the process of testing the code to be tested according to the code test case, the test results for the code to be tested are accurately obtained, so that the test results meet the current application scenario of the code to be tested; and the generated code test cases include code test cases for covered code paths of the code to be tested and code test cases for uncovered code paths, so as to achieve sufficient and effective detection of the code to be tested.
[0118] Each module in the above test case generation device can be implemented in whole or in part by software, hardware and a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.
[0119] In one embodiment, a computer device is provided, which may be a terminal, and its internal structure diagram may be shown in FIG7. The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit, and an input device. The processor, the memory, and the input / output interface are connected via a system bus, and the communication interface, the display unit, and the input device are connected to the system bus via the input / output interface. The processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal in a wired or wireless manner, and the wireless manner may be implemented through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. When the computer program is executed by the processor, a method for generating a test case is implemented. The display unit of the computer device is used to form a visually visible picture, which may be a display screen, a projection device, or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer device can be a touch layer covering the display screen, or a button, trackball or touchpad set on the computer device shell, or an external keyboard, touchpad or mouse.
[0120] Those skilled in the art will understand that the structure shown in FIG. 7 is merely a block diagram of a partial structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.
[0121] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the following steps are implemented:
[0122] Obtain at least one historical test data for the code to be tested;
[0123] Build a target test case template for the code to be tested based on the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0124] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0125] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0126] Extract requirement rules based on the application scenarios of the code to be tested to obtain functional requirements and business rules that meet the application scenarios;
[0127] Build initial test case templates for the code to be tested based on functional requirements;
[0128] The initial test case template is improved according to business rules to obtain the target test case template.
[0129] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0130] Performing logic analysis on the code to be tested to obtain the logic analysis result of the code to be tested;
[0131] The initial test case template is improved according to the logic analysis results and business rules to obtain the target test case template.
[0132] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0133] Performing code static analysis on the code to be tested to obtain a first analysis result for the code to be tested;
[0134] Performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested;
[0135] The first analysis result and the second analysis result are used as the logic analysis result of the code to be tested.
[0136] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0137] Perform pattern mining on the code to be tested based on historical test data to obtain test pattern parameters for the code to be tested;
[0138] According to the target test case template and the test mode parameters, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0139] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:
[0140] Determine the testing priority of each code path based on the risk level of each code path of the code to be tested;
[0141] According to the test priority of each code path, the test priority of the code test case corresponding to each code path is determined.
[0142] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:
[0143] Obtain at least one historical test data for the code to be tested;
[0144] Build a target test case template for the code to be tested based on the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0145] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0146] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0147] Extract requirement rules based on the application scenarios of the code to be tested to obtain functional requirements and business rules that meet the application scenarios;
[0148] Build initial test case templates for the code to be tested based on functional requirements;
[0149] The initial test case template is improved according to business rules to obtain the target test case template.
[0150] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0151] Performing logic analysis on the code to be tested to obtain the logic analysis result of the code to be tested;
[0152] The initial test case template is improved according to the logic analysis results and business rules to obtain the target test case template.
[0153] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0154] Performing code static analysis on the code to be tested to obtain a first analysis result for the code to be tested;
[0155] Performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested;
[0156] The first analysis result and the second analysis result are used as the logic analysis result of the code to be tested.
[0157] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0158] Perform pattern mining on the code to be tested based on historical test data to obtain test pattern parameters for the code to be tested;
[0159] According to the target test case template and the test mode parameters, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0160] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0161] Determine the testing priority of each code path based on the risk level of each code path of the code to be tested;
[0162] According to the test priority of each code path, the test priority of the code test case corresponding to each code path is determined.
[0163] In one embodiment, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the following steps:
[0164] Obtain at least one historical test data for the code to be tested;
[0165] Build a target test case template for the code to be tested based on the functional requirements and business rules corresponding to the application scenario of the code to be tested;
[0166] According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0167] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0168] Extract requirement rules based on the application scenarios of the code to be tested to obtain functional requirements and business rules that meet the application scenarios;
[0169] Build initial test case templates for the code to be tested based on functional requirements;
[0170] The initial test case template is improved according to business rules to obtain the target test case template.
[0171] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0172] Performing logic analysis on the code to be tested to obtain the logic analysis result of the code to be tested;
[0173] The initial test case template is improved according to the logic analysis results and business rules to obtain the target test case template.
[0174] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0175] Performing code static analysis on the code to be tested to obtain a first analysis result for the code to be tested;
[0176] Performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested;
[0177] The first analysis result and the second analysis result are used as the logic analysis result of the code to be tested.
[0178] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0179] Perform pattern mining on the code to be tested based on historical test data to obtain test pattern parameters for the code to be tested;
[0180] According to the target test case template and the test mode parameters, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested.
[0181] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:
[0182] Determine the testing priority of each code path based on the risk level of each code path of the code to be tested;
[0183] According to the test priority of each code path, the test priority of the code test case corresponding to each code path is determined.
[0184] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with relevant laws, regulations and standards of relevant countries and regions.
[0185] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, etc., but are not limited to this.
[0186] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.
[0187] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.
Claims
1. A method for generating a test case, characterized in that: The method comprises: Obtain at least one historical test data for the code to be tested; Constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested; According to the target test case template, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
2. The method according to claim 1, characterized in that The step of constructing a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested includes: Extracting requirement rules for the application scenario of the code to be tested to obtain functional requirements and business rules that meet the application scenario; According to the functional requirements, construct an initial test case template for the code to be tested; The initial test case template is improved according to the business rules to obtain a target test case template.
3. The method according to claim 2, characterized in that The step of improving the initial test case template according to the business rules to obtain a target test case template includes: Performing logic analysis on the code to be tested to obtain a logic analysis result of the code to be tested; The initial test case template is improved according to the logic analysis result and the business rules to obtain a target test case template.
4. The method according to claim 3, characterized in that The logic analysis process includes code static analysis and code dynamic analysis. The logic analysis process is performed on the code to be tested to obtain the logic analysis result of the code to be tested, including: Performing code static analysis on the code to be tested to obtain a first analysis result for the code to be tested; Performing code dynamic analysis on the code to be tested to obtain a second analysis result for the code to be tested; The first analysis result and the second analysis result are used as logic analysis results of the code to be tested.
5. The method according to claim 1, characterized in that The generating code test cases for the covered code paths and uncovered code paths of the code to be tested respectively according to the target test case template comprises: Performing pattern mining on the code to be tested according to the historical test data to obtain test pattern parameters for the code to be tested; According to the target test case template and the test mode parameters, code test cases are generated for the covered code paths and uncovered code paths of the code to be tested respectively.
6. The method according to claim 1, characterized in that The method further comprises: Determining the test priority of each code path according to the problem risk level of each code path of the code to be tested; According to the test priority of each code path, the test priority of the code test case corresponding to each code path is determined.
7. A test case generation device, characterized in that: The device comprises: An acquisition module, used to acquire at least one historical test data for the code to be tested; A construction module, used to construct a target test case template for the code to be tested according to the functional requirements and business rules corresponding to the application scenario of the code to be tested; A generation module is used to generate code test cases for covered code paths and uncovered code paths of the code to be tested according to the target test case template.
8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
Citation Information
Patent Citations
Test case generation method and device and storage medium
CN112905443A
Test case generation method and device, terminal equipment and storage medium
CN116991711A
Test case generation method and device, equipment and storage medium thereof
CN116991729A
Application program testing method and device, equipment, medium and computer program product
CN117743159A
Code automatic testing method and system, computer equipment and storage medium
CN118585453A
Cited By
Intelligent detection method and system for variable frequency controller for air conditioner
CN120595779A
Test case generation method and device, server and storage medium
CN120631787A
Test case generation method and device, server and storage medium
CN120631787B