Test case knowledge base construction method, device, electronic device and storage medium
By modularizing the mobile target project and renaming nodes, an accurate association between test cases and code modules is established, which solves the problem of inaccurate association between test cases and code, and improves the recommendation accuracy and coverage data of test cases. reliability.
Patent Information
- Application Number
- CN202110903393.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-08-06
- Publication Date
- 2024-10-22
- Estimated Expiration
- 2041-08-06
AI Technical Summary
In the existing technology, the bidirectional relationship between test cases and code in mobile terminal precision testing is inaccurate, which affects the accuracy of the test case recommendation results. Especially when processing coverage data of different programming languages, the anonymous function naming method causes the function to The name is not unique, which affects the accuracy of the association.
By modularizing the code of the target project, renaming the nodes to generate unique identities, and collecting code coverage data after the test cases are executed, establishing an association between test cases and code modules to ensure one-to-one correspondence between nodes and code modules. , improve the accuracy of the association relationship.
It improves the accuracy of the correlation between test cases and code modules, ensures accurate recommendation of test cases, and enhances the accuracy and reliability of test coverage data.
Smart Images

Figure CN113704094B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a test case knowledge base construction method, device, electronic equipment and storage medium. Background Art
[0002] In software engineering, testers use test cases to test whether application software or software systems work correctly. Among them, precision testing is a general test analysis system whose internal algorithm implementation is independent of specific business, and the same set of methods can be used to confirm various types of application software. Its most basic method is to establish a relationship between test cases and codes. Any type of software will have a two-way association between test cases and codes, so the application scope of precision testing is wide.
[0003] The main purpose of the industry's mobile precision testing practice is to support code coverage analysis for unit testing or manual testing, and to obtain single version coverage and incremental coverage after test execution through code instrumentation and coverage tools, so as to achieve the purpose of verifying the test execution effect. It can be seen that the accuracy of coverage data affects the accuracy of the bidirectional association between test cases and codes, and thus affects the accuracy of the recommended results of test cases during precision testing.
[0004] In the related art, the coverage data of different programming language projects handles the function or method names differently, especially for anonymous function names of a large number of applications, which are basically only handled in a basic way. For example, in RN type projects, the function names in the coverage data will be expressed as an anonymous suffix with a serial number, such as "anonymous_x", and the name of the same function in the coverage data may be different after each execution, and it cannot be associated with the code block. This can easily affect the accuracy of the two-way association between test cases and codes, and thus affect the accuracy of precision testing. Summary of the invention
[0005] The embodiments of the present invention provide a test case knowledge base construction method, device, electronic device and storage medium to solve the problem that the accuracy of the existing two-way association relationship between test cases and codes is poor, thereby affecting the accuracy of precise testing.
[0006] In order to solve the above-mentioned technical problems, the present invention is achieved as follows:
[0007] In a first aspect, an embodiment of the present invention provides a method for constructing a test case knowledge base, comprising:
[0008] For any test case, the test case is executed based on a target project, and code coverage data of the test case for the target project is generated, the target project is a project that has been instrumented, and the code coverage data at least includes an identity identifier of at least one node in the target project;
[0009] Parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base;
[0010] The identity of each node is obtained by renaming the node based on the node type of the node before generating the code coverage data.
[0011] Optionally, the step of parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base includes:
[0012] Taking each source file included in the target project as a unit, parsing the code coverage data, and obtaining the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0013] An association relationship between the target code module, the execution times of the target code module and the test case is established in the test case knowledge base to determine the priority of each test case according to the execution times of any code module under each test case associated with it.
[0014] Optionally, the step of parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base includes:
[0015] Taking each source file included in the target project as a unit, parsing the code coverage data, and obtaining the target node covered by the test case in each source file;
[0016] For any of the target nodes, obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node;
[0017] An association relationship between the target code module, the identification information of the code line and the test case is established in the test case knowledge base, so that when recommending test cases, when multiple recommended test cases are associated with the same code module and the code lines covered by each test case in the same code module are the same, the recommended test cases are deduplicated.
[0018] Optionally, the step of parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base includes:
[0019] Taking each source file included in the target project as a unit, parsing the code coverage data, and obtaining the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0020] For any of the target nodes, obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node;
[0021] An association relationship between the target code module, identification information of the code line, execution times of the target code module and the test case is established in the test case knowledge base.
[0022] Optionally, the step of obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node includes:
[0023] Obtaining the target code lines covered by the test case in the target code module corresponding to the target node, and filtering out non-logical information in the target code lines to obtain valid code lines; the non-logical information includes at least one of blank characters, curly brackets occupying a single line, and parentheses occupying a single line;
[0024] Summary information is generated based on the valid code line as identification information of the code line.
[0025] Optionally, after the step of parsing the code coverage data in units of each source file included in the target project and obtaining the target node covered by the test case in each source file, the method further includes:
[0026] For any of the target nodes, in response to the identity identifier of the target node containing a specified string, the specified string in the identity identifier is replaced with the file name of the source file where the target node is located, so as to obtain an updated identity identifier of the target node;
[0027] The specified character string includes at least one of export_default_class and export_default_function.
[0028] Optionally, after the step of parsing the code coverage data in units of each source file included in the target project and obtaining the target node covered by the test case in each source file, the method further includes:
[0029] For any of the target nodes, detecting whether the target node has the same identity as other target nodes in the source file where the target node is located;
[0030] In response to the target node and at least one other target node in the source file where the target node is located having the same identity, setting a different marker in the identity of the target node and each node in the at least one other target node to distinguish the target node and each node in the at least one other target node;
[0031] Alternatively, in response to the target node having the same identity as at least one other target node in the source file where the target node is located, merging the target nodes having the same identity into one target node;
[0032] The other target nodes are other nodes covered by any test case except the target node.
[0033] Optionally, before the step of generating the code coverage data of the test case for the target project, the method further includes:
[0034] For any node in the abstract syntax tree of the target project, in response to the node satisfying a specified condition, the node is renamed according to the node type of the node, so that when generating the code coverage data, the renamed node name is used as the identity of the node.
[0035] Optionally, when the target project is a React Native project, the step of renaming any node in the abstract syntax tree according to the node type of the node in response to the node satisfying a specified condition includes:
[0036] For any node in the abstract syntax tree, in response to a target attribute of the node being empty and a node type of the node being a class method of a general class, renaming the node with a combination of a class name and a method name of the class method;
[0037] In response to the target attribute of the node being empty, and the node type of the node being a class method of an exported default class, renaming the node with a combination of export_default_class and a method name;
[0038] In response to the target attribute of the node being empty and the node type of the node being an object method, renaming the node with a combination of an object name and a method name of the object method;
[0039] In response to the target attribute of the node being empty, and the node type of the node being any one of a function declaration, a function expression, and an arrow function expression, determining a renaming method for the node according to the node type of a parent node of the node and renaming the node;
[0040] In response to the target attribute of the node being not completely empty, determining a renaming method for the node according to a node type of a parent node of the node and renaming the node;
[0041] The target attribute includes at least one of the id.name attribute and the name attribute of the node.
[0042] Optionally, after the step of renaming any node in the abstract syntax tree according to the node type of the node in response to the node satisfying a specified condition, the method further includes:
[0043] For any renamed node, in response to the renamed node name containing a specified string, the specified string in the node name is replaced with the file name of the source file where the node is located, to obtain the final node name of the target node;
[0044] The specified character string includes at least one of export_default_class and export_default_function.
[0045] Optionally, the code coverage data includes at least an identity of at least one node in the target project that meets the specified condition.
[0046] Optionally, the target project includes at least one of a project built based on JavaScript / TypeScript, a project built based on a programming language supported by Android, and a project built based on a programming language supported by iOS, and the project built based on JavaScript includes at least a React Native project.
[0047] Optionally, the method further comprises:
[0048] Clear the code coverage data of the current test case, and for another test case, enter the step of executing the test case based on the target project and generating the code coverage data of the test case for the target project, until all test cases are executed.
[0049] In a second aspect, an embodiment of the present invention provides a test case knowledge base construction device, comprising:
[0050] A coverage data acquisition module is used to execute any test case based on a target project and generate code coverage data of the test case for the target project, wherein the target project is a project that has been instrumented, and the code coverage data at least includes an identity identifier of at least one node in the target project;
[0051] A coverage data parsing module, used to parse the code coverage data, obtain the target code module covered in the target project after the test case is executed, and establish an association relationship between the target code module and the test case in a test case knowledge base;
[0052] The identity of each node is obtained by renaming the node based on the node type of the node before generating the code coverage data.
[0053] Optionally, the coverage data parsing module includes:
[0054] A first coverage data parsing submodule is used to parse the code coverage data based on each source file included in the target project, and obtain the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0055] The first relationship establishment submodule is used to establish an association relationship between the target code module, the execution times of the target code module and the test case in the test case knowledge base, so as to determine the priority of each test case according to the execution times of any code module under each test case associated with it.
[0056] Optionally, the coverage data parsing module includes:
[0057] A second coverage data parsing submodule is used to parse the code coverage data based on each source file included in the target project, and obtain the target node covered by the test case in each source file;
[0058] A covered code line identification submodule is used to obtain, for any of the target nodes, identification information of the code lines covered by the test case in the target code module corresponding to the target node;
[0059] The second relationship establishment submodule is used to establish an association relationship between the target code module, the identification information of the code line and the test case in the test case knowledge base, so that when recommending test cases, when multiple recommended test cases are associated with the same code module and the code lines covered by each test case in the same code module are the same, the recommended test cases can be deduplicated.
[0060] Optionally, the coverage data parsing module includes:
[0061] A first coverage data parsing submodule is used to parse the code coverage data based on each source file included in the target project, and obtain the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0062] A covered code line identification submodule is used to obtain, for any of the target nodes, identification information of the code lines covered by the test case in the target code module corresponding to the target node;
[0063] The third relationship establishment submodule is used to establish an association relationship between the target code module, the identification information of the code line, the execution times of the target code module and the test case in the test case knowledge base.
[0064] Optionally, the covered code line identification submodule is specifically used to:
[0065] Obtaining the target code lines covered by the test case in the target code module corresponding to the target node, and filtering out non-logical information in the target code lines to obtain valid code lines; the non-logical information includes at least one of blank characters, curly brackets occupying a single line, and parentheses occupying a single line;
[0066] Summary information is generated based on the valid code line as identification information of the code line.
[0067] Optionally, the coverage data parsing module further includes:
[0068] A node identity updating submodule is used for, for any of the target nodes, in response to the identity of the target node containing a specified string, replacing the specified string in the identity with the file name of the source file where the target node is located, so as to obtain an updated identity of the target node;
[0069] The specified character string includes at least one of export_default_class and export_default_function.
[0070] Optionally, the coverage data parsing module further includes:
[0071] An identity filter submodule, used for detecting, for any of the target nodes, whether the target node has the same identity as other target nodes in the source file where the target node is located;
[0072] an identity processing submodule, for, in response to the target node and at least one other target node in the source file where the target node is located having the same identity, respectively setting a different marker in the identity of the target node and each node in the at least one other target node to distinguish the target node and each node in the at least one other target node; or, in response to the target node and at least one other target node in the source file where the target node is located having the same identity, merging the target nodes with the same identity into one target node;
[0073] The other target nodes are other nodes covered by any test case except the target node.
[0074] Optionally, the device further comprises:
[0075] A node renaming module is used to rename any node in the abstract syntax tree of the target project according to the node type of the node in response to the node satisfying a specified condition, so that the renamed node name is used as the identity of the node when generating the code coverage data.
[0076] Optionally, when the target project is a React Native project, the node renaming module is specifically used to:
[0077] For any node in the abstract syntax tree, in response to a target attribute of the node being empty and a node type of the node being a class method of a general class, renaming the node with a combination of a class name and a method name of the class method;
[0078] In response to the target attribute of the node being empty, and the node type of the node being a class method of an exported default class, renaming the node with a combination of export_default_class and a method name;
[0079] In response to the target attribute of the node being empty and the node type of the node being an object method, renaming the node with a combination of an object name and a method name of the object method;
[0080] In response to the target attribute of the node being empty, and the node type of the node being any one of a function declaration, a function expression, and an arrow function expression, determining a renaming method for the node according to the node type of a parent node of the node and renaming the node;
[0081] In response to the target attribute of the node being not completely empty, determining a renaming method for the node according to a node type of a parent node of the node and renaming the node;
[0082] The target attribute includes at least one of the id.name attribute and the name attribute of the node.
[0083] Optionally, the device further comprises:
[0084] A node name adjustment module is used for, for any renamed node, in response to the renamed node name containing a specified string, replacing the specified string in the node name with the file name of the source file where the node is located, so as to obtain the final node name of the target node;
[0085] The specified character string includes at least one of export_default_class and export_default_function.
[0086] Optionally, the code coverage data includes at least an identity of at least one node in the target project that meets the specified condition.
[0087] Optionally, the target project includes at least one of a project built based on JavaScript / TypeScript, a project built based on a programming language supported by Android, and a project built based on a programming language supported by iOS, and the project built based on JavaScript includes at least a React Native project.
[0088] Optionally, the device further comprises:
[0089] The code coverage data clearing module is used to clear the code coverage data of the current test case and return to the coverage data acquisition module for another test case until all test cases are executed.
[0090] In a third aspect, an embodiment of the present invention further provides an electronic device, comprising: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the steps of the test case knowledge base construction method as described in the first aspect are implemented.
[0091] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, characterized in that a computer program is stored on the computer-readable storage medium, and when the computer program is executed by a processor, the steps of the test case knowledge base construction method as described in the first aspect are implemented.
[0092] In an embodiment of the present invention, the target project is modularized, and the nodes corresponding to at least one code module are renamed so that the nodes are fixedly corresponding to the corresponding code modules. When the test case is executed, the test coverage data for the target project is collected and parsed in real time, so as to establish an association relationship between the test case and the code module and add it to the test case execution, thereby improving the accuracy of the association relationship between the test case and the code module, and making it possible to accurately recommend test cases based on the test case execution. BRIEF DESCRIPTION OF THE DRAWINGS
[0093] Figure 1 It is one of the step flow charts of the test case knowledge base construction method in the embodiment of the present invention;
[0094] Figure 2 This is the second step flow chart of the method for building a test case knowledge base in an embodiment of the present invention;
[0095] Figure 3 This is the third step flow chart of the method for building a test case knowledge base in an embodiment of the present invention;
[0096] Figure 4 This is the fourth step flow chart of the method for building a test case knowledge base in an embodiment of the present invention;
[0097] Figure 5A is an example of original code coverage data in an embodiment of the present invention;
[0098] Figure 5B is an example of code coverage data after renaming the nodes in an embodiment of the present invention;
[0099] Figure 6 It is a flow chart of creating a test case knowledge base for precise testing of a target project on a mobile testing terminal in an embodiment of the present invention.
[0100] Figure 7 This is an example of a specific method for renaming a node in an embodiment of the present invention.
[0101] Figure 8 It is one of the structural schematic diagrams of the test case knowledge base construction device in the embodiment of the present invention;
[0102] Fig. 9 This is a second structural diagram of the test case knowledge base construction device in an embodiment of the present invention;
[0103] Fig.10 This is a third structural diagram of the test case knowledge base construction device in an embodiment of the present invention;
[0104] Fig.11 This is the fourth structural diagram of the test case knowledge base construction device in the embodiment of the present invention. DETAILED DESCRIPTION
[0105] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0106] Reference Figure 1 , shows a step flow chart of a method for building a test case knowledge base in an embodiment of the present invention.
[0107] Step 110: for any test case, execute the test case based on a target project, and generate code coverage data of the test case for the target project, wherein the target project is a project that has been instrumented, and the code coverage data at least includes an identity identifier of at least one node in the target project;
[0108] Step 120, parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base;
[0109] The identity of each node is obtained by renaming the node based on the node type of the node before generating the code coverage data.
[0110] like Figure 5AThe following is an example of the original code coverage data of the RN project in the prior art. The function map (fnMap) records the function name, location, number of lines, and other information. The name part is expressed as an anonymous suffix with a serial number "anonymous_x". The name of the same function in the coverage data may be different after each execution, and it cannot be associated with the code block, thus affecting the accuracy of the association between the code module and the test case in the subsequent test case knowledge base.
[0111] Therefore, in an embodiment of the present invention, in order to avoid the above-mentioned problems, the source code of the target project can be modularized in advance, and the identity corresponding to each code module can be re-determined, so that each code block can be corresponded to the identity in the code coverage data, and the association between the identity and the code block can be realized, thereby realizing an accurate and stable association relationship between the test case and the code module in the test case knowledge base.
[0112] Specifically, in order to build the association between test cases and the code to be tested, the business code can generally be divided into appropriate code modules and marked by any available technical means, and then the association between the test cases and the corresponding one or more groups of code modules can be built by recording. In this way, when the code is changed, the association between the test cases and code modules stored in the test case knowledge base can be matched to recommend the test cases that need to be executed to the tester.
[0113] For example, the source code of the target project can be divided based on each node or a node of a specified type (such as a function type, a method type, etc.) contained in the abstract syntax tree of the target project to obtain the code modules corresponding to the corresponding nodes, and one node can correspond to one code module. Then, by renaming the corresponding node, the renamed node name is used as the identity of the code module corresponding to the corresponding node in the code coverage data.
[0114] In order to count the code coverage of the target project after each test case is executed, the target project code to be tested can be automatically instrumented, and then the code coverage data after executing a test case for the target project can be obtained through the coverage tool based on the probes inserted into the target project code by the automatic instrumentation tool. At this time, since the identity of each node in the obtained code coverage data is a renamed node name, the identity of each node can also be fixedly corresponding to the code module one by one, so that the code module covered by the current test case can be accurately determined.
[0115] Among them, the specific method of renaming the nodes can be customized according to the needs, and the embodiment of the present invention is not limited to this. In addition, in the embodiment of the present invention, the code of the target project can be modularized by any available method, and the abstract syntax tree of the target project can be generated. At this time, each node in the abstract syntax tree can correspond to a code module, and the embodiment of the present invention is not limited to this. Moreover, when dividing the code module, only the code module corresponding to the node included in the code coverage data can be divided from the source code of the target project, or only the nodes included in the code coverage can be renamed. Of course, the code modules corresponding to all nodes can also be divided according to the needs, or all nodes can be renamed, and the embodiment of the present invention is not limited to this.
[0116] Specifically, the renaming method of the corresponding node can be determined according to the node type of the node, and then the node is renamed according to the corresponding renaming method. The renaming methods of different node types can be customized according to requirements, which is not limited in the embodiment of the present invention.
[0117] For example, in practical applications, the code of the target project may include multiple source files, so modular processing can be performed for the code in each source file, and the code may include a variety of different code objects such as classes, methods, functions, expressions, etc., so the nodes in the abstract syntax tree may be any code object. Therefore, in an embodiment of the present invention, different renaming methods can be set based on the node type of the node, and the renaming method can be injected into the project in the form of code, such as being placed in a plug-in tool, so that when the plug-in tool is used for plugging, the node type of each node in the abstract syntax tree of the target project can be based on the node type, and the node name of each corresponding node can be renamed, and then the node name after each node is renamed in the code coverage data is used as the identity of each corresponding node. Moreover, in the case where the renaming method is relatively fixed, for the same target project, when executing multiple test cases, the node name of the corresponding node of each code module in the target project after modularization can be used all the time, that is, the identity of the same node in the coverage data after each execution is the same, so it can be associated with the code module one by one.
[0118] In the embodiment of the present invention, the code coverage data can be parsed in any available manner to obtain the target code module covered in the target project after the current test case is executed, which is not limited by the embodiment of the present invention.
[0119] like Figure 5BThe figure shows an example of code coverage data after renaming the nodes. As mentioned above, the fnMap records the name (that is, the renamed node name), location, number of lines, and other information of the function (that is, the node). In addition, the code coverage data also includes an f array, which records the number of executions of the code module corresponding to each node during this test. Then, the covered nodes can be determined according to the number of executions recorded in the f array, and then the code modules corresponding to each covered node can be obtained, which are the target code modules covered by this test case.
[0120] For example, if the number of executions of a code module corresponding to a node in this test is greater than or equal to 1, it indicates that the code module corresponding to the node is covered by the test case of this test.
[0121] Reference Figure 2 In the embodiment of the present invention, the step 120 may further include:
[0122] Step A121, parsing the code coverage data based on each source file included in the target project, and obtaining the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0123] Step A122, establishing an association relationship between the target code module, the execution times of the target code module and the test case in the test case knowledge base, so as to determine the priority of each test case according to the execution times of any code module under each test case associated with it.
[0124] The analysis of code coverage data mainly associates the function map data in the coverage data with the covered functions, i.e., nodes. Among them, since a project generally contains multiple source files, and modular processing is performed on the target project, it is generally processed in units of source files, and the code in each source file is divided and modularized. Therefore, when parsing the code coverage data, the code coverage data can also be parsed in units of source files. Of course, other methods can also be used to parse the code coverage data according to needs, and this embodiment of the present invention is not limited to this.
[0125] Among them, the screening of covered nodes is mainly obtained by traversing the information of fnMap and f array in the code coverage data. In addition, by obtaining the specific execution times of the code module corresponding to the node from the code coverage data and binding it in the test case knowledge base, when recommending test cases later, it is possible not only to recommend related test cases based on code changes, but also to mark the recommended execution priority of each corresponding test case according to the execution times of the code module to be tested in each associated test case (for example, it can be set that the higher the execution times of the code module, the higher the priority, and the higher the execution priority and / or recommendation priority of the corresponding test case).
[0126] If the number of executions corresponding to a node identity in the code coverage data is greater than or equal to 1, it can be considered that the code module corresponding to the node is covered by the current test case.
[0127] At this time, in the test case knowledge base, while recording the association relationship between the test case and the code module, the execution times of the corresponding code module under the corresponding test case conditions can also be recorded, that is, an association relationship between the test case, the code module, and the execution times of the corresponding code module under the corresponding test case conditions is established, so as to facilitate obtaining the execution times of any code module when tested under any test case associated with it in the test case knowledge base.
[0128] It should be noted that in an embodiment of the present invention, in the test case knowledge base, the node name of each code model corresponding node after being renamed can be used as its identity to establish an association relationship with the identity of the test case, or the identity of each code module can be restored to the original node name of its corresponding node before renaming in the test case knowledge base based on the correspondence between the node names before and after renaming obtained during renaming. This embodiment of the present invention is not limited to this.
[0129] Reference Figure 3 In the embodiment of the present invention, the step 120 may further include:
[0130] Step B121, parsing the code coverage data based on each source file included in the target project, and obtaining the target node covered by the test case in each source file;
[0131] Step B122: for any of the target nodes, obtaining identification information of the code lines covered by the test case in the target code module corresponding to the target node;
[0132] Step B123, establishing an association relationship between the target code module, the identification information of the code line and the test case in the test case knowledge base, so that when recommending test cases, when multiple recommended test cases are associated with the same code module and the code lines covered by each test case in the same code module are the same, the recommended test cases are deduplicated.
[0133] In actual applications, the same code module generally contains multiple lines of code. For test cases, when using them to test the target project, they may cover multiple code modules in the target project's code, but the specific lines of code covered in each code module may be the same or different.
[0134] For a code module with a certain code change, when recommending test cases for it, when multiple recommended test cases are associated with the same code module and the specific code lines covered by each test case in the code module are the same, it is easy to cause the test case finally selected to be a test case for the same code line with a high probability. This will cause the same code line in the code module to be repeatedly tested multiple times through multiple test cases, while other code lines are not tested, which will also affect the comprehensiveness of the code test results.
[0135] Therefore, in an embodiment of the present invention, in order to avoid the above-mentioned problems, for any target node covered by the current test case, the identification information of the specific covered code lines in the corresponding code module (such as the position of the code lines in the code module, the number of code lines, the content of the code lines, etc.) can be read and added to the test case knowledge base.
[0136] Specifically, the target code module, the identification information of the code line, and the association relationship between the test cases can be established in the test case knowledge base, so that the identification information of the code line specifically covered by any test case in any code module associated with it can be obtained in the test case knowledge base. This is to facilitate additional judgments when recommending test cases in the future. When multiple recommended test cases are associated with the same code module, and the specific code lines covered by each test case in the same code module are the same, a part of the recommended test cases can be reasonably filtered out, so as to achieve the purpose of streamlining the recommendation results and improving the test efficiency at the same time.
[0137] Reference Figure 4 In the embodiment of the present invention, the step 120 may further include:
[0138] Step C121, parsing the code coverage data based on each source file included in the target project, and obtaining the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0139] Step C122, for any of the target nodes, obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node;
[0140] Step C123, establishing an association relationship between the target code module, the identification information of the code line, the execution times of the target code module and the test case in the test case knowledge base.
[0141] In an embodiment of the present invention, the number of executions of the target code module corresponding to each target node associated with any test case during the execution of the test case, and the identification information of the code lines covered by the test case associated with the code module corresponding to any node can also be recorded in the test case knowledge base at the same time. For details, please refer to A121-122 and B121-123 above, which will not be described in detail here.
[0142] Optionally, in the embodiment of the present invention, the process of obtaining identification information of the code line covered by the test case in the target code module corresponding to the target node may specifically include:
[0143] Step S1, obtaining the target code lines covered by the test case in the target code module corresponding to the target node, and filtering out the non-logical information in the target code lines to obtain valid code lines; the non-logical information includes at least one of blank characters, curly brackets occupying a single line, and parentheses occupying a single line;
[0144] Step S2: Generate summary information based on the valid code line as identification information of the code line.
[0145] In actual applications, code modules generally contain blank characters, parentheses that occupy a single line, and other special symbols (such as "(", ")", "{", "}", "[", "]", " / / ", etc.). The above content is non-logical information. When obtaining the code lines specifically covered by the test case, the above non-logical information has little effect and no substantial logical meaning. Therefore, it can be ignored. If the code lines covered by the same test case in two code modules only deviate from the above non-logical information, it can be considered to cover the same code lines.
[0146] Therefore, in an embodiment of the present invention, in order to improve the accuracy of specific coverage code lines, the target code lines covered by the current test case in the target code module corresponding to the target node can be obtained, and non-logical information in the target code lines can be filtered out to obtain the final valid code lines.
[0147] In order to facilitate recording and reduce the amount of data in the test case knowledge base, the information of valid code lines can be further processed. For example, summary information (such as MD5 summary) can be generated based on the information of the valid code lines as identification information of the final covered code lines and recorded in the test case knowledge base.
[0148] Of course, in the embodiment of the present invention, the identification information of the code lines covered by the test cases in the code module may also be generated by any other available method, which is not limited by the embodiment of the present invention.
[0149] Optionally, in the embodiment of the present invention, after step A121 / B121 / C121, the following steps may also be included:
[0150] Step 124, for any of the target nodes, in response to the identity identifier of the target node containing a specified string, replacing the specified string in the identity identifier with the file name of the source file where the target node is located, to obtain an updated identity identifier of the target node;
[0151] The specified character string includes at least one of export_default_class and export_default_function.
[0152] When the name of the renamed node, that is, the identity identifier contains specified strings such as export_default_class or export_default_function, in order to avoid conflicts between the node and methods with the same name in other source files and confuse the binding results in the test case knowledge base, the source code file name (that is, the file name of the source file where the node is located) can be used to replace the specified string (such as the above-mentioned export_default_class, export_default_function) part in the corresponding identity identifier, so as to obtain the updated identity identifier as the final identity identifier of the corresponding node, that is, the node name, thereby achieving compatibility with these two syntaxes.
[0153] At this time, when establishing the mapping relationship between the test case and the code module in the test case knowledge base, if the mapping relationship between the identity identifier of the node corresponding to the code module and the test case identifier is established, the updated identity identifier of the node can be used as the identity identifier of the code module corresponding to the corresponding node in the test case knowledge base.
[0154] Optionally, in the embodiment of the present invention, after step A121 / B121 / C121, the following steps may also be included:
[0155] Step 125, for any of the target nodes, detecting whether the target node has the same identity as other target nodes in the source file where the target node is located;
[0156] Step 126, in response to the target node and at least one other target node in the source file where the target node is located having the same identity, respectively set a different marker in the identity of the target node and each node in the at least one other target node to distinguish the target node and each node in the at least one other target node; or, in response to the target node and at least one other target node in the source file where the target node is located having the same identity, merge the target nodes with the same identity into one target node. The other target nodes are other nodes covered by any test case except the target node.
[0157] In addition, since the identity identifiers (i.e., node names) of certain nodes in the abstract syntax tree are traced back to their parent nodes during the renaming process, certain duplicate names may occur. In order to avoid causing exceptions, these node names need to be deduplicated.
[0158] At this time, one way to deduplicate processing is to merge multiple target nodes with the same identity and covered by the same or different test cases into one target node, and retain the identity as the identity of the merged target node, and at the same time merge the corresponding code modules of each target node into one code module corresponding to the corresponding merged target node, and the test cases associated with the code modules corresponding to each target node before the merger can be associated with the merged code module; another way to deduplicate processing is to set a different marker (for example, different numbers, letters, etc.) in the identity of multiple nodes with the same identity, so as to adjust the identity of each node to a different identity. At this time, there is no need to adjust the association between the test case and the code module corresponding to each node.
[0159] It should be noted that the deduplication processing operation of the above steps 125-126 can be performed only on each target node belonging to the same source file, or can be performed on the target nodes in the global scope of the entire target project, which is not limited in this embodiment of the present invention.
[0160] Moreover, the above-mentioned deduplication processing method 2 can also set a different marker for multiple identical node names obtained by initial renaming when renaming each node in the abstract syntax tree of the target project, thereby ensuring that the identity identifier of each corresponding node in the subsequent code coverage will not be duplicated, which is not limited to this embodiment of the present invention.
[0161] Reference Figure 2 In the embodiment of the present invention, before step 110, the following steps may also be included:
[0162] Step 210, for any node in the abstract syntax tree, in response to the node satisfying a specified condition, rename the node according to the node type of the node, so that when generating code coverage data, the renamed node name is used as the identity of the node.
[0163] Optionally, the code coverage data includes at least an identity of at least one node in the target project that meets the specified condition.
[0164] The specified conditions can be customized according to the requirements, and the embodiments of the present invention do not limit this. For example, in practical applications, for nodes where both the id.name attribute and the name attribute are empty, it is easy to use the above-mentioned anonymous suffix and a serial number expression to name them, which is easy to affect the accuracy of the association relationship. Therefore, the nodes where both the id.name attribute and the name attribute are empty can be renamed. In this case, the specified conditions can be set to include that both the id.name attribute and the name attribute are empty.
[0165] In addition, when renaming nodes, only some nodes with a larger proportion can be considered. In this case, a node type set can be preset, which contains at least one specified node type that needs to be renamed, and then the specified conditions can be set, including the node type being any specified node type in the node type set, and so on.
[0166] Alternatively, the above two situations may be combined, for example, setting the specified condition to be that both the id.name attribute and the name attribute are empty, and the node type is any specified node type in the node type set, etc., which is not limited in this embodiment of the present invention.
[0167] The specified node type can be customized according to the requirements, which is not limited in the embodiment of the present invention. For example, the specified node type can be set to include one or more of class method, object method, function declaration, function expression, arrow function expression, nested function, method call expression, and assignment expression.
[0168] Moreover, the code coverage data may include the identities of all nodes that meet the above-mentioned specified conditions, or may include the identities of some nodes that meet the above-mentioned specified conditions, such as the identities of one or more or all nodes under the above-mentioned partially specified node types, or the identities of some nodes under all specified node types, and so on.
[0169] Optionally, in the embodiment of the present invention, the following may also be included:
[0170] Step 130, clear the code coverage data of the current test case, and return to step 110 for another test case until all test cases are executed.
[0171] In an embodiment of the present invention, in order to obtain code coverage data, the code of the target project to be tested can be automatically instrumented, and the source code in source files of types such as .js (javascript), .ts (typescript), .tsx, etc. in the target project can be modularized according to certain rules and each code module is given a unique name for associating test cases when subsequently constructing a test case knowledge base.
[0172] In order to collect code coverage data, you can also deploy and start the JS coverage collection service, whose main function is to receive and parse the uploaded code coverage data in real time and generate a test case knowledge base. For example, you can inject the real-time coverage upload function module into the target project code and start the target project.
[0173] Enter the target project test entry page on a mobile device or other electronic device to execute specific test cases. After each test case is executed, the test case knowledge base generation function is triggered. This function will parse the code coverage data uploaded to the coverage collection service and generate the code modules covered after the test case is executed. This process is called knowledge base recording.
[0174] When the knowledge base recording of a test case is completed, the code coverage data of the JS coverage collection service can be automatically cleared to prepare for the recording of the next test case. The above-mentioned knowledge base recording process is performed once for each test case, and the final test case knowledge base can be obtained, which contains the association relationship between each test case and each code module in the target project. Among them, the final test case knowledge base can be output in any available format such as Json according to the agreed test case knowledge base structure. The test case knowledge base structure can be customized according to needs, and this embodiment of the present invention is not limited to this.
[0175] like Figure 6 The figure shows a flow chart of creating a test case knowledge base for precise testing of a target project on a mobile testing terminal.
[0176] Among them, the JavaScript and TypeScript source codes in the target project can be parsed by any available method such as Babel tools with reference to the abstract syntax tree, and the code modules are divided mainly based on functions and taking into account information such as statements and classes to ensure the uniqueness of the names of each module. The division and naming of code modules are determined by the node type of the node corresponding to the code module parsed by Babel in the abstract syntax tree.
[0177] Optionally, in an embodiment of the present invention, the target project may include but is not limited to at least one of a project built based on JavaScript / TypeScript, a project built based on a programming language supported by Android (such as Java / Kotlin), and a project built based on a programming language supported by iOS (such as Object C / Swift). The project built based on JavaScript / TypeScript includes at least a React Native project, i.e., an RN project.
[0178] Among them, for projects built in different programming languages, the requirements of the programming languages can be referred to, and the nodes corresponding to each code module of the project source code can be renamed at different times. Moreover, under different programming languages, the conditions that need to be met by the nodes to be renamed may also be different accordingly. This can be customized according to specific needs, and the embodiments of the present invention are not limited to this.
[0179] For example, for projects created with programming languages supported by Android, since it provides interfaces (such as ClassCoverage, MethodCoverage, etc.) for analyzing and reading parameters related to code coverage, the code coverage file can be parsed by calling the corresponding interface to obtain the coverage result. In addition, there are relatively few anonymous nodes in the abstract syntax tree of the source code of the project created with programming languages supported by Android, so the relevant methods of renaming can be omitted from the instrumentation tool, that is, there is no need to transform the instrumentation tool. Instead, individual anonymous classes, inner classes, etc. can be processed later (for example, inner classes are converted into corresponding outer classes, etc.).
[0180] For RN projects, where anonymous classes are more prominent, the instrumentation tool can be modified by injecting a specific renaming method into it, thereby renaming the anonymous nodes in the abstract syntax tree of the RN project source code during the instrumentation process.
[0181] Optionally, in the embodiment of the present invention, when the target project is a RN project, step 210 may further include:
[0182] Step 211, for any node in the abstract syntax tree, in response to the target attribute of the node being empty and the node type of the node being a class method of a general class, renaming the node with a combination of the class name and method name of the class method;
[0183] Step 212, in response to the target attribute of the node being empty, and the node type of the node being a class method of an exported default class, renaming the node with a combination of export_default_class and a method name;
[0184] Step 213, in response to the target attribute of the node being empty and the node type of the node being an object method, renaming the node with a combination of the object name and the method name of the object method;
[0185] Step 214, in response to the target attribute of the node being empty, and the node type of the node being any one of a function declaration, a function expression, and an arrow function expression, determining a renaming method for the node according to the node type of the parent node of the node and renaming the node;
[0186] Step 215, in response to the target attribute of the node being not completely empty, determining a renaming method for the node according to the node type of the parent node of the node and renaming the node;
[0187] The target attribute includes at least one of the id.name attribute and the name attribute of the node.
[0188] In an embodiment of the present invention, when renaming nodes, we can mainly consider application scenarios where anonymous nodes are likely to appear, such as class methods (ClassMethod), object methods (ObjectMethod), function declarations (FunctionDeclaration), function expressions (FunctionExpression), arrow function expressions (ArrowFunctionExpression), nested function scenarios, method call expressions (CallExpression), and assignment expressions (AssignmentExpression).
[0189] In particular, if all the name-related attributes (i.e., the target attributes) of a node are empty, then the node is more likely to be named with an anonymous suffix and a serial number. Therefore, the corresponding nodes can be renamed based on the node type to avoid anonymous node names. Specifically, different renaming methods can be considered for different node types.
[0190] In addition, for nodes whose target attributes are not completely empty, although they can obtain a real name, since there may be nodes with the same target attribute value in different source files in the same project, if the node name is determined directly based on the target attribute, it is easy for nodes in different source files to have the same name, which will lead to abnormal situations in which the association relationship is confused when the test case knowledge base is subsequently constructed. Therefore, nodes whose target attributes are not completely empty can also be renamed.
[0191] like Figure 7 The figure shows an example of a specific method for preferably renaming nodes, wherein the function nodes may include class methods, arrow function expressions, object methods, function declarations, function expressions, etc., as shown in the figure.
[0192] Among them, assuming that the id.name or name attribute of a node is empty, and the node is an arrow function expression (ArrowFunctionExpression), the type of its parent node can be further determined. If the parent node of the node is a function call expression (CallExpression), the calling form of the parent node can be further determined. If the caller type is identifier, the caller variable name can be directly used as the node name after the node is renamed; if it is a chain call, the caller variable name can be spliced. Specifically, the caller variable name can be pushed into the stack in a loop until the caller type is identifier or the caller type is NewExpression, then the stacking stops.
[0193] Assuming that the id.name or name attribute of another node is not empty, and the parent node of the node is a class method (ClassMethod), it can be further determined whether the parent node is a general class or an exported default class (ExportDefaultClass). If it is a general class, the combination of the class name and the parent node method name can be directly used as the node name after the corresponding node is renamed.
[0194] Assuming that the id.name or name attribute of a node is empty, and the node is a function expression (FunctionExpression), its parent node type can be further determined. If its parent node is an expression statement (ExpressionStatement), and specifically a function call expression (CallExpression), the outer parent node function name of the expression, that is, the outer function name of the expression of the parent node can be used as the node name of the corresponding node after it is renamed; and if its parent node is an expression statement, and specifically an assignment expression (AssignmentExpression), the parent node's assignment object name, that is, the object name of the object assigned to the parent node can be directly used as the node name of the corresponding node after it is renamed; and so on.
[0195] also, Figure 7 The renaming methods in other cases shown in can refer to the above logic, based on Figure 7 The process branches shown are determined separately and will not be described in detail here.
[0196] Optionally, in the embodiment of the present invention, after step 210, the following steps may be further included:
[0197] Step 220, for any renamed node, in response to the node name after the node is renamed containing a specified string, replace the specified string in the node name with the file name of the source file where the node is located, to obtain the final node name of the target node;
[0198] The specified character string includes at least one of export_default_class and export_default_function.
[0199] That is, after each node is initially renamed, the renamed node name needs to be further processed through the above step 220 so that the final node name does not contain the specified character string.
[0200] In the embodiment of the present invention, when renaming a node, the renamed node name may be processed for compatibility with special JavaScript / TypeScript syntax such as export default class and export default function. The specific compatibility processing method can refer to the above step 124, which will not be described in detail here.
[0201] In an embodiment of the present invention, the target project is modularized, and the nodes corresponding to at least one code module are renamed so that the nodes are fixedly corresponding to the corresponding code modules. When the test case is executed, the test coverage data for the target project is collected and parsed in real time, so as to establish an association relationship between the test case and the code module and add it to the test case execution, thereby improving the accuracy of the association relationship between the test case and the code module, and making it possible to accurately recommend test cases based on the test case execution.
[0202] Moreover, in an embodiment of the present invention, a customized processing idea for renaming nodes according to node types is disclosed, and further deduplication and special syntax compatibility processing can be performed on the renamed node names to improve the uniqueness of the renamed node names, so that the node names can correspond one-to-one with the code modules, thereby further improving the accuracy of the association relationship between test cases and code modules.
[0203] Reference Figure 8 , shows a schematic diagram of the structure of a test case knowledge base construction device in an embodiment of the present invention.
[0204] The test case knowledge base construction device of the embodiment of the present invention includes: a coverage data acquisition module 310 and a coverage data parsing module 320 .
[0205] The functions of each module and the interactions between them are introduced in detail below.
[0206] A coverage data acquisition module 310 is used to execute any test case based on a target project and generate code coverage data of the test case for the target project, wherein the target project is a project that has been instrumented, and the code coverage data at least includes an identity identifier of at least one node in the target project;
[0207] A coverage data parsing module 320 is used to parse the code coverage data, obtain the target code module covered in the target project after the test case is executed, and establish an association relationship between the target code module and the test case in a test case knowledge base;
[0208] The identity of each node is obtained by renaming the node based on the node type of the node before generating the code coverage data.
[0209] Reference Fig. 9 In the embodiment of the present invention, the coverage data parsing module 320 may include:
[0210] The first coverage data parsing submodule 321 is used to parse the code coverage data based on each source file included in the target project, and obtain the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0211] The first relationship establishing submodule 322 is used to establish an association relationship between the target code module, the execution times of the target code module and the test case in the test case knowledge base, so as to determine the priority of each test case according to the execution times of any code module under each test case associated with it.
[0212] Reference Fig.10 In the embodiment of the present invention, the coverage data parsing module 320 may include:
[0213] A second coverage data parsing submodule 323 is used to parse the code coverage data based on each source file included in the target project, and obtain the target node covered by the test case in each source file;
[0214] A covered code line identification submodule 324 is used to obtain, for any of the target nodes, identification information of the code lines covered by the test case in the target code module corresponding to the target node;
[0215] The second relationship establishment submodule 325 is used to establish an association relationship between the target code module, the identification information of the code line and the test case in the test case knowledge base, so that when recommending test cases, when multiple recommended test cases are associated with the same code module and the code lines covered by each test case in the same code module are the same, the recommended test cases can be deduplicated.
[0216] Reference Fig.11 In the embodiment of the present invention, the coverage data parsing module 320 may include:
[0217] A first coverage data parsing submodule 326 is used to parse the code coverage data based on each source file included in the target project, and obtain the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case;
[0218] A covered code line identification submodule 327 is used to obtain, for any of the target nodes, identification information of the code lines covered by the test case in the target code module corresponding to the target node;
[0219] The third relationship establishment submodule 328 is used to establish an association relationship between the target code module, the identification information of the code line, the execution times of the target code module and the test case in the test case knowledge base.
[0220] Optionally, in the embodiment of the present invention, the covered code line identification submodule may be specifically used for:
[0221] Obtaining the target code lines covered by the test case in the target code module corresponding to the target node, and filtering out non-logical information in the target code lines to obtain valid code lines; the non-logical information includes at least one of blank characters, curly brackets occupying a single line, and parentheses occupying a single line;
[0222] Summary information is generated based on the valid code line as identification information of the code line.
[0223] Optionally, in the embodiment of the present invention, the coverage data parsing module may further include:
[0224] A node identity updating submodule is used for, for any of the target nodes, in response to the identity of the target node containing a specified string, replacing the specified string in the identity with the file name of the source file where the target node is located, so as to obtain an updated identity of the target node;
[0225] The specified character string includes at least one of export_default_class and export_default_function.
[0226] Optionally, in the embodiment of the present invention, the coverage data parsing module may further include:
[0227] An identity filter submodule, used for detecting, for any of the target nodes, whether the target node has the same identity as other target nodes in the source file where the target node is located;
[0228] An identity processing submodule is used to set a different identifier in the identity of the target node and each node in the at least one other target node in response to the target node having the same identity as at least one other target node in the source file where the target node is located, so as to distinguish the target node and each node in the at least one other target node; or, in response to the target node having the same identity as at least one other target node in the source file where the target node is located, merge the target nodes with the same identity into one target node; the other target nodes are other nodes covered by any test case except the target node.
[0229] Reference Figure 8 , the device may further include:
[0230] The node renaming module 410 is used to rename any node in the abstract syntax tree of the target project according to the node type of the node in response to the node satisfying a specified condition, so that the renamed node name is used as the identity of the node when generating the code coverage data.
[0231] Optionally, when the target project is a React Native project, the node renaming module 410 may be specifically used to:
[0232] For any node in the abstract syntax tree, in response to a target attribute of the node being empty and a node type of the node being a class method of a general class, renaming the node with a combination of a class name and a method name of the class method;
[0233] In response to the target attribute of the node being empty, and the node type of the node being a class method of an exported default class, renaming the node with a combination of export_default_class and a method name;
[0234] In response to the target attribute of the node being empty and the node type of the node being an object method, renaming the node with a combination of an object name and a method name of the object method;
[0235] In response to the target attribute of the node being empty, and the node type of the node being any one of a function declaration, a function expression, and an arrow function expression, determining a renaming method for the node according to the node type of a parent node of the node and renaming the node;
[0236] In response to the target attribute of the node being not completely empty, determining a renaming method for the node according to a node type of a parent node of the node and renaming the node;
[0237] The target attribute includes at least one of the id.name attribute and the name attribute of the node.
[0238] Optionally, the device may further include:
[0239] A node name adjustment module is used for, for any renamed node, in response to the renamed node name containing a specified string, replacing the specified string in the node name with the file name of the source file where the node is located, so as to obtain the final node name of the target node;
[0240] The specified character string includes at least one of export_default_class and export_default_function.
[0241] Optionally, the code coverage data includes at least an identity of at least one node in the target project that meets the specified condition.
[0242] Optionally, the target project includes at least one of a project built based on JavaScript / TypeScript, a project built based on a programming language supported by Android, and a project built based on a programming language supported by iOS, and the project built based on JavaScript includes at least a React Native project.
[0243] Optionally, the device may further include:
[0244] The code coverage data clearing module is used to clear the code coverage data of the current test case and return to the coverage data acquisition module for another test case until all test cases are executed.
[0245] The test case knowledge base construction device provided by the embodiment of the present invention can realize Figures 1 to 7 To avoid repetition, the various processes implemented in the method embodiment are not described here.
[0246] Preferably, an embodiment of the present invention also provides an electronic device, comprising: a processor, a memory, and a computer program stored in the memory and executable on the processor. When the computer program is executed by the processor, the various processes of the above-mentioned test case knowledge base construction method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be described here.
[0247] The embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, each process of the test case knowledge base construction method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0248] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0249] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present invention.
[0250] The embodiments of the present invention are described above in conjunction with the accompanying drawings, but the present invention is not limited to the above-mentioned specific implementation modes, which are merely illustrative rather than restrictive. Under the guidance of the present invention, ordinary technicians in this field can also make many forms without departing from the scope of protection of the present invention and the claims, all of which are within the protection of the present invention.
[0251] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed in the embodiments of the present invention can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0252] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0253] In the embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0254] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0255] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0256] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, mobile hard disks, ROM, RAM, magnetic disks, or optical disks.
[0257] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A method for constructing a test case knowledge base, characterized in that: include: For any test case, the test case is executed based on a target project, and code coverage data of the test case for the target project is generated, the target project is a project that has been instrumented, and the code coverage data at least includes an identity identifier of at least one node in the target project; Parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base; Before the step of generating the code coverage data of the test case for the target project, the method further includes: For any node in the abstract syntax tree of the target project, in response to the node satisfying a specified condition, renaming the node according to the node type of the node, so that when generating the code coverage data, the renamed node name is used as the identity of the node; In the case where the target project is a React Native project, the step of renaming any node in the abstract syntax tree according to the node type of the node in response to the node satisfying a specified condition includes: For any node in the abstract syntax tree, in response to a target attribute of the node being empty and a node type of the node being a class method of a general class, renaming the node with a combination of a class name and a method name of the class method; In response to the target attribute of the node being empty, and the node type of the node being a class method of an exported default class, renaming the node with a combination of export_default_class and a method name; In response to the target attribute of the node being empty and the node type of the node being an object method, renaming the node with a combination of an object name and a method name of the object method; In response to the target attribute of the node being empty, and the node type of the node being any one of a function declaration, a function expression, and an arrow function expression, determining a renaming method for the node according to the node type of a parent node of the node and renaming the node; In response to the target attribute of the node being not completely empty, determining a renaming method for the node according to a node type of a parent node of the node and renaming the node; The target attribute includes at least one of the id.name attribute and the name attribute of the node.
2. The method according to claim 1, characterized in that The step of parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base includes: Taking each source file included in the target project as a unit, parsing the code coverage data, and obtaining the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case; An association relationship between the target code module, the execution times of the target code module and the test case is established in the test case knowledge base to determine the priority of each test case according to the execution times of any code module under each test case associated with it.
3. The method according to claim 1, characterized in that The step of parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base includes: Taking each source file included in the target project as a unit, parsing the code coverage data, and obtaining the target node covered by the test case in each source file; For any of the target nodes, obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node; An association relationship between the target code module, the identification information of the code line and the test case is established in the test case knowledge base, so that when recommending test cases, when multiple recommended test cases are associated with the same code module and the code lines covered by each test case in the same code module are the same, the recommended test cases are deduplicated.
4. The method according to claim 1, characterized in that: The step of parsing the code coverage data, obtaining the target code module covered in the target project after the test case is executed, and establishing an association relationship between the target code module and the test case in a test case knowledge base includes: Taking each source file included in the target project as a unit, parsing the code coverage data, and obtaining the target nodes covered by the test case in each source file, and the number of executions of the target code module corresponding to each target node during the execution of the test case; For any of the target nodes, obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node; An association relationship between the target code module, identification information of the code line, execution times of the target code module and the test case is established in the test case knowledge base.
5. The method according to claim 3 or 4, characterized in that: The step of obtaining identification information of code lines covered by the test case in the target code module corresponding to the target node includes: Obtaining the target code lines covered by the test case in the target code module corresponding to the target node, and filtering out non-logical information in the target code lines to obtain valid code lines; the non-logical information includes at least one of blank characters, curly brackets occupying a single line, and parentheses occupying a single line; Summary information is generated based on the valid code line as identification information of the code line.
6. The method according to any one of claims 2 to 4, characterized in that: After the step of parsing the code coverage data in units of each source file included in the target project and obtaining the target node covered by the test case in each source file, the method further includes: For any of the target nodes, in response to the identity identifier of the target node containing a specified string, the specified string in the identity identifier is replaced with the file name of the source file where the target node is located, so as to obtain an updated identity identifier of the target node; The specified character string includes at least one of export_default_class and export_default_function.
7. The method according to any one of claims 2 to 4, characterized in that: After the step of parsing the code coverage data in units of each source file included in the target project and obtaining the target node covered by the test case in each source file, the method further includes: For any of the target nodes, detecting whether the target node has the same identity as other target nodes in the source file where the target node is located; In response to the target node and at least one other target node in the source file where the target node is located having the same identity, setting a different marker in the identity of the target node and each node in the at least one other target node to distinguish the target node and each node in the at least one other target node; Alternatively, in response to the target node having the same identity as at least one other target node in the source file where the target node is located, merging the target nodes having the same identity into one target node; The other target nodes are other nodes covered by any test case except the target node.
8. The method according to claim 1, characterized in that: After the step of renaming any node in the abstract syntax tree according to the node type of the node in response to the node satisfying a specified condition, the step further includes: For any renamed node, in response to the renamed node name containing a specified string, the specified string in the node name is replaced with the file name of the source file where the node is located, to obtain the final node name of the target node; The specified character string includes at least one of export_default_class and export_default_function.
9. The method according to claim 1, characterized in that: The code coverage data at least includes the identity of at least one node in the target project that meets the specified condition.
10. The method according to any one of claims 1 to 4, characterized in that: The target project includes at least one of a project built based on JavaScript / TypeScript, a project built based on a programming language supported by Android, and a project built based on a programming language supported by iOS, and the project built based on JavaScript includes at least a ReactNative project.
11. The method according to claim 1, characterized in that: Also includes: Clear the code coverage data of the current test case, and for another test case, enter the step of executing the test case based on the target project and generating the code coverage data of the test case for the target project, until all test cases are executed.
12. A test case knowledge base construction device, characterized in that: include: A coverage data acquisition module is used to execute any test case based on a target project and generate code coverage data of the test case for the target project, wherein the target project is a project that has been instrumented, and the code coverage data at least includes an identity identifier of at least one node in the target project; A coverage data parsing module, used to parse the code coverage data, obtain the target code module covered in the target project after the test case is executed, and establish an association relationship between the target code module and the test case in a test case knowledge base; The device further includes: a node renaming module, for renaming any node in the abstract syntax tree of the target project according to the node type of the node in response to the node satisfying a specified condition, so that when generating the code coverage data, the renamed node name is used as the identity of the node; When the target project is a React Native project, the node rename module is specifically used to: For any node in the abstract syntax tree, in response to a target attribute of the node being empty and a node type of the node being a class method of a general class, renaming the node with a combination of a class name and a method name of the class method; In response to the target attribute of the node being empty, and the node type of the node being a class method of an exported default class, renaming the node with a combination of export_default_class and a method name; In response to the target attribute of the node being empty and the node type of the node being an object method, renaming the node with a combination of an object name and a method name of the object method; In response to the target attribute of the node being empty, and the node type of the node being any one of a function declaration, a function expression, and an arrow function expression, determining a renaming method for the node according to the node type of a parent node of the node and renaming the node; In response to the target attribute of the node being not completely empty, determining a renaming method for the node according to a node type of a parent node of the node and renaming the node; The target attribute includes at least one of the id.name attribute and the name attribute of the node.
13. An electronic device, characterized in that: include: A processor, a memory, and a computer program stored in the memory and executable on the processor, wherein when the computer program is executed by the processor, the steps of the test case knowledge base construction method according to any one of claims 1 to 11 are implemented.
14. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the test case knowledge base construction method according to any one of claims 1 to 11 are implemented.
