Что бы протестировать метод Divide нужно 4 шага
Пример функции которую будем тестировать:
Tfrm_Calc = class (TForm ) public Function Divide(iA,iB:integer):integer; ... Function Tfrm_Calc.Divide(iA,iB:integer):integer; begin result:=iA div iB; end;
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs //************* Start Copy ***************** {$IFDEF TESTME} ,TestMe,TestMe_Types ,TestCodeType, TestCodeLevelUnits {$ENDIF} //************* End Copy ******************* ;
type Tfrm_Calc = class (TForm ) //************* Start Copy ***************** {$IFDEF TESTME} private class var F_pIndexClass:TPIndexValue; class var F_pIndexUnit:TPIndexValue; class var F_pIndexApp:TPIndexValue; class Procedure _RegisterMethod; class Function _TestMethods( IMethod:IRunMethodParam):TTestResultParam; {$ENDIF} //************* End Copy ******************* public
implementation {$R *.dfm} Function Tfrm_Calc.Divide(iA,iB:integer):integer; begin result:=iA div iB; end; //************* Start Copy ***************** {$IFDEF TESTME} type TTestMethod = (tmDiv); { template Type TTestMethod=( // --------------- 1 sample ------------- // base methods tmCreate // 1 execute ,tmInit // 2 execute ,tmDone // MAX_POS_TESTMETHOD -1 // pre last execute ,tmFree // MAX_POS_TESTMETHOD // last execute // methods for test ,tmDiv ,tmMul ); // --------------- 2 sample ------------- // with prepare data for test TTestMethod=( // base methods tmDataTestPrepare // 1 execute If need prepare data for tests tmCreate // 2 execute ,tmInit // 3 execute ,tmDone // MAX_POS_TESTMETHOD -2 // pre pre last execute ,tmFree // MAX_POS_TESTMETHOD -1 // pre last execute tmDataTestFree // MAX_POS_TESTMETHOD // last execute // methods for test ,tmDiv ,tmMul ); } class procedure TCalc._RegisterMethod; begin if not GetTestMe.bIsTestCode then exit; with GetTestMe.ITestCode do begin if not SetPosClassUnitApp(F_pIndexClass,F_pIndexUnit,F_pIndexApp) then exit; { // ******************************************** // --------------- 1 sample ------------- // ******************************************** // template create-free // 1,2,3,MAX_POS_TESTMETHOD-1,MAX_POS_TESTMETHOD - absolute order execution RegMethod(tmCreate,'tmCreate','tmCreate' ,pAddOneEmptyParam,1); // 1 execute RegMethod(tmInit, 'tmInit' ,'tmInit' ,pAddOneEmptyParam,2); // 2 execute RegMethod(tmDone, 'tmDone' ,'tmDone' ,pAddOneEmptyParam,MAX_POS_TESTMETHOD-1); // Pre last call RegMethod(tmFree, 'tmFree' ,'tmFree' ,pAddOneEmptyParam,MAX_POS_TESTMETHOD); // last call // RegMethod(tmUserMethod, 'tmUserMethod' ,'tmUserMethod'); // write with defaults parameters // RegParam([1,2],[3],'write logic test'); // True result // ******************************************** // --------------- 2 sample ------------- // ******************************************** // template create-free - USE EXTERNAL DATA FOR TEST // 1,2,3,MAX_POS_TESTMETHOD-1,MAX_POS_TESTMETHOD - absolute order execution RegMethod(tmDataTestPrepare,'tmDataTestPrepare','tmDataTestPrepare' ,pAddOneEmptyParam,1); // 1 execute RegMethod(tmCreate,'tmCreate','tmCreate' ,pAddOneEmptyParam,2); // 2 execute RegMethod(tmInit, 'tmInit' ,'tmInit' ,pAddOneEmptyParam,3); // 3 execute RegMethod(tmDone, 'tmDone' ,'tmDone' ,pAddOneEmptyParam,MAX_POS_TESTMETHOD-2); // Pre Pre last call RegMethod(tmFree, 'tmFree' ,'tmFree' ,pAddOneEmptyParam,MAX_POS_TESTMETHOD-1); // Pre last call RegMethod(tmDataTestFree,'tmDataTestFree','tmDataTestFree' ,pAddOneEmptyParam,MAX_POS_TESTMETHOD); // Last call // RegMethod(tmUserMethod, 'tmUserMethod' ,'tmUserMethod'); // write with defaults parameters // RegParam([1,2],[3],'write logic test'); // True result } RegMethod(tmDiv ,'tmDiv' ,'Test Div operation');// Test methods RegParam([15,5],[3],'15 div 5=3'); // True result RegParam([2,2],[1],'2 div 2=1'); // True result // опишем, но реализовывать не будем RegMethod(tmMul ,'tmMul' ,'Test Mul operation');// Test methods RegParam([15,5],[75],'15 * 5=75'); // True result RegParam([9,9],[9],'3 * 3=9'); // True result end; //withend; end; class function TCalc._TestMethods( IMethod: IRunMethodParam): TTestResultParam; begin case TTestMethod(IMethod.GetTestMethod) of tmDiv: begin result:=trError; if (Form1.divide(IMethod.IGetArrParam(0).AsInteger,IMethod.IGetArrParam(1).AsInteger) = IMethod.IGetArrResultForTestMethod(0).AsInteger) then result:=trOk; end; tmMul: begin // для идей или будущего функционала // которые нужно вписать в план для реализации result:=trTestNotCreated; end; end; // case end;
//************* Start Copy ***************** initialization if GetTestMe.bIsTestCode then begin with GetTestMe.ITestCode do begin TForm1.F_pIndexApp := RegApp(HInstance,'Tested My First App Test'); TForm1.F_pIndexUnit := RegUnit(ltApplication,'Unit1','Unit1 registeger'); TForm1.F_pIndexClass := RegClass(TForm1._RegisterMethod,TForm1._TestMethods,'TForm1','Test many reg methods'); end; end; {$ENDIF} //************* End Copy ******************* end.
Итоговый тест приложения (видео)
-