Table of Contents

Quick start (details)

Here you will learn how to test methods correctly.

Final application test (video)

-

Download and try

Download Now

Project settings

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.

define.jpg

How to register a class for testing

1. Let's create a mini project for testing. Division operation DIV (видео)

2. Let's add code for testing (video)

2.1. Uses plug in modules TestMe

uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs
       {$IFDEF TESTME}
        ,TestMe,TestMe_Types
        ,TestCodeType, TestCodeLevelUnits
       {$ENDIF}
    ;

2.2. Define 2 methods for class registration and testing _RegisterMethod и _TestMethods

type
    Tfrm_Calc = class (TForm )
       {$IFDEF TESTME}
         private
           class var F_pIndexClass:TPIndexValue;
           class Procedure _RegisterMethod;
           class Function _TestMethods( IMethod:IRunMethodParam):TTestResultParam;
         {$ENDIF}
    public

2.3.Identify and code the methods you will be testing

public
       Function Divide(iA,iB:integer):integer;
 ....
function TForm1.Divide(iA, iB: integer): integer;
begin
 result:=iA div iB;
end;

2.4. Let's define a block of code for testing

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)