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 Add
functions is the class function, Mul
the function is an ordinary function.
Then select the main menu 新建/DUnitX
, select DUnitX Project
, check the demand to create a DUnitX
test project.
Rename the file test code untCalcTest
reads 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, Setup
is initialized at the beginning, TearDown
it 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.
Ignore
Means 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.