A fine-grained test case detection method based on machine learning

Through the fine-grained test case detection method based on machine learning, the function-granularity features of product code changes are extracted and input into the classification model, which solves the problem of low efficiency of test case update and positioning in the existing technology, and realizes fine-grained prediction and update at the function level. Accurate test code updates.

CN116560976BActive Publication Date: 2024-10-18SOUTHERN UNIVERSITY OF SCIENCE AND TECHNOLOGY
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202310352631.9
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-03-30
Publication Date
2024-10-18
Estimated Expiration
2043-03-30

AI Technical Summary

Technical Problem

Existing technologies can only perform coarse-grained predictions at the file level and cannot provide finer-grained test case update results, causing developers to spend a lot of energy locating test case code that needs to be updated.

Method used

A fine-grained test case detection method based on machine learning is adopted to obtain product code change information, extract function granular features, construct feature vectors, and input a pre-trained classification model to achieve fine-grained prediction of change types and specific locations of test cases.

Benefits of technology

It achieves function-level fine-grained real-time prediction of outdated test cases, improves the efficiency of locating test case code that needs to be updated, and provides more accurate test code update results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116560976B_ABST
    Figure CN116560976B_ABST
Patent Text Reader

Abstract

The present invention provides a fine-grained test case detection method based on machine learning, the method comprising: obtaining product code change information of target software, obtaining a changed target product function according to the product code change information; extracting function granularity features corresponding to the target product function, obtaining a target feature vector corresponding to the target product function according to the function granularity features; inputting the target feature vector into a pre-trained classification model, obtaining a change type result of a test case that needs to be changed in the test code of the target software and specific location information of the test case. The present invention achieves function-level fine-grained instant prediction of outdated test cases by obtaining a changed target product function, extracting function granularity features corresponding to the target product function to obtain a target feature vector, and inputting the target feature vector into a pre-trained classification model, thereby improving the efficiency of locating test case codes that need to be updated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of software testing, and in particular to a fine-grained test case detection method based on machine learning. Background Art

[0002] Software testing can check the code and behavior of software to find defects such as programming errors and usability problems. It can effectively help developers avoid serious logical problems in the process of software development and maintenance, help product managers expand or modify requirements, and ultimately improve user experience. In the actual product development process, building a test framework and writing test code (for example, unit test cases) are essential activities in the entire software development process. As product code is constantly updated, test cases may become outdated or insufficient to test the current version of product code. For example, they may not be able to test new functions, or test cases may report false failures due to inappropriate assertions, which will cause unnecessary losses of manpower, material and financial resources. Therefore, the best option is that the test code needs to be updated synchronously with the product code, so that the existing test code can always cover the latest business logic, avoid outdated test code causing trouble to developers, and even bring potential threats to the production environment. This is called "co-evolution" of product code and test code.

[0003] In order to associate test code with product code, existing work uses the file naming convention of Java projects to map Java classes to their corresponding test classes (for example, test class TestFoo tests product class Foo). For test update prediction, a machine learning algorithm is used to train a model from the historical commits of the project. The trained model considers multiple factors (for example, changes in program structure in product code, and the complexity of code changes) to infer whether the test class needs to be updated when the class under test changes. However, existing solutions have the following limitations:

[0004] The above work can only perform coarse-grained predictions at the file level and can only predict test updates on Java classes. That is, given a change in product code, this work can only rely on naming conventions to find the test class that tests the changed product class and determine whether the test class needs to be updated, without providing more fine-grained results. And because a test class may contain a large number of test methods (or test functions), users still need to spend a lot of effort to locate the code in the test case that needs to be updated.

[0005] Therefore, the prior art has defects and needs to be improved and developed. Summary of the invention

[0006] The technical problem to be solved by the present invention is to provide a fine-grained test case detection method based on machine learning in response to the above-mentioned defects of the prior art, aiming to solve the problem in the prior art that users need to spend a lot of effort to locate the code in the test case that needs to be updated.

[0007] The technical solution adopted by the present invention to solve the technical problem is as follows:

[0008] A fine-grained test case detection method based on machine learning, wherein the method comprises:

[0009] Acquire product code change information of the target software, and obtain the changed target product function according to the product code change information;

[0010] Extracting function granularity features corresponding to the target product function, and obtaining a target feature vector corresponding to the target product function according to the function granularity features;

[0011] The target feature vector is input into a pre-trained classification model to obtain the change type results of the test cases that need to be changed in the test code of the target software and the specific location information of the test cases.

[0012] In one implementation, the acquiring product code change information of the target software and obtaining the changed target product function according to the product code change information includes:

[0013] Obtain product code change information of the target software, and search for target Java files that have been changed in the product code change information in units of code lines;

[0014] The line number interval range of each function in the target Java file is parsed by a syntax parser, and the changed target product function is obtained according to the changed code line number.

[0015] In one implementation, the training step of the classification model includes:

[0016] Find the changed product code and test code from all historical commits of the target code library, and determine the changed sample changed functions according to the changed product code and test code;

[0017] If the Java class where the sample change function is located is a product class, then find a test class that tests the class where the sample change function is located, and obtain positive samples and negative samples of function granularity according to the test class;

[0018] The initial classification model is trained using the positive samples and negative samples to obtain a trained classification model.

[0019] In one implementation, searching for changed product codes and test codes from all historical submissions of the target code library, and determining changed sample changed functions according to the changed product codes and test codes, includes:

[0020] Find the changed production code and test code from all historical commits of the target code base;

[0021] In units of code lines, searching for code lines that are changed in the product code and the test code;

[0022] The line number interval range of each function in the file where the code line is located is parsed by a syntax parser to obtain a changed sample function.

