Code testing method and device

By dividing unit test cases in code testing and conducting independent use case testing, the problem of full testing in the existing technology after code changes is solved, and a more efficient code testing process is achieved.

CN114116505BActive Publication Date: 2025-02-28BEIJING DAJIA INTERNET INFORMATION TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202111457312.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-02
Publication Date
2025-02-28
Estimated Expiration
2041-12-02

AI Technical Summary

Technical Problem

Existing code testing methods require full unit test cases to be executed after the code changes, resulting in a long test time and even if the code is modified small, full tests are required.

Method used

By obtaining code test information, dividing it into several unit test cases, and determining whether the code used for testing has been modified. If there is any modification, the relevant unit test cases will be used for independent use case testing. If the test passes, no full code test will be performed.

Benefits of technology

It effectively reduces the number of unit test cases execution, improves the efficiency of code testing, and only tests the modified code parts, avoiding unnecessary full testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114116505B_ABST
    Figure CN114116505B_ABST
Patent Text Reader

Abstract

The present disclosure provides a code testing method and device. The code testing method includes: obtaining code testing information, and dividing the code testing information into a number of unit test cases; determining whether the code used for testing the several unit test cases has been modified; if the code used for testing at least one unit test case among the several unit test cases has been modified, using the at least one unit test case to perform an independent case test; if the independent case test of the at least one unit test case passes, determining not to perform code testing.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure generally relates to the field of electronic technology, and more specifically, to a code testing method and device. Background Art

[0002] After each code change (for example, when a developer modifies the code), the modified code needs to be tested with all unit test cases. If all unit test cases are executed successfully, it means that there is no problem with the modified code. If a unit test case fails to execute, it means that there is a problem with the modified code. This code testing method can be used to determine whether there is a problem with the modified code. However, since each test requires a complete test of the entire code, even if relatively minor changes and adjustments are made to the code, it still takes a lot of time to test. Summary of the invention

[0003] The exemplary embodiments of the present disclosure provide a code testing method and device to at least solve the problems in the above-mentioned related technologies. The technical solutions of the present disclosure are as follows:

[0004] According to a first aspect of an embodiment of the present disclosure, a code testing method is provided, comprising: obtaining code testing information, and dividing the code testing information into a number of unit test cases; determining whether the code used for testing the number of unit test cases has been modified; if the code used for testing at least one unit test case among the number of unit test cases has been modified, performing independent case testing using the at least one unit test case; if the independent case test of the at least one unit test case passes, determining not to perform code testing.

[0005] Optionally, the step of determining whether the code used to test the several unit test cases has been modified includes: determining the class file associated with each unit test case in the several unit test cases in the class file of the modified code; determining the class file corresponding to the code part modified this time; based on the class file associated with each unit test case, determining the unit test case associated with the class file corresponding to the code part modified this time, and using the determined unit test case as: the at least one unit test case in which the code used for testing has been modified.

[0006] Optionally, the step of determining the class file associated with each unit test case in the class file of the modified code includes: determining the method call chain of the modified code, wherein the method call chain is used to represent the calling relationship between multiple methods abstracted from the modified code; determining the class file associated with each unit test case based on the method that needs to be called for each unit test case and the method call chain of the modified code.

[0007] Optionally, the step of determining the method call chain of the code after the current modification includes: determining the method call chain of the code after the current modification according to the abstract syntax tree of the code after the current modification.

[0008] Optionally, the step of determining the class file associated with each unit test case based on the method that needs to be called for each unit test case and the method call chain of the code modified this time includes: for each unit test case, determining the method call chain to which the method needed to be called by the unit test case belongs from the method call chain of the code modified this time; and determining the class file corresponding to the method call chain as: the class file associated with the unit test case; wherein, the class file corresponding to the method call chain is: the class file where the class included in the method call chain is located.

[0009] Optionally, the step of determining the class file associated with each unit test case in the class file of the modified code includes: for each unit test case, traversing the class file of the modified code by bytecode analysis, and determining the class file whose content involves the method that needs to be called by the unit test case as: the class file associated with the unit test case.

