[Notes] Delphi simple example used to do unit testing DUnitX

Delphi XE provides DUnitX supported by the record one of the most simple example.

First create item A, then creating unit untCalc, as follows:

unit untCalc;

interface

type
  TCalc = class(TObject)
  public
    class function Add(a, b: Integer): Integer;
    function Mul(a, b: Integer): Integer;
  end;

implementation

{ TCalc }

class function TCalc.Add(a, b: Integer): Integer;
begin
  Result := a + b;
end;

function TCalc.Mul(a, b: Integer): Integer;
begin
  Result := a * b;
end;

end.

In order to reflect the diversity of Addfunctions is the class function, Multhe function is an ordinary function.

Then select the main menu 新建/DUnitX, select DUnitX Project, check the demand to create a DUnitXtest project.

Rename the file test code untCalcTestreads as follows:

unit untCalcTest;

interface

uses
  DUnitX.TestFramework, untCalc;

type

  [TestFixture]
  TClacTest = class
  protected
    class var FCalc: TCalc;
  public
    [Setup]
    procedure Setup;

    [TearDown]
    procedure TearDown;

    [Test]
    [TestCase('正数相加', '1|2|3', '|')]
    [TestCase('负数相加', '-1,-2,-3')]
    procedure TestAdd(const a, b, c: Integer);

    [Test]
    [Ignore('需要重新修改')]
    procedure TestMul(const a, b, c: Integer);
  end;

implementation

procedure TClacTest.Setup;
begin
  FCalc := TCalc.Create;
end;

procedure TClacTest.TearDown;
begin
  FCalc.Free;
end;

procedure TClacTest.TestAdd(const a, b, c: Integer);
begin
  Assert.AreEqual(TCalc.Add(a, b), c);
end;

procedure TClacTest.TestMul(const a, b, c: Integer);
begin
  Assert.AreEqual(FCalc.Mul(a, b), c);
end;

initialization

TDUnitX.RegisterTestFixture(TClacTest);

end.

The above code can be seen, Setupis initialized at the beginning, TearDownit is the processing of exit.
Can be added by adding a portion of the function declaration attribute simple test mode, the default parameters are separated by commas in the string, if other delimiters (e.g., "|"), needs to be added later as a parameter separator.
IgnoreMeans skip this test item.

Results are as follows:

**********************************************************************
*        DUnitX - (c) 2015-2018 Vincent Parrett & Contributors       *
*                                                                    *
*        License - http://www.apache.org/licenses/LICENSE-2.0        *
**********************************************************************

  Fixture : untCalcTest
  -------------------------------------------------
     Fixture : untCalcTest.TClacTest
     -------------------------------------------------
       Test : untCalcTest.TClacTest.TestAdd.正数相加
       -------------------------------------------------
       Running Setup for : TestAdd.正数相加

       Executing Test : TestAdd.正数相加


        Running Teardown for Test : TestAdd.正数相加

         Success.

       Test : untCalcTest.TClacTest.TestAdd.负数相加
       -------------------------------------------------
       Running Setup for : TestAdd.负数相加

       Executing Test : TestAdd.负数相加


        Running Teardown for Test : TestAdd.负数相加

         Success.

       Test : untCalcTest.TClacTest.TestMul
       -------------------------------------------------
         Test Ignored : TestMul : 需要重新修改

      Running Fixture Teardown Method : Destroy



Done testing.
Tests Found   : 3
Tests Ignored : 1
Tests Passed  : 2
Tests Leaked  : 0
Tests Failed  : 0
Tests Errored : 0
Done.. press <Enter> key to quit

Then modify a test case, test deliberately failed to read as follows:

[TestCase('正数相加', '1|2|4', '|')]

Results are as follows:

**********************************************************************
*        DUnitX - (c) 2015-2018 Vincent Parrett & Contributors       *
*                                                                    *
*        License - http://www.apache.org/licenses/LICENSE-2.0        *
**********************************************************************

  Fixture : untCalcTest
  -------------------------------------------------
     Fixture : untCalcTest.TClacTest
     -------------------------------------------------
       Test : untCalcTest.TClacTest.TestAdd.正数相加
       -------------------------------------------------
       Running Setup for : TestAdd.正数相加

       Executing Test : TestAdd.正数相加


        Running Teardown for Test : TestAdd.正数相加

         Test failed : TestAdd.正数相加 : Expected 3 but got 4

       Test : untCalcTest.TClacTest.TestAdd.负数相加
       -------------------------------------------------
       Running Setup for : TestAdd.负数相加

       Executing Test : TestAdd.负数相加


        Running Teardown for Test : TestAdd.负数相加

         Success.

       Test : untCalcTest.TClacTest.TestMul
       -------------------------------------------------
         Test Ignored : TestMul : 需要重新修改

      Running Fixture Teardown Method : Destroy



Done testing.
Tests Found   : 3
Tests Ignored : 1
Tests Passed  : 1
Tests Leaked  : 0
Tests Failed  : 1
Tests Errored : 0

Failing Tests

  untCalcTest.TClacTest.TestAdd.正数相加
  Message: Expected 3 but got 4


Done.. press <Enter> key to quit.

Visible, the error is displayed out of place.

These are just the simplest example, demand more functionality dig it.

Guess you like

Origin www.cnblogs.com/journeyonmyway/p/11831256.html