110 likes | 272 Views
Programmeerimine Delphi keskkonnas MTAT.03.214. Jelena Zaitseva jellen@ut.ee. D U nit - testing framework for Borland Delphi programs http://dunit.sourceforge.net/. Main idea. While developing program code, develop appropriate verification tests at the same time.
E N D
Programmeerimine Delphi keskkonnasMTAT.03.214 Jelena Zaitseva jellen@ut.ee
DUnit - testing framework for Borland Delphi programs http://dunit.sourceforge.net/
Main idea • While developing program code, develop appropriate verification tests at the same time. • Keep your tests up-to-date and run them at regular intervals
Main idea • While developing program code, develop appropriate verification tests at the same time. • Keep your tests up-to-date and run them at regular intervals • Benefits: • easiness in producing reliable code • confidence in existing code stability • writing tests also helps you to plan your code structure
Getting started • download DUnit from https://sourceforge.net/projects/dunit/ • in the Delphi IDE set path to DUnit source: Tools Environment Options Library
Example: first testing project (1/3) • create a new application • close Unit1.pas (that Delphi automatically generates) without saving it • save new project as FirstTestProject.dpr • create a new unit (FirstTestCase.pas): File New Unit. • save it as something like FirstTestCases.pas • declare a class TFirstTestCase derived from TTestCase • implement a single method TestFirst • register the TTestCaseFirst class with the DUnit framework unit FirstTestCase;interfaceusesTestFrameWork;type TFirstTestCase = class(TTestCase)published procedure TestFirst;end;implementationprocedure TFirstTestCase.TestFirst;begin Check(1 + 1 = 2, 'Catastrophic arithmetic failure!');end;initialization TestFramework.RegisterTest(TFirstTestCase.Suite);end.
Example: first testing project (1/3) • create a new application • close Unit1.pas (that Delphi automatically generates) without saving it • save new project as FirstTestProject.dpr • create a new unit (FirstTestCase.pas): File New Unit. • save it as something like FirstTestCases.pas • declare a class TFirstTestCase derived from TTestCase • implement a single method TestFirst • register the TTestCaseFirst class with the DUnit framework unit FirstTestCase;interfaceuses TestFrameWork;type TFirstTestCase = class(TTestCase)published procedure TestFirst;end;implementationprocedure TFirstTestCase.TestFirst;beginCheck(1 + 1 = 2, 'Catastrophic arithmetic failure!');end;initialization TestFramework.RegisterTest(TFirstTestCase.Suite);end.
Example: first testing project (2/3) • open the project's source (Project View Source)and edit it • add TestFrameWork and GUITestRunner to the uses clause • Remove the default Application code, and replace it with the one: program FirstTestProject; uses Forms, TestFrameWork, GUITestRunner, FirstTestCase in 'FirstTestCase.pas'; {$R *.res} begin GUITestRunner.runTest(TestFramework.registeredTests); end.
Example: first testing project (3/3) • run the program no errors occur: error exists:
Test existing projects • create a new application • close Unit1.pas (that Delphi automatically generates) without saving it • save new project as Test<ExistingProject>.dpr(to the same directory where <ExistingProject>.dpr is located) • create new unit(s)for testing. • save it (them)according to testing elements(Test<ClassName>.pas) • declare a class TTest<ClassName> derived from TTestCase • add units (of <ExistingProject>.dpr) to be tested to the project Test<ExistingProject>.dpr: Project Add to Project... • implement test-methods • register the TTest<ClassName> with the DUnit framework • open the project's source (Project View Source)and edit it • add TestFrameWork and GUITestRunner to the uses clause • Remove the default Application code, and replace it with the GUITestRunner.runTest(TestFramework.registeredTests); • run the program (i.e. tests)
Test suites • Test suites– tests that can contain other tests (including other test suites) RegisterTest('Simple suite', TTestTrivial1.Suite);RegisterTest('Simple suite', TTestTrivial2.Suite); function SomeTrivialTests: ITestSuite;begin Result := TTestSuite.Create('Some trivial tests', [ TTestTrivial1.Suite, TTestTrivial2.Suite ]);end; initialization RegisterTest('Simple Trivial Test', SomeTrivialTests);end.