IdTest.pas 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. unit IdTest;
  2. {
  3. uses rtti helper from
  4. http://chris.lichti.org/Lab/RTTI_Lib/RTTI_Lib.shtml
  5. demonstrates how to use rtti to discover and call published methods
  6. }
  7. interface
  8. uses
  9. classes,
  10. sysutils,
  11. clRtti,
  12. btTest;
  13. type
  14. TIdTest = class;
  15. TIdTestClass = class of TIdTest;
  16. TIdTestMethod = procedure of object;
  17. //TIdTest adapts indy's test classes to my test framework
  18. TIdTest = class(TbtTest)
  19. protected
  20. //this is what my test framework calls
  21. procedure btDoTest;override;
  22. public
  23. //this is the only method that has to be provided
  24. class procedure RegisterTest(const aClass:TbtTestClass);
  25. end;
  26. implementation
  27. procedure TIdTest.btDoTest;
  28. var
  29. i:Integer;
  30. aList:TStringList;
  31. aMethod:TMethod;
  32. aObjMethod:TIdTestMethod;
  33. begin
  34. inherited;
  35. aList:=TStringList.Create;
  36. try
  37. //build list of published methods for this class
  38. GetMethodList(Self,aList);
  39. //for each method, check it runs without failure
  40. for i:=0 to aList.Count-1 do
  41. begin
  42. aMethod.Code:=Pointer(aList.Objects[i]);
  43. aMethod.Data:=Self;
  44. aObjMethod:=TIdTestMethod(aMethod);
  45. //run the test
  46. //aTest.DoBeforeTest;
  47. try
  48. btExpectPass(aObjMethod);
  49. finally
  50. // aTest.DoAfterTest;
  51. end;
  52. end;
  53. finally
  54. FreeAndNil(aList);
  55. end;
  56. end;
  57. class procedure TIdTest.RegisterTest(const aClass:TbtTestClass);
  58. begin
  59. //singleton list where test classes are registered in my framework
  60. SbtTestClassList.Add(aClass);
  61. end;
  62. end.