[0023] In one implementation, if the Java class where the sample modification function is located is a product class, then searching for a test class that tests the class where the sample modification function is located, and obtaining positive samples and negative samples of function granularity according to the test class, including:

[0024] If the Java class where the sample modification function is located is a product class, determining a test class for testing the Java class where the sample modification function is located according to a preset naming convention;

[0025] Finding the multi-layer calling relationship of functions between the product class and the test class through a syntax parser, and determining all sample test functions for testing the sample change function according to the multi-layer calling relationship;

[0026] Obtaining an initial sample label of the sample change function according to a co-evolution relationship between the sample test function and the sample change function;

[0027] If the same change of the same sample change function corresponds to multiple initial sample labels, the initial sample labels are merged according to the preset merging rules to obtain positive samples and negative samples at the function granularity.

[0028] In one implementation, obtaining an initial sample label of the sample change function according to the co-evolution relationship between the sample test function and the sample change function includes:

[0029] Finding the first change time corresponding to the sample change function and the second change time corresponding to all sample test functions;

[0030] Calculate the shortest interval between each of the second change time and the first change time, and use the second change time corresponding to the shortest interval as the target second change time;

[0031] If the shortest interval time is less than a preset first time threshold, determining the change information of the sample change function between the first change time and the target second change time;

[0032] If the change information indicates that there is no further change between the first change time and the target second change time, determining the change type of the sample test function between the first change time and the target second change time, and recording the change type as the initial sample label of the sample change function;

[0033] The change type is should be changed; or the change type includes should be created, should be edited and should be deleted.

[0034] In an implementation manner, after calculating the shortest interval time between each of the second change time and the first change time, the method further includes:

[0035] If the shortest interval time is greater than or equal to the preset first time threshold, obtaining a preset second time threshold, and comparing the shortest interval time with the preset second time threshold;

[0036] If the shortest interval time is greater than the preset second time threshold, the initial sample label of the sample change function is recorded as unchanged.

[0037] In one implementation, the initial classification model is trained using the positive samples and the negative samples to obtain a trained classification model, including:

[0038] Preliminarily screen all AST node types in Java to obtain a feature list;

[0039] Determine the code position information of each feature in the feature list, and construct a segment tree according to each feature and the corresponding code position information;

[0040] Querying the segment tree to determine sample features contained in the changed product code lines in the positive sample and the negative sample;

[0041] Obtaining a feature vector corresponding to each sample according to the sample feature, wherein the feature vector represents the total number of lines increased or decreased in the target change function that contains the sample feature;

[0042] The initial classification model is trained using the feature vectors corresponding to the positive samples and the negative samples to obtain a trained classification model.

[0043] The present invention also discloses a fine-grained test case detection device based on machine learning, wherein the device comprises:

[0044] An acquisition module, used for acquiring product code change information of the target software, and obtaining the changed target product function according to the product code change information;

[0045] An extraction module, used for extracting the function granularity feature corresponding to the target product function, and obtaining the target feature vector corresponding to the target product function according to the function granularity feature;

[0046] The detection module is used to input the target feature vector into a pre-trained classification model to obtain the change type results of the test cases that need to be changed in the test code of the target software and the specific location information of the test cases.

[0047] The present invention also discloses a terminal, which includes: a memory, a processor, and a fine-grained test case detection program based on machine learning stored in the memory and executable on the processor. When the fine-grained test case detection program based on machine learning is executed by the processor, the steps of the fine-grained test case detection method based on machine learning as described above are implemented.

[0048] The present invention also discloses a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and the computer program can be executed to implement the steps of the fine-grained test case detection method based on machine learning as described above.

[0049] The present invention provides a fine-grained test case detection method based on machine learning, the method comprising: obtaining product code change information of target software, obtaining a changed target product function according to the product code change information; extracting function granularity features corresponding to the target product function, obtaining a target feature vector corresponding to the target product function according to the function granularity features; inputting the target feature vector into a pre-trained classification model, obtaining a change type result of a test case that needs to be changed in the test code of the target software and specific location information of the test case. The present invention achieves function-level fine-grained instant prediction of outdated test cases by obtaining a changed target product function, extracting function granularity features corresponding to the target product function to obtain a target feature vector, and inputting the target feature vector into a pre-trained classification model, thereby improving the efficiency of locating test case codes that need to be updated. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Figure 1 It is a flowchart of the fine-grained test case detection method based on machine learning in the present invention.

[0051] Figure 2 It is a flowchart of a preferred embodiment of the fine-grained test case detection method based on machine learning in the present invention.

[0052] Figure 3 It is a flow chart for determining the multi-layer calling relationship between product functions and test functions in the present invention.

[0053] Figure 4 It is a flow chart of binary classification prediction of outdated test cases in the present invention.

[0054] Figure 5 It is a functional principle block diagram of a preferred embodiment of the fine-grained test case detection device based on machine learning in the present invention.

[0055] Figure 6 It is a functional principle block diagram of a preferred embodiment of the terminal in the present invention. DETAILED DESCRIPTION

[0056] In order to make the purpose, technical solution and advantages of the present invention clearer and more specific, the present invention is further described in detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.

[0057] Although the co-evolution of product code and test code is very necessary, it is not easy to keep the test code up to date with the changes in product code. The specific reason is that it is not easy to locate the test code that needs to be updated when the product code evolves. It is difficult to maintain the traceability link between product code and test code in actual projects. If the correspondence between test code and product code is not accurately known, it is difficult to make further reasoning and analysis; whether to update the test code can be determined by many factors, and generally it is necessary to understand the purpose of the product code change. It is ineffective to use some pre-defined heuristic rules about simple factors (for example, adding / deleting / editing product code causes test code to be added / deleted / edited), and this method lacks scalability and has poor overall performance; updating test code (for example, adding new test cases, deleting or modifying inapplicable test cases) is also time-consuming. Due to the above reasons, it is challenging to automatically perform test code updates.