[0010] Optionally, the step of determining the unit test case associated with the class file corresponding to the code portion modified this time according to the class file associated with each unit test case includes: according to the correspondence between each unit test case and the class file associated with it, determining the unit test case that has a correspondence with the class file corresponding to the code portion modified this time as: the unit test case associated with the class file corresponding to the code portion modified this time.

[0011] Optionally, the code testing method further includes: if the independent case test of the at least one unit test case fails, determining that there is a problem with the modification of the code used to test the at least one unit test case.

[0012] According to a second aspect of an embodiment of the present disclosure, a code testing device is provided, comprising: a unit test acquisition unit, configured to acquire code test information and divide the code test information into a number of unit test cases; a modification determination unit, configured to determine whether the code used for testing the number of unit test cases has been modified; an independent case test unit, configured to perform an independent case test using the at least one unit test case among the number of unit test cases if the code used for testing the number of unit test cases has been modified; and a determination unit, configured to determine not to perform code testing if the independent case test of the at least one unit test case has passed.

[0013] Optionally, the modification determination unit is configured to: determine the class file associated with each unit test case in the several unit test cases in the class file of the code after this modification; determine the class file corresponding to the code part modified this time; determine the unit test case associated with the class file corresponding to the code part modified this time based on the class file associated with each unit test case, and use the determined unit test case as: the at least one unit test case in which the code used for testing has been modified.

[0014] Optionally, the modification determination unit is configured to determine the method call chain of the code after this modification; and determine the class file associated with each unit test case based on the method that needs to be called by each unit test case and the method call chain of the code after this modification; wherein the method call chain is used to represent the calling relationship between multiple methods into which the code after this modification is abstracted.

[0015] Optionally, the modification determination unit is configured to determine a method call chain of the modified code according to an abstract syntax tree of the modified code.

[0016] Optionally, the modification determination unit is configured to determine, for each unit test case, from the method call chain of the modified code, the method call chain to which the method needed to be called by the unit test case belongs; and determine the class file corresponding to the method call chain as: the class file associated with the unit test case; wherein the class file corresponding to the method call chain is: the class file where the class included in the method call chain is located.

[0017] Optionally, the modification determination unit is configured to traverse the class files of the modified code for each unit test case by bytecode analysis, and determine the class files whose contents involve the methods that need to be called by the unit test case as: the class files associated with the unit test case.

[0018] Optionally, the modification determination unit is configured to determine the unit test case that has a corresponding relationship with the class file corresponding to the code part modified this time according to the correspondence between each unit test case and the class file associated with it as: the unit test case associated with the class file corresponding to the code part modified this time.

[0019] Optionally, the determining unit is configured to determine that there is a problem with the modification of the code used to test the at least one unit test case if the independent case test of the at least one unit test case fails.

[0020] According to a third aspect of an embodiment of the present disclosure, there is provided an electronic device, comprising: at least one processor; and at least one memory storing computer executable instructions, wherein when the computer executable instructions are executed by the at least one processor, the at least one processor is prompted to execute the code testing method as described above.

[0021] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by at least one processor, the at least one processor is prompted to execute the code testing method as described above.

[0022] According to a fifth aspect of an embodiment of the present disclosure, there is provided a computer program product, comprising computer instructions, which implement the code testing method as described above when executed by at least one processor.

[0023] According to the code testing method and device of the exemplary embodiments of the present disclosure, independent case testing is performed by using only at least one unit test case in which the code used for testing has been modified, that is, only executing the unit test case associated with the code portion modified this time, and testing the modified code. This can effectively determine whether there are problems with the modified code, and can also effectively reduce the number of executions of unit test cases, thereby improving the efficiency of code testing.

[0024] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0025] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.

[0026] Figure 1 A flowchart showing a code testing method according to an exemplary embodiment of the present disclosure;

[0027] Figure 2A flowchart showing a method for determining whether codes tested by several unit test cases have been modified according to an exemplary embodiment of the present disclosure;

[0028] Figure 3 A structural block diagram of a code testing device according to an exemplary embodiment of the present disclosure is shown;

