Disclosure of Invention
An exemplary embodiment of the present disclosure is to provide a code testing method and apparatus to solve at least the above-mentioned problems in the related art. The technical scheme of the present disclosure is as follows:
According to a first aspect of the disclosed embodiments, a code testing method is provided, which includes obtaining code testing information, dividing the code testing information into a plurality of unit testing cases, determining whether codes used for testing by the unit testing cases are modified, if the codes used for testing by at least one unit testing case in the unit testing cases are modified, using the at least one unit testing case to conduct independent case testing, and if the independent case testing of the at least one unit testing case is passed, determining that the code testing is not conducted.
Optionally, the step of determining whether the code used for testing by the plurality of unit test cases is modified includes determining a class file associated with each unit test case in the plurality of unit test cases in a class file of the code after modification, determining a class file corresponding to the code part modified at this time, determining a unit test case associated with the class file corresponding to the code part modified at this time according to the class file associated with each unit test case, and taking the determined unit test case as the at least one unit test case modified by the code used for testing.
Optionally, determining the class file associated with each unit test case in the class file of the code after the modification includes determining a method call chain of the code after the modification, wherein the method call chain is used for representing a call relationship among a plurality of methods abstracted by the code after the modification, and determining the class file associated with each unit test case according to the method required to be called by each unit test case and the method call chain of the code after the modification.
Optionally, the step of determining the method call chain of the modified code comprises determining the method call chain of the modified code according to the abstract syntax tree of the modified code.
Optionally, determining the class file associated with each unit test case according to the method required to be called by each unit test case and the method call chain of the code after the modification comprises determining, for each unit test case, the method call chain to which the method required to be called by the unit test case belongs from the method call chain of the code after the modification, 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 in which the class included by the method call chain is located.
Optionally, determining the class file associated with each unit test case in the class file of the code after the modification includes traversing the class file of the code after the modification by a byte code analysis mode for each unit test case, and determining the class file of the method of which the content relates to the unit test case needs to be called as the class file associated with the unit test case.
Optionally, the step of determining the unit test cases associated with the class file corresponding to the code portion modified at this time according to the class file associated with each unit test case includes determining the unit test cases having the corresponding relationship with the class file corresponding to the code portion modified at this time as the unit test cases associated with the class file corresponding to the code portion modified at this time according to the corresponding relationship between each unit test case and the class file associated with each unit test case.
Optionally, the code testing method further comprises determining that the modification of the code used for testing by the at least one unit test case is problematic if the independent case test of the at least one unit test case fails.
According to a second aspect of the embodiments of the present disclosure, a code testing device is provided, which includes a unit test obtaining unit configured to obtain code test information and divide the code test information into a plurality of unit test cases, a modification determining unit configured to determine whether codes used for testing by the plurality of unit test cases are modified, an independent case testing unit configured to use at least one unit test case of the plurality of unit test cases to perform independent case testing if the codes used for testing by the at least one unit test case are modified, and a determining unit configured to determine not to perform code testing if the independent case testing of the at least one unit test case is passed.
Optionally, the modification determining unit is configured to determine a 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 a class file corresponding to the code portion of the modification, determine a unit test case associated with the class file corresponding to the code portion of the modification according to the class file associated with each unit test case, and take the determined unit test case as the at least one unit test case modified by the code used for testing.
Optionally, the modification determining unit is configured to determine a method call chain of the code after the modification, and determine a class file associated with each unit test case according to a method required to be called by each unit test case and the method call chain of the code after the modification, wherein the method call chain is used for representing a call relation among a plurality of methods abstracted by the code after the modification.
Optionally, the modification determining unit is configured to determine a method call chain of the code after the modification according to the abstract syntax tree of the code after the modification.
Optionally, the modification determining unit is configured to determine, for each unit test case, a method call chain to which a method to be called by the unit test case belongs from a method call chain of the code after the modification, and determine a class file corresponding to the method call chain to be a class file associated with the unit test case, where the class file corresponding to the method call chain is a class file in which a class included in the method call chain is located.
Optionally, the modification determining unit is configured to traverse the class file of the code modified this time by means of byte code analysis for each unit test case, and determine the class file of the method whose content relates to the unit test case to be called as the class file associated with the unit test case.
Optionally, the modification determining unit is configured to determine, as the unit test cases associated with the class file corresponding to the code portion modified at this time, the unit test cases having the correspondence with the class file corresponding to the code portion modified at this time according to the correspondence between each unit test case and the class file associated therewith.
Optionally, the determining unit is configured to determine that there is a problem with modification of the code for which the at least one unit test case is used if the independent case test of the at least one unit test case fails.
According to a third aspect of embodiments of the present disclosure, there is provided an electronic device comprising at least one processor, at least one memory storing computer executable instructions, wherein the computer executable instructions, when executed by the at least one processor, cause the at least one processor to perform a code testing method as described above.
According to a fourth aspect of embodiments of the present disclosure, there is provided a computer-readable storage medium, which when executed by at least one processor, causes the at least one processor to perform the code testing method as described above.
According to a fifth aspect of embodiments of the present disclosure, there is provided a computer program product comprising computer instructions which, when executed by at least one processor, implement a code testing method as described above.
According to the code testing method and device of the exemplary embodiment of the disclosure, the independent Case test is performed by using at least one unit test Case with the modified code used for the test, namely, only the unit test Case associated with the code part modified at this time is executed, and the code modified at this time is tested, so that whether the modified code has a problem or not can be effectively determined, the execution quantity of unit test Case cases can be effectively reduced, and the efficiency of the code test is improved.
It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure.
Detailed Description
In order to enable those skilled in the art to better understand the technical solutions of the present disclosure, the technical solutions of the embodiments of the present disclosure will be clearly and completely described below with reference to the accompanying drawings.
It should be noted that the terms "first," "second," and the like in the description and claims of the present disclosure and in the foregoing figures are used for distinguishing between similar objects and not necessarily for describing a particular sequential or chronological order. It is to be understood that the data so used may be interchanged where appropriate such that the embodiments of the disclosure described herein may be capable of operation in sequences other than those illustrated or described herein. The implementations described in the following exemplary examples are not representative of all implementations consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with some aspects of the present disclosure as detailed in the accompanying claims.
It should be noted that, in this disclosure, "at least one of the items" refers to a case where three types of juxtaposition including "any one of the items", "a combination of any of the items", "an entirety of the items" are included. For example, "comprising at least one of A and B" includes the case of juxtaposition of three of (1) comprising A, (2) comprising B, and (3) comprising A and B. For example, "at least one of the first and second steps is executed", that is, three cases are shown in parallel, namely (1) execute the first step, (2) execute the second step, and (3) execute the first and second steps.
Fig. 1 illustrates a flowchart of a code testing method according to an exemplary embodiment of the present disclosure.
Referring to fig. 1, in step S101, code test information is acquired and divided into a plurality of unit test cases.
As an example, the code test information is information for testing the object code, for example, information for verifying whether the object code has a problem or not and whether a desired function can be implemented.
Case, unit test Case, a method (function) to achieve simple function, tests whether a certain code block is functioning properly with a set of specific inputs, and returns the correct output.
In step S102, it is determined whether the code used for testing by the unit test cases has been modified.
As an example, the code testing method according to an exemplary embodiment of the present disclosure may further include determining the code portion of the current modification.
The code part modified at this time is a part different from the original code in the code modified at this time. The original code is the basis of the current modification, and the current modification is performed based on the original code. The modified code can be the modified engineering source code.
As an example, the code portion of the present modification may be determined in an appropriate manner, which is not limited by the present disclosure. For example, MR Diff may be generated using a git tool, specifically, the git tool may compare the difference between the current modified code and the original code, and then give a Diff file including code change information, where the Diff file is in text format, and in which lines of codes are modified according to a specific format, and the current modified code portion may be analyzed by parsing the file.
MR (Merge Request), a development mode of a code management tool, if a developer needs to merge into a target code branch after modifying a code, the developer needs to provide an MR for others to review, and the developer can only merge after the review is finished. The code in the target code branch is the original unmodified code, the code modified for the original unmodified code is in the MR, and the original code becomes the modified code in the MR after the MR is combined with the target code branch. As an example, a code testing method according to an exemplary embodiment of the present disclosure may be performed before the MR is reviewed by others.
As an example, when at least one unit test case for testing the code portion modified at this time (for example, testing a function related to the code portion modified at this time) exists in the plurality of unit test cases, that is, when the code used for testing by the at least one unit test case is modified, it may be determined that the code used for testing by the plurality of unit test cases is modified.
As an example, a unit test case associated with a class file corresponding to the code portion of the current modification (i.e., the class file of the current modification) may be determined from the plurality of unit test cases, and the determined unit test case may be used as the at least one unit test case for which the code for the test has modification.
With respect to class files, a class is a concept in code that is written in a file, and thus, one file may contain one or more classes, i.e., a file containing a class.
The class file corresponding to the code part modified at this time is the class file modified at this time determined according to the code part modified at this time, namely the class file related to the code part modified at this time. The modified class file, that is, the file corresponding to the modified class, for example, one class file may be changed by only a few lines, and the modified class file may be referred to as a file corresponding to the modified class.
For example, the current modified class file may be analyzed based on the current MR Diff.
An exemplary embodiment of step S102 will be described below in conjunction with fig. 2.
If it is determined in step S102 that the code used for testing by at least one of the plurality of unit test cases is modified, step S103 is executed, and an independent case test is performed using the at least one unit test case. To determine whether the current modified code has problems.
In step S104, if the independent case test of the at least one unit test case passes, it is determined that the code test is not performed.
As an example, if the independent case test of the at least one unit test case passes, it is determined that the code modified this time has no problem, so it is determined that the full-scale code test is not required.
The unit test is a test method for codes in software engineering, and can be understood as running the set of cases, the case execution is free of problems, the case execution is failed, and the object of the case execution is Apk files compiled by all modified source codes.
In fact, the present disclosure considers that the code is tested after the initial completion or after each subsequent modification, and is applied after the test is passed, so that the portion of the original code that is not modified is tested before the test, and the portion of the original code that is not modified is passed, and the repeated test is not required. The method and the device can only use the corresponding unit test cases for the code part modified at this time to carry out independent case test, namely, only test the functions related to the code part modified at this time, so that whether the code modified at this time has problems or not can be effectively determined, omission does not occur, and the test time is saved.
By way of example, the code testing method according to the exemplary embodiment of the present disclosure may further include determining that the current modification of the code portion for which the at least one unit test case is used is problematic if the independent case test of the at least one unit test case fails.
FIG. 2 illustrates a flowchart of a method of determining whether code used by the unit test cases for testing is modified according to an exemplary embodiment of the present disclosure.
Referring to fig. 2, in step S201, a class file associated with each of the unit test cases in the plurality of unit test cases in the class file of the code modified at this time is determined.
In one embodiment, step S201 may include determining a method call chain of the code after the current modification, and then determining a class file associated with each unit test case according to a method required to be called by each unit test case and the method call chain of the code after the current modification. The method call chain is used for representing call relations among a plurality of methods into which the code after the modification is abstracted.
As an example, a method call chain of the code after the modification can be determined according to an abstract syntax tree of the code after the modification. That is, a method call chain of the code can be analyzed according to an abstract syntax tree of the code, which is equivalent to abstracting the code into one method, and the code relation is that one method calls another method, so that a call chain is connected in series.
For each unit test case, a method call chain to which a method to be called by the unit test case belongs is determined from a method call chain of the code modified at this time, and a class file corresponding to the method call chain is determined as a class file associated with the unit test case.
The class file corresponding to the method call chain is the class file in which the class included in the method call chain is located.
In other words, the class file affected by the Case can be found according to the method call relation of the Case and the method call chain of the code, which call chain the method called in the Case belongs to can be understood as traversing the loop to judge, and which class the call chain has, so that the class file specifically affected is found.
In another embodiment, step S201 may include traversing the class file of the code modified this time by way of byte code analysis for each unit test case, and determining the class file of the method whose content relates to the unit test case to be called as the class file associated with the unit test case.
The bytecode analysis may scan the structured code file for the data needed therein, which in this embodiment amounts to traversing all class files to see which class file content includes identification information (e.g., name) of a certain method contained in the case.
In step S202, a class file corresponding to the code portion modified this time is determined.
In step S203, according to 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 at this time, and taking the determined unit test case as the at least one unit test case modified by the code used for testing.
As an example, a unit test case having a correspondence relationship with a class file corresponding to the code portion of the present modification may be determined as a unit test case associated with a class file corresponding to the code portion of the present modification according to a correspondence relationship (e.g., a mapping relationship) between each unit test case and the class file associated therewith.
For example, according to the method called in the method call chain and the case, which class file is affected by the case is judged, the mapping relation between the case and the class file is obtained, and then according to the class file corresponding to the code part modified at this time, the associated case is obtained through the mapping relation.
According to the exemplary embodiment of the 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 modification is calculated according to the MR Diff, so that the correlation between the unit test Case and the MR Diff is improved, the execution quantity of the cases is reduced, and the automatic test efficiency is improved.
Fig. 3 illustrates a block diagram of a code testing apparatus according to an exemplary embodiment of the present disclosure.
As shown in fig. 3, the code testing apparatus 10 according to the exemplary embodiment of the present disclosure includes a unit test acquisition unit 101, a modification determination unit 102, an independent use case test unit 103, and a determination unit 104.
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.
The modification determination unit 102 is configured to determine whether the code for which the unit test cases are used for testing is modified.
The independent case test unit 103 is configured to perform independent case test by using at least one unit test case of the plurality of unit test cases if the code used for the test by the at least one unit test case is modified.
The determining unit 104 is configured to determine not to perform the code test if the independent case test of the at least one unit test case passes.
As an example, the modification determination unit 102 may be configured to determine a class file associated with each of the plurality of unit test cases in the class file of the code after the current modification, determine a class file corresponding to the code portion of the current modification, determine a unit test case associated with the class file corresponding to the code portion of the current modification according to the class file associated with each unit test case, and take the determined unit test case as the at least one unit test case for which the code used for the test has modification.
As an example, the modification determining unit 102 may be configured to determine a method call chain of the code after the modification, and determine a class file associated with each unit test case according to a method required to be called by each unit test case and the method call chain of the code after the modification, where the method call chain is used to represent a call relationship between a plurality of methods to which the code after the modification is abstracted.
As an example, the modification determining unit 102 may be configured to determine a method call chain of the current modified code according to an abstract syntax tree of the current modified code.
As an example, the modification determining unit 102 may be configured to determine, for each unit test case, a method call chain to which a method to be called by the unit test case belongs from a method call chain of the code modified this time, and determine a class file corresponding to the method call chain to be a class file associated with the unit test case, where the class file corresponding to the method call chain is a class file in which a class included in the method call chain is located.
As an example, the modification determination unit 102 may be configured to traverse the class file of the code modified this time by way of bytecode analysis for each unit test case, and determine the class file whose content relates to the method that the unit test case needs to call as the class file associated with the unit test case.
As an example, the modification determining unit 102 may be configured to determine, as the unit test cases associated with the class file corresponding to the code portion of the present modification, the unit test cases having a correspondence relationship with the class file corresponding to the code portion of the present modification according to the correspondence relationship between each unit test case and the class file associated therewith.
As an example, the determining unit 104 may be configured to determine that there is a problem with modification of the code for which the at least one unit test case is used if the independent case test of the at least one unit test case fails.
With respect to the code testing device 10 in the above-described embodiment, the specific manner in which the respective units perform the operations has been described in detail in the embodiment regarding the method, and will not be described in detail here.
Furthermore, it should be understood that the various units in the code testing apparatus 10 according to the exemplary embodiments of the present disclosure may be implemented as hardware components and/or software components. The individual units may be implemented, for example, using a Field Programmable Gate Array (FPGA) or an Application Specific Integrated Circuit (ASIC), depending on the processing performed by the individual units as defined.
Fig. 4 shows a block diagram of an electronic device according to an exemplary embodiment of the present disclosure. Referring to fig. 4, the electronic device 20 includes at least one memory 201 and at least one processor 202, the at least one memory 201 having stored therein a set of computer-executable instructions that, when executed by the at least one processor 202, perform the code testing method as described in the above-described exemplary embodiments.
By way of example, the electronic device 20 may be a PC computer, tablet device, personal digital assistant, smart phone, or other device capable of executing the above-described set of instructions. Here, the electronic device 20 is not necessarily a single electronic device, but may be any apparatus or a collection of circuits capable of executing the above-described instructions (or instruction sets) 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 that interfaces with either locally or remotely (e.g., via wireless transmission).
In electronic device 20, processor 202 may include a Central Processing Unit (CPU), a Graphics Processor (GPU), a programmable logic device, a special purpose processor system, a microcontroller, or a microprocessor. By way of example, and not limitation, processor 202 may also include an analog processor, a digital processor, a microprocessor, a multi-core processor, a processor array, a network processor, and the like.
The processor 202 may execute instructions or code stored in the memory 201, wherein the memory 201 may also store data. The instructions and data may also be transmitted and received over a network via a network interface device, which may employ any known transmission protocol.
The memory 201 may be integrated with the processor 202, for example, RAM or flash memory disposed within an integrated circuit microprocessor or the like. In addition, the memory 201 may include a stand-alone device, such as an external disk drive, a storage array, or other storage device usable by any 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, network connection, etc., such that the processor 202 is able to read files stored in the memory.
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, mouse, touch input device, etc.). All components of the electronic device 20 may be connected to each other via a bus and/or a network.
According to an exemplary embodiment of the present disclosure, there may also be provided a computer-readable storage medium storing instructions, wherein the instructions, when executed by at least one processor, cause the at least one processor to perform the code testing method as described in the above exemplary embodiments. Examples of computer readable storage media herein 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, nonvolatile 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 memory, hard Disk Drive (HDD), solid State Disk (SSD), card memory (such as a multimedia card, secure Digital (SD) card or ultra-fast digital (XD) card), magnetic tape, floppy disk, magneto-optical data storage device, hard disk, solid state disk, and any other device configured to non-temporarily store a computer program and any associated data, data files and data structures and to cause the computer program and any associated data, data file and data structures to be provided to a processor or processor to execute the computer program. The computer programs in the computer readable storage media described above can be run in an environment deployed in a computer device, such as a client, host, proxy device, server, etc., and further, in one example, the computer programs and any associated data, data files, and data structures are distributed across networked computer systems such that the computer programs and any associated data, data files, and data structures are stored, accessed, and executed in a distributed fashion by one or more processors or computers.
According to an exemplary embodiment of the present disclosure, a computer program product may also be provided, instructions in which are executable by at least one processor to perform the code testing method as described in the above exemplary embodiment.
Other embodiments of the disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the disclosure disclosed herein. This application is intended to cover any adaptations, uses, or adaptations of the disclosure following, in general, the principles of the disclosure and including such departures from the present disclosure as come within known or customary practice within the art to which the disclosure pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the disclosure being indicated by the following claims.
It is to be understood that the present disclosure is not limited to the precise arrangements and instrumentalities shown in the drawings, and that various modifications and changes may be effected without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.