[0058] As mentioned above, defining simple heuristic rules (e.g., adding / deleting / editing product code causes the corresponding test code to be added / deleted / edited) performs poorly and is insufficient to meet prediction needs. In order to associate test code with product code, existing work uses the file naming convention of Java projects to map Java classes to their corresponding test classes (e.g., test class TestFoo tests product class Foo). For test update prediction, a machine learning algorithm is used to train a model from the historical commits of the project. The trained model takes into account multiple factors (e.g., changes in program structure in product code, and the complexity of code changes) to infer whether the test class needs to be updated when the class under test changes. However, existing solutions have the following limitations:

[0059] 1. Although the relationship between test code and product code can be inferred by analyzing file or function names, developers may have different coding styles and may not strictly follow naming conventions or recommended practices to name files or functions. Therefore, the traceability link between the constructed product code and test code is usually incomplete (i.e., part of it is missing).

[0060] 2. Only manually specified features (for example, a function parameter list or if statement as a feature) are used to train machine learning models. Such empirically selected features may not be comprehensive and cannot fully cover all program logic and code behavior, and cannot play a good predictive role in test updates.

[0061] 3. The above work can only perform coarse-grained predictions at the file level and can only predict test updates on Java classes. That is, given a change in product code, this work can only rely on naming conventions to find the test class that tests the changed product class and determine whether the test class needs to be updated, without providing more fine-grained results. And because a test class may contain a large number of test methods (or test functions), users still need to spend a lot of effort to locate the specific test methods that need to be updated.

[0062] To solve the problems of the prior art, the present invention conducts a large-scale empirical study on Java projects of different purposes on Github, and proposes a fine-grained automatic identification method for outdated test cases based on the findings of the comprehensive study, which is used to predict the update of unit test cases at the function level. The method extracts positive and negative samples of function granularity from the historical data of product code and test code changes, and extracts feature vector information from the positive and negative samples for subsequent classifier training. The implementation process of the method includes data preprocessing, function granularity feature extraction and classifier training, and finally achieves more accurate positioning of test cases that need to be updated. Through large-scale empirical research, some test case update rules are found at the function granularity, and function-level product code features that may be related to test case updates are screened. First, all AST node types are screened to obtain a preliminary feature list, and then a classification model is trained for the feature list, and an attempt is made to use the model to find an effective combination of these features to identify outdated test cases. The present invention proposes a method for learning classification rules using a machine learning algorithm, extracting method-level code change features from positive and negative samples of co-evolution of product code and test code, and using a more accurate tracing method to perform fine-grained instant prediction of outdated test cases. The present invention uses an interval puncture problem solving algorithm based on segment trees, which effectively improves the operating efficiency of the feature extraction step. The present invention uses common machine learning classification algorithms such as naive Bayes, decision trees, logistic regression, support vector machines, random forests, and multi-layer perceptrons to train the above algorithm models and screen out the best performing algorithms. Compared with other existing work, this method is more excellent in both the accuracy of prediction and the positioning accuracy of the test code change position, and can ultimately achieve efficient and accurate promotion of the co-evolution of unit test code and product code.

[0063] See also Figure 1 , Figure 1 This is a flow chart of the fine-grained test case detection method based on machine learning in the present invention. The method is specifically divided into three parts: data preprocessing, function granularity feature extraction and classifier training. The present invention achieves more accurate positioning of test cases that need to be updated through the above three parts.

[0064] See also Figure 2 , Figure 2 FIG. 1 is a flow chart of a preferred embodiment of a fine-grained test case detection method based on machine learning in the present invention. Figure 2 As shown, the fine-grained test case detection based on machine learning described in the embodiment of the present invention includes:

[0065] Step S100: Acquire product code change information of the target software, and obtain the changed target product function according to the product code change information.

[0066] Specifically, when product software needs to be upgraded or updated, R&D personnel or other software maintenance personnel need to change the product code. After the upgrade or update, the code repository saves the product code change information. The test case detection method of the present invention can obtain the changed target product function according to the product code change information.

[0067] In one implementation, step S100 specifically includes:

[0068] Step S110, obtaining product code change information of the target software, and searching for target Java files that have been changed in the product code change information in units of code lines;

[0069] Step S120: parsing the line number range of each function in the target Java file through a syntax parser, and obtaining the changed target product function according to the changed code line number.

[0070] Specifically, the present invention can accurately determine the target product function that has been changed by determining the change information in units of code and line, thereby providing strong support for subsequent judgments on the need to change the test class.

[0071] like Figure 2 As shown, the fine-grained test case detection based on machine learning described in the embodiment of the present invention also includes:

[0072] Step S200: extracting the function granularity features corresponding to the target product function, and obtaining the target feature vector corresponding to the target product function according to the function granularity features.

[0073] Specifically, the corresponding function granularity features are extracted according to the modified code lines of the target product function, and the target feature vector corresponding to the target product function is obtained according to the function granularity features. The present invention extracts function granularity features by locating the modification at the code line level, adjusting the fine granularity to the function granularity, and can extract function granularity features.

[0074] like Figure 2 As shown, the fine-grained test case detection based on machine learning described in the embodiment of the present invention also includes:

[0075] Step S300: Input the target feature vector into a pre-trained classification model to obtain a change type result and code location information of the test function on the test case.