[0029] Figure 4 A structural block diagram of an electronic device according to an exemplary embodiment of the present disclosure is shown. DETAILED DESCRIPTION

[0030] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.

[0031] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.

[0032] It should be noted that the phrase "at least one of the items" in the present disclosure includes three types of parallel situations: "any one of the items", "a combination of any number of the items", and "all of the items". For example, "including at least one of A and B" includes the following three parallel situations: (1) including A; (2) including B; (3) including A and B. Another example is "executing at least one of step 1 and step 2" which means the following three parallel situations: (1) executing step 1; (2) executing step 2; (3) executing step 1 and step 2.

[0033] Figure 1 A flowchart of a code testing method according to an exemplary embodiment of the present disclosure is shown.

[0034] Reference Figure 1 In step S101, code test information is obtained, and the code test information is divided into a number of unit test cases.

[0035] As an example, the code test information is information used to test the target code, for example, information used to verify whether there is a problem with the target code and whether the required functions can be realized.

[0036] Case, unit test case, a method (function) that implements a simple function, uses a set of specific inputs to test whether a code block functions normally and returns the correct output.

[0037] In step S102, it is determined whether the codes used for testing the plurality of unit test cases have been modified.

[0038] As an example, the code testing method according to the exemplary embodiment of the present disclosure may further include: determining the code portion to be modified this time.

[0039] The modified code part is the part of the modified code that is different from the original code. The original code is the basis of this modification, and this modification is based on it. The modified code can be the source code of the project after this modification.

[0040] As an example, the modified code portion can be determined by an appropriate method, and the present disclosure does not limit this. For example, the git tool can be used to generate MR Diff. Specifically, the git tool can compare the difference between the modified code and the original code, and then generate a Diff file including code change information. The Diff file is in text format, which specifies which file has modified which lines of code in a specific format. By parsing this file, the modified code portion can be analyzed.

[0041] MR (Merge Request) is a development mode of code management tools. After developers modify the code, if they need to merge it into the target code branch, they need to submit an MR for others to review. The code in the target code branch is the original unmodified code, and the MR contains the modified code for the original unmodified code. After the MR is merged into the target code branch, the original code becomes the modified code in the MR. As an example, the code testing method according to the exemplary embodiment of the present disclosure can be executed before the MR is reviewed by others.

[0042] As an example, when there is at least one unit test case among the several unit test cases that is used to test the code portion that is modified this time (for example, to test the functions related to the code portion that is modified this time), that is, the code used to test by the at least one unit test case is modified, then it is determined that the code used to test by the several unit test cases is modified.

[0043] As an example, a unit test case associated with the class file corresponding to the code portion modified this time (that is, the class file modified this time) can be determined from the several unit test cases, and the determined unit test case can be used as: the at least one unit test case in which the code used for testing has been modified.

[0044] Regarding class files, a class is a concept in the code, and the code is written in the file. Therefore, a file can contain one or more classes. A class file is a file that contains a class.

[0045] The class file corresponding to the modified code part is: the modified class file determined according to the modified code part, that is, the class file involved in the modified code part. The modified class file is also the file corresponding to the modified class. For example, a class file may only have a few lines changed. This modified class file can be called the file corresponding to the modified class.

[0046] For example, the modified class files can be analyzed based on the current MR Diff.

[0047] The following will be combined Figure 2 An exemplary embodiment of step S102 is described below.

[0048] If it is determined in step S102 that the code used for testing at least one of the plurality of unit test cases has been modified, step S103 is executed to perform an independent case test using the at least one unit test case to determine whether there is a problem with the modified code.

[0049] In step S104, if the independent case test of the at least one unit test case passes, it is determined not to perform code testing.

[0050] As an example, if the independent case test of the at least one unit test case passes, it is determined that there is no problem with the modified code, and therefore it is determined that full code testing is not required.

[0051] Unit testing is a method for testing code in software engineering. It can be understood as running a set of cases. If the case passes, there is no problem. If the case fails, there is a problem. The object of case execution is the Apk file compiled from all modified source codes.

