Detailed Description
Reference will now be made in detail to the exemplary embodiments, examples of which are illustrated in the accompanying drawings. When the following description refers to the accompanying drawings, like numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present application. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present application, as detailed in the appended claims.
With the development of computer technology and the popularization of informatization, the dependence of people on computer technology is increasingly enhanced in aspects of life. In order to meet the requirements of users, the updating iteration speed of application software is faster and faster, unit testing becomes an important link in the whole life cycle of software development, the unit testing is the basis and premise of the whole software testing process, and the method can help developers to improve the code quality and discover software errors in advance.
In the prior art, unit testing usually artificially writes a test case corresponding to a functional module for a certain module in a software development process, and evaluates the development effect of the module according to the test result of the test case, that is, a source program to be tested includes a plurality of unit modules, and writes a test case corresponding to each unit module, and the success or failure of software development can be known only after all the test cases are executed.
However, in the prior art, extra test codes need to be written, so that development of developers is often doubled, and if a problem occurs in testing, more time is often consumed for manually writing corresponding test cases to test developed software; meanwhile, the situation of incomplete consideration exists in manual writing test, so that the test result has deviation, the test efficiency is reduced, and a large amount of manpower is wasted.
Aiming at the technical problems, the method for automatically testing the source program to be tested is realized on the basis of the JavaAgent technology without additionally writing codes. Connecting the unit test program with a source code to be tested by a JavaAgent technology, and obtaining a class file of the source program to be tested by loading the code of the source program to be tested; acquiring byte codes of class files based on a Retransformclass method provided by a JavaAgent technology; and then all methods for acquiring the class files by using a byte code analysis technology, judging the method to be executed by the unit test program according to the method name rule, adding the method to be executed into a thread pool for waiting execution, and acquiring a test result.
Specifically, the present application provides a unit testing method. The method comprises the following steps: performing bytecode-based code analysis processing on a source code to be tested, determining a class file corresponding to the source code and an execution instruction corresponding to the class file, constructing at least one testing thread of the source code according to the file name of the class file and the execution instruction, and creating an instantiation object and a corresponding parameter list for each testing thread according to the class file in the source code; and executing each test thread on the source code according to the instantiation object and the corresponding parameter list to obtain a test result. The method improves the efficiency of unit testing.
The following describes technical solutions of embodiments of the present application and how to solve the above technical problems with specific embodiments. The following several specific embodiments may be combined with each other, and details of the same or similar concepts or processes may not be repeated in some embodiments. Embodiments of the present application will be described below with reference to the accompanying drawings.
Referring to fig. 1, fig. 1 is a schematic diagram of a network architecture on which the present application is based, and as shown in fig. 1, the network architecture includes aserver 1.
Theserver 1 is specifically a server cluster capable of processing mass data, and a unit testing device provided by the present application may be integrated or installed therein, wherein the unit testing device may process a source program to be tested based on the unit testing method provided by the present application.
Example one
Fig. 2 is a schematic flow chart of a unit testing method provided in the present application, as shown in fig. 2, the method includes:
step 201, performing byte code-based code analysis processing on a source code to be tested, and determining a class file corresponding to the source code and an execution instruction corresponding to the class file;
step 202, constructing at least one test thread of the source code according to the file name of the class file and the execution instruction;
step 203, creating an instantiation object and a corresponding parameter list for each test thread according to the class file in the source code;
and 204, executing each test thread on the source code according to the instantiation object and the corresponding parameter list to obtain a test result.
Specifically, the execution subject of the unit testing method provided by the present application is the aforementioned unit testing apparatus, and as mentioned above, the unit testing apparatus may be specifically installed or carried in theaforementioned server 1.
It should be noted that the unit testing method provided in the present application is executed on the premise of being based on a spring environment.
Instep 201, when the unit testing device executes the source code to be tested, it needs to perform byte code-based parsing on the source code to be tested, and determine a class file corresponding to the source code and an execution instruction corresponding to the class file.
Specifically, the unit testing device associates a unit testing program with a source program to be tested based on the JavaAgent technology, namely, a source code to be tested is input into the unit testing method; according to the JavaAgent technology, a source code to be tested is loaded to obtain a class file in the source code, then a retransFormClass method provided by the JavaAgent technology is used for obtaining byte codes of the class, and then an execution instruction corresponding to the class file is obtained by using a byte code analyzing technology in the Java technology. The execution instruction refers to an operation instruction of the computer, which can be divided into an operation instruction, a data transmission instruction, a program control instruction, and an input/output instruction.
The bytecode is a binary file comprising an executive program, a sequence of operation codes, namely, numerical constants, references, instructions and the like, or a data pair, and is an intermediate code, so that the applicability of the unit test method to the application environment is enhanced. The byte code analysis refers to the escape of the byte code of the binary system, so that the virtual machine required by the unit test method can identify the byte code and execute corresponding processing. The virtual machine instruction refers to an abstracted computer instruction, which corresponds to a virtualized computer, i.e., a virtual machine.
After the unit test obtains the class file and the execution instruction corresponding to the class file, the unit test apparatus performsstep 202 to obtain a test thread that the unit test program needs to be executed, that is, the unit test apparatus constructs at least one test thread of the source code according to the file name of the class file and the execution instruction. Wherein the test thread refers to one of all functional functions provided by the unit test.
In particular, at least one test thread building the source code may originate from two different test units, a first test unit and a second test unit. The first test unit represents a set of unit test methods which can carry out corresponding calling operation according to the class file name, and at least one test line which is called as a first test thread and used for constructing the source code by calling the first unit test method by a unit test device corresponding to the first test unit; the second test unit represents a set of unit test methods capable of performing corresponding calling operations according to the instruction type, and at least one test line called a second test thread, in which the unit test device corresponding to the second test unit calls the second unit test method to construct the source code.
Further, the first test thread and the second test thread constitute at least one test thread of the source code, and it is understood that the test thread constructing the source code may be one or more, and the test thread is composed of the first test thread and the second test thread.
Step 202 may also be said to include, according to the file name of the class file, calling a first unit test method corresponding to the file name to construct a first test thread corresponding to the first unit test method; calling a second unit test method corresponding to the instruction type according to the instruction type of the execution instruction to construct a second test thread corresponding to the second unit test method; the first test thread and the second test thread constitute at least one test thread of the source code.
And after the unit testing device constructs the testing thread of the source code to be tested, putting the testing thread into a thread pool to wait for execution.
Before the unit testing device executes the testing thread, an instantiation object and a corresponding parameter list are required to be created for the testing thread to be executed. Instep 203, the unit test device will create an instantiation object and a corresponding parameter list for each test thread according to the class file in the source code.
Specifically, the unit testing device determines an instantiation object corresponding to the class file according to the interface type of the class file in the source code and/or the file name of the class file, and then constructs a parameter list of the instantiation object according to the parameter type in the instantiation object.
The interface type of the class file is an API provided by Java technology, and the interface types assigned to each class file in the source code to be tested are different, so that the unit testing device can obtain an instantiation object of the class file according to the interface type of the class file.
The instantiated object corresponding to the class file is determined according to the name of the class file, and can be understood as the class file and the instantiated object corresponding to the class file, and the class file and the instantiated object are associated together through similar names, for example, the class file is a student class, and the instantiated object is student A, that is, the instantiated object corresponding to the class file can be obtained through names of students.
After the unit testing device obtains the instantiation object, a parameter list of the instantiation object is constructed according to the parameter type in the instantiation object, the parameter type field value corresponding to the parameter type field in the instantiation object is determined, and the parameter list of the instantiation object is constructed according to the parameter list type represented by the parameter type field value.
The parameter type field in the instantiation object is composed of a data type and a field name, that is, is composed of a "data type field name" and is used to describe attribute information of the instantiation object, such as the instantiation object student1, and the parameter type field in the instantiation object includes: a character type name field, an integer age field, etc., and the parameter type field value corresponding to the parameter type field value of student1 may be "zhang san, 23".
Specifically, the parameter list types include a Java data type list and a Java complex data type list, and the unit testing device constructs the parameter list of the instantiated object according to the parameter list type represented by the parameter type field value.
If the parameter type field value indicates that the parameter list type is a Java data type list, traversing an instantiation object according to a preset basic field type to obtain the field value of the instantiation object under each basic field type; and generating a parameter list of the instantiation object according to the basic field type and the field value of the basic field type.
If the parameter type field value indicates that the parameter list type is a Java complex data type list, traversing an instantiation object according to basic field types to obtain the field value of the instantiation object under each basic field type; assigning values to field values of the instantiation objects under the complex field types according to the relation of the field values of the instantiation objects under the basic field types; and generating a parameter list of the instantiation object according to the basic field type, the field value of the basic field type, the complex field type and the field value under the complex field type.
The Java data types comprise integer int, character string, Boolean bolean and the like; the Java complex data type means that an instantiation object is also contained in a parameter type field of the instantiation object, for example, a parameter type field of thestub 2 includes: "character type name", "integer age", "class address", wherein "class address" indicates another class, which includes parameter type fields of "character type province", "character type, city", "character type, cell unit, and house number".
As shown in table 1, table 1 shows a parameter list in which an instantiation object is a Java data type.
TABLE 1
| Type (B) | Value of |
| int | 0 |
| String | “0” |
| boolean | false |
| Date | new Date() |
| Timestamp | New Timestamp(System.currentTimeMillis()) |
| Map | new HashMap() |
Further, when the unit testing device detects that the parameter list type represented by the parameter type field value of the parameter list of the instantiated object is a Java data type, the instantiated object is traversed according to a preset basic field type, for example, the field of the character type name in thestudent 2 is traversed, and the field value of the instantiated object under the character type name type is obtained to be 0, and then, the unit testing device stores all the basic field types and the field values of the basic field types into a data list according to the sequence of the type name field values, so as to generate the parameter list of the instantiated object.
And when the unit testing device detects that the parameter list type is a Java complex data type, instantiating the parameter list of the basic field type according to the above, and acquiring the field value of the parameter of the basic field type.
Then, the unit testing device traverses the parameter type fields in sequence from top to bottom and gives corresponding field values to the parameter type fields according to the relationship of the field values of the instantiated objects under the types of the basic fields, wherein the relationship of the field values means that at least one of the type parameter fields of the instantiated objects is another instantiated object, and the other instantiated object also has corresponding type parameter fields.
Finally, the unit testing device sorts the parameter field types and the field values according to the sequence of the type name field values of the basic field types, the complex field types and the field values under the complex field types, and generates a parameter list of the instantiation object in a sorted result storage data list.
After the unit testing device finishes processing the parameter list of the instantiation objects of the threads to be tested, the unit testing device executes the test threads in sequence according to the waiting sequence of the test threads in the thread pool.
Specifically, the unit testing device generates a test log according to a test result obtained by performing a plurality of unit tests on the source code to be tested, and performs statistical analysis on the test log to obtain a test report.
Further, the unit testing device executes the testing thread of the source code to be tested according to the Java reflection mechanism, and then generates the testing result as a testing log, where the testing log includes information on whether the testing is successful or not, and if the testing is unsuccessful, the unit testing device stores the testing log in the disk according to data accumulated in the testing log or according to a certain time interval, so as to analyze the source code to be tested at a later time.
The Java reflection execution mechanism refers to finding out more corresponding test threads of the class files according to the class files.
The application provides a unit testing method. The method comprises the following steps: performing bytecode-based code analysis processing on a source code to be tested, determining a class file corresponding to the source code and an execution instruction corresponding to the class file, constructing at least one testing thread of the source code according to the file name of the class file and the execution instruction, and creating an instantiation object and a corresponding parameter list for each testing thread according to the class file in the source code; and executing each test thread on the source code according to the instantiation object and the corresponding parameter list to obtain a test result. The method improves the efficiency of unit testing.
Example two
Fig. 3 is a schematic structural diagram of a unit testing apparatus according to the present application, corresponding to the unit testing method of the present application. For ease of illustration, only the portions relevant to the present application are shown.
Referring to fig. 3, the unit testing apparatus includes:
theanalysis module 10 is configured to perform bytecode-based code analysis processing on a source code to be tested, and determine a class file corresponding to the source code and an execution instruction corresponding to the class file;
abuilding module 20, configured to build at least one test thread of the source code according to the file name of the class file and the execution instruction;
a creatingmodule 30, configured to create an instantiation object and a corresponding parameter list for each test thread according to the class file in the source code;
and thetest module 40 is configured to execute each test thread on the source code according to the instantiation object and the corresponding parameter list to obtain a test result.
Thebuilding block 20 is specifically configured to:
calling a first unit test method corresponding to the file name according to the file name of the class file to construct a first test thread corresponding to the first unit test method;
calling a second unit test method corresponding to the instruction type according to the instruction type of the execution instruction to construct a second test thread corresponding to the second unit test method;
the first test thread and the second test thread constitute at least one test thread of the source code.
The creatingmodule 30 is specifically configured to:
determining an instantiation object corresponding to the class file according to the interface type of the class file in the source code and/or the file name of the class file;
and constructing a parameter list of the instantiation object according to the parameter type in the instantiation object.
The creatingmodule 30 is specifically further configured to:
determining a parameter type field value corresponding to a parameter type field in the instantiation object;
and constructing a parameter list of the instantiation object according to the parameter list type represented by the parameter type field value.
The creatingmodule 30 is specifically further configured to:
if the parameter type field value indicates that the parameter list type is a Java data type list, traversing an instantiation object according to a preset basic field type to obtain the field value of the instantiation object under each basic field type; generating a parameter list of the instantiation object according to the basic field type and the field value of the basic field type;
if the parameter type field value indicates that the parameter list type is a Java complex data type list, traversing an instantiation object according to basic field types to obtain the field value of the instantiation object under each basic field type; assigning values to field values of the instantiation objects under the complex field types according to the relation of the field values of the instantiation objects under the basic field types; and generating a parameter list of the instantiation object according to the basic field type, the field value of the basic field type, the complex field type and the field value under the complex field type.
Thetest module 40 is specifically configured to:
performing a plurality of unit tests according to the source code to be tested to obtain a test result, and generating a test log;
and carrying out statistical analysis on the test logs to obtain a test report.
The implementation principle of the unit testing device provided by the present application is similar to that in any of the above embodiments, and is not described herein again.
The application provides a unit testing device. The method comprises the following steps: performing bytecode-based code analysis processing on a source code to be tested, determining a class file corresponding to the source code and an execution instruction corresponding to the class file, constructing at least one testing thread of the source code according to the file name of the class file and the execution instruction, and creating an instantiation object and a corresponding parameter list for each testing thread according to the class file in the source code; and executing each test thread on the source code according to the instantiation object and the corresponding parameter list to obtain a test result. The method improves the efficiency of unit testing.
EXAMPLE III
Fig. 4 is a schematic diagram of a unit testing system provided in the present application, corresponding to the unit testing method provided in the present application, and only the relevant parts are shown for convenience of explanation.
Referring to fig. 4, the unit test system includes:unit testing device 101, source code file to be tested 2.
The unit testing device is used for testing a source code to be tested, testing the source code to be tested by the unit testing method, and storing a testing result in a disk.
Example four
Fig. 5 is a schematic diagram of a hardware structure of the electronic device provided in the present application, and for convenience of description, only a part related to the present application is shown.
Referring to fig. 5, a schematic structural diagram of an electronic device 1000 suitable for implementing an embodiment of the present application is shown, where the electronic device 1000 may be a terminal device. Among them, the terminal Device may include, but is not limited to, a mobile terminal such as a mobile phone, a notebook computer, a Digital broadcast receiver, a Personal Digital Assistant (PDA), a tablet computer (PAD), a Portable Multimedia Player (PMP), a car mounted Device (e.g., car navigation terminal), etc., and a fixed terminal such as a Digital TV, a desktop computer, etc. The electronic device shown in fig. 5 is only an example, and should not bring any limitation to the functions and the scope of use of the embodiments of the present application.
As shown in fig. 5, the electronic device 1000 may include an output device (e.g., a central processing unit, a graphics processor, etc.) 1007 that may perform various appropriate actions and processes according to a program stored in a Read Only Memory (ROM) 1002 or a program loaded from astorage device 1008 into a Random Access Memory (RAM) 1003. In theRAM 1003, various programs and data necessary for the operation of the electronic apparatus 1000 are also stored. Theprocessing device 1001, the ROM1002, and theRAM 1003 are connected to each other by abus 1004. An input/output (I/O)interface 1005 is also connected tobus 1004.
Generally, the following devices may be connected to the I/O interface 1005:input devices 1006 including, for example, a touch screen, touch pad, keyboard, mouse, camera, microphone, accelerometer, gyroscope, etc.; anoutput device 1007 including, for example, a Liquid Crystal Display (LCD), a speaker, a vibrator, and the like;storage devices 1008 including, for example, magnetic tape, hard disk, and the like; and acommunication device 1009. Thecommunication device 1009 may allow the electronic device 1000 to communicate with other devices wirelessly or by wire to exchange data. While fig. 5 illustrates an electronic device 1000 having various means, it is to be understood that not all illustrated means are required to be implemented or provided. More or fewer devices may alternatively be implemented or provided.
In particular, according to embodiments of the application, the processes described above with reference to the flow diagrams may be implemented as computer software programs. For example, embodiments of the present application include a computer program product comprising a computer program embodied on a computer readable medium, the computer program comprising program code for performing the method illustrated by the flow chart. In such an embodiment, the computer program may be downloaded and installed from a network through the communication means 1009, or installed from the storage means 1008, or installed from theROM 1002. When executed by theprocessing device 1001, the computer program performs the above-described functions defined in the method of the embodiment of the present application.
It should be noted that the computer readable medium mentioned above in the present application may be a computer readable signal medium or a computer readable storage medium or any combination of the two. A computer readable storage medium may be, for example, but not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination of the foregoing. More specific examples of the computer readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
In the present application, a computer readable storage medium may be any tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. In this application, however, a computer readable signal medium may include a propagated data signal with computer readable program code embodied therein, for example, in baseband or as part of a carrier wave. Such a propagated data signal may take many forms, including, but not limited to, electro-magnetic, optical, or any suitable combination thereof. A computer readable signal medium may also be any computer readable medium that is not a computer readable storage medium and that can communicate, propagate, or transport a program for use by or in connection with an instruction execution system, apparatus, or device. Program code embodied on a computer readable medium may be transmitted using any appropriate medium, including but not limited to: electrical wires, optical cables, RF (radio frequency), etc., or any suitable combination of the foregoing.
The computer readable medium may be embodied in the electronic device; or may exist separately without being assembled into the electronic device.
The computer readable medium carries one or more programs which, when executed by the electronic device, cause the electronic device to perform the methods shown in the above embodiments.
A computer program product is provided for carrying out the operations of the present disclosure and may be written in any combination of one or more programming languages, including an object oriented programming language such as Java, Smalltalk, C + +, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code may execute entirely on the user's computer, partly on the user's computer, as a stand-alone software package, partly on the user's computer and partly on a remote computer or entirely on the remote computer or media library. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of Network, including a Local Area Network (LAN) or a Wide Area Network (WAN), or the connection may be made to an external computer (for example, through the Internet using an Internet service provider).
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units described in the embodiments of the present application may be implemented by software or hardware. Where the name of a unit does not in some cases constitute a limitation of the unit itself, for example, the first retrieving unit may also be described as a "unit for retrieving at least two internet protocol addresses".
The functions described herein above may be performed, at least in part, by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field Programmable Gate Arrays (FPGAs), Application Specific Integrated Circuits (ASICs), Application Specific Standard Products (ASSPs), systems on a chip (SOCs), Complex Programmable Logic Devices (CPLDs), and the like.
In the context of this application, a machine-readable medium may be a tangible medium that can contain, or store a program for use by or in connection with an instruction execution system, apparatus, or device. The machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any suitable combination of the foregoing. More specific examples of a machine-readable storage medium would include an electrical connection based on one or more wires, a portable computer diskette, a hard disk, a Random Access Memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
Other embodiments of the present application will be apparent to those skilled in the art from consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of the invention following, in general, the principles of the application and including such departures from the present disclosure as come within known or customary practice within the art to which the invention pertains. It is intended that the specification and examples be considered as exemplary only, with a true scope and spirit of the application being indicated by the following claims.
It will be understood that the present application is not limited to the precise arrangements 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 application is limited only by the appended claims.