[0076] Specifically, the pre-trained classification model is trained using the submission history records of the project warehouse or other warehouses corresponding to the target software. After training, the classifier can help predict whether the developer should update (or create, edit or delete accordingly) the test case for testing the product function when changing the product function. The present invention supports a variety of machine learning algorithms, and supports both binary classification and multi-classification, and can predict what types of changes should be made to the test code. Among them, the machine learning method includes: naive Bayes, decision tree, logistic regression, support vector machine, random forest and gradient boosting, etc. Through experiments, commonly used classifiers (common machine learning classification algorithms such as naive Bayes, decision tree, logistic regression, support vector machine, random forest and gradient boosting) are compared, and the algorithm with the best performance in this problem is selected as the final classifier algorithm.

[0077] It can be understood that the test case refers to the test code for testing the product code of the target software.

[0078] In one implementation, the training step of the classification model specifically includes:

[0079] Find the changed product code and test code from all historical commits of the target code library, and determine the changed sample changed functions according to the changed product code and test code;

[0080] If the Java class where the sample change function is located is a product class, then find a test class that tests the class where the sample change function is located, and obtain positive samples and negative samples of function granularity according to the test class;

[0081] The initial classification model is trained using the positive samples and negative samples to obtain a trained classification model.

[0082] Specifically, the target code library includes: project warehouse, code warehouse, "patch" original source code, original source code of production class, etc. If the target software has too few data samples at the initial stage, the code library corresponding to other software can be adaptively adopted. The present invention needs to extract available information from the target code library and pre-process the data for subsequent use. First, find the changed product code and test code from all historical submissions of the target code library, and determine the changed sample change function according to the changed product code and the test code. Next, determine the traceability link relationship between the product function and the test function (i.e., unit test case). First, determine if the Java class where the changed function is located is a product class, and find out whether the Java class where the changed function is located has a corresponding test class according to the naming convention. If the project is relatively standardized, the Java class where the function of the corresponding class is changed should be found; if it is a test class, it is discarded; if the relevant test class cannot be found, then find the relevant test class according to the call relationship in the grammar parser parsing class (the specific operation is to find the test class that imports the class in the import statement and the test class with the class name written in the code). After determining the relevant test classes, the multi-layer calling relationship of the functions between the two classes is found according to the parsing results of the syntax parser, and the relationship related to the changed function is retained.

[0083] The present invention can determine the traceability link relationship between product functions and test functions, and can determine the multi-layer calling relationship between product functions and test functions. Figure 3 , Figure 3 The flowchart for determining the multi-layer calling relationship between product functions and test functions in the present invention includes:

[0084] Step A1, obtain the product class where the function where the change occurs;

[0085] Step B1, obtain the corresponding test class of the product class where the changed function is located;

[0086] Step A2, find all function names in the product class;

[0087] Step B2, find all function names in the test class;

[0088] Step B3, create an empty list for each test function;

[0089] Step B4, find the test function called by each test function;

[0090] Step A3, searching for the product function called by each test function and saving the product function name to a related empty list, where the related list is a list storing product functions;

[0091] Step C1, searching for the product function called by the test function in the previous step, and saving the function name to a related list, where the related list is a list storing product functions;

[0092] Step B5, find the test function called by the test function in the previous step;

[0093] Step A4, search for the product function of the function call saved in the previous step in each list, and save the function name to the list;

[0094] Step D1, search for the product function called by the test function in the previous step, and save the function name to a related list, where the related list is a list storing product functions;

[0095] Step B6, find the test function called by the test function in the previous step;

[0096] Step A5, search for the product function of the function call saved in the previous step in each list, and save the function name to the list;

[0097] Step E1, search for the product function called by the test function in the previous step, and save the function name to a related list, where the related list is a list storing product functions;

[0098] Step B7, find the test function called by the test function in the previous step;

[0099] Step A6: Find the product function of the function call saved in the previous step in each list, and save the function name to the list;

[0100] Step B8, search for the product function of the function call saved in the previous step in each list, and save the function name to the list;

[0101] Step A7, search for the product function of the function call saved in the previous step in each list, and save the function name to the list;

[0102] Step F1: After executing step A7 and step B8, determine whether the changed product function is in a certain list; if so, execute step F2.

[0103] Step F2: Save the list and related test functions.

[0104] Figure 3In the above example, steps A4-A7 save the product function name to the list, which is the product function called by the product function saved in the previous step; steps C1, D1, E1, and B8 save the product function name to the list, which is the product function called by the test function in the previous step; steps B5-B7 save the test function name, which is the test function called by the test function in the previous step. Through nested search and saving, multi-level relationships can be traced.

[0105] In the present invention, the hierarchical relationships of product function calling product function, test function calling test function, and test function calling product function are all processed. Through the above steps, a test function that has been called at multiple levels can be obtained. The positive and negative samples of the function granularity are found through the multi-level relationships traced above. The positive samples are samples of the co-evolution of the test function and the product function, and the negative samples are samples of the test function that do not co-evolve with the product function. The initial classification model is trained using the positive and negative samples to obtain a trained classification model. The present invention provides a more accurate tracing method by determining the multi-level calling relationship between product functions and test functions, which can accurately predict the update of the test class and effectively avoid the situation where the test class update is missed due to the inaccuracy of the tracing method.

[0106] Please note that Figure 3 This is only used as an example. The number of call-level queries in actual application scenarios can be adjusted according to actual conditions.

[0107] In one implementation, searching for changed product codes and test codes from all historical submissions of the target code library, and determining changed sample changed functions according to the changed product codes and test codes, includes:

[0108] Find the changed production code and test code from all historical commits of the target code base;

[0109] In units of code lines, searching for code lines that are changed in the product code and the test code;

[0110] The line number interval range of each function in the file where the code line is located is parsed by a syntax parser to obtain a changed sample function.

