Code testing method, device, computer equipment and storage medium for dividing code
By dividing and weighting the code coverage data, the problem of inaccurately reflecting code quality in the prior art is solved, and a more accurate code coverage evaluation is achieved.
Patent Information
- Application Number
- CN202210927135.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-08-03
- Publication Date
- 2025-07-22
- Estimated Expiration
- 2042-08-03
AI Technical Summary
In the existing code coverage test, the quality of the application code data to be tested cannot be accurately reflected because the differences between the code statements are not considered.
By code division of the target code coverage data, the attribute information and initial coverage weight of each subcode coverage data are determined, and the target code coverage is obtained by weighting processing.
Accurately reflect the quality of the application code data to be tested, considers the differences between code statements, and improves the accuracy of code coverage.
Smart Images

Figure CN115344477B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of application testing, and in particular to a code testing method, device, computer device, and storage medium for dividing code. Background Art
[0002] In modern software R & D systems, test measurement activities are an essential part of the software R & D process. Through measurement, the effectiveness, integrity of testing, and the quality of work products are judged, and quantitative guidance is provided for the improvement of the R & D process. The measurement scope includes time schedule, cost investment, scale, test quality, product quality, etc. Among them, the measurement of test quality is the most important dimension, which has direct reference value for evaluating the final release quality of work products. Test quality measurement includes various methods, such as requirement coverage rate, case pass rate, code coverage rate, etc. Among them, the measurement of code coverage rate is the most accurate and intuitive method to reflect the test effect.
[0003] In existing code coverage testing, after using a test tool to execute the code on the code data to be tested and obtaining the corresponding code coverage data, the corresponding code coverage rate is directly obtained according to the execution situation in the code coverage data, without considering the differences between code statements in the code data to be tested, resulting in the code coverage rate obtained not being able to accurately reflect the quality of the code data of the application to be tested. Summary of the Invention
[0004] Aiming at the deficiencies in the prior art, the present invention provides a code testing method, device, computer device, and storage medium for dividing code.
[0005] In a first aspect, in one embodiment, the present invention provides a code testing method for dividing code, including:
[0006] Obtain a test request sent by a calling party and the corresponding code data to be tested;
[0007] Execute the code on the code data to be tested according to the test request sent by the calling party to obtain the target code coverage data of the code data to be tested;
[0008] Divide the code statements in the target code coverage data to obtain multiple sub-code coverage data;
[0009] Determine the sub-code coverage rate of each sub-code coverage data, and determine the initial coverage rate weight of each sub-code coverage rate according to the attribute information of each sub-code coverage data;
[0010] Perform weighted processing on the sub-code coverage rate of each sub-code coverage data according to the initial coverage rate weight of each sub-code coverage data to obtain the target code coverage rate of the target code coverage data.
[0011] In one embodiment, according to the initial coverage rate weights of each sub-code coverage data, the sub-code coverage rates of each sub-code coverage data are weighted to obtain the target code coverage rate of the target code coverage data, including:
[0012] Determine the quantity ratio of each sub-code coverage data to the target code coverage data;
[0013] According to the initial coverage rate weights of each sub-code coverage data and the quantity ratio of each sub-code coverage data to the target code coverage data, obtain the target coverage rate weights of each sub-code coverage data;
[0014] According to the target coverage rate weights of each sub-code coverage data, the sub-code coverage rates of each sub-code coverage data are weighted to obtain the target code coverage rate.
[0015] In one embodiment, the code statements in the target code coverage data are code-divided to obtain multiple sub-code coverage data, including:
[0016] Determine the code types of the code statements in the target code coverage data; the code types include conditional judgment types, loop execution types, and transfer types;
[0017] According to the code types of the code statements in the target code coverage data, the code statements in the target code coverage data are code-divided to obtain multiple sub-code coverage data; each sub-code coverage data corresponds to one code type.
[0018] In one embodiment, determining the code types of the code statements in the target code coverage data includes:
[0019] Determine the characteristic code characters of the code statements in the target code coverage data;
[0020] According to the characteristic code characters, determine the code types of the code statements in the target code coverage data.
[0021] In one embodiment, the attribute information includes the code types of the code statements in the target code coverage data; determining the initial coverage rate weights of each sub-code coverage rate according to the attribute information of each sub-code coverage data includes:
[0022] Obtain a preset target weight configuration table; the target weight configuration table includes multiple sample coverage rate weights and the sample code types corresponding to the sample coverage rate weights;
[0023] For each sub-code coverage data, according to the code type corresponding to the sub-code coverage data, match the corresponding sample code type in the target weight configuration table, and set the sample coverage rate weight corresponding to the matched sample code type as the initial coverage rate weight of the sub-code coverage data block.
[0024] In one embodiment, according to the initial coverage rate weights of each sub-code coverage data, the sub-code coverage rates of each sub-code coverage data are weighted to obtain the target code coverage rate of the target code coverage data, including:
[0025] The sub-code coverage rates of each sub-code coverage data are weighted and averaged according to the initial coverage rate weights of each sub-code coverage data to obtain the target code coverage rate.
[0026] In one embodiment, after the step of obtaining the target code coverage rate of the target code coverage data, the above-mentioned code testing method for dividing code further includes:
[0027] Evaluating and analyzing the target code coverage rate to obtain a coverage rate analysis report corresponding to the target code coverage rate;
[0028] Outputting a prompt message according to the coverage rate analysis report.
[0029] In a second aspect, in one embodiment, the present invention provides a code testing device for dividing code, including:
[0030] A data acquisition module, configured to acquire a test request sent by a caller and corresponding to-be-tested code data;
[0031] A code execution module, configured to execute the to-be-tested code data according to the test request sent by the caller to obtain target code coverage data of the to-be-tested code data;
[0032] A code division module, configured to divide code statements in the target code coverage data to obtain a plurality of sub-code coverage data;
[0033] A coverage determination module, configured to determine the sub-code coverage rates of each sub-code coverage data, and determine the initial coverage rate weights of each sub-code coverage rate according to the attribute information of each sub-code coverage data;
[0034] A weighting processing module, configured to weight the sub-code coverage rates of each sub-code coverage data according to the initial coverage rate weights of each sub-code coverage data to obtain the target code coverage rate of the target code coverage data.
[0035] In a third aspect, in one embodiment, the present invention provides a computer device, including a memory and a processor; the memory stores a computer program, and the processor is configured to run the computer program in the memory to execute the steps in the above-mentioned code testing method for dividing code in any one of the embodiments.
[0036] Fourth aspect, in one embodiment, the present invention provides a storage medium storing a computer program which is loaded by a processor to execute the steps in the code testing method for dividing code in any of the above embodiments.
[0037] Through the above code testing method, device, computer device and storage medium for dividing code, after executing the code on the code data to be tested, the code statements in the obtained target code coverage data are divided to obtain a plurality of sub-code coverage data, and the influence magnitude of the sub-code coverage data is determined according to the attribute information of each sub-code coverage data, that is, the initial coverage rate weight of the sub-code coverage rate of each sub-code coverage data is determined, and finally the target code coverage rate is directly obtained by weighting each sub-code coverage rate according to the corresponding initial coverage rate weight; the testing process takes into account the differences between the code statements in the code data to be tested, and the obtained target code coverage rate can accurately reflect the quality of the code data of the application to be tested. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] To more clearly illustrate the technical solutions in the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments. Obviously, the following drawings are only some embodiments of the present invention, and those skilled in the art can obtain other drawings without creative efforts based on these drawings.
[0039] Figure 1 It is a schematic diagram of the application scenario of the code testing method for dividing code in an embodiment of the present invention;
[0040] Figure 2 It is a schematic flow diagram of the code testing method for dividing code in an embodiment of the present invention;
[0041] Figure 3 It is a schematic structural diagram of the code testing device for dividing code in an embodiment of the present invention;
[0042] Figure 4 It is a schematic internal structure diagram of a computer device in an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0043] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the drawings in the embodiments of the present invention. Obviously, the described embodiments are only some of the embodiments of the present invention, rather than all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative efforts fall within the protection scope of the present invention.
[0044] In the description of the present invention, it should be understood that the orientation or positional relationship indicated by the terms "center", "longitudinal", "transverse", "length", "width", "thickness", "upper", "lower", "front", "rear", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer", etc. is based on the orientation or positional relationship shown in the drawings. It is only for the convenience of describing the present invention and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation. Therefore, it should not be construed as a limitation of the present invention. In addition, the terms "first" and "second" are only used for descriptive purposes and cannot be understood as indicating or implying relative importance or implicitly specifying the quantity of the indicated technical features. Thus, the features defined with "first" and "second" may explicitly or implicitly include one or more of the described features. In the description of the present application, "a plurality" means two or more unless otherwise specifically defined. In the present application, the term "exemplary" is used to mean "serving as an example, illustration, or explanation". Any embodiment described as "exemplary" in the present application is not necessarily to be construed as more preferred or more advantageous than other embodiments. In order for any person skilled in the art to implement and use the present invention, the following description is given. In the following description, details are set forth for purposes of explanation. It should be understood that those of ordinary skill in the art can recognize that the present invention can be implemented without the use of these specific details. In other instances, well-known structures and processes are not elaborated in detail to avoid obscuring the description of the present invention with unnecessary details. Therefore, the present invention is not intended to be limited to the embodiments shown, but is to be accorded the widest scope consistent with the principles and features disclosed in the present application.
[0045] The code testing method for dividing codes in the embodiments of the present invention is applied to a code testing device for dividing codes, and the code testing device for dividing codes is set in a computer device; the computer device can be a terminal, for example, a mobile phone or a tablet computer, and the computer device can also be a server, or a service cluster composed of multiple servers.
[0046] As Figure 1 shown, Figure 1 FIG. is a schematic diagram of the application scenario of the code testing method for dividing codes in the embodiments of the present invention. The application scenario of the code testing method for dividing codes in the embodiments of the present invention includes a computer device 100 (the code testing device for dividing codes is integrated in the computer device 100), and a computer-readable storage medium in the computer device 100 that runs the steps corresponding to the code testing method for dividing codes to execute the steps of the code testing method for dividing codes.
[0047] It can be understood that Figure 1The computer device in the application scenario of the code testing method for the division code shown, or the devices included in the computer device, do not constitute a limitation to the embodiments of the present invention. That is, the number of devices, the types of devices included in the application scenario of the code testing method for the division code, or the number of devices and the types of devices included in each device do not affect the overall implementation of the technical solution in the embodiments of the present invention, and can all be regarded as equivalent replacements or derivatives of the technical solution required to be protected in the embodiments of the present invention.
[0048] In the embodiments of the present invention, the computer device 100 can be an independent device, or a device network or device cluster composed of devices. For example, the computer device 100 described in the embodiments of the present invention includes, but is not limited to, a computer, a network host, a single network device, a set of multiple network devices, or a cloud device composed of multiple devices. Among them, the cloud device is composed of a large number of computers or network devices based on cloud computing.
[0049] Those skilled in the art can understand that Figure 1 the application scenario shown is only an application scenario corresponding to the technical solution of the present invention, and does not constitute a limitation to the application scenario of the technical solution of the present invention. Other application scenarios can also include more or fewer computer devices than Figure 1 shown, or the network connection relationship of computer devices. For example Figure 1 only 1 computer device is shown in . It can be understood that the scenario of the code testing method for the division code can also include one or more other computer devices, which are not specifically limited here; the computer device 100 can also include a memory for storing information related to the code testing method for the division code.
[0050] In addition, in the application scenario of the code testing method for the division code in the embodiments of the present invention, the computer device 100 can be provided with a display device, or the computer device 100 is not provided with a display device and is communicatively connected to an external display device 200. The display device 200 is used to output the result of the execution of the code testing method for the division code in the computer device. The computer device 100 can access the background database 300 (the background database 300 can be the local memory of the computer device 100, and the background database 300 can also be set in the cloud). The background database 300 stores information related to the code testing method for the division code.
[0051] It should be noted that Figure 1 the application scenario of the code testing method for the division code shown is only an example. The application scenario of the code testing method for the division code described in the embodiments of the present invention is to more clearly illustrate the technical solution of the embodiments of the present invention, and does not constitute a limitation to the technical solution provided by the embodiments of the present invention.
[0052] Based on the application scenario of the code testing method for the divided code, embodiments of the code testing method for the divided code are proposed.
[0053] In a first aspect, as Figure 2 shown, in one embodiment, the present invention provides a code testing method for divided code, including:
[0054] Step 201, obtain a test request sent by a caller and the corresponding code data to be tested;
[0055] Among them, the code data to be tested can be directly included in the test request sent by the caller (in this way, it is necessary to parse the test request to obtain the code data to be tested), or stored at a certain address, and the address is included in the test request sent by the caller. Using the address method can reduce the data volume of the test request, thereby saving transmission resources;
[0056] Among them, the caller refers to a terminal device that needs to send a test request to call the service provider to perform code coverage testing on the corresponding software application. Similar to the caller, the execution entity of the code testing method for the divided code in this embodiment is the service provider, which refers to a terminal device that responds to the test request of the caller to perform code coverage testing; usually, the caller and the service provider send test requests and respond to test requests based on different terminal devices. Among them, the caller and the service provider can send and receive test requests through the HTTP protocol (HyperText Transfer Protocol, which is an application layer transmission protocol based on the TCP protocol. Simply put, it is a rule for data transmission between the client and the server), that is, the test request is an http request (the http request includes an http request line, an http request header, and an http request body). In this http request, the code data to be tested or the address data is stored in the http request body;
[0057] Step 202, execute the code on the code data to be tested according to the test request sent by the caller to obtain the target code coverage data of the code data to be tested;
[0058] Among them, the target code coverage data refers to the code data obtained after the code data to be tested is executed by a corresponding test tool, which contains marks of the executed code data, and the marked executed code data can be used for subsequent calculation of the code coverage rate;
[0059] Step 203, divide the code statements in the target code coverage data to obtain multiple sub-code coverage data;
[0060] Among them, code partitioning refers to disassembling all code statements in the target code coverage data to obtain multiple sets of code statements, and each set serves as the corresponding sub-code coverage data; as mentioned in the above steps, the target code coverage data contains marked executed code data, and in this step, only all code statements in the target code coverage data are extracted and recombined. Therefore, the sub-code coverage data obtained still contains the originally marked executed code data, that is, the sub-code coverage rate of the sub-code coverage data can be directly calculated based on the sub-code coverage data subsequently.
[0061] Among them, code partitioning is to obtain multiple sub-code coverage data, so that the corresponding sub-code coverage rates can be integrated into a target code coverage rate through weighted processing in the subsequent process, so as to reflect the differences between code statements in the code data to be tested; therefore, code partitioning can be carried out in a random manner. For example, through a preset random mechanism, any number of code statements in the target code coverage data are extracted, and then the corresponding sub-code coverage data is combined; it can also be carried out in an ordered manner. For example, the code statements from line x to line x + n are extracted, and then the corresponding sub-code coverage data is combined.
[0062] Step 204, determine the sub-code coverage rates of each sub-code coverage data, and determine the initial coverage weights of each sub-code coverage rate according to the attribute information of each sub-code coverage data.
[0063] Among them, for each sub-code coverage data, the corresponding sub-code coverage rate can be obtained through different coverage analysis strategies; for example, the decision coverage analysis strategy (indicating the correct or incorrect execution of each boolean expression in the source code, and the goal of decision coverage is to cover and verify all accessible source code by checking and ensuring that each branch of each decision point is executed at least once), the branch coverage analysis strategy (testing each result from a code module (statement or loop), and the purpose of branch coverage is to ensure that each decision condition from each branch is executed at least once, which helps to measure the percentage of independent code segments and find the parts without branches; for example, if the result is of boolean type, both True and False results need to be tested) or the condition coverage analysis strategy (used to test and evaluate variables or sub-expressions in conditional statements, and the goal of condition coverage is to check the individual results of each logical condition. Compared with decision coverage, condition coverage is more sensitive to the control flow).
[0064] Among them, the attribute information may be the code type corresponding to the sub-code coverage data. Specifically, it may be the proportion of code statements of each code type in the sub-code coverage data. For example, the code statements of a certain code type have a greater impact when the entire code data to be tested is actually executed (for example, when the code statements of this code type are missing, it may seriously affect the execution process or even cause the inability to execute). Therefore, it is possible to further analyze whether there are code statements of this code type in the sub-code coverage data and how many code statements of this code type exist, so as to determine the initial coverage rate weight of the sub-code coverage rate of the sub-code coverage data;
[0065] Step 205, according to the initial coverage rate weights of each sub-code coverage data, perform weighted processing on the sub-code coverage rates of each sub-code coverage data to obtain the target code coverage rate of the target code coverage data;
[0066] Among them, the weighted processing can be in the way of weighted summation. For example, the multiple sub-code coverage data obtained by division are sub-code coverage data A, sub-code coverage data B, sub-code coverage data C, and sub-code coverage data D respectively. If the obtained sub-code coverage rates are 80%, 80%, 90%, and 90% respectively, and the corresponding initial coverage rate weights are 30%, 30%, 20%, and 20% respectively, then the final target code coverage rate is:
[0067] 80% * 30% + 80% * 30% + 90% * 20% + 90% * 20% = 24% + 24% + 18% + 18% = 84%.
[0068] Through the above code testing method for dividing code, after the code data to be tested is executed, the code statements in the obtained target code coverage data are divided into code, so as to obtain multiple sub-code coverage data. According to the attribute information of each sub-code coverage data, the influence size of the sub-code coverage data is determined, that is, the initial coverage rate weight of the sub-code coverage rate of each sub-code coverage data is determined. Finally, the target code coverage rate is directly obtained by weighting each sub-code coverage rate according to the corresponding initial coverage rate weight; making the testing process consider the differences between each code statement in the code data to be tested, and the obtained target code coverage rate can accurately reflect the quality of the code data of the application to be tested.
[0069] In one embodiment, according to the initial coverage rate weights of each sub-code coverage data, performing weighted processing on the sub-code coverage rates of each sub-code coverage data to obtain the target code coverage rate of the target code coverage data includes:
[0070] Determine the quantity ratio of each sub-code coverage data to the target code coverage data;
[0071] Among them, since the sub-code coverage data is obtained by code division, the number of statements between different sub-code coverage data may be different, and the number of statements of the sub-code coverage data will affect the result of subsequent weighting processing. For example, if multiple sub-code coverage data are sub-code coverage data A and sub-code coverage data B respectively, and the obtained sub-code coverage rates are 90% and 20% respectively (this data is only for illustrative purposes and does not require the actual code coverage rate to reach this limit), and the corresponding initial coverage rate weights are 50% and 50% respectively. At this time, if the quantity ratio is not considered, the obtained target code coverage rate is 90% * 50% + 20% * 50% = 55%. If the quantity ratios of sub-code coverage data A and sub-code coverage data B to the target code coverage data are 90% and 10% respectively, it indicates that in the actual situation, the code coverage rates of most code data in the code data to be tested are relatively high, and only a small part of the code data has a relatively low code coverage rate. Therefore, the final target code coverage rate should be relatively high. Therefore, it is necessary to determine the quantity ratio of each sub-code coverage data to the target code coverage data and apply the quantity ratio to the subsequent weighting processing to avoid the distortion problem caused by the quantity difference between each sub-code coverage data. It should be noted that when the quantity ratios of multiple sub-code coverage data obtained by division to the target code coverage data are basically the same, there is no need to apply the quantity ratio to the subsequent weighting processing;
[0072] According to the initial coverage rate weights of each sub-code coverage data and the quantity ratio of each sub-code coverage data to the target code coverage data, obtain the target coverage rate weights of each sub-code coverage data;
[0073] Among them, for example, if multiple sub-code coverage data are sub-code coverage data A and sub-code coverage data B respectively, and the obtained sub-code coverage rates are 90% and 20% respectively, the corresponding initial coverage rate weights are 50% and 50% respectively, and the quantity ratios of sub-code coverage data A and sub-code coverage data B to the target code coverage data are 90% and 10% respectively, then the obtained target coverage rate weights are 50% * 90% = 45% and 50% * 10% = 5% respectively. Since the sum of the obtained target coverage rate weights is not equal to 100%, they can be converted. The finally obtained target coverage rate weights are 90% and 10% respectively, which is obviously significantly different from the initial coverage rate weights of 50% and 50%;
[0074] According to the target coverage rate weights of each sub-code coverage data, perform weighting processing on the sub-code coverage rates of each sub-code coverage data to obtain the target code coverage rate;
[0075] Among them, taking the example in the above steps as an example, the target code coverage rate is:
[0076] 90% * 90% + 20% * 10% = 83%,
[0077] Obviously, this result is more in line with the actual situation and there is no distortion.
[0078] In one embodiment, the code statements in the target code coverage data are divided into multiple sub-code coverage data, including:
[0079] Determine the code type of the code statements in the target code coverage data;
[0080] Among them, the code types include conditional judgment type, loop execution type, and transfer type;
[0081] Among them, in addition to control statement types such as conditional judgment type, loop execution type, and transfer type, it may also include expression statement type, function call statement type, compound statement type, and empty statement type, etc.;
[0082] Among them, after determining the code type, the code type can be used as the condition for code division, thus completing the code division;
[0083] According to the code type of the code statements in the target code coverage data, the code statements in the target code coverage data are divided into multiple sub-code coverage data; each sub-code coverage data corresponds to one code type;
[0084] Among them, since the code type is used as the condition for code division, each sub-code coverage data obtained by division will only contain code statements of one code type, so that it can be more efficient and accurate to determine the initial code coverage weight of the sub-code coverage data according to the code type of the code statements in the sub-code coverage data subsequently.
[0085] In one embodiment, determining the code type of the code statements in the target code coverage data includes:
[0086] Determine the characteristic code characters of the code statements in the target code coverage data;
[0087] Among them, the characteristic code characters refer to one or more characters in the code statements, such as printf, if, switch, do while, while, for, break, goto, continue, return, etc.;
[0088] According to the characteristic code characters, determine the code type of the code statements in the target code coverage data;
[0089] Among them, the feature code character can represent the function of the corresponding code statement, that is, it can show the code type of the corresponding code statement. For example, when the feature code character is printf, it can be determined that the code type of the corresponding code statement is a function call statement type. For example, when the feature code characters are if and switch, it can be determined that the code type of the corresponding code statement is a conditional judgment type. For example, when the feature code characters are do while, while, and for, it can be determined that the code type of the corresponding code statement is a loop execution type. For example, when the feature code characters are break, goto, continue, and return, it can be determined that the code type of the corresponding code statement is a transfer type.
[0090] In one embodiment, the attribute information includes the code type of the code statement in the target code coverage data. Determining the initial coverage rate weight of each sub-code coverage rate according to the attribute information of each sub-code coverage data includes:
[0091] Obtain a preset target weight configuration table;
[0092] Among them, the target weight configuration table includes multiple sample coverage rate weights and the sample code types corresponding to the sample coverage rate weights;
[0093] Among them, the target weight configuration table is preset by the tester according to empirical values, and the coverage rate weights of all code types are included in the target weight configuration table;
[0094] For each sub-code coverage data, according to the code type corresponding to the sub-code coverage data, match the corresponding sample code type in the target weight configuration table, and set the sample coverage rate weight corresponding to the matched sample code type as the initial coverage rate weight of the sub-code coverage data block;
[0095] Among them, after obtaining the target weight configuration table, the corresponding coverage rate weight can be matched according to the code type. For example, if the determined code types include conditional judgment type, loop execution type, and transfer type, only the sample coverage rate weights corresponding to these three code types need to be matched in the target weight configuration table and set as the initial coverage rate weights.
[0096] In one embodiment, before the step of obtaining the preset target weight configuration table, the above code testing method for dividing codes further includes:
[0097] Set multiple sample code types; the sample code types include conditional judgment type, loop execution type, and transfer type;
[0098] Respond to the weight setting instruction and set the sample coverage rate weights corresponding to each sample code type;
[0099] Construct a configuration table according to each sample code type and the corresponding sample coverage weight to obtain a target weight configuration table;
[0100] Among them, the sample coverage weight of the conditional decision type is greater than the sample coverage weights of the loop execution type and the branch type; under normal circumstances, the code statements of the conditional decision type have a greater impact on the execution process.
[0101] In one embodiment, obtaining a preset target weight configuration table includes:
[0102] Obtain a preset configuration table template library;
[0103] Among them, the configuration table template library includes multiple sample weight configuration tables and the sample application types corresponding to the sample weight configuration tables;
[0104] Among them, testers can pre-set multiple weight configuration tables for application types according to experience values. Different types of applications have different test focuses. For example, some applications may place more emphasis on condition coverage and decision coverage, while others may place more emphasis on other coverages. Therefore, different types of applications require different weight configuration tables during testing, that is, the configuration table template library contains weight configuration tables corresponding to all application types;
[0105] Determine the target application type of the application under test corresponding to the code data to be tested;
[0106] Among them, the target application type can be obtained by identifying and analyzing the code data to be tested, or it can be obtained by the tester adding the target application type to the test request when sending the test request through the calling party, and then parsing it after receiving the test request;
[0107] Among them, when obtaining the target application type through identification and analysis, an AI (Artificial Intelligence) model can be used to perform efficient processing using various typical high-performance machine learning algorithms; specifically, obtain a trained type recognition model, input the initial code data to be tested into the type recognition module, and obtain the target application type output by the type recognition model. By taking advantage of the AI algorithm, through training and learning with a large amount of data, the type recognition model can automatically avoid some errors, thereby greatly improving the accuracy and efficiency of recognition;
[0108] According to the target application type, match the corresponding sample application type in the configuration table template library, and set the sample weight configuration table corresponding to the matched sample application type as the target weight configuration table;
[0109] Among them, after obtaining the configuration table template library, the corresponding weight configuration table can be matched according to the application type. For example, if the target application type of the application to be tested is "focusing on condition coverage and decision coverage", only the sample weight configuration table corresponding to "focusing on condition coverage and decision coverage" needs to be matched in the configuration table template library and set as the target weight configuration table. If the application to be tested is of other application types, the matching can be carried out in the configuration table module library according to the application type in the same way.
[0110] In one embodiment, the training steps of the type recognition model include:
[0111] Obtain a training sample set, which includes multiple training samples. Each training sample includes training code data to be tested and a training application type. Among them, the training application type is obtained by manually processing the training code data to be tested;
[0112] Obtain a training sample, use the training code data to be tested as the input of the type recognition model, use the training application type as the expected output of the type recognition model, and train the type recognition model, that is, complete one training;
[0113] Determine the comparison result between the actual output and the expected output of the type recognition model. If the comparison result does not meet the requirements, update the model parameters of the type recognition model according to the comparison result;
[0114] Obtain the next training sample, and then re-enter the step of using the training code data to be tested as the input of the type recognition model, using the training application type as the expected output of the type recognition model, and training the type recognition model, until the obtained comparison result meets the requirements, then stop training to obtain the trained type recognition model.
[0115] In one embodiment, according to the initial coverage rate weights of each sub-code coverage data, performing weighted processing on the sub-code coverage rates of each sub-code coverage data to obtain the target code coverage rate of the target code coverage data, including:
[0116] Perform weighted averaging on the sub-code coverage rates of each sub-code coverage data according to the initial coverage rate weights of each sub-code coverage data to obtain the target code coverage rate;
[0117] Among them, since the code types in the target code coverage data corresponding to different applications to be tested are different, for example, the code types in the target code coverage data A include code type A, code type B, and code type C, and the code types in the target code coverage data B include code type A and code type B, if the target weight configuration table required by the target code coverage data A and the target code coverage data B is the same, then the target weight configuration table at least includes the sample coverage weights corresponding to code type A, code type B, and code type C. If the sum of these three sample coverage weights is 100%, when weighted summation is used, the total value of the target code coverage corresponding to the target code coverage data A is 100%. Since the value of the sample coverage weight is fixed, when weighted summation is still used, the total value of the target code coverage of the target code coverage data B that lacks code type C will be lower than 100%, which is not convenient for subsequent testers to view and requires further conversion. Through weighted average processing, the total value of the target code coverage of each target code coverage data can be made 100%, which is convenient for testers to view.
[0118] In one embodiment, determining the subcode coverage rate of each subcode coverage data includes:
[0119] Determine the executed code data and the total code data in the sub-code coverage data;
[0120] As mentioned above, in the sub-code coverage data, the executed code data is marked, so the executed code data can be obtained based on the mark, and the total code data is the sub-code coverage data itself;
[0121] The sub-code coverage is determined based on the ratio of the executed code data to the total code data;
[0122] Among them, determining the corresponding code coverage rate according to the quantity ratio is an efficient coverage analysis strategy, namely, the statement coverage analysis strategy (all executable statements in the source code are executed at least once, which is used to calculate the number of statements executed in the source code. The main purpose of statement coverage is to cover all possible paths, lines and statements in the source code);
[0123] Among them, if the executed code data includes 800 statements and the total code data includes 1000 statements, the corresponding quantity ratio is 800 / 1000. Converting it into a percentage, we can get the corresponding initial code coverage rate: 80%.
[0124] The structure of the statement coverage analysis strategy is simple and can easily handle the test code data with a large amount of code.
[0125] In one embodiment, after the step of obtaining the target code coverage rate of the target code coverage data, the above-mentioned code testing method for dividing code further includes:
[0126] Evaluating and analyzing the target code coverage rate to obtain a coverage analysis report corresponding to the target code coverage rate;
[0127] Outputting a prompt message according to the coverage analysis report;
[0128] Among them, the coverage analysis report can be directly added to the prompt message and output to the terminal device of the tester together. When the tester receives the prompt message, the corresponding coverage analysis report can be immediately obtained;
[0129] Among them, the obtained target code coverage rate is only a percentage number, and relevant personnel cannot perform in-depth analysis based on this percentage number. In the test service provider, due to the existence of various intermediate data, the specific composition of the target code coverage rate can be further analyzed, such as which part of the code has a lower coverage rate and which part of the code has a higher coverage rate, so as to obtain a comprehensive coverage analysis report. When relevant personnel view this coverage analysis report, they can very intuitively judge the quality of the initial code data to be tested;
[0130] Among them, the output of the prompt message can be in the forms of email, text message, and phone call, etc. It can be understood that the data forms of the prompt messages corresponding to different output methods are different, including forms such as voice, text, picture, and video;
[0131] Among them, the prompt message can be adjusted according to the evaluation identifier of the coverage analysis report. The evaluation identifier is used to represent the size of the target code coverage rate. For example, if the target code coverage rate is lower than 60%, the evaluation identifier can be "poor". For example, if the target code coverage rate is higher than 90%, the evaluation identifier can be "good"; if the evaluation identifier is "poor", the prompt message can be output through multiple channels such as email, text message, and phone call at the same time. If the evaluation identifier is "good", the prompt message can be output only through email in a single channel, so that the output of the prompt message takes into account the urgency of the coverage analysis report, in order to use the output resources more efficiently.
[0132] In a second aspect, as Figure 3 shown, in one embodiment, the present invention provides a code testing device for dividing code, including:
[0133] A data acquisition module 301, configured to acquire a test request sent by a calling party and corresponding code data to be tested;
[0134] The code execution module 302 is configured to execute the code on the code data to be tested according to the test request sent by the caller, and obtain the target code coverage data of the code data to be tested;
[0135] The code division module 303 is configured to divide the code statements in the target code coverage data to obtain multiple sub-code coverage data;
[0136] The coverage determination module 304 is configured to determine the sub-code coverage rate of each sub-code coverage data, and determine the initial coverage rate weight of each sub-code coverage rate according to the attribute information of each sub-code coverage data;
[0137] The weighted processing module 305 is configured to perform weighted processing on the sub-code coverage rate of each sub-code coverage data according to the initial coverage rate weight of each sub-code coverage data, and obtain the target code coverage rate of the target code coverage data.
[0138] Through the above-mentioned code testing device for dividing code, after executing the code on the code data to be tested, the code statements in the obtained target code coverage data are divided to obtain multiple sub-code coverage data, and the influence size of the sub-code coverage data is determined according to the attribute information of each sub-code coverage data, that is, the initial coverage rate weight of the sub-code coverage rate of each sub-code coverage data is determined, and finally the sub-code coverage rates are directly weighted according to the corresponding initial coverage rate weights to obtain the target code coverage rate; the test process takes into account the differences between the code statements in the code data to be tested, and the obtained target code coverage rate can accurately reflect the quality of the code data of the application to be tested.
[0139] In one embodiment, the weighted processing module is specifically configured to determine the quantity ratio of each sub-code coverage data to the target code coverage data; according to the initial coverage rate weight of each sub-code coverage data and the quantity ratio of each sub-code coverage data to the target code coverage data, obtain the target coverage rate weight of each sub-code coverage data; perform weighted processing on the sub-code coverage rate of each sub-code coverage data according to the target coverage rate weight of each sub-code coverage data, and obtain the target code coverage rate.
[0140] In one embodiment, the code division module is specifically configured to determine the target code type of the code statements in the target code coverage data; the target code type includes conditional determination type, loop execution type and transfer type; according to the target code type of the code statements in the target code coverage data, divide the code statements in the target code coverage data to obtain the sub-code coverage data corresponding to each target code type.
[0141] In one embodiment, the code division module is specifically configured to determine the characteristic code characters of the code statements in the target code coverage data; and determine the target code type of the code statements in the target code coverage data according to the characteristic code characters.
[0142] In one embodiment, the attribute information includes the target code type of the code statements in the target code coverage data; the coverage determination module is specifically configured to obtain a preset target weight configuration table; the target weight configuration table includes a plurality of sample coverage weights and the sample code types corresponding to the sample coverage weights; for each sub-code coverage data, according to the target code type corresponding to the sub-code coverage data, match the corresponding sample code type in the target weight configuration table, and set the sample coverage weight corresponding to the matched sample code type as the initial coverage weight of the sub-code coverage data block.
[0143] In one embodiment, the weighting processing module is specifically configured to perform weighted averaging on the sub-code coverage rates of each sub-code coverage data according to the initial coverage weights of each sub-code coverage data to obtain the target code coverage rate.
[0144] In one embodiment, the above-mentioned code testing device for dividing code further includes:
[0145] A prompt output module, configured to, after the step of obtaining the target code coverage rate, evaluate and analyze the target code coverage rate to obtain a coverage analysis report corresponding to the target code coverage rate; and output a prompt message according to the coverage analysis report.
[0146] In a third aspect, in one embodiment, the present invention provides a computer device, as Figure 4 shown, which shows the structure of the computer device involved in the present invention. Specifically:
[0147] The computer device may include a processor 401 with one or more processing cores, a memory 402 with one or more computer-readable storage media, a power supply 403, an input unit 404 and other components. Those skilled in the art can understand that Figure 4 the structure of the computer device shown in
[0148] The processor 401 is the control center of the computer device, connecting various parts of the entire computer device through various interfaces and circuits. By running or executing software programs and / or modules stored in the memory 402, and by invoking the data stored in the memory 402, it executes various functions of the computer device and processes data, thereby monitoring the computer device as a whole. Optionally, the processor 401 may include one or more processing cores; preferably, the processor 401 may integrate an application processor and a modem processor. Among them, the application processor mainly processes the operating system, user interface, computer programs, etc., and the modem processor mainly processes wireless communications. It can be understood that the above-mentioned modem processor may not be integrated into the processor 401 either.
[0149] The memory 402 can be used to store software programs and modules. The processor 401 executes various functional applications and data processing by running the software programs and modules stored in the memory 402. The memory 402 may mainly include a program storage area and a data storage area. Among them, the program storage area can store the operating system, computer programs required for at least one function (such as the sound playback function, image playback function, etc.), etc.; the data storage area can store data created according to the use of the server, etc. In addition, the memory 402 may include high-speed random access memory, and may also include non-volatile memory, such as at least one magnetic disk storage device, flash memory device, or other volatile solid-state storage devices. Correspondingly, the memory 402 may also include a memory controller to provide the processor 401 with access to the memory 402.
[0150] The computer device further includes a power supply 403 for supplying power to each component. Preferably, the power supply 403 can be logically connected to the processor 401 through a power management system, so as to realize functions such as management of charging, discharging, and power consumption management through the power management system. The power supply 403 may also include any components such as one or more DC or AC power supplies, a recharge system, a power failure detection circuit, a power converter or inverter, and a power status indicator.
[0151] The computer device may further include an input unit 404, which can be used to receive input digital or character information, and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.
[0152] Although not shown, the computer device may also include a display unit, etc., which will not be elaborated here. Specifically, in this embodiment, the processor 401 in the computer device will load the executable files corresponding to the processes of one or more computer programs into the memory 402 according to the following instructions, and the processor 401 will run the computer programs stored in the memory 402 to execute the following steps:
[0153] Obtain the test request sent by the caller and the corresponding code data to be tested;
[0154] Execute the code on the code data to be tested according to the test request sent by the caller, and obtain the target code coverage data of the code data to be tested;
[0155] Divide the code statements in the target code coverage data to obtain multiple sub-code coverage data;
[0156] Determine the sub-code coverage rate of each sub-code coverage data, and determine the initial coverage rate weight of each sub-code coverage rate according to the attribute information of each sub-code coverage data;
[0157] Perform weighted processing on the sub-code coverage rate of each sub-code coverage data according to the initial coverage rate weight of each sub-code coverage data to obtain the target code coverage rate of the target code coverage data.
[0158] Through the above computer device, after executing the code on the code data to be tested, divide the code statements in the obtained target code coverage data to obtain multiple sub-code coverage data, determine the influence size of the sub-code coverage data according to the attribute information of each sub-code coverage data, that is, determine the initial coverage rate weight of the sub-code coverage rate of each sub-code coverage data, and finally directly weight each sub-code coverage rate according to the corresponding initial coverage rate weight to obtain the target code coverage rate; enabling the test process to consider the differences between each code statement in the code data to be tested, and the obtained target code coverage rate can accurately reflect the quality of the code data of the application to be tested.
[0159] Those of ordinary skill in the art can understand that all or part of the steps in any of the above methods can be completed by a computer program, or by controlling related hardware through a computer program. The computer program can be stored in a computer-readable storage medium and loaded and executed by a processor.
[0160] In a fourth aspect, in an embodiment, the present invention provides a storage medium, in which multiple computer programs are stored, and the computer programs can be loaded by a processor to execute the following steps:
[0161] Obtain the test request sent by the caller and the corresponding code data to be tested;
[0162] Execute the code on the code data to be tested according to the test request sent by the caller, and obtain the target code coverage data of the code data to be tested;
[0163] Divide the code statements in the target code coverage data to obtain multiple sub-code coverage data;
[0164] Determine the sub-code coverage rate of each sub-code covering data, and determine the initial coverage rate weight of each sub-code coverage rate according to the attribute information of each sub-code covering data;
[0165] According to the initial coverage rate weights of each sub-code covering data, perform weighted processing on the sub-code coverage rates of each sub-code covering data to obtain the target code coverage rate of the target code covering data.
[0166] Through the above storage medium, after the code of the code to be tested is executed, the code statements in the obtained target code covering data are divided to obtain multiple sub-code covering data. Determine the influence size of the sub-code covering data according to the attribute information of each sub-code covering data, that is, determine the initial coverage rate weight of the sub-code coverage rate of each sub-code covering data. Finally, directly weight each sub-code coverage rate according to the corresponding initial coverage rate weight to obtain the target code coverage rate; enabling the test process to consider the differences between each code statement in the code data to be tested, and the obtained target code coverage rate can accurately reflect the quality of the code data of the application to be tested.
[0167] Those of ordinary skill in the art can understand that any reference to a memory, storage, database, or other medium used in the embodiments provided by the present invention may include non-volatile and / or volatile memories. Non-volatile memory may include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory may include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDR SDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), etc.
[0168] Since the computer program stored in the storage medium can execute the steps in the code test method for dividing code in any one of the embodiments provided by the present invention, therefore, the beneficial effects that can be achieved by the code test method for dividing code in any one of the embodiments provided by the present invention can be realized. For details, see the previous embodiments and will not be repeated here.
[0169] For the specific implementation of each of the above operations, reference can be made to the previous embodiments and will not be repeated here.
[0170] In the above embodiments, the descriptions of the respective embodiments have their own emphases. For parts not elaborated in a certain embodiment, reference may be made to the detailed descriptions of other embodiments above, which will not be repeated here.
[0171] The above has introduced in detail a code testing method, device, computer device and storage medium for dividing codes provided by the present invention. Specific examples are used in this article to elaborate on the principle and implementation manner of the present invention. The descriptions of the above embodiments are only used to help understand the method and its core idea of the present invention; at the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
[0172] The technical features of the above embodiments can be combined arbitrarily. For the sake of concise description, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, it should be considered as the scope recorded in this specification.
Claims
1. A code testing method for dividing codes, characterized in that, Including: Obtain a test request sent by a caller and corresponding code data to be tested; Execute the code on the code data to be tested according to the test request sent by the caller, and obtain target code coverage data of the code data to be tested; Determine the code type of the code statements in the target code coverage data; the code types include conditional judgment type, loop execution type, and transfer type; According to the code types of the code statements in the target code coverage data, divide the code statements in the target code coverage data to obtain multiple sub-code coverage data; each sub-code coverage data corresponds to one code type; Determine the sub-code coverage rate of each sub-code coverage data; Obtain a preset target weight configuration table; the target weight configuration table includes multiple sample coverage weights and the sample code types corresponding to the sample coverage weights; For each sub-code coverage data, according to the code type corresponding to the sub-code coverage data, match the corresponding sample code type in the target weight configuration table, and set the sample coverage weight corresponding to the matched sample code type as the initial coverage weight of the sub-code coverage data; According to the initial coverage weights of the sub-code coverage data, perform weighted processing on the sub-code coverage rates of the sub-code coverage data to obtain the target code coverage rate of the target code coverage data.
2. The code testing method for dividing codes according to claim 1, wherein The step of performing weighted processing on the sub-code coverage rates of the sub-code coverage data according to the initial coverage weights of the sub-code coverage data to obtain the target code coverage rate of the target code coverage data includes: Determine the quantity ratio of each sub-code coverage data to the target code coverage data; According to the initial coverage weights of the sub-code coverage data and the quantity ratio of each sub-code coverage data to the target code coverage data, obtain the target coverage weights of the sub-code coverage data; According to the target coverage weights of the sub-code coverage data, perform weighted processing on the sub-code coverage rates of the sub-code coverage data to obtain the target code coverage rate.
3. The code testing method for dividing codes according to claim 1, characterized in that, The step of determining the code type of the code statements in the target code coverage data includes: Determine the characteristic code characters of the code statements in the target code coverage data; According to the characteristic code characters, determine the code type of the code statements in the target code coverage data.
4. The code testing method for dividing codes according to claim 1, characterized in that, The step of performing weighted processing on the sub-code coverage rates of the sub-code coverage data according to the initial coverage weights of the sub-code coverage data to obtain the target code coverage rate of the target code coverage data includes: Perform weighted averaging on the sub-code coverage rates of the sub-code coverage data according to the initial coverage weights of the sub-code coverage data to obtain the target code coverage rate.
5. The code testing method for dividing codes according to claim 1, characterized in that After the step of obtaining the target code coverage rate of the target code coverage data, it further includes: Evaluate and analyze the target code coverage rate to obtain a coverage analysis report corresponding to the target code coverage rate; Output a prompt message according to the coverage analysis report.
6. A code testing device for dividing codes, characterized in that, Including: A data acquisition module for acquiring a test request sent by a caller and corresponding to-be-tested code data; A code execution module for executing the to-be-tested code data according to the test request sent by the caller to obtain target code coverage data of the to-be-tested code data; A code division module for determining the code type of code statements in the target code coverage data; the code type includes conditional determination type, loop execution type, and jump type, and dividing the code statements in the target code coverage data according to the code type of the code statements in the target code coverage data to obtain a plurality of sub-code coverage data; each of the sub-code coverage data corresponds to one code type; A coverage determination module for determining the sub-code coverage rate of each of the sub-code coverage data, obtaining a preset target weight configuration table, the target weight configuration table including a plurality of sample coverage rate weights and the sample code types corresponding to the sample coverage rate weights, and for each of the sub-code coverage data, matching the corresponding sample code type in the target weight configuration table according to the code type corresponding to the sub-code coverage data, and setting the sample coverage rate weight corresponding to the matched sample code type as the initial coverage rate weight of the sub-code coverage data; A weighted processing module for performing weighted processing on the sub-code coverage rates of the sub-code coverage data according to the initial coverage rate weights of the sub-code coverage data to obtain the target code coverage rate of the target code coverage data.
7. A computer device, characterized in that, It includes a memory and a processor; the memory stores a computer program, and the processor is used to run the computer program in the memory to execute the steps in the code testing method for dividing code according to any one of claims 1 to 5.
8. A storage medium, characterized in that, The storage medium stores a computer program, and the computer program is loaded by a processor to execute the steps in the code testing method for dividing code according to any one of claims 1 to 5.
Citation Information
Patent Citations
Code coverage rate monitoring method and device, electronic equipment and storage medium
CN111400189A
Method, device and equipment for detecting code coverage rate and storage medium
CN114721926A