Summary of the invention
In view of this, the technical problem to be solved in the present invention provides a kind of unit test system and method, can reduce in the unit testing process driving function and write workload, and the realization difficulty of this method is less, can realize under various operating systems, translation and compiling environment.
For realizing the technical problem to be solved in the present invention, unit test system of the present invention comprises: unit under test, buffer cell, general driving unit, unit of testing and controlling, information extraction unit; Described information extraction unit is input with the unit under test, is used to extract the information of unit under test; Described unit of testing and controlling is used for the information of the unit under test of recalls information extraction unit acquisition, and before test the information of calling is put into buffer cell, and controls described general driving unit and test; The general driving unit obtains information from the buffering unit, and puts into the test environment of setting, calls tested function, and output valve is put into buffer cell; Described unit of testing and controlling also is used for extracting the judgement that output valve is finished test result from the buffering unit.
Preferably, the information extracted from unit under test of described information extraction unit comprises: the data structure of unit under test, function parameter, rreturn value, global variable.
Preferably, also comprise the symbol table unit, be used for the information that the packaging information extraction unit extracts, and external interface is provided.
Preferably, described information extraction unit is specifically extracted the information of unit under test by the static scanning analysis mode.
The unit test method that the present invention proposes comprises following treatment step:
1) information extraction unit information extraction from unit under test;
2) unit of testing and controlling is used for recalls information extraction unit acquired information and deposits buffer cell in;
3) the general driving unit obtains information from the buffering unit, and puts into the test environment of setting, calls tested function, carries out test process, and deposits output valve in buffer cell;
4) unit of testing and controlling also is used for extracting the judgement that output valve is finished test result from the buffering unit.
Preferably, the information of extracting in the described step 1) comprises: the data structure of unit under test, function parameter, rreturn value, global variable.
Preferably, comprise also after the described step 1) that information extraction unit deposits the information of extracting in the treatment step of symbol table unit.
Preferably, information extraction unit adopts the static scanning analysis mode to extract the information of unit under test in the described step 1).
The unit test system and the method that adopt the present invention to propose can effectively improve the efficient and the quality of unit testing, reduce human cost, and test case can constantly revise multiplexingly, significantly reduce the duplication of labour.Specifically comprise following advantage:
1, by tested module is extracted structural information, make the user not need to be concerned about the structure of program;
2, driving function has only one, does not need to write a large amount of driving functions and test script, has reduced the technical ability requirement to the tester;
3, responsibility is clear, and the user only need provide function needed inputoutput data, and other are all handled by the general driving function, help realizing the unit testing of data-driven, and reusability and dirigibility improve greatly.
If 4 by visualization interface editor inputoutput data, ease for use will improve greatly.
5, after tested program once compiles, can create and move a plurality of test cases flexibly, reduce the compiling number of times of system;
6, tested program can be on target machine, also can be on PC, and can the multiple test environment of flexible adaptation.
Embodiment
Core concept of the present invention is: replace a large amount of driving functions with a general driving function, and with the direct access program storehouse of the method for similar compiler, thus can be the different driving function unification of various statements to same driving function.
For making purpose of the present invention, technology and advantage clearer, the present invention is described in further detail below in conjunction with accompanying drawing.
Fig. 1 shows according to unit test system modular structure synoptic diagram of the present invention, and the main modular structure among the present invention has been described.The present invention includes general driving unit 101, buffer cell 102, information extraction unit 103, symbol table unit 104, unit under test 105, unit of testing and controlling 106.
Wherein, general driving unit 101 is major parts of the present invention, and its main and buffer cell 102 carries out alternately.It (comprising: the value of parameter, global variable), by certain method value is put into current execution environment, call tested function, and the output valve of tested function (is comprised from the input value that buffering unit 102 obtains tested function; Rreturn value, go out the value of ginseng, global variable) put into buffer cell 102.
Buffer cell 102 modules provide an information that is used for storage function parameter, global variable, rreturn value, carry out before, carry out back result's buffer zone, and the API that visits this buffer zone, this module has solved the storage problem of data.Mainly comprised following field in the buffer zone structure: the index of Index-parameter, the title of VarName-global variable, size-shared byte-sized, pointerflag-whether be pointer (array and pointer are handled equally), data-be used for the depositing data block of numerical value.Unit of testing and controlling 106 need be put into buffer zone to the value of tested function parameters, global variable before test; After carrying out general driving unit 101, unit of testing and controlling 106 need obtain from buffering unit 102 tested function rreturn value, go out the value of ginseng, global variable, whether judged result correct.
Information extraction modules 103 is input with unit under test 105, by unit under test 105 being analyzed (as the tested module source file is carried out the static scanning analysis, but the present invention also goes for other forms of analytic process), obtain data structure, function parameter, the rreturn value of unit under test (105), the information of global variable, and by these information of symbol table unit 104 accesses, these information are bases of general driving unit 101 operations.
Symbol table unit 104 has encapsulated the information that information extraction unit 103 is obtained, and external interface is provided, and obtains the information such as type, size of tested function parameters, rreturn value, global variable for other modules.
The tested function of unit under test 105 representatives was the source file that has comprised one group of tested function before compiling, was one group of binary code that is generated by tested function source file after compiling.
Unit of testing and controlling 106 is to realize the module of test control, its recalls information extraction unit 103 obtains data structure, function parameter, the rreturn value of unit under test 105, the information of global variable, before test, the input value of tested function (value of parameter, global variable) is put into buffer cell 102, and calling back, general driving unit 101 obtains tested function from buffering unit 105 rreturn value, the value that goes out ginseng, global variable, whether judged result is correct.Because the design of buffer cell has been arranged, unit of testing and controlling is easy to accomplish the automatic test of data-driven.
Go out as shown in Figure 2 for general driving unit flowchart, comprise the steps:
Step 201 is obtained function address according to function name, is designated as FuncAddr.According to different operating system, different language, different compilers realize that this step has diverse ways.The API of some operating system as vxworks itself just provides, some operating system such as windows directly do not provide API, but can obtain function address by .exe or the .map file of analyzing the compiler generation.Because this step is not a key content of the present invention, so no longer describe in detail.
Step 202 is searched buffer cell, obtains the tested function parameter that presets, the value of global variable.
Among the present invention, buffer cell provides following API to carry out the setting of buffer zone and to read:
Read the function parameter value in the buffer cell;
Function parameter value in the buffer cell is set;
Read the global variable value in the buffer zone;
Global variable value in the buffer zone is set;
Step 203 is obtained tested function parameters and global variable information from the buffering unit, and its global variable value is set, and will go into ginseng and be pressed into the program current stack according to certain rule.
Global variable is the same as with function globak symbol, is assigned the address when compiling, reads its prevalue from buffer zone, and the value that this global variable is set is a prevalue.
Tested function parameters is set visits the storehouse of this tested function, the parameter value when carrying out so that it to be set.Stack accessing is by top-of-stack pointer ESP visit, and parameter is stacked according to the about definite sequence of tested function calls, and safeguards top-of-stack pointer simultaneously, guarantees the correctness of storehouse.The detail and the compiler in this step are closely related, with byte-aligned and different variation of function call agreement.
Below with byte alignment, _ _ the cdecl method of calling is that example describes step 203 in detail:
1. for n parameter of tested function, to first parameter, be done as follows successively from n parameter:
(1) obtains the buffer zone parabuffer of this parameter, obtain the shared byte number size of this parameter;
(2), then that the address of the data data block of parabuffer is stacked if this parameter is a pointer.The stack manipulation of this parameter is finished, and gets back to 1, carries out the stack manipulation of next parameter;
(3) if the parameter size is not 4 integral multiple, the integral multiple of polishing to 4: size=size+ (4-size%4) then;
(4) execution command sub esp, size is about to top-of-stack pointer and reduces size;
(5) because the push instruction can only be to data manipulation in 4 bytes, consider big byte parameter, we adopt the mode that is similar to memcpy, the whole code stream of this parameter is stacked, in order to prevent that the memcpy function call from exerting an influence to storehouse, directly use assembly language to substitute the memcpy function and realize memory copying.
(6) the shared total bytes of all parameters of tested function that adds up is designated as AllSize;
2. for each global variable in the tested function, do following operation successively:
(1) obtains the buffer zone of this global variable, be designated as pGlovar;
(2) obtain the address of this global variable, be designated as GlovarAdd;
(3) if this global variable is a pointer, then the address is copied among the GlovarAdd, otherwise the code stream of global variable is copied among the GlovarAdd.
Step 204 is carried out this tested function, and code is _ _ asm call FuncAddr.
Step 205 is obtained global variable, is gone out ginseng, function return value, deposits buffer zone in, and recovers the function storehouse;
The following describes the detailed process of step 205:
1. for the global variable of tested function, be done as follows successively:
(1) if this global variable is a pointer, deposited the result after carrying out at this moment in the data block of this global variable buffer zone, need not other operations, skip this global variable;
(2) if this global variable is not a pointer, need obtain the value of global variable, deposit back in the buffer zone.
2. for the value that goes out ginseng, obviously, having only parameter is under the situation of pointer, and this parameter could be as going out to join, and in the case, has deposited the result after the execution of this parameter in the data block of the buffer zone of this parameter.
3. the step of obtaining function return value is as follows:
(1) obtains the buffer zone of tested function return value, be designated as reBuffer;
(2) obtain the address of register eax: _ _ asm mov retval, eax
(3) if function return value is a pointer, then the value in the eax is deposited in the data block of reBuffer;
(4), directly should value take out and deposit buffering in and get final product if the shared byte number of function return value less than 4 bytes, has at this moment been deposited the value of rreturn value in the register eax;
(5) if the shared byte number of function return value greater than 4 bytes, at this moment deposits in the register eax is the address of function return value, need deposit the taking-up of the value this address in buffer zone;
4. recovery function address
Use the shared total bytes AllSize of all parameters of tested function of record in the step 203 to adjust storehouse, pseudo-code is: _ _ asm add esp, AllSize.
With the INTEL processor, the C language illustrates in the above description, does not represent the restriction to range of application of the present invention.
By the introduction of above modular structure figure and general driving function flowchart, main thought of the present invention has been set forth clear.Using a general driving function to substitute a large amount of driving functions is fleshes and bloods of the present invention, and the present invention has multiple different scheme when realizing, related herein specific implementation is just wherein a kind of.
Above content be in conjunction with concrete preferred implementation to further describing that the present invention did, can not assert that concrete enforcement of the present invention is confined to these explanations.For the general technical staff of the technical field of the invention, without departing from the inventive concept of the premise, can also make some simple deduction or replace, all should be considered as belonging to protection scope of the present invention.