[0111] Specifically, the code lines that have been changed in the product code and the test code are searched in units of code lines. The line number interval range of each function in the file where the code line is located is parsed by a syntax parser to obtain the sample changed function that has been changed. The present invention parses the line number interval range of each function in the file where the code line is located by a syntax parser to obtain the sample changed function that has been changed, providing a more fine-grained result and providing strong support for subsequent fine-grained positioning of test cases.

[0112] In one implementation, searching for changed code lines in the product code and the test code in units of code lines includes:

[0113] If the Java class where the sample modification function is located is a product class, determining a test class for testing the Java class where the sample modification function is located according to a preset naming convention;

[0114] Finding the multi-layer calling relationship of functions between the product class and the test class through a syntax parser, and determining all sample test functions for testing the sample change function according to the multi-layer calling relationship;

[0115] Obtaining an initial sample label of the sample change function according to a co-evolution relationship between the sample test function and the sample change function;

[0116] If the same change of the same sample change function corresponds to multiple initial sample labels, the initial sample labels are merged according to the preset merging rules to obtain positive samples and negative samples at the function granularity.

[0117] Specifically, the naming convention is that the test code corresponding to the product code is named by adding "test" or "testcase" to the product code name. For example, the test class name of the agreed product class a is aTest or testa or aTestCase. First, determine whether the Java class where the sample change function is located is a product class. If the sample change function is a product class, determine the corresponding test class according to the preset naming convention; find the multi-layer call relationship of the function between the product class and the test class through the syntax parser, and determine all sample test functions that test the sample change function according to the multi-layer call relationship; obtain the initial sample label of the sample change function according to the co-evolution relationship between the sample test function and the sample change function. When the sample change function is changed, its corresponding sample test function will change accordingly within a certain period of time. Such a relationship is a co-evolution relationship. If the same change of the same sample change function corresponds to multiple initial sample labels, then each initial sample label is merged according to the preset merging rule to obtain positive samples and negative samples of function granularity. Among them, each positive sample stores a certain modification of a certain product function and a certain modification of the test function related to the product function. Each negative sample stores a modification of a product function and the specific location of the test function related to the product function. The sample is related to the change of the product function, and there may be multiple samples related to the same product function.

[0118] That is, the present invention needs to merge the sample labels of all samples related to the same change of the same product function, because each sample related to the same change of the same product function can only represent the change of part of the test function that tests the product function. If they are not merged, since the sample labels of the above samples may be different, while the feature vectors of the samples are the same, the training results of the classifier will be poor. Therefore, it is necessary to merge the initial sample labels according to the preset merging rules.

[0119] In the present invention, the sample labels of all samples related to the same change of the same product function are merged according to the preset merging rules, thereby ensuring the effect of subsequent classifier training. This avoids the situation where the subsequent classifier training effect is poor due to different sample labels of each sample but the same feature vector of the sample.

[0120] In one implementation, obtaining the initial sample label of the sample change function according to the co-evolution relationship between the sample test function and the sample change function includes:

[0121] Finding the first change time corresponding to the sample change function and the second change time corresponding to all sample test functions;

[0122] Calculate the shortest interval between each of the second change time and the first change time, and use the second change time corresponding to the shortest interval as the target second change time;

[0123] If the shortest interval time is less than a preset first time threshold, determining the change information of the sample change function between the first change time and the target second change time;

[0124] If the change information indicates that there is no further change between the first change time and the target second change time, determining the change type of the sample test function between the first change time and the target second change time, and recording the change type as the initial sample label of the sample change function;

[0125] The change type is should be changed; or the change type includes: should be created, should be edited, and should be deleted.

[0126] Specifically, check whether the test function corresponding to the sample change function (i.e., the changed product function) in the multi-level call relationship has changed within a certain period of time (the change types are divided into three categories: deletion, creation, and editing). If the test function change closest to the change time of the sample change function is within a certain period of time, then determine whether there are other changes to the sample change function between a certain change of the sample change function and the change of the corresponding test function closest to the change time of the sample change function. If not, save the corresponding changes of the two functions. In this link, when predicting binary categories, the initial sample label of the sample change function is "should be changed (SC)", and in multi-classification predictions, the initial sample label of the sample change function is "should be created (SSC)", "should be edited (SSE)" or "should be deleted (SSD)".

[0127] The present invention realizes accurate judgment of whether the test function follows the sample change function within a certain period of time by judging whether there are other changes of the sample change function between a certain change of the sample change function and a change of the test function corresponding to the most recent change time of the sample change function.

[0128] In an implementation manner, after calculating the shortest interval time between each of the second change time and the first change time, the method further includes:

[0129] If the shortest interval time is greater than or equal to the preset first time threshold, obtaining a preset second time threshold, and comparing the shortest interval time with the preset second time threshold;

[0130] If the shortest interval time is greater than the preset second time threshold, the initial sample label of the sample change function is recorded as unchanged.

[0131] Specifically, if the test function change closest to the change time of the sample change function is not within a certain time, then check whether the most recent change is within a certain period of time. If no change occurs within a certain period of time, regardless of binary classification and multi-classification prediction, the initial sample label of the sample change function is "no change (NC)". That is, the present invention sets a time threshold. If the time interval between the product function change and the corresponding test function change exceeds this time threshold, there is no need to search for whether the test function has changed from the time when the sample change function was changed, and it is deemed that the corresponding test function has not undergone corresponding changes. This can effectively avoid the waste of system resources.

[0132] The present invention obtains positive samples and negative samples of function granularity through the above process and in combination with preset merging rules. Figure 4 , Figure 4This is a binary classification prediction flow chart for outdated test cases in the present invention. The multi-classification prediction process is the same.

[0133] Step A1: Get the code repository;

[0134] Step A2: Search the code change lines submitted historically from the code repository to obtain all code change lines;