[0052] In fact, the present disclosure takes into account that the code will be tested after the initial completion or after each subsequent modification, and will not be applied until the test passes. Therefore, the code portion of the original code that has not been modified this time has been tested and passed the test before, and there is no need to repeat the test on this part. However, in the prior art, after each modification, the unit test case is fully executed, and the cases that are not related to the development and modification of the code are also executed. This part of the case is not necessary to execute and slows down the test execution efficiency. The present disclosure proposes that the corresponding unit test case can be used only for the code portion modified this time to perform independent case testing, that is, only the functions related to the code portion modified this time are tested, which can effectively determine whether there are problems with the modified code this time without omissions, and also save testing time.

[0053] As an example, the code testing method according to the exemplary embodiment of the present disclosure may further include: if the independent case test of the at least one unit test case fails, determining that there is a problem with the current modification of the code portion tested by the at least one unit test case.

[0054] Figure 2 The flowchart shows a method for determining whether codes tested by the unit test cases have been modified according to an exemplary embodiment of the present disclosure.

[0055] Reference Figure 2 In step S201, the class files in the modified code that are associated with each unit test case in the plurality of unit test cases are determined.

[0056] In one embodiment, step S201 may include: first determining the method call chain of the modified code; then determining the class file associated with each unit test case according to the method to be called by each unit test case and the method call chain of the modified code. The method call chain is used to represent the call relationship between the multiple methods abstracted into the modified code.

[0057] As an example, the method call chain of the modified code can be determined based on the abstract syntax tree of the modified code. That is, the method call chain of the code can be analyzed based on the abstract syntax tree of the code, which is equivalent to abstracting the code into methods one by one. The code relationship is that one method calls another method, thereby forming a call chain in series.

[0058] As an example, for each unit test case, the method call chain to which the method to be called by the unit test case belongs can be determined from the method call chain of the modified code; and the class file corresponding to the method call chain can be determined as: the class file associated with the unit test case.

[0059] The class file corresponding to the method call chain is: the class file where the class included in the method call chain is located.

[0060] In other words, the class files affected by the case can be found based on the method calling relationship of the case and the method calling chain of the code. This can be understood as traversing the loop to determine which call chain the method called in the case belongs to and which classes this call chain has, so as to find the specific affected class files.

[0061] In another embodiment, step S201 may include: for each unit test case, traversing the class files of the modified code by bytecode analysis, and determining the class files whose contents involve the methods that need to be called by the unit test case as: the class files associated with the unit test case.

[0062] Bytecode analysis can scan structured code files to find the required data therein. In this embodiment, it is equivalent to traversing all class files to see which class file content includes the identification information (for example, name) of a method included in the case.

[0063] In step S202, the class file corresponding to the code portion modified this time is determined.

[0064] In step S203, the unit test case associated with the class file corresponding to the modified code portion is determined according to the class file associated with each unit test case, and the determined unit test case is used as: the at least one unit test case for which the code used for testing has been modified.

[0065] As an example, based on the correspondence (for example, mapping relationship) between each unit test case and the class file associated with it, the unit test case that has a corresponding relationship with the class file corresponding to the code portion modified this time can be determined as: the unit test case associated with the class file corresponding to the code portion modified this time.

[0066] For example, based on the method call chain and the methods called in the case, you can determine which class file the case affects, and thus derive the mapping relationship between the case and the class file. Then, based on the class file corresponding to the modified code portion, you can get the associated case through the mapping relationship.

[0067] According to an exemplary embodiment of the present disclosure, the correlation between the unit test case and the class file can be automatically analyzed, and the unit test case most relevant to the current modification can be inferred based on MR Diff, thereby improving the correlation between the unit test case and MRDiff, reducing the number of case executions, and improving the efficiency of automated testing.

[0068] Figure 3A structural block diagram of a code testing device according to an exemplary embodiment of the present disclosure is shown.

[0069] like Figure 3 As shown, the code testing device 10 according to the exemplary embodiment of the present disclosure includes: a unit test acquisition unit 101 , a modification determination unit 102 , an independent case test unit 103 , and a determination unit 104 .

