вторник, 17 марта 2015 г.

Testing of calculator №3. Expanding of test coverage

Original in Russian: http://programmingmindstream.blogspot.com/2014/02/3.html

Table of contents

All source code is available here.
In the previous series, we’ve implemented the test of ADDITION.

Right now, let’s add three more tests – tests of SUBTRACTION, MULTIPLICATION and DIVISION.

First, let’s make over our TPlusTest
We create one more ABSTRACT test - TOperationTest:

unit OperationTest;
 
interface
 
uses
  CalculatorGUITest,
  MainForm
  ;
 
type
  TOperation = (opAdd, opMinus, opMul, opDiv);
 
  TOperationTest = class(TCalculatorGUITest)
   protected
    procedure VisitForm(aForm: TfmMain); override;
    function  GetOp: TOperation; virtual; abstract;
  end;//TOperationTest
 
implementation
 
uses
  TestFrameWork,
  SysUtils
  ;
 
procedure TOperationTest.VisitForm(aForm: TfmMain);
const
 aA = 10;
 aB = 20;
begin
 aForm.Edit1.Text := IntToStr(aA);
 aForm.Edit2.Text := IntToStr(aB);
 case GetOp of
  opAdd:
  begin
   aForm.Button1.Click;
   Check(StrToFloat(aForm.Edit3.Text) = aA + aB);
  end;
  opMinus:
  begin
   aForm.Button2.Click;
   Check(StrToFloat(aForm.Edit3.Text) = aA - aB);
  end;
  opMul:
  begin
   aForm.Button3.Click;
   Check(StrToFloat(aForm.Edit3.Text) = aA * aB);
  end;
  opDiv:
  begin
   aForm.Button4.Click;
   Check(StrToFloat(aForm.Edit3.Text) = aA / aB);
  end;
 end;//case GetOp
end;
 
end.

TPlusTest now looks as follows:

unit PlusTest;
 
interface
 
uses
  OperationTest
  ;
 
type
  TPlusTest = class(TOperationTest)
   protected
    function  GetOp: TOperation; override;
  end;//TPlusTest
 
implementation
 
uses
  TestFrameWork,
  SysUtils
  ;
 
function TPlusTest.GetOp: TOperation;
begin
 Result := opAdd;
end;
 
initialization
 TestFramework.RegisterTest(TPlusTest.Suite);
 
end.

At this point, we add TMinusTest, TMulTest and TDivTest:

unit MinusTest;
 
interface
 
uses
  OperationTest
  ;
 
type
  TMinusTest = class(TOperationTest)
   protected
    function  GetOp: TOperation; override;
  end;//TMinusTest
 
implementation
 
uses
  TestFrameWork,
  SysUtils
  ;
 
function TMinusTest.GetOp: TOperation;
begin
 Result := opMinus;
end;
 
initialization
 TestFramework.RegisterTest(TMinusTest.Suite);
 
end.


unit MulTest;
 
interface
 
uses
  OperationTest
  ;
 
type
  TMulTest = class(TOperationTest)
   protected
    function  GetOp: TOperation; override;
  end;//TMulTest
 
implementation
 
uses
  TestFrameWork,
  SysUtils
  ;
 
function TMulTest.GetOp: TOperation;
begin
 Result := opMul;
end;
 
initialization
 TestFramework.RegisterTest(TMulTest.Suite);
 
end.


unit DivTest;
 
interface
 
uses
  OperationTest
  ;
 
type
  TDivTest = class(TOperationTest)
   protected
    function  GetOp: TOperation; override;
  end;//TDivTest
 
implementation
 
uses
  TestFrameWork,
  SysUtils
  ;
 
function TDivTest.GetOp: TOperation;
begin
 Result := opDiv;
end;
 
initialization
 TestFramework.RegisterTest(TDivTest.Suite);
 
end.

That’s what we get:

What are the results?

We have covered ALL operations of the calculator with tests.

We have, one might say, “ensured the minimal test coverage” of all precedents in our application.

We could speak of testing of "various data sets” or “random data sets”.

We could also speak about testing of boundary conditions like, for example, division by zero.

We could speak of testing of input data validation.

We’ll discuss these in the next chapters.

We’ll also speak of “changing the architecture”. We’ve come close to it.

We will speak, but in the next posts.

Meanwhile, I take a pause and I’d like you to have a look at what I’ve given above.

I advise you to read this article if you have not yet - http://18delphi.blogspot.com/2015/04/how-to-test-totally-untestable.html.

P.S. I’ve been asked to draw UML-diagram for classes and tests of the application. Is it INTERESTING? Do you NEED it? Can I get away with classes diagram ALONE or do you need sequence-diagram too?

Repository

Комментариев нет:

Отправить комментарий