[0135] Step A3, by locating the function where the code change line is located, obtain all the functions that have been changed;

[0136] Step A4: determine whether the modified function is a product function; if so, execute step A5, otherwise, abandon;

[0137] Step A5, find the related class for testing the class where the changed function is located;

[0138] Step A6, finding the multi-layer calling relationship between the product class where the changed function is located and the function in the corresponding test class;

[0139] Step A7, obtaining all test functions for testing the modified function;

[0140] Step A8: Determine whether the test function has been changed; if so, execute step A9; if not, execute step B1;

[0141] Step A9, obtaining the shortest time interval, wherein the shortest time interval = the most recent test function change time - the product function change time;

[0142] Step B1, save the sample label as NC;

[0143] Step A10: determine whether the shortest time interval is less than a certain value; if so, execute step A11; if not, execute step C1;

[0144] Step A11, determining whether there are other changes to the sample change function between a certain change of the sample change function and a change of the test function corresponding to the closest change time of the sample change function, that is, determining whether the product function is changed again within the interval; if not, executing step A12, if yes, executing step D1;

[0145] Step C1, determine whether the shortest interval is greater than a set time threshold; if so, execute step C2, if not, execute step E1;

[0146] Step A12, save the sample label as SC;

[0147] Step C2, save the sample label as NC;

[0148] Step D1, discard;

[0149] Step E1, discard;

[0150] Step A13, merge sample information according to preset rules, the sample information comes from step A12, step C2 and step B1; if the stored sample labels have both SC and NC, execute F1, if the stored sample labels are all SC, execute F2, if the stored sample labels are all NC, execute F3.

[0151] Step F1, obtain a positive sample with a merged sample label of SC;

[0152] Step F2, obtain a positive sample with a merged sample label of SC;

[0153] Step F3: obtain a negative sample whose merged sample label is NC.

[0154] That is, in the binary classification prediction, two samples of the same product function with exactly the same change time are merged. If the saved sample labels are both "should change (SC)", they are finally merged into a positive sample with the sample label "should change (SC)"; if the saved sample labels have both "should change (SC)" and "do not change (NC)", they are finally merged into a positive sample with the sample label "should change (SC)"; if the saved sample label is only "do not change (NC)", it is finally merged into a negative sample with the sample label "do not change (NC)".

[0155] In one implementation, in a multi-classification prediction method, samples related to the same change of the same product function are merged. If the labels of the saved samples are all "should be created (SSC)", they are finally merged into positive samples with the sample label "should be created (SSC)"; if the labels of the saved samples are all "should be edited (SSE)", they are finally merged into positive samples with the sample label "should be edited (SSE)"; if the labels of the saved samples are all "should be deleted (SSD)", they are finally merged into positive samples with the sample label "should be deleted (SSD)"; if the labels of the saved samples are all "do not change (NC)", they are finally merged into negative samples with the sample label If the sample labels are both "Do Not Change (NC)" and "Should Create (SSC)", "Should Edit (SSE)", and "Should Delete (SSD)", they are finally merged into positive samples, and the sample labels are any one of the three "Should Create (SSC)", "Should Edit (SSE)", and "Should Delete (SSD)"; if the sample labels are any two or more of the three "Should Create (SSC)", "Should Edit (SSE)", and "Should Delete (SSD)", they are finally merged into positive samples, and the sample labels are any one of the three "Should Create (SSC)", "Should Edit (SSE)", and "Should Delete (SSD)".

[0156] In one implementation, the using the positive samples and the negative samples to train the initial classification model to obtain the trained classification model includes:

[0157] Preliminarily screen all AST node types in Java to obtain a feature list;

[0158] Determine the code position information of each feature in the feature list, and construct a segment tree according to each feature and the corresponding code position information;

[0159] Querying the segment tree to determine sample features contained in the changed product code lines in the positive sample and the negative sample;

[0160] Obtaining a feature vector corresponding to each sample according to the sample feature, wherein the feature vector represents the total number of lines increased or decreased in the target change function that contains the sample feature;

[0161] The initial classification model is trained using the feature vectors corresponding to the positive samples and the negative samples to obtain a trained classification model.

[0162] Specifically, for each sample, features are extracted from the original source code of the "patch" and production class. After data preprocessing, all AST node types in Java (each node of the abstract syntax tree can be regarded as a structure in the source code) are preliminarily screened as a feature list. The preliminarily screened features are filtered out for features that are commonly present or not commonly present in both positive and negative samples. The preliminarily screened feature list is shown in Table 1.

[0163]

[0164]

[0165]

[0166]

[0167]

[0168] Table 1

[0169] Each language structure in a Java file corresponds to an interval. Through "interval puncture", we determine which interval (which language structure) each changed line in the product code corresponds to. That is, the task can be expressed as: given a set of language structures (i.e., features) in the source code, each structure corresponds to an interval, and the changed lines of the queried product function code are the points that pierce the given interval. For deleted lines, we query the interval where the original code is located; for added lines, we query the interval where the modified code is located. In addition, in order to optimize query efficiency, a segment tree is constructed. The segment tree includes each feature in the feature list and the code location information corresponding to the feature. The code location information is in units of code lines, which enables one-time tree construction and multiple queries. The query time is reduced to a logarithm, which is more efficient than the brute force solution and more suitable for complex and large-scale projects in reality.

[0170] The product code lines that are changed in the positive and negative samples are located at the function granularity, and each line is analyzed one by one to see if it contains a certain feature, and the complete feature vector of the sample is obtained. The dimensions of the feature vector are divided into two types: adding a certain feature line and reducing a certain feature line. Each dimension of the feature vector represents the total number of lines added / reduced in the code that contains a certain feature in the product function change of the sample, so the overall dimension is the number of feature vectors multiplied by 2. Each dimension is a number, which is initialized to 0. It is subsequently changed according to the number of lines added / deleted in the code that contains the feature in this change of the product function. After completing the function granular feature extraction, the complete feature vector is obtained. The feature vector plays a role in describing the sample in the subsequent training process.