[0070] Specifically, the unit test acquisition unit 101 is configured to acquire code test information and divide the code test information into a number of unit test cases.

[0071] The modification determination unit 102 is configured to determine whether the codes tested by the plurality of unit test cases have been modified.

[0072] The independent case testing unit 103 is configured to perform an independent case test using at least one unit test case among the plurality of unit test cases if the code used for testing by the at least one unit test case is modified.

[0073] The determining unit 104 is configured to determine not to perform code testing if an independent case test of the at least one unit test case passes.

[0074] As an example, the modification determination unit 102 can be configured to: determine the class file associated with each unit test case in the several unit test cases in the class file of the code after this modification; determine the class file corresponding to the code part modified this time; and based on the class file associated with each unit test case, determine the unit test case associated with the class file corresponding to the code part modified this time, and use the determined unit test case as: the at least one unit test case in which the code used for testing has been modified.

[0075] As an example, the modification determination unit 102 can be configured to determine the method call chain of the code after this modification; and determine the class file associated with each unit test case based on the method that needs to be called by each unit test case and the method call chain of the code after this modification; wherein the method call chain is used to represent the calling relationship between multiple methods into which the code after this modification is abstracted.

[0076] As an example, the modification determination unit 102 may be configured to determine the method call chain of the modified code according to the abstract syntax tree of the modified code.

[0077] As an example, the modification determination unit 102 can be configured to determine, for each unit test case, from the method call chain of the modified code, the method call chain to which the method needed to be called by the unit test case belongs; and determine the class file corresponding to the method call chain as: the class file associated with the unit test case; wherein, the class file corresponding to the method call chain is: the class file where the class included in the method call chain is located.

[0078] As an example, the modification determination unit 102 may be configured to traverse the class files of the modified code for each unit test case through bytecode analysis, and determine the class files whose contents involve the methods that need to be called by the unit test case as: the class files associated with the unit test case.

[0079] As an example, the modification determination unit 102 can be configured to determine the unit test case that has a corresponding relationship with the class file corresponding to the code part modified this time according to the correspondence between each unit test case and the class file associated with it as: the unit test case associated with the class file corresponding to the code part modified this time.

[0080] As an example, the determining unit 104 may be configured to determine that there is a problem with the modification of the code used to test the at least one unit test case if the independent case test of the at least one unit test case fails.

[0081] Regarding the code testing device 10 in the above embodiment, the specific manner in which each unit performs operations has been described in detail in the embodiment of the method, and will not be elaborated here.

[0082] In addition, it should be understood that the various units in the code testing device 10 according to the exemplary embodiment of the present disclosure can be implemented as hardware components and / or software components. Those skilled in the art can implement the various units, for example, using a field programmable gate array (FPGA) or an application specific integrated circuit (ASIC), according to the processing performed by the defined various units.

[0083] Figure 4 FIG. 1 is a block diagram showing a structure of an electronic device according to an exemplary embodiment of the present disclosure. Figure 4 The electronic device 20 includes: at least one memory 201 and at least one processor 202, wherein the at least one memory 201 stores a set of computer executable instructions, and when the computer executable instruction set is executed by the at least one processor 202, the code testing method described in the above exemplary embodiment is executed.

[0084] As an example, the electronic device 20 may be a PC, a tablet device, a personal digital assistant, a smart phone, or other device capable of executing the above instruction set. Here, the electronic device 20 is not necessarily a single electronic device, but may also be any device or circuit collection capable of executing the above instruction (or instruction set) individually or in combination. The electronic device 20 may also be part of an integrated control system or system manager, or may be configured as a portable electronic device interconnected with a local or remote (e.g., via wireless transmission) interface.

[0085] In the electronic device 20, the processor 202 may include a central processing unit (CPU), a graphics processing unit (GPU), a programmable logic device, a dedicated processor system, a microcontroller or a microprocessor. By way of example and not limitation, the processor 202 may also include an analog processor, a digital processor, a microprocessor, a multi-core processor, a processor array, a network processor, etc.

