Here you will learn how to test methods correctly.
Final application test (video)
-
Set DEFINE in project options.
Define in options «Conditionals Define» - TESTME to test and remove when you want to disable the test code in the app.
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs {$IFDEF TESTME} ,TestMe,TestMe_Types ,TestCodeType, TestCodeLevelUnits {$ENDIF} ;
type Tfrm_Calc = class (TForm ) {$IFDEF TESTME} private class var F_pIndexClass:TPIndexValue; class Procedure _RegisterMethod; class Function _TestMethods( IMethod:IRunMethodParam):TTestResultParam; {$ENDIF} public
public Function Divide(iA,iB:integer):integer; .... function TForm1.Divide(iA, iB: integer): integer; begin result:=iA div iB; end;
1. Let's limit the DEFINE block for testing
2. We will describe the methods that will be tested, a full description in paragraphs 4 and 5.
{$IFDEF TESTME} type TTestMethod = (tmDiv); class procedure Tfrm_Calc._RegisterMethod; begin end; class function Tfrm_Calc._TestMethods(IMethod: IRunMethodParam): TTestResultParam; begin end;
3. Register the module that will be tested in the initialization block
initialization if GetTestMe.bIsTestCode then begin with GetTestMe.ITestCode do begin TForm1.F_pIndexClass := IRegisterClass(TForm1._RegisterMethod,TForm1._TestMethods,'TForm1','Test many reg methods'); end; end; {$ENDIF} end.
4. Register methods that will test the procedure _RegisterMethod.
class procedure TCalc._RegisterMethod; begin if not GetTestMe.bIsTestCode then Exit; with GetTestMe.ITestCode do begin if not ISetDefaultIndClassUnitApp(F_pIndexClass) then exit; // Set position IRegisterMethod(tmDiv ,'tmDiv' ,'Test Div operation');// Test methods IRegisterMethodParams([15,5],[3],'15 div 5=3'); // True result IRegisterMethodParams([2,2],[1],'2 div 2=1'); // True result IRegisterMethodParams([4,4],[4],'4 div 4<>4 True=Error'); // Error IRegisterMethodParams([4,4],[4],'4 div 4=4 trError it''s Ok',trError); // Error = Ok IRegisterMethodParams([4,0],[4],'4 div 0 (!) Exception'); // Exception IRegisterMethodParams([4,0],[4],'4 div 0 (!) trException it''s Ok',trException); // Exception = Ok end; //withend; end;
Video with brief description:
5. Кодируем сам тест в функции _TestMethods.
class function TCalc._TestMethods( IMethod: IRunMethodParam): TTestResultParam; begin case TTestMethod(IMethod.IGetNumParamUser) of tmDiv: begin if (Form1.divide(IMethod.IGetArrParam(0).AsInteger,IMethod.IGetArrParam(1).AsInteger) = IMethod.IGetArrResultForTestMethod(0).AsInteger) then result:=trOk else result:=trError; end; end; // case end;
Final application test (video)