[0171] The present invention can obtain a complete feature vector of the sample by extracting many features of the function granularity, and effectively improves the operating efficiency of the feature extraction step through an interval puncture problem solving algorithm based on a segment tree.

[0172] In one embodiment, Figure 5 As shown, based on the above-mentioned fine-grained test case detection method based on machine learning, the present invention also provides a fine-grained test case detection device based on machine learning, including:

[0173] An acquisition module 100 is used to acquire product code change information of a target software, and obtain a changed target product function according to the product code change information;

[0174] An extraction module 200 is used to extract the function granularity feature corresponding to the target product function, and obtain the target feature vector corresponding to the target product function according to the function granularity feature;

[0175] The detection module 300 is used to input the target feature vector into a pre-trained classification model to obtain the change type results of the test cases that need to be changed in the test code of the target software and the specific location information of the test cases.

[0176] In one embodiment, the present invention also provides a terminal, such as Figure 6 As shown, it includes: a memory 20, a processor 10, and a machine learning-based fine-grained test case detection program 30 stored in the memory 20 and executable on the processor 10. When the machine learning-based fine-grained test case detection program 30 is executed by the processor 10, the steps of the machine learning-based fine-grained test case detection method as described above are implemented.

[0177] The present invention also provides a computer-readable storage medium, which stores a computer program, and the computer program can be executed to implement the steps of the fine-grained test case detection method based on machine learning as described above.

[0178] In summary, the present invention provides a fine-grained test case detection method based on machine learning, the method comprising: obtaining product code change information of the target software, and obtaining the changed target product function according to the product code change information; extracting the function granularity feature corresponding to the target product function, and obtaining the target feature vector corresponding to the target product function according to the function granularity feature; inputting the target feature vector into a pre-trained classification model to obtain the change type result of the test case that needs to be changed in the test code of the target software and the specific location information of the test case. The present invention obtains the changed target product function, extracts the function granularity feature corresponding to the target product function to obtain the target feature vector, and inputs the target feature vector into the pre-trained classification model, thereby realizing function-level fine-grained instant prediction of outdated test cases and improving the efficiency of locating the test case code that needs to be updated.

[0179] It should be noted that the classifier algorithm used in the present invention can be any machine learning classification algorithm, and the machine learning algorithm used can be replaced with an integrated learning algorithm.

[0180] It should be understood that the application of the present invention is not limited to the above examples. For ordinary technicians in this field, improvements or changes can be made based on the above description. All these improvements and changes should fall within the scope of protection of the claims attached to the present invention.

Claims

1. A fine-grained test case detection method based on machine learning, characterized in that: The method comprises: Acquire product code change information of the target software, and obtain the changed target product function according to the product code change information; Extracting function granularity features corresponding to the target product function, and obtaining a target feature vector corresponding to the target product function according to the function granularity features; Inputting the target feature vector into a pre-trained classification model to obtain a change type result of a test case that needs to be changed in the test code of the target software and specific location information of the test case; The training steps of the classification model include: Find the changed product code and test code from all historical commits of the target code library, and determine the changed sample changed functions according to the changed product code and test code; If the Java class where the sample change function is located is a product class, then find a test class that tests the class where the sample change function is located, and obtain positive samples and negative samples of function granularity according to the test class; Using the positive samples and negative samples to train the initial classification model to obtain a trained classification model; If the Java class where the sample modification function is located is a product class, then a test class for testing the class where the sample modification function is located is searched, and positive samples and negative samples of function granularity are obtained according to the test class, including: If the Java class where the sample modification function is located is a product class, determining a test class for testing the Java class where the sample modification function is located according to a preset naming convention; Finding the multi-layer calling relationship of functions between the product class and the test class through a syntax parser, and determining all sample test functions for testing the sample change function according to the multi-layer calling relationship; Obtaining an initial sample label of the sample change function according to a co-evolution relationship between the sample test function and the sample change function; If the same change of the same sample change function corresponds to multiple initial sample labels, then the initial sample labels are merged according to the preset merging rules to obtain positive samples and negative samples at the function granularity; If the relevant test class cannot be found, look for the test class that imports the class in the import statement and the test class with the class name written in the code; Obtaining an initial sample label of the sample change function according to the co-evolution relationship between the sample test function and the sample change function, including: Find the first change time corresponding to the sample change function and the second change time corresponding to all sample test functions; Calculate the shortest interval between each of the second change time and the first change time, and use the second change time corresponding to the shortest interval as the target second change time; If the shortest interval time is less than a preset first time threshold, determining the change information of the sample change function between the first change time and the target second change time; If the change information indicates that there is no further change between the first change time and the target second change time, determining the change type of the sample test function between the first change time and the target second change time, and recording the change type as the initial sample label of the sample change function; Wherein, the change type is should be changed; or, the change type includes: should be created, should be edited and should be deleted; when all sample labels are should be changed, they are merged into positive samples; when all sample labels are should be created, they are merged into positive samples; when all sample labels are should be edited, they are merged into positive samples; when all sample labels are should be deleted, they are merged into positive samples; when any two or more of the three sample labels are should be created, should be edited and should be deleted, they are merged into positive samples; The initial classification model is trained using the positive samples and the negative samples to obtain a trained classification model, including: Preliminarily screen all AST node types in Java to obtain a feature list; Determine the code position information of each feature in the feature list, and construct a segment tree according to each feature and the corresponding code position information; Querying the segment tree to determine sample features contained in the changed product code lines in the positive sample and the negative sample; Obtaining a feature vector corresponding to each sample according to the sample feature, wherein the feature vector represents the total number of lines increased or decreased in the target change function that contains the sample feature; The initial classification model is trained using the feature vectors corresponding to the positive samples and the negative samples to obtain a trained classification model.

