Automatic software test case generation method and system based on machine learning
By generating an initial population matrix and dynamically adjusting policy parameters using machine learning-based methods, the problems of insufficient input space coverage and local convergence in existing software testing are solved, achieving efficient automated test case generation and defect discovery.
Patent Information
- Application Number
- CN202511789055.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-01
- Publication Date
- 2026-03-03
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
Existing software testing technologies rely on manually written test cases or simple random generation, resulting in insufficient input space coverage, making it difficult to discover deep-seated defects. Furthermore, automated testing methods are prone to local convergence and cannot adaptively adjust strategy parameters, resulting in insufficient coverage and diversity.
By using machine learning-based methods, an initial population matrix is generated, a set of policy parameters and a set of action operators are constructed, the action selection probability is calculated, the learning rate and diversity incentive weights are dynamically adjusted, and the test case coverage is optimized to ensure high dispersion of the input space and maximized coverage.
It improves the comprehensiveness of testing and the ability to find defects, enhances the efficiency of automated testing, overcomes the problems of local convergence and insufficient coverage, and is suitable for intelligent automated testing of complex software systems.
Smart Images

Figure CN121597578A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software testing technology, and in particular to an automated software test case generation method and system based on machine learning. Background Technology
[0002] Existing software testing techniques mainly rely on manually writing test cases or simply generating test cases randomly, resulting in insufficient input space coverage and difficulty in discovering deep-seated defects.
[0003] Meanwhile, existing automated testing methods are prone to getting stuck in local convergence during iterative optimization, and cannot adaptively adjust strategy parameters according to coverage and diversity metrics; some methods ignore the high dispersion of input variable distribution when generating test cases, resulting in redundant test samples or repeated coverage structures, which limits testing efficiency and defect discovery capabilities.
[0004] Therefore, existing technologies have significant shortcomings in ensuring test comprehensiveness, improving coverage, and adaptive optimization. There is an urgent need for an intelligent testing method that can combine diverse incentives, dynamic strategy adjustment, and coverage structure optimization.
[0005] Chinese Patent Publication No. CN118171288A discloses an advanced vulnerability discovery and automated testing method and system based on a large-scale language model, comprising: constructing a software knowledge language model; generating a first test case set through the software knowledge language model; executing the first test case set to obtain a first test execution status; if the first execution status fails, identifying a target vulnerability; generating a remediation plan through the software knowledge language model based on the target vulnerability and its corresponding target vulnerability code; after determining that the target vulnerability has been remediated, obtaining a new version of the source code; inputting the new version of the source code into the software knowledge language model to obtain a second test case set; executing the second test case set to obtain a second test execution status; and determining the effectiveness of the remediation plan based on the second test execution status.
[0006] Existing software testing techniques rely on fixed model outputs for test case generation, lacking guarantees of high input space dispersion; they do not perform adaptive strategy optimization on execution results, which easily leads to local convergence and insufficient coverage; and the verification accuracy of repair solutions is limited, with insufficient closed-loop optimization. Summary of the Invention
[0007] To address this, the present invention provides an automated software test case generation method and system based on machine learning, which, while ensuring high dispersion of the input space, uses adaptive strategies to iteratively optimize test case coverage and dynamically adjust strategy parameters to overcome the problem of local convergence and inability to adaptively adjust in the prior art.
[0008] To achieve the above objectives, in one aspect, the present invention provides an automated software test case generation method based on machine learning, comprising: Obtain the statement set, branch set, and input variable set of the program under test in the current iteration round, and determine the variable domain of the input variable set based on the variable domain definition method, perform sampling operation on the variable domain, and generate an initial population matrix; Construct an initial set of policy parameters and a set of action operators, obtain the action value vector corresponding to the set of action operators, calculate the selection probability of each action operator based on the initial set of policy parameters and the action value vector, and select the action operator to be applied based on the selection probability. The action operator is applied to the initial population matrix to obtain the action result vector; The statement coverage and branch coverage of the program under test are calculated based on the statement set and the branch set, as well as the diversity index corresponding to the population matrix, to form a comprehensive index vector. The fitness score for the current iteration round is calculated by weighting the comprehensive index vector. Obtain the fitness score sequence of consecutive iterations, and when the fitness score sequence is determined to be in a locally converged state and the diversity index is less than the preset minimum diversity threshold, perform step size update on the initial learning rate class parameters and the initial diversity incentive weights in the initial policy parameter set. When the diversity index is greater than or equal to the minimum diversity threshold and the branch coverage is less than the preset target coverage threshold, the step size update is performed on the coverage initial incentive weight and the diversity initial incentive weight in the strategy parameter set. When the branch coverage reaches the target coverage threshold or the iteration round count reaches the maximum number of rounds, the test dataset with the optimal coverage is output.
[0009] Furthermore, the process of constructing the initial population matrix includes: Based on the variable domain definition method, the variable domain of the input variable set is determined, and a sampling operation is performed on the variable domain to generate a candidate variable value vector; Based on the sampling results, the candidate variable value vectors are arranged and encoded according to the variable order corresponding to the input variable set to form an initial individual vector; Based on the initial individual vectors, an initial population matrix is constructed according to a predetermined matrix size and variable dimension organization method.
[0010] Furthermore, the calculation of the diversity index includes: The diversity index is defined as the degree of dispersion of the population in the input space, which is obtained by calculating the normalized distance between all input vector pairs and taking the average value. Based on the normalized distance function between any two input vectors in the population matrix, calculate the normalized distance value corresponding to the input vector; A global averaging operation is performed on all normalized distance values determined by the normalized distance function to obtain an average distance index characterizing the dispersion of the population in the input space.
[0011] Furthermore, the process of calculating the fitness score for the current iteration round includes: Based on the values of each indicator in the comprehensive indicator vector, assign corresponding weight coefficients to each indicator value and calculate the fitness score for the current iteration round. The values of each indicator in the comprehensive indicator vector are statement coverage, branch coverage, diversity index, and execution cost.
[0012] Furthermore, the process of updating the step size for the initial learning rate class parameters and the diversity initial incentive weights in the initial policy parameter set includes: The dynamic update factor of the initial learning rate class parameter is calculated based on the fitness score sequence of consecutive iterations, and the initial learning rate class parameter is adjusted according to the dynamic update factor to obtain the corrected learning rate class parameter. Based on the diversity index of the population matrix in the current iteration round and the modified learning rate class parameter, the diversity adjustment factor of the initial diversity incentive weight is calculated, and the initial diversity incentive weight is adjusted according to the diversity adjustment factor to obtain the modified diversity incentive weight.
[0013] Furthermore, the process of adjusting according to the aforementioned diversity adjustment factor includes: The diversity adjustment factor is calculated based on the diversity index of the population matrix in the current iteration round and the modified learning rate class parameter, and the diversity adjustment factor is compared with the preset factor reasonable range. When the diversity adjustment factor is less than the lower limit of the preset factor reasonable range, the initial diversity incentive weight is adjusted according to the diversity adjustment factor to obtain the diversity correction incentive weight, which is used for action selection and strategy optimization in subsequent iteration rounds. When the diversity adjustment factor is greater than the upper limit of the preset reasonable range, the current adjustment is terminated, and the diversity adjustment factor is recalculated until it falls within the preset reasonable range.
[0014] Furthermore, the process of performing step-size updates on the coverage initial incentive weights and diversity initial incentive weights in the policy parameter set includes: The coverage adjustment factor of the initial coverage incentive weight is calculated based on the coverage performance sequence of the test sample set in continuous iterations, and the initial coverage incentive weight is updated according to the coverage adjustment factor to obtain the coverage correction incentive weight. Based on the diversity index of the population matrix in the current iteration round and the coverage correction incentive weight, the diversity update factor of the initial diversity incentive weight is calculated, and the initial diversity incentive weight is adjusted according to the diversity update factor to obtain the diversity correction incentive weight.
[0015] Furthermore, the process of outputting the test dataset with optimal coverage includes: Calculate the combined coverage score of statement coverage and branch coverage based on the test sample set of the current iteration round; The test sample set is sorted according to the comprehensive coverage score to determine the coverage ranking sequence; The test samples with the highest coverage scores are extracted from the coverage sorting sequence to form the optimal coverage test dataset.
[0016] Furthermore, the process of constructing the optimal test dataset for coverage includes: A coverage score is generated based on the statement coverage result and branch coverage result corresponding to each test sample, and the coverage scores are sorted to form a coverage score sequence; When the difference in coverage scores between adjacent test samples is greater than a preset coverage difference threshold, the test sample set with the highest coverage score is selected from the coverage score sequence as the test dataset with the optimal coverage. When the difference between adjacent coverage scores is less than or equal to the preset coverage difference threshold, the corresponding test samples are determined to belong to the coverage structure similarity category, and structural duplication removal is performed on the test samples with similar coverage structures. After the removal is completed, the optimal test dataset for coverage is re-determined based on the coverage score of the remaining test samples.
[0017] On the other hand, the present invention also provides a system for an automated software test case generation method based on machine learning, comprising: The parameter acquisition module is used to acquire the set of statements, branches, and input variables of the program under test in the current iteration round, and to determine the variable domain of the input variable set based on the variable domain definition method, and to perform sampling operations on the variable domain to generate an initial population matrix; The strategy initialization module, connected to the parameter acquisition module, is used to construct an initial strategy parameter set and an action operator set, obtain the action value vector corresponding to the action operator set, calculate the selection probability of each action operator based on the initial strategy parameter set and the action value vector, and select the action operator to be applied based on the selection probability. An action module, connected to the parameter acquisition module and the strategy initialization module, is used to apply the action operator to the initial population matrix to obtain the action result vector. The index calculation module, connected to the parameter acquisition module and the action module, is used to calculate the statement coverage rate and branch coverage rate of the program under test, as well as the diversity index corresponding to the population matrix, based on the statement set and the branch set, so as to form a comprehensive index vector. The fitness score calculation module, connected to the index calculation module, is used to calculate the fitness score of the current iteration round based on the weighted calculation of the comprehensive index vector. The convergence adjustment module, connected to the fitness score calculation module, is used to obtain the fitness score sequence of consecutive iterations, and when it is determined that the fitness score sequence is in a local convergence state and the diversity index is less than the preset minimum diversity threshold, it performs step size update on the initial learning rate class parameters and the initial diversity incentive weights in the initial strategy parameter set. The step size update module, connected to the convergence adjustment module, performs step size update on the coverage initial incentive weight and diversity initial incentive weight in the strategy parameter set when the diversity index is greater than or equal to the minimum diversity threshold and the branch coverage rate is less than the preset target coverage rate threshold. The test dataset output module, connected to the step size update module, outputs the test dataset with optimal coverage when the branch coverage reaches the target coverage threshold or the iteration round count reaches the maximum number of rounds.
[0018] Compared with existing technologies, the beneficial effects of this invention are as follows: by acquiring the statement set, branch set, and input variable set of the program to be tested and generating an initial population matrix based on variable domain sampling, constructing a policy parameter set and an action operator set and calculating the action selection probability to select the action operator, applying the action operator to the population matrix to obtain the action result vector, calculating the fitness score based on statement coverage, branch coverage, and diversity index, dynamically adjusting learning rate parameters and diversity incentive weights when local convergence or insufficient coverage is determined, performing coverage and diversity step size updates, and finally outputting the test dataset with optimal coverage; by dynamically adjusting diversity incentives and policies, the invention maximizes test case coverage and ensures high dispersion of the input space, enhances test comprehensiveness and defect discovery capabilities, improves test efficiency and automation level, overcomes the problems of local convergence and insufficient coverage in existing technologies, and is suitable for intelligent automated testing of complex software systems.
[0019] Furthermore, by sampling the true physical or logical domain of the input variables through Latin hypercube sampling, the initial test cases are ensured to have excellent spatial dispersion and high diversity in the multidimensional input space. This effectively avoids the aggregation and redundancy that may be caused by random sampling, and improves the efficiency of automated test case generation and the ability to discover complex defects. Attached Figure Description
[0020] Figure 1 This is a flowchart illustrating the automated software test case generation method based on machine learning, as described in an embodiment of the present invention. Figure 2 A schematic diagram illustrating the process of constructing the initial population matrix in an embodiment of the present invention; Figure 3 This is a flowchart illustrating the process of outputting the test dataset with optimal coverage in an embodiment of the present invention. Figure 4 This is a schematic diagram of the structure of the automated software test case generation system based on machine learning, according to an embodiment of the present invention. Detailed Implementation
[0021] To make the objectives and advantages of the present invention clearer, the present invention will be further described below with reference to embodiments; it should be understood that the specific embodiments described herein are merely for explaining the present invention and are not intended to limit the present invention.
[0022] Preferred embodiments of the present invention will now be described with reference to the accompanying drawings. Those skilled in the art should understand that these embodiments are merely illustrative of the technical principles of the present invention and are not intended to limit the scope of protection of the present invention.
[0023] It should be noted that in the description of this invention, the terms "upper", "lower", "left", "right", "inner", "outer", etc., which indicate directions or positional relationships, are based on the directions or positional relationships shown in the accompanying drawings. This is only for the convenience of description and is not intended to indicate or imply that the device or element must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, it should not be construed as a limitation of this invention.
[0024] Furthermore, it should be noted that, in the description of this invention, unless otherwise explicitly specified and limited, the terms "installation," "connection," and "linking" should be interpreted broadly. For example, they can refer to a fixed connection, a detachable connection, or an integral connection; they can refer to a mechanical connection or an electrical connection; they can refer to a direct connection or an indirect connection through an intermediate medium; and they can refer to the internal connection of two components. Those skilled in the art can understand the specific meaning of the above terms in this invention according to the specific circumstances.
[0025] Please see Figure 1 The diagram shown is a flowchart illustrating an automated software test case generation method based on machine learning according to an embodiment of the present invention. The present invention provides an automated software test case generation method based on machine learning, comprising: Step S1: Obtain the statement set, branch set, and input variable set of the program to be tested in the current iteration round, and determine the variable domain of the input variable set based on the variable domain definition method, perform sampling operation on the variable domain, and generate an initial population matrix; Step S2: Construct an initial policy parameter set and an action operator set, obtain the action value vector corresponding to the action operator set, calculate the selection probability of each action operator based on the initial policy parameter set and the action value vector, and select the action operator to be applied based on the selection probability. Step S3: Apply the action operator to the initial population matrix to obtain the action result vector; Step S4: Calculate the statement coverage and branch coverage of the program under test, as well as the diversity index corresponding to the population matrix, based on the statement set and the branch set, to form a comprehensive index vector; Step S5: Calculate the fitness score for the current iteration round based on the weighted average of the comprehensive index vector; Step S6: Obtain the fitness score sequence of consecutive iterations, and when it is determined that the fitness score sequence is in a locally converged state and the diversity index is less than the preset minimum diversity threshold, perform step size update on the initial learning rate class parameters and the initial diversity incentive weights in the initial policy parameter set. Step S7: When the diversity index is greater than or equal to the minimum diversity threshold and the branch coverage is less than the preset target coverage threshold, perform step size update on the coverage initial incentive weight and diversity initial incentive weight in the strategy parameter set; Step S8: When the branch coverage reaches the target coverage threshold or the iteration round count reaches the maximum number of rounds, output the test dataset with the optimal coverage.
[0026] In this embodiment, the statement set and branch set are obtained through static analysis of the control flow graph, which is the set of all executable instructions and the set of all control flow decision points of the program under test. The initial policy parameter set refers to the three core control parameters in the reinforcement learning policy: exploration rate, coverage incentive weight, and diversity incentive weight, wherein the values of the three parameters are all in the range of [0,1]. The action operator set is a custom set, containing two operation operators: mutation operator and crossover operator; Among them, the mutation operator randomly modifies the value of a single input variable, and the crossover operator combines two parent inputs to generate a new child input; The action value vector is a concept used in reinforcement learning, representing the long-term expected reward of each action in the action set. Its update follows the temporal difference principle. The value range of the action value vector is [0,1], which has the same numerical scale as the fitness score. The action result vector is a custom data structure that records the coverage information obtained from the test data after the action operator is applied to the initial population matrix. The action result vector precisely includes the number of statements actually covered, the number of branches, and the execution cost. The fitness score sequence refers to the sequence of fitness scores calculated in consecutive iterations; this sequence is used for subsequent convergence assessment. The local convergence state is a custom determination, referring to the state in which the standard deviation of the fitness score sequence is less than a preset minimum convergence threshold in consecutive iterations.
[0027] By acquiring the statement set, branch set, and input variable set of the program under test and generating an initial population matrix based on variable domain sampling, a policy parameter set and an action operator set are constructed, and the action selection probability is calculated to select the action operator. The action operator is applied to the population matrix to obtain the action result vector. The fitness score is calculated based on statement coverage, branch coverage, and diversity index. When local convergence or insufficient coverage is determined, the learning rate parameters and diversity incentive weights are dynamically adjusted, and coverage and diversity step size updates are performed. Finally, the test dataset with the optimal coverage is output. By dynamically adjusting diversity incentives and policies, the test case coverage is maximized and the high dispersion of the input space is guaranteed, which enhances the comprehensiveness of testing and the ability to find defects, improves testing efficiency and automation level, overcomes the problems of local convergence and insufficient coverage in existing technologies, and is suitable for intelligent automated testing of complex software systems.
[0028] See Figure 2 The diagram shown is a flowchart of constructing the initial population matrix according to an embodiment of the present invention. Specifically, the process of constructing the initial population matrix includes: Based on the variable domain definition method, the variable domain of the input variable set is determined, and a sampling operation is performed on the variable domain to generate a candidate variable value vector; Based on the sampling results, the candidate variable value vectors are arranged and encoded according to the variable order corresponding to the input variable set to form an initial individual vector; Based on the initial individual vectors, an initial population matrix is constructed according to a predetermined matrix size and variable dimension organization method.
[0029] In this embodiment, the variable domain definition method determines the variable domain of the input variable set based on the data type and business logic constraints of each variable in the input variable set, using the actual physical or logical value range of the variable, rather than the normalized range. For example: if the set of input variables includes a variable representing temperature, its domain may be [0, 100] degrees Celsius; if the set of input variables includes a variable representing the user's age, its domain may be [18, 65] depending on the corresponding data. The sampling operation uses Latin hypercube sampling as the sampling operator. Latin hypercube sampling generates an initial candidate variable value vector in the high-dimensional space of the variable domain to ensure that it has good spatial dispersion in the real physical or logical scope. The candidate variable value vector is a set of unnormalized numerical values with actual physical meaning obtained from the variable domain by the sampling operation; The initial individual vector is a vector representing a single complete test case, formed by arranging and encoding the candidate variable value vectors in the variable order. The encoding method is consistent with the original data type of the variable. The predetermined matrix size refers to the number of rows in the initial population matrix, i.e., the number of individuals and the number of columns, i.e., the variable dimension; the variable dimension organization method refers to the arrangement of the row vectors as individuals and the column vectors as variables in the initial population matrix. For example, the set of input variables contains 5 parameters, such as: [temperature, pressure, user ID, salinity, enable flag]; Perform Latin hypercube sampling 50 times on the domains of these 5 variables; for example, the domain of temperature is [0,100], and the domain of pressure is [50,150]. The sampling results are 50 sets of values within these real physical ranges. These 50 initial individual vectors are used as row vectors, and an initial population matrix is constructed with individuals as rows and variables as columns.
[0030] By sampling the true physical or logical domain of the input variables using Latin hypercube sampling, the initial test cases are ensured to have excellent spatial dispersion and high diversity in the multidimensional input space. This effectively avoids the aggregation and redundancy that may be caused by random sampling, and improves the efficiency of automated test case generation and the ability to discover complex defects.
[0031] Specifically, the calculation of the diversity index includes: The diversity index is defined as the degree of dispersion of the population in the input space, which is obtained by calculating the normalized distance between all input vector pairs and taking the average value. Based on the normalized distance function between any two input vectors in the population matrix, calculate the normalized distance value corresponding to the input vector; A global averaging operation is performed on all normalized distance values determined by the normalized distance function to obtain an average distance index characterizing the dispersion of the population in the input space.
[0032] In this embodiment, the diversity index is a custom index, defined as the degree of dispersion of the population in the input space; the index has a value range of [0,1], is a dimensionless value, and is used to quantify the degree of dispersion of the current test case population in the input space. An input vector pair refers to a pair of vectors consisting of any two distinct initial individual vectors in the initial population matrix; if the population matrix contains N initial individual vectors, the total number of input vector pairs formed is N(N-1) / 2; The normalized distance function is a user-defined function used to calculate the distance between any two input vectors and map the distance value to the dimensionless range [0,1]. In this embodiment, Euclidean distance is used as the basic distance metric, and the Min-Max normalization method is used to map it to [0,1]. Before calculating the normalized distance function, it must be ensured that all input vectors have been feature-scaled in the variable dimension to eliminate the influence of differences in the physical dimensions and numerical scales of different variables. The normalized distance value is the distance between any pair of input vectors calculated based on the normalized distance function, and its value range is limited to [0,1]. The global average operation refers to performing an arithmetic average operation on all normalized distance values determined by the normalized distance function. The result of the operation is the average distance index used to characterize the degree of dispersion of the population in the input space. The average distance index, also known as the diversity index, has its final value range limited to [0,1]. For example, the initial population matrix contains N=50 initial individual vectors; feature scaling is performed on all variable features of the 50 initial individual vectors; the Euclidean distance between all 50*49 / 2=1225 input vector pairs is calculated; the Min-Max normalization method is used to map the 1225 Euclidean distance values to normalized distance values, whose values are limited to the range [0,1]; the arithmetic mean is performed on these 1225 normalized distance values, and the final value obtained is the diversity index for this iteration round.
[0033] By using precise normalized distance and global averaging, the spatial dispersion of the test case population is quantified, the repeatability and redundancy of the population are objectively evaluated, the high efficiency and diversity of the test case set and the uniform and comprehensive coverage of the input space are ensured, and the ability to discover complex defects is effectively improved.
[0034] Specifically, the process of calculating the fitness score for the current iteration round includes: Based on the values of each indicator in the comprehensive indicator vector, assign corresponding weight coefficients to each indicator value and calculate the fitness score for the current iteration round. The values of each indicator in the comprehensive indicator vector are statement coverage, branch coverage, diversity index, and execution cost.
[0035] In this embodiment, the indicators of the comprehensive indicator vector include: statement coverage SC, branch coverage BC, diversity indicator TD and execution cost E. These indicators have all been normalized and their value range is strictly limited to [0,1], and they are dimensionless values. The execution cost E is used to measure the resources consumed by the execution of the current test case; The execution cost E is the weighted sum of CPU time and memory resources consumed by the test case during the execution of the program under test; The calculation of the execution cost E requires measurement, normalization, and weighted summation. Accurately measure two raw cost metrics: raw CPU time and total CPU time required for test cases to complete execution, in milliseconds; The raw memory resources are the maximum memory peak reached by the program under test during the execution of the test cases, in megabytes. The normalization method involves applying min-max normalization to each original index. ; in, and These are the minimum and maximum values of a specific metric in a pre-defined historical test set or benchmark test, respectively, to obtain the normalized time. and normalized memory Both of them take values in the range of [0,1]. The execution cost E is calculated as and The linear weighted sum is calculated using the following formula: ; Central processor time weighting is used to determine the proportion of normalized central processor time in the total execution cost E calculation; W M Memory resource weights are used to determine the proportion of normalized memory resources in the calculation of the total execution cost E; At the same time W T +W M =1, and W T and W M All are in [0,1]; In the preferred settings of the embodiment, if the main bottleneck of the system is time rather than memory, which is a common situation in many automated tests, the weights are preferably set as follows: CPU time weight W T The memory resource weight W is 0.60. M It is 0.40; The weight coefficients refer to the weights α, β, γ, δ assigned to each index value in the comprehensive index vector; the values of the four weights are all within [0,1], and α+β+γ+δ=1; To maximize branch coverage and ensure population diversity, the preferred weighting coefficients are set as follows: branch coverage weight β is preferred to be 0.40; diversity index weight γ is preferred to be 0.35; statement coverage weight α is preferred to be 0.15; execution cost weight δ is preferred to be 0.10. The fitness score F t It is obtained by performing a linear weighted summation of the values of each indicator in the comprehensive indicator vector and their corresponding weight coefficients: ; Fitness score F t The final value range is limited to [0,1]. It is a dimensionless value that measures the overall quality of the test cases generated in the current iteration round. Assuming the calculated metrics for a given round are: SC=0.80, BC=0.60, TD=0.50, and the normalized execution cost E=0.30, then the fitness score F is calculated. t It is 0.505.
[0036] By using precise weighted statement coverage, branch coverage, diversity, and execution cost, complex test objectives are quantified into single, optimizable dimensionless values, providing comprehensive and accurate optimization signals for action selection and value updates in reinforcement learning.
[0037] Specifically, the process of updating the step size for the initial learning rate parameters and diversity initial incentive weights in the initial policy parameter set includes: The dynamic update factor of the initial learning rate class parameter is calculated based on the fitness score sequence of consecutive iterations, and the initial learning rate class parameter is adjusted according to the dynamic update factor to obtain the corrected learning rate class parameter. Based on the diversity index of the population matrix in the current iteration round and the modified learning rate class parameter, the diversity adjustment factor of the initial diversity incentive weight is calculated, and the initial diversity incentive weight is adjusted according to the diversity adjustment factor to obtain the modified diversity incentive weight.
[0038] In this embodiment, the following two conditions must be met: The standard deviation of the fitness score sequence in consecutive iterations is less than the minimum convergence threshold, which is 0.01 in this embodiment; preferably, the value of the consecutive iterations k is limited to 10; The diversity index of the current iteration round is less than the minimum diversity threshold, which is 0.2 in this embodiment; In this embodiment, the initial learning rate parameter specifically refers to the exploration rate ε in the reinforcement learning strategy, and its value range is [0,1]; the diversity initial incentive weight refers to the fitness score. ; Weighting coefficients corresponding to diversity indicators The value range is [0,1]; The dynamic update factor DF is a dimensionless value calculated based on the convergence magnitude of the fitness score sequence, used to determine the adjustment ratio of the exploration rate ε. The dynamic update factor is determined by the standard deviation of the fitness score sequence. The proportional relationship with the convergence threshold was calculated as follows: ; The value range of DF is limited to [0,1]; when equal When DF equals 0, it is determined that there is no local convergence, and no exploratory adjustment is performed. when When DF equals 0, DF equals 1; at this time, the fitness score remains unchanged, the system is in the theoretical maximum convergence state, and the maximum exploratory adjustment intensity is required; The magnitude of DF decreases monotonically in the interval [0,1], reflecting the intensity of the stimulus adjustment to the exploratory parameter; The preferred step size for adjusting the exploration rate is 0.05, resulting in a corrected exploration rate. Within the range [0,1]; Diversity adjustment factor It is based on the current diversity index TD and the revised exploration rate. The product is calculated using the following formula: ; The diversity adjustment factor is used to amplify the impact of insufficient TD on diversity weights. The incentive, and the incentive intensity increases with Increase and increase, The value range is [0,1]; The initial incentive weights for diversity are adjusted based on the diversity adjustment factor and the preset step size to obtain the corrected incentive weights for diversity, calculated using the following formula: ; The preferred adjustment step size for diversity weights is 0.02. After correction... Within the range [0,1].
[0039] By dynamically updating factors, the convergence degree is precisely quantified, enabling adaptive and nonlinear stimulus adjustment of exploration parameters. This effectively avoids convergence traps and ensures the continuous exploration capability and final coverage of the test case generation process.
[0040] Specifically, the process of adjusting according to the diversity adjustment factor includes: The diversity adjustment factor is calculated based on the diversity index of the population matrix in the current iteration round and the modified learning rate class parameter, and the diversity adjustment factor is compared with the preset factor reasonable range. When the diversity adjustment factor is less than the lower limit of the preset factor reasonable range, the initial diversity incentive weight is adjusted according to the diversity adjustment factor to obtain the diversity correction incentive weight, which is used for action selection and strategy optimization in subsequent iteration rounds. When the diversity adjustment factor is greater than the upper limit of the preset reasonable range, the current adjustment is terminated, and the diversity adjustment factor is recalculated until it falls within the preset reasonable range.
[0041] In this embodiment, the diversity adjustment factor is calculated based on the current diversity index and the corrected exploration rate, and its calculation formula is as follows: ; The value range is [0,1]; The preset reasonable range for factors is a user-defined sub-interval that is strictly less than the range [0,1]. This interval is used to limit The actual scope of effectiveness, preventing extreme TD or Value leads to Too large or too small, thus ensuring that the adjustment step size of the diversity weight remains within a stable and effective range; Lower limit of the interval The preferred setting is 0.1, the upper limit of the interval. The preferred setting is 0.8; Diversity of initial incentive weights This refers to the weight of the diversity indicator TD in the fitness score calculation before adjustment; Diversity-modified incentive weights This refers to the new diversity weights used in subsequent iterations after adjustment, and their calculation formula is as follows: ; in, The diversity adjustment factor is adjusted when its calculation result exceeds the preset reasonable range of the factor. At that time, the correction value is determined according to the correction rules; This ensures that adjustments to the weighting of diversity incentives will not be affected by... Values that are too small or too large can cause instability or over-incentivization in strategy updates; for example, if the diversity adjustment factor is less than the lower limit of a preset reasonable range, and the technical limitation is to prevent the adjustment effect from being completely lost, then... The value is assigned to the lower limit of the interval. Even if the calculated value of the diversity adjustment factor is extremely low, it is guaranteed that at least a minimum effective adjustment strength of 0.1 will be used to update the diversity weights. To maintain the minimum responsiveness of the strategy.
[0042] By setting a preset reasonable range for factors and an upper and lower limit comparison mechanism, the intensity of diversity incentives is precisely and safely corrected, preventing sudden changes in incentive weights and instability in strategy updates caused by extreme calculation values. This ensures that the adjustment step size of diversity weights in each iteration remains within a stable, effective, and controllable range.
[0043] Specifically, the process of updating the step size of the coverage initial incentive weights and diversity initial incentive weights in the policy parameter set includes: The coverage adjustment factor of the initial coverage incentive weight is calculated based on the coverage performance sequence of the test sample set in continuous iterations, and the initial coverage incentive weight is updated according to the coverage adjustment factor to obtain the coverage correction incentive weight. Based on the diversity index of the population matrix in the current iteration round and the coverage correction incentive weight, the diversity update factor of the initial diversity incentive weight is calculated, and the initial diversity incentive weight is adjusted according to the diversity update factor to obtain the diversity correction incentive weight.
[0044] In this embodiment, the coverage performance sequence is a sequence composed of branch coverage rates from a continuous k-round test sample set, used to determine the growth trend of the coverage rate; the coverage adjustment factor is a dimensionless value calculated based on the average growth rate of the coverage performance sequence, used to determine the adjustment ratio of the initial incentive weight for coverage, and the calculation formula is: ; in, is the target branch coverage threshold, and is the desired coverage level; in this embodiment, it is set to 0.9. The value of CF ranges from [0,1]. A larger value indicates that a larger coverage adjustment stimulus is required. The initial coverage stimulus weights are adjusted according to the coverage adjustment factor and the preset step size to obtain the coverage correction stimulus weights, calculated using the following formula: ; The preferred setting is 0.03. The preferred setting is 0.05; The diversity update factor DF is based on the current diversity index TD and coverage adjustment incentive weights. The product is calculated as follows: The initial incentive weights for diversity are adjusted based on the diversity update factor and the preset step size to obtain the corrected incentive weights for diversity. ; Adjustment step size of diversity weights The preferred setting is 0.02.
[0045] The coverage adjustment factor is calculated by covering performance sequence, and the coverage incentive weight is dynamically adjusted in step size during the iteration process so that the coverage growth trend can be amplified in time. The diversity update factor is calculated by combining diversity index and coverage adjustment incentive weight to realize synchronous adjustment of diversity incentive, so that the search space expansion direction forms a linkage feedback between coverage-driven and diversity-driven approaches.
[0046] See Figure 3 As shown, it is a flowchart illustrating the process of outputting the test dataset with optimal coverage in an embodiment of the present invention; Specifically, the process of outputting the test dataset with optimal coverage includes: Calculate the combined coverage score of statement coverage and branch coverage based on the test sample set of the current iteration round; The test sample set is sorted according to the comprehensive coverage score to determine the coverage ranking sequence; The test samples with the highest coverage scores are extracted from the coverage sorting sequence to form the optimal coverage test dataset.
[0047] In this embodiment, based on the execution results of the test sample set of the current iteration round, the statement coverage rate and branch coverage rate are calculated respectively, and linear weighted integration is performed according to the preset weight coefficients to obtain the comprehensive coverage score; wherein, the statement coverage rate and branch coverage rate are both normalized dimensionless indicators, the sum of the statement coverage rate weight and the branch coverage rate weight is 1, preferably set to 0.40 and 0.60 respectively; the comprehensive coverage score is limited to the range of [0,1]; The test samples are sorted according to the comprehensive coverage score to form a coverage ranking sequence; the sorting is performed in an incremental update manner, so that the ranking position of each new test sample can be determined after local comparison; After sorting, the test sample with the highest overall coverage score is selected from the sorted sequence to form the test dataset with the best coverage performance.
[0048] By integrating statement coverage and branch coverage with preset weights into a comprehensive coverage score, and constructing a coverage ranking sequence in an incremental update manner, the differences in coverage structure can be reflected in real time within the iteration rounds. The test samples with the best coverage performance can be efficiently screened in the ranking structure, and coverage redundancy is significantly reduced.
[0049] Specifically, the process of constructing the test dataset with optimal coverage includes: A coverage score is generated based on the statement coverage result and branch coverage result corresponding to each test sample, and the coverage scores are sorted to form a coverage score sequence; When the difference in coverage scores between adjacent test samples is greater than a preset coverage difference threshold, the test sample set with the highest coverage score is selected from the coverage score sequence as the test dataset with the optimal coverage. When the difference between adjacent coverage scores is less than or equal to the preset coverage difference threshold, the corresponding test samples are determined to belong to the coverage structure similarity category, and structural duplication removal is performed on the test samples with similar coverage structures. After the removal is completed, the optimal test dataset for coverage is re-determined based on the coverage score of the remaining test samples.
[0050] In this embodiment, the coverage difference threshold is used to identify significant differences in coverage capability between adjacent test samples. In this embodiment, the threshold can preferably be set in the range of 0.05-0.10, and preferably, the value is 0.07. When the difference between adjacent coverage scores is greater than the preset coverage difference threshold, it is determined that the coverage capability of the corresponding test sample is clearly stratified, and the test sample set with the highest coverage score can be directly selected from the beginning of the coverage score sequence as the optimal test dataset for coverage. When the difference between adjacent coverage scores is less than or equal to the preset coverage difference threshold, it is determined that the coverage capabilities of the corresponding samples are similar, and further judgment on coverage structure similarity is required. Coverage structure similarity comparison is calculated by the ratio of the intersection and union structure of the statement coverage set and the branch coverage set. The structure similarity ratio is preferably set to a structural similarity threshold in the range of 0.70-0.85, and preferably, the value is 0.75. When the structural similarity ratio is greater than the structural similarity threshold, it indicates that the two test samples cover the same structure and are judged to be in the structural similarity category. Then, structural duplication removal is performed. When the structural similarity ratio is less than or equal to the structural similarity threshold, it indicates that the coverage structure is still different and does not need to enter the elimination process. It is directly retained in the coverage score sequence for final sample selection. When the structural similarity is determined to be yes, that is, the structural similarity ratio exceeds the threshold, structural duplication is eliminated in the order of execution cost, diversity index and coverage score.
[0051] While maximizing test coverage, the system effectively eliminates test samples with highly repetitive coverage structures, reduces redundant data, and improves the execution efficiency of test case sets. At the same time, through dual judgment of coverage difference threshold and structural similarity threshold, it achieves fine identification of test sample coverage capability stratification and structural similarity, ensuring that the test dataset with optimal coverage includes samples with strong coverage capabilities.
[0052] See Figure 4 As shown, it is a schematic diagram of the structure of the automated software test case generation system based on machine learning according to an embodiment of the present invention; The present invention also provides an automated software test case generation system based on machine learning, as described above, comprising: The parameter acquisition module is used to acquire the set of statements, branches, and input variables of the program under test in the current iteration round, and to determine the variable domain of the input variable set based on the variable domain definition method, and to perform sampling operations on the variable domain to generate an initial population matrix; The strategy initialization module, connected to the parameter acquisition module, is used to construct an initial strategy parameter set and an action operator set, obtain the action value vector corresponding to the action operator set, calculate the selection probability of each action operator based on the initial strategy parameter set and the action value vector, and select the action operator to be applied based on the selection probability. An action module, connected to the parameter acquisition module and the strategy initialization module, is used to apply the action operator to the initial population matrix to obtain the action result vector. The index calculation module, connected to the parameter acquisition module and the action module, is used to calculate the statement coverage rate and branch coverage rate of the program under test, as well as the diversity index corresponding to the population matrix, based on the statement set and the branch set, so as to form a comprehensive index vector. The fitness score calculation module, connected to the index calculation module, is used to calculate the fitness score of the current iteration round based on the weighted calculation of the comprehensive index vector. The convergence adjustment module, connected to the fitness score calculation module, is used to obtain the fitness score sequence of consecutive iterations, and when it is determined that the fitness score sequence is in a local convergence state and the diversity index is less than the preset minimum diversity threshold, it performs step size update on the initial learning rate class parameters and the initial diversity incentive weights in the initial strategy parameter set. The step size update module, connected to the convergence adjustment module, performs step size update on the coverage initial incentive weight and diversity initial incentive weight in the strategy parameter set when the diversity index is greater than or equal to the minimum diversity threshold and the branch coverage rate is less than the preset target coverage rate threshold. The test dataset output module, connected to the step size update module, outputs the test dataset with optimal coverage when the branch coverage reaches the target coverage threshold or the iteration round count reaches the maximum number of rounds.
[0053] The technical solution of the present invention has been described above with reference to the preferred embodiments shown in the accompanying drawings. However, it will be readily understood by those skilled in the art that the scope of protection of the present invention is obviously not limited to these specific embodiments. Without departing from the principles of the present invention, those skilled in the art can make equivalent changes or substitutions to the relevant technical features, and the technical solutions after these changes or substitutions will all fall within the scope of protection of the present invention.
[0054] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A method for generating automated software test cases based on machine learning, characterized in that, include: Obtain the statement set, branch set, and input variable set of the program under test in the current iteration round, and determine the variable domain of the input variable set based on the variable domain definition method, perform sampling operation on the variable domain, and generate an initial population matrix; Construct an initial set of policy parameters and a set of action operators, obtain the action value vector corresponding to the set of action operators, calculate the selection probability of each action operator based on the initial set of policy parameters and the action value vector, and select the action operator to be applied based on the selection probability. The action operator is applied to the initial population matrix to obtain the action result vector; The statement coverage and branch coverage of the program under test are calculated based on the statement set and the branch set, as well as the diversity index corresponding to the population matrix, to form a comprehensive index vector. The fitness score for the current iteration round is calculated by weighting the comprehensive index vector. Obtain the fitness score sequence of consecutive iterations, and when the fitness score sequence is determined to be in a locally converged state and the diversity index is less than the preset minimum diversity threshold, perform step size update on the initial learning rate class parameters and the initial diversity incentive weights in the initial policy parameter set. When the diversity index is greater than or equal to the minimum diversity threshold and the branch coverage is less than the preset target coverage threshold, the step size update is performed on the coverage initial incentive weight and the diversity initial incentive weight in the strategy parameter set. When the branch coverage reaches the target coverage threshold or the iteration round count reaches the maximum number of rounds, the test dataset with the optimal coverage is output.
2. The automated software test case generation method based on machine learning according to claim 1, characterized in that, The process of constructing the initial population matrix includes: Based on the variable domain definition method, the variable domain of the input variable set is determined, and a sampling operation is performed on the variable domain to generate a candidate variable value vector; Based on the sampling results, the candidate variable value vectors are arranged and encoded according to the variable order corresponding to the input variable set to form an initial individual vector; Based on the initial individual vectors, an initial population matrix is constructed according to a predetermined matrix size and variable dimension organization method.
3. The automated software test case generation method based on machine learning according to claim 1, characterized in that, The calculation of the diversity index includes: The diversity index is defined as the degree of dispersion of the population in the input space, which is obtained by calculating the normalized distance between all input vector pairs and taking the average value. Based on the normalized distance function between any two input vectors in the population matrix, calculate the normalized distance value corresponding to the input vector; A global averaging operation is performed on all normalized distance values determined by the normalized distance function to obtain an average distance index characterizing the dispersion of the population in the input space.
4. The automated software test case generation method based on machine learning according to claim 1, characterized in that, The process of calculating the fitness score for the current iteration round includes: Based on the values of each indicator in the comprehensive indicator vector, assign corresponding weight coefficients to each indicator value and calculate the fitness score for the current iteration round. The values of each indicator in the comprehensive indicator vector are statement coverage, branch coverage, diversity index, and execution cost.
5. The automated software test case generation method based on machine learning according to claim 1, characterized in that, The process of updating the step size for the initial learning rate class parameters and the diversity initial incentive weights in the initial policy parameter set includes: The dynamic update factor of the initial learning rate class parameter is calculated based on the fitness score sequence of consecutive iterations, and the initial learning rate class parameter is adjusted according to the dynamic update factor to obtain the corrected learning rate class parameter. Based on the diversity index of the population matrix in the current iteration round and the modified learning rate class parameter, the diversity adjustment factor of the initial diversity incentive weight is calculated, and the initial diversity incentive weight is adjusted according to the diversity adjustment factor to obtain the modified diversity incentive weight.
6. The automated software test case generation method based on machine learning according to claim 5, characterized in that, The process of adjusting according to the aforementioned diversity adjustment factor includes: The diversity adjustment factor is calculated based on the diversity index of the population matrix in the current iteration round and the modified learning rate class parameter, and the diversity adjustment factor is compared with the preset factor reasonable range. When the diversity adjustment factor is less than the lower limit of the preset factor reasonable range, the initial diversity incentive weight is adjusted according to the diversity adjustment factor to obtain the diversity correction incentive weight, which is used for action selection and strategy optimization in subsequent iteration rounds. When the diversity adjustment factor is greater than the upper limit of the preset reasonable range, the current adjustment is terminated, and the diversity adjustment factor is recalculated until it falls within the preset reasonable range.
7. The automated software test case generation method based on machine learning according to claim 1, characterized in that, The process of performing step size updates on the coverage initial incentive weights and diversity initial incentive weights in the policy parameter set includes: The coverage adjustment factor of the initial coverage incentive weight is calculated based on the coverage performance sequence of the test sample set in continuous iterations, and the initial coverage incentive weight is updated according to the coverage adjustment factor to obtain the coverage correction incentive weight. Based on the diversity index of the population matrix in the current iteration round and the coverage correction incentive weight, the diversity update factor of the initial diversity incentive weight is calculated, and the initial diversity incentive weight is adjusted according to the diversity update factor to obtain the diversity correction incentive weight.
8. The automated software test case generation method based on machine learning according to claim 1, characterized in that, The process of outputting the test dataset with optimal coverage includes: Calculate the combined coverage score of statement coverage and branch coverage based on the test sample set of the current iteration round; The test sample set is sorted according to the comprehensive coverage score to determine the coverage ranking sequence; The test samples with the highest coverage scores are extracted from the coverage sorting sequence to form the optimal coverage test dataset.
9. The automated software test case generation method based on machine learning according to claim 8, characterized in that, The process of constructing the optimal test dataset for coverage includes: A coverage score is generated based on the statement coverage result and branch coverage result corresponding to each test sample, and the coverage scores are sorted to form a coverage score sequence; When the difference in coverage scores between adjacent test samples is greater than a preset coverage difference threshold, the test sample set with the highest coverage score is selected from the coverage score sequence as the test dataset with the optimal coverage. When the difference between adjacent coverage scores is less than or equal to the preset coverage difference threshold, the corresponding test samples are determined to belong to the coverage structure similarity category, and structural duplication removal is performed on the test samples with similar coverage structures. After the removal is completed, the optimal test dataset for coverage is re-determined based on the coverage score of the remaining test samples.
10. A machine learning-based automated software test case generation system, applied to the machine learning-based automated software test case generation method according to any one of claims 1-9, characterized in that, include: The parameter acquisition module is used to acquire the set of statements, branches, and input variables of the program under test in the current iteration round, and to determine the variable domain of the input variable set based on the variable domain definition method, and to perform sampling operations on the variable domain to generate an initial population matrix; The strategy initialization module, connected to the parameter acquisition module, is used to construct an initial strategy parameter set and an action operator set, obtain the action value vector corresponding to the action operator set, calculate the selection probability of each action operator based on the initial strategy parameter set and the action value vector, and select the action operator to be applied based on the selection probability. An action module, connected to the parameter acquisition module and the strategy initialization module, is used to apply the action operator to the initial population matrix to obtain the action result vector. The index calculation module, connected to the parameter acquisition module and the action module, is used to calculate the statement coverage rate and branch coverage rate of the program under test, as well as the diversity index corresponding to the population matrix, based on the statement set and the branch set, so as to form a comprehensive index vector. The fitness score calculation module, connected to the index calculation module, is used to calculate the fitness score of the current iteration round based on the weighted calculation of the comprehensive index vector. The convergence adjustment module, connected to the fitness score calculation module, is used to obtain the fitness score sequence of consecutive iterations, and when it is determined that the fitness score sequence is in a local convergence state and the diversity index is less than the preset minimum diversity threshold, it performs step size update on the initial learning rate class parameters and the initial diversity incentive weights in the initial strategy parameter set. The step size update module, connected to the convergence adjustment module, performs step size update on the coverage initial incentive weight and diversity initial incentive weight in the strategy parameter set when the diversity index is greater than or equal to the minimum diversity threshold and the branch coverage rate is less than the preset target coverage rate threshold. The test dataset output module, connected to the step size update module, outputs the test dataset with optimal coverage when the branch coverage reaches the target coverage threshold or the iteration round count reaches the maximum number of rounds.
Citation Information
Patent Citations
Advanced vulnerability mining and automatic testing method and testing system based on large language model
CN118171288A