[0086] The processor 202 may execute instructions or codes stored in the memory 201, wherein the memory 201 may also store data. Instructions and data may also be sent and received over a network via a network interface device, wherein the network interface device may employ any known transmission protocol.

[0087] The memory 201 may be integrated with the processor 202, for example, by placing RAM or flash memory within an integrated circuit microprocessor or the like. In addition, the memory 201 may include a separate device, such as an external disk drive, a storage array, or any other storage device that can be used by a database system. The memory 301 and the processor 202 may be operatively coupled, or may communicate with each other, such as through an I / O port, a network connection, etc., so that the processor 202 can read files stored in the memory.

[0088] In addition, the electronic device 20 may also include a video display (such as a liquid crystal display) and a user interaction interface (such as a keyboard, a mouse, a touch input device, etc.) All components of the electronic device 20 may be connected to each other via a bus and / or a network.

[0089] According to an exemplary embodiment of the present disclosure, a computer-readable storage medium storing instructions may also be provided, wherein when the instructions are executed by at least one processor, the at least one processor is prompted to execute the code testing method as described in the above exemplary embodiment. Examples of computer-readable storage media here include: read-only memory (ROM), random access programmable read-only memory (PROM), electrically erasable programmable read-only memory (EEPROM), random access memory (RAM), dynamic random access memory (DRAM), static random access memory (SRAM), flash memory, non-volatile memory, CD-ROM, CD-R, CD+R, CD-RW, CD+RW, DVD-ROM, DVD-R, DVD+R, DVD-RW, DVD+RW, DVD-RAM, BD-ROM, BD-R, BD-R LTH, BD-RE, Blu-ray or optical disk storage, hard disk drive (HDD), solid state drive (SSD), card storage (such as, multimedia card, secure digital (SD) card or extreme digital (XD) card), magnetic tape, floppy disk, magneto-optical data storage device, optical data storage device, hard disk, solid state disk and any other device, any other device is configured to store computer programs and any associated data, data files and data structures in a non-transitory manner and provide the computer programs and any associated data, data files and data structures to a processor or computer so that the processor or computer can execute the computer program. The computer program in the above-mentioned computer-readable storage medium can be run in an environment deployed in a computer device such as a client, a host, an agent device, a server, etc. In addition, in one example, the computer program and any associated data, data files and data structures are distributed on a networked computer system, so that the computer program and any associated data, data files and data structures are stored, accessed and executed in a distributed manner by one or more processors or computers.

[0090] According to an exemplary embodiment of the present disclosure, a computer program product may also be provided. Instructions in the computer program product may be executed by at least one processor to implement the code testing method as described in the above exemplary embodiment.

[0091] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The specification and examples are intended to be exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.

[0092] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.

Claims

1. A code testing method, characterized in that: include: Acquire code test information, and divide the code test information into a plurality of unit test cases; Determine whether the codes used for testing of the plurality of unit test cases have been modified; If the code used for testing at least one unit test case among the plurality of unit test cases is modified, use the at least one unit test case to perform independent test case testing; If the independent case test of the at least one unit test case passes, determining not to perform code testing; The step of determining whether the codes used for testing the plurality of unit test cases have been modified comprises: Determine the class file in the modified code that is associated with each unit test case in the plurality of unit test cases; Determine the class file corresponding to the modified code part; Determine, according to the class file associated with each unit test case, the unit test case associated with the class file corresponding to the modified code portion, and use the determined unit test case as: the at least one unit test case for which the code to be tested has been modified; The step of determining the class file associated with each unit test case in the class file of the modified code includes: Determine a method call chain of the modified code, wherein the method call chain is used to represent a call relationship between multiple methods abstracted from the modified code; According to the method that needs to be called by each unit test case and the method call chain of the modified code, the class file associated with each unit test case is determined.

2. The code testing method according to claim 1, characterized in that: The steps to determine the method call chain of the modified code include: According to the abstract syntax tree of the modified code, the method call chain of the modified code is determined.