2. The fine-grained test case detection method based on machine learning according to claim 1 is characterized in that: The step of obtaining product code change information of the target software and obtaining a changed target product function according to the product code change information includes: Obtain product code change information of the target software, and search for target Java files that have been changed in the product code change information in units of code lines; The line number interval range of each function in the target Java file is parsed by a syntax parser, and the changed target product function is obtained according to the changed code line number.

3. The fine-grained test case detection method based on machine learning according to claim 1 is characterized in that: The step of searching for the changed product code and test code from all historical submissions of the target code library, and determining the changed sample changed function according to the changed product code and test code, comprises: Find the changed production code and test code from all historical commits of the target code base; In units of code lines, searching for code lines that are changed in the product code and the test code; The line number interval range of each function in the file where the code line is located is parsed by a syntax parser to obtain a changed sample function.

4. The fine-grained test case detection method based on machine learning according to claim 1 is characterized in that: After calculating the shortest interval between each of the second change time and the first change time, the method further includes: If the shortest interval time is greater than or equal to the preset first time threshold, obtaining a preset second time threshold, and comparing the shortest interval time with the preset second time threshold; If the shortest interval time is greater than the preset second time threshold, the initial sample label of the sample change function is recorded as unchanged; when the sample labels are all unchanged, they are merged into negative samples; when the sample labels have both unchanged and any one of the three: should be created, should be edited, and should be deleted, they are merged into positive samples.

5. A fine-grained test case detection device based on machine learning, characterized in that: The device comprises: An acquisition module, used for acquiring product code change information of the target software, and obtaining the changed target product function according to the product code change information; An extraction module, used for extracting the function granularity feature corresponding to the target product function, and obtaining the target feature vector corresponding to the target product function according to the function granularity feature; A detection module, used for inputting the target feature vector into a pre-trained classification model to obtain the change type result of the test case that needs to be changed in the test code of the target software and the specific location information of the test case; The training steps of the classification model include: Find the changed product code and test code from all historical commits of the target code library, and determine the changed sample changed functions according to the changed product code and test code; If the Java class where the sample change function is located is a product class, then find a test class that tests the class where the sample change function is located, and obtain positive samples and negative samples of function granularity according to the test class; Using the positive samples and negative samples to train the initial classification model to obtain a trained classification model; If the Java class where the sample modification function is located is a product class, then a test class for testing the class where the sample modification function is located is searched, and positive samples and negative samples of function granularity are obtained according to the test class, including: If the Java class where the sample modification function is located is a product class, determining a test class for testing the Java class where the sample modification function is located according to a preset naming convention; Finding the multi-layer calling relationship of functions between the product class and the test class through a syntax parser, and determining all sample test functions for testing the sample change function according to the multi-layer calling relationship; Obtaining an initial sample label of the sample change function according to a co-evolution relationship between the sample test function and the sample change function; If the same change of the same sample change function corresponds to multiple initial sample labels, then the initial sample labels are merged according to the preset merging rules to obtain positive samples and negative samples at the function granularity; If the relevant test class cannot be found, look for the test class that imports the class in the import statement and the test class with the class name written in the code; Obtaining an initial sample label of the sample change function according to the co-evolution relationship between the sample test function and the sample change function, including: Find the first change time corresponding to the sample change function and the second change time corresponding to all sample test functions; Calculate the shortest interval between each of the second change time and the first change time, and use the second change time corresponding to the shortest interval as the target second change time; If the shortest interval time is less than a preset first time threshold, determining the change information of the sample change function between the first change time and the target second change time; If the change information indicates that there is no further change between the first change time and the target second change time, determining the change type of the sample test function between the first change time and the target second change time, and recording the change type as the initial sample label of the sample change function; Wherein, the change type is should be changed; or, the change type includes: should be created, should be edited and should be deleted; when all sample labels are should be changed, they are merged into positive samples; when all sample labels are should be created, they are merged into positive samples; when all sample labels are should be edited, they are merged into positive samples; when all sample labels are should be deleted, they are merged into positive samples; when any two or more of the three sample labels are should be created, should be edited and should be deleted, they are merged into positive samples; The initial classification model is trained using the positive samples and the negative samples to obtain a trained classification model, including: Preliminarily screen all AST node types in Java to obtain a feature list; Determine the code position information of each feature in the feature list, and construct a segment tree according to each feature and the corresponding code position information; Querying the segment tree to determine sample features contained in the changed product code lines in the positive sample and the negative sample; Obtaining a feature vector corresponding to each sample according to the sample feature, wherein the feature vector represents the total number of lines increased or decreased in the target change function that contains the sample feature; The initial classification model is trained using the feature vectors corresponding to the positive samples and the negative samples to obtain a trained classification model.

6. A terminal, characterized in that: include: A memory, a processor, and a fine-grained test case detection program based on machine learning stored in the memory and executable on the processor, wherein the fine-grained test case detection program based on machine learning, when executed by the processor, implements the steps of the fine-grained test case detection method based on machine learning as described in any one of claims 1 to 4.

7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which can be executed to implement the steps of the fine-grained test case detection method based on machine learning as described in any one of claims 1 to 4.

Citation Information

Patent Citations

  • Fuzzy test case generation method based on machine learning method

    CN110443045A

  • Method for promoting co-evolution of test code and product code and terminal equipment

    CN113010420A

  • Computer-implemented method of predicting testcase status

    EP3839744A1