Content of the invention
The embodiment of the present invention provides a kind of generation method of function test code and device, solves the application journey of prior art
There is the low technical problem of testing efficiency in sequence development process, improve testing efficiency.
The embodiment of the present application provides the generation method that a kind of function tests code, and methods described includes:
Arrange function test to bury a little to object function to be tested, the function test is buried a little as the sign object function
The position of each ingredient;
Buried a little based on function test, read and parse the code source file of the object function, obtain the target
The function name of function, function parameter, the value of function return type;
Based on the function name of the target, function parameter, function return type value, construct the survey of the object function
Trial function, and generate the test code file of the object function.
Optionally, described to object function arrange function test bury a little, including:
Function test is arranged to the function name of the object function, function parameter, return type respectively to bury a little.
Optionally, described test based on the function is buried a little, reads and parses the code source file of the object function, wrap
Include:
The source file that the object function is opened by document reading manner;
The source file of opening is converted to inlet flow, function test is obtained from inlet flow and buries burying and indicate a little
Symbol;
The target data between the corresponding original position mark of an indications and end position mark is buried described in reading;
The target data is returned class as the corresponding function name of an indications, function parameter or the function of burying
Type.
Optionally, described based on the function name of the target, function parameter, function return type value, construction is described
The test function of object function, including:
Based on the function name that the value of the function name of the object function constructs the test function;
Parsed by data interchange format JSON, the corresponding all parameters of the value of function parameter in the acquisition object function
Value, the function parameter for constructing the test function by all parameter values;
Arrange and receive and preserve the test letter with the value identical variable of the function return type of the object function
Several function return values.
Optionally, the test code file for generating the object function, including:
By the function name of the test function, function parameter, function return value and the printing function return value work(
The information write of energy is described to be tested in code file.
The embodiment of the present application also provides the generating means that a kind of function tests code, and described device includes:
Dot element being buried, being buried a little for function test is arranged to object function to be tested, it be a little table that the function test is buried
Levy the position of each ingredient of the object function;
Resolution unit, for burying a little based on function test, reads and parses the code source file of the object function,
Obtain the function name of the object function, function parameter, the value of function return type;
Signal generating unit, for based on the function name of the target, function parameter, function return type value, construct institute
The test function of object function is stated, and generates the test code file of the object function.
Optionally, described bury dot element, be used for:Respectively to the function name of the object function, function parameter, return class
Type arranges function test and buries a little.
Optionally, the resolution unit, including:
Subelement is opened, for the source file of the object function is opened by document reading manner;
Subelement is obtained, and for the source file of opening is converted to inlet flow, the function test is obtained from inlet flow
Bury a little bury an indications;
Subelement is read, for reading described burying between the corresponding original position mark of an indications and end position mark
Target data;The target data is returned as the corresponding function name of an indications, function parameter or the function of burying
Return type.
Optionally, the signal generating unit, including:
Title constructs subelement, and the value for the function name based on the object function constructs the letter of the test function
Several titles;
Parametric configuration subelement, for parsing by data interchange format JSON, obtains function ginseng in the object function
The corresponding all parameter values of several values, the function parameter for constructing the test function by all parameter values;
Return value constructs subelement, and the value identical variable for arranging with the function return type of the object function comes
Receive and preserve the function return value of the test function.
Optionally, the signal generating unit, is additionally operable to:The function name of the test function, function parameter, function are returned
Value and print the function return value function information write described test code file in.
Said one or multiple technical schemes in the embodiment of the present application, at least imitates with following one or more technology
Really:
The embodiment of the present application provides the generation method that a kind of function tests code, arranges function to object function to be tested
Test is buried a little, and buries a function name for reading and parsing object function, function parameter and function return class based on function test
The value of type, and then the value construction test function of the function name according to object function, function parameter and function return type life
Into test code file, it is achieved that test automatically generating for code, it is no longer necessary to which developer writes test code manually, so as to
The low technical problem of testing efficiency in the process of application development of prior art is solved, testing efficiency is improve.
Embodiment one
Fig. 1 is refer to, the embodiment of the present application provides the generation method that a kind of function tests code, and the method includes:
S101:Arrange function test to bury a little to object function to be tested, the function test is buried a little as the sign mesh
The position of each ingredient of scalar functions;
S102:Buried a little based on function test, read and parse the code source file of the object function, obtained described
The function name of object function, function parameter, the value of function return type;
S103:Based on the function name of the target, function parameter, function return type value, construct the target letter
Several test functions, and generate the test code file of the object function.
In specific implementation process, test code is automatically generated in order to side, the embodiment of the present application is first carried out S101
Carry out burying a setting in source code.Function test is set and buries and started with burying an indications
Test description information.Wherein, it is the word for distinguishing the variable or function of object function place application program to bury an indications
Symbol, and the operation of application program is not affected, for example:In Java grammers, variable or function etc. all can not be started using@, then
@can be used in java applet as burying an indications.It should be noted that the embodiment of the present application is not intended to limit buries an indications
Concrete form, the program for becoming language using difference, it is possible to use different buries an indications such as assumes # in C++ programs
Cannot be used for the beginning of variable or function, then can be using # as burying an indications in C++ programs.
Below with Junit unit testing frameworks under, carry out burying a little with the trial function to be measured in source code in java applet
As a example by, the setting of burying that the embodiment of the present application is provided is described in detail.
One function has generally comprised three ingredients:1st, function name;2nd, function parameter;3rd, function return value, so right
Function carries out burying when arranging, and can arrange function test respectively to function name, function parameter and function return type and bury a little,
The setting that is buried a little by these three function tests just can accurately identify a function very much, parse life in order to follow-up to which
Into corresponding test code.
Function test is buried a little is used for the position for characterizing each ingredient of object function, including:An indications is buried, and mark is buried
Show that the corresponding original position mark of symbol and end position are identified, for example:@funName:[function name] ,@funParam:[function
Parameter] ,@return:[return type], " [" and "] " is respectively the corresponding original position marks of@and end position is identified, that is, exist
Target data in " [" and "] " is the function data for needing to be read out and parse.
a)@funName:[function name]
Understand to bury for convenience and a little watch attentively rule, above-mentioned form is exemplified below:@funName:[addFun], it is right to represent
The function (addFun is specific function name) of addFun carries out burying a note, buries
Then analysis generates the corresponding test code of the function.
b)@funParam:[function parameter]
Likewise, form is exemplified below:@funParam:[A], representing carries out burying a note to parameter A, needs to the ginseng
Number A carries out parsing and generates corresponding test code.
By JSON, ((JavaScript Object Notation, are one to function parameter wherein in " [function parameter] "
The data interchange format of kind of lightweight) form to be being described.JSON can very easily describe one or more object
Specifying information.Very easily multiple parameters can be described clearly by JSON.
For example:Following form is describing the information of Person parameters:
JSON character strings above are explained as follows:
Param1:Param1 represents first parameter, and it is exactly that param is several to have several parameters.It is to the ginseng inside Param1
Several descriptions, the parameter in example in Param1 are Person.
Person:It is a customization type, the content inside following Person is the description to Person, wherein retouches
It is 10 years old to have stated age (age), and name (name) is tom, and height (height) is 180.
Param2:Param2 represents the second parameter of the incoming function, and this parameter only becomes comprising a hobby
Amount, its value is play.
c)@return:[return type]
If a function has return value so to carry out burying a little the return value by@return sentences, such as:@
return:[String], the declaration of will function of the words have return value, and return value is String types.
For the object function that is a little provided with is buried, execute S102 and read and parse the code source file of object function, have
Body implementation process is as follows:
Step 1, the source file for opening object function by document reading manner.
Wherein, specifically can be realized by File classes (tool-class that Java language JDK is carried), for example:By calling
File file=File (filePath) (File is method name, and filePath represents the absolute path that file is located), with regard to energy
Enough by corresponding for filePath File Open, and it is file by the file designation that opens.
Step 2, the source file of opening is converted to inlet flow, function test is obtained from inlet flow and buries burying and indicate a little
Symbol.
Specifically, fileInputStream=new FileInputStream (file) can be adopted, file is changed
Become inlet flow, and inlet flow be named as fileInputStream) for inlet flow fileInputStream, can adopt
Go to read a line in file line by line with readline methods (reading the functional based method of a full line of file corresponding to inlet flow)
Information.Using read line by line be exactly circulation go call the readline methods in fileInputStream, and will read
The data of current line are saved in the variable lineString of String types.
In lineString, by calling contains ("@") method, (the method major function is to judge the character string
In whether comprising this character string of@) judging whether this buries an indications comprising@in current line.If comprising@this
Indications, then continue the data for reading next line, as shown in Figure 2.The data for reading next line are equally called
Readline methods in fileInputStream.If containing this indications execution step 3 of@.
Step 3, reading bury the target data between the corresponding original position mark of an indications and end position mark.
Due to read this indications, illustrate in this line comprising the information for needing parsing.The step in then need
The position of indications in lineString to be determined.In lineString can by call indexOf ("@") function come
The positional information of positioning indications@, this function major function are to return to this character string position of@in lineString
Information (is assumed to represent this position with pos1).From the beginning of this position of pos1, then taken out by way of searching loop
All data within first " [] " next symbol, as shown in Figure 2.From the beginning of this position of pos1, a word is once taken out
Symbol (obtains the string value of pos1 using the function of charAt (pos1), each positional value adds 1 in cyclic process), then sentences
Break this character whether and " ["or"] ", the two characters are equal.And if " [" is equal, then record from that point on
Data below.And if "] " equal, then stop recording data.From the beginning of at the mark of starting position, to end position mark
Terminate at knowledge, the data for reading are exactly the truthful data value for needing to obtain, i.e. target data.
Step 4, using target data as burying the corresponding function name of an indications, function parameter or function return type
Value.If@followed by character string be funName, then the target data in@first " [] " symbol below is then
The value of function name;If@followed by character string be funParam, then the target in@first " [] " symbol below
Data value then for function parameter;If@followed by character string be return, then in@first " [] " symbol below
Target data then for function return type value.
It should be noted that:In order to subsequent descriptions are convenient, funName is come representative function title@for the embodiment of the present application
The concrete value information of funName;With returnValue come the concrete value information of representative function return type return;With
ParamValue carrys out the concrete value information of representative function parameter funParam.
After S102, values of the S103 based on the function name, function parameter, function return type for reading, construction is executed
The test function of object function, and generate the test code file of object function.
Fig. 3 is refer to, the test function of object function is constructed, is specifically included following steps:
S31:Based on the function name that the value of the function name of object function constructs test function.
Have determined that by the parsing of S102 above it is funName to need the object function title that tests, for Uniform Name
It is testfunName that rule can construct the title of test function, is exactly before interpolation test before original function name
Sew, its main purpose is for clear and definite test function function and Uniform Name rule.The embodiment of the present application is not intended to limit test letter
The concrete name of several function names, or t-funName, funNametest etc..
S32:Parsed by data interchange format JSON, the corresponding all parameters of the value of function parameter in acquisition object function
Value, the function parameter for constructing test function by all parameter values.
As function parameter is the JSON forms of standard, (current techique) can be parsed by JSON, be parsed
All of parameter value in paramValue, then constructs the function parameter of test function according to the value for parsing.Example
Such as:The information of above-mentioned Person parameters, the occurrence parsed in Person include:"age":"10","name":"tom","
height":180, then by the constructed fuction instantiation Person objects of Person out.
S33:Arrange and receive and preserve test function with the value identical variable of the function return type of object function
Function return value.
Have confirmed that what the returned data of object function is by returnValue, to this end it is possible to use, same type
Variable is carried out being received and saved in returnValue2 to the value of the return type of test function.Function return value is printed
Come, by the printf System.println (" returnValue2 ") of system, (this function major function is to print, will
Information in bracket is printed on control station), the return value of function is printed in control station.
By the construction of above-mentioned test function, the function name of test function, function parameter, function return value is obtained,
Next, generating the test code file of object function.Specifically, by the function name of above-mentioned test function, function parameter, letter
The information such as number return value and printing return value function is all written in file.The mode of write file is to pass through
FileWriter classes (tool-class that JDK is provided) are completed.By the write functions that call in FileWriter, (function is main
Function is write information in file), corresponding information is written in file.
The operation of write file is as follows:
1st, the constructed fuction by FileWriter classes obtains FileWriter objects, and constructed fuction is (for example:
FileWriter("C:\\demo1.java",false);) in need to transmit 2 parameters:First parameter:It is to need write text
The position of part, that is, the file for writing;Second parameter:Whether file continues (namely file appending write), if true
It is exactly to add write, if false is exactly to restart to write.
2nd, the constructed fuction of FileWriter obtains the object fw of a FileWriter after returning, and has one inside fw
Write functions, the major function of this function is write file operation.(for example:Fw.write (" writing some information "), this
Sample will be in the content of double quotation marks write file).
3rd, after file write is finished, inside fw, there is a close function, after calling close functions, system can be closed
Close the resources such as the resource occupation of corresponding file operation, the internal memory of releasing document read-write.
Achieve automatization in the generation method that code is tested by a kind of function that above-described embodiment is provided generate test
The function of code, can greatly save the time that developer writes test code, greatly improve the survey in development process
Examination efficiency.
Same inventive concept is based on, the embodiment of the present application is also tested the generation method of code, correspondingly carried based on above-mentioned function
For the generating means that a kind of function tests code, as shown in figure 4, the device includes:
Dot element 41 being buried, being buried a little for function test is arranged to object function to be tested, the function test is buried and is a little
Characterize the position of each ingredient of the object function;
Resolution unit 42, for burying a little based on function test, reads and parses the code source document of the object function
Part, the function name of the acquisition object function, function parameter, the value of function return type;
Signal generating unit 43, for based on the function name of the target, function parameter, function return type value, construction
The test function of the object function, and generate the test code file of the object function.
In specific implementation process, described bury dot element 41, be used for:Respectively to the function name of the object function, letter
Number parameter, return type arrange function test and bury a little.
The resolution unit 42 includes:Subelement is opened, subelement is obtained and is read subelement.Opening subelement is used for leading to
Cross the source file that document reading manner opens the object function;Obtaining subelement is used for for the source file of opening being converted to input
Stream, obtain from inlet flow function test bury a little bury an indications;Read subelement and be used for reading and described bury a sign
Accord with the target data between corresponding original position mark and end position mark;The target data is buried a mark as described
Show the corresponding function name of symbol, function parameter or function return type.
The signal generating unit 43, including:Title construction subelement, parametric configuration subelement and return value construction subelement.
Title construction subelement is used for the function name that the value of the function name based on the object function constructs the test function;Ginseng
Number construction subelement is used for parsing by data interchange format JSON, and the value for obtaining function parameter in the object function is corresponding
All parameter values, the function parameter for constructing the test function by all parameter values;Return value construction subelement is used for
Arrange the function of the test function is received and preserve with the value identical variable of the function return type of the object function
Return value.
The signal generating unit 43 generate test code file when, specifically for:By the function name of the test function,
The information write of function parameter, function return value and the printing function return value function is described to be tested in code file.
Device in regard to above-described embodiment, wherein unit execute the concrete mode of operation in relevant the method
Embodiment in be described in detail, explanation will be not set forth in detail herein.
One or more technical schemes in by the embodiment of the present application, it is possible to achieve one or more technology are imitated as follows
Really:
The embodiment of the present application provides the generation method that a kind of function tests code, arranges function to object function to be tested
Test is buried a little, and buries a function name for reading and parsing object function, function parameter and function return class based on function test
The value of type, and then the value construction test function of the function name according to object function, function parameter and function return type life
Into test code file, it is achieved that test automatically generating for code, it is no longer necessary to which developer writes test code manually, so as to
The low technical problem of testing efficiency in the process of application development of prior art is solved, testing efficiency is improve.
Those skilled in the art are it should be appreciated that embodiments of the invention can be provided as method, system or computer program
Product.Therefore, the present invention can adopt complete hardware embodiment, complete software embodiment or with reference to software and hardware in terms of reality
Apply the form of example.And, the present invention can be adopted in one or more computers for wherein including computer usable program code
The upper computer program that implements of usable storage medium (including but not limited to disk memory, CD-ROM, optical memory etc.) is produced
The form of product.
The present invention is the flow process with reference to method according to embodiments of the present invention, equipment (system) and computer program
Figure and/or block diagram are describing.It should be understood that can be by computer program instructions flowchart and/or each stream in block diagram
Journey and/or the combination of square frame and flow chart and/or the flow process in block diagram and/or square frame.These computer programs can be provided
Instruct the processor of general purpose computer, special-purpose computer, Embedded Processor or other programmable data processing devices to produce
A raw machine so that produced for reality by the instruction of computer or the computing device of other programmable data processing devices
The device of the function of specifying in present one flow process of flow chart or one square frame of multiple flow processs and/or block diagram or multiple square frames.
These computer program instructions may be alternatively stored in and can guide computer or other programmable data processing devices with spy
Determine in the computer-readable memory that mode works so that the instruction being stored in the computer-readable memory is produced to be included referring to
Make the manufacture of device, the command device realize in one flow process of flow chart or one square frame of multiple flow processs and/or block diagram or
The function of specifying in multiple square frames.
These computer program instructions can be also loaded in computer or other programmable data processing devices so that in meter
Series of operation steps is executed on calculation machine or other programmable devices to produce computer implemented process, so as in computer or
The instruction executed on other programmable devices is provided for realization in one flow process of flow chart or multiple flow processs and/or block diagram one
The step of function of specifying in individual square frame or multiple square frames.
, but those skilled in the art once know basic creation although preferred embodiments of the present invention have been described
Property concept, then can make other change and modification to these embodiments.So, claims are intended to be construed to include excellent
Select embodiment and fall into the had altered of the scope of the invention and change.
Obviously, those skilled in the art can carry out the essence of various changes and modification without deviating from the present invention to the present invention
God and scope.So, if these modifications of the present invention and modification belong to the scope of the claims in the present invention and its equivalent technologies
Within, then the present invention is also intended to comprising these changes and modification.