3. The code testing method according to claim 1, characterized in that: According to the method that needs to be called by each unit test case and the method call chain of the modified code, the steps of determining the class file associated with each unit test case include: For each unit test case, determine the method call chain to which the method to be called by the unit test case belongs from the method call chain of the code modified this time; and determine the class file corresponding to the method call chain as: the class file associated with the unit test case; The class file corresponding to the method call chain is: the class file where the class included in the method call chain is located.

4. The code testing method according to claim 1, characterized in that: The step of determining the class file associated with each unit test case in the class file of the modified code further includes: For each unit test case, the class files of the modified code are traversed by bytecode analysis, and the class files whose contents involve the methods to be called by the unit test case are determined as: the class files associated with the unit test case.

5. The code testing method according to claim 1, characterized in that: The step of determining the unit test case associated with the class file corresponding to the code portion modified this time according to the class file associated with each unit test case comprises: According to the correspondence between each unit test case and its associated class file, the unit test case having a correspondence with the class file corresponding to the modified code portion is determined as: the unit test case associated with the class file corresponding to the modified code portion.

6. The code testing method according to claim 1, characterized in that: The code testing method also includes: If the independent case test of the at least one unit test case fails, it is determined that there is a problem with the modification of the code used to test the at least one unit test case.

7. A code testing device, characterized in that: include: A unit test acquisition unit is configured to acquire code test information and divide the code test information into a plurality of unit test cases; A modification determination unit, configured to determine whether the codes used for testing the plurality of unit test cases have been modified; An independent case test unit, configured to use at least one unit test case among the plurality of unit test cases to perform an independent case test if the code used for testing by the at least one unit test case is modified; A determination unit, configured to determine not to perform code testing if an independent case test of the at least one unit test case passes; The modification determination unit is configured to: determine the class file associated with each unit test case in the plurality of unit test cases in the class file of the code after the modification; determine the class file corresponding to the code portion modified this time; determine the unit test case associated with the class file corresponding to the code portion modified this time according to the class file associated with each unit test case, and use the determined unit test case as: the at least one unit test case in which the code used for the test has been modified; Among them, the modification determination unit is configured to determine the method call chain of the code after this modification; and determine the class file associated with each unit test case based on the method required to be called by each unit test case and the method call chain of the code after this modification; wherein, the method call chain is used to represent the calling relationship between multiple methods abstracted into which the code after this modification is formed.

8. The code testing device according to claim 7, characterized in that: The modification determination unit is configured to determine the method call chain of the modified code according to the abstract syntax tree of the modified code.

9. The code testing device according to claim 7, characterized in that: The modification determination unit is configured to determine, for each unit test case, from the method call chain of the code modified this time, the method call chain to which the method to be called by the unit test case belongs; and determine the class file corresponding to the method call chain as: the class file associated with the unit test case; The class file corresponding to the method call chain is: the class file where the class included in the method call chain is located.

10. The code testing device according to claim 7, characterized in that: The modification determination unit is also configured to traverse the class files of the modified code for each unit test case through bytecode analysis, and determine the class files whose contents involve the methods that need to be called by the unit test case as: the class files associated with the unit test case.

11. The code testing device according to claim 7, characterized in that: The modification determination unit is configured to determine the unit test case having a corresponding relationship with the class file corresponding to the code portion modified this time according to the correspondence between each unit test case and the class file associated therewith as: the unit test case associated with the class file corresponding to the code portion modified this time.

12. The code testing device according to claim 7, characterized in that: The determining unit is configured to determine that there is a problem with the modification of the code used to test the at least one unit test case if the independent case test of the at least one unit test case fails.

13. An electronic device, characterized in that: include: at least one processor; at least one memory storing computer executable instructions, When the computer executable instructions are executed by the at least one processor, the at least one processor is prompted to execute the code testing method according to any one of claims 1 to 6.

14. A computer-readable storage medium, characterized in that: When the instructions in the computer-readable storage medium are executed by at least one processor, the at least one processor is prompted to perform the code testing method according to any one of claims 1 to 6.

15. A computer program product comprising computer instructions, characterized in that When the computer instructions are executed by at least one processor, the code testing method according to any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Case test method and terminal

    CN112445692A

  • Unit testing method and device, equipment, storage medium and program product

    CN113127357A