Skip to content

Commit 7dcc37c

Browse files
Added AutoNameTestCase attribute
Will auto generate the test case name in the form MethodName(parameters)
1 parent 5102f48 commit 7dcc37c

File tree

6 files changed

+63
-4
lines changed

6 files changed

+63
-4
lines changed

Examples/DUnitX.Examples.General.pas

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ TMyExampleTests = class
5757
[TestCase('Case 3','5,6')]
5858
procedure TestOne(param1 : integer; param2 : integer);
5959

60+
61+
[Test]
62+
[AutoNameTestCase('1,2')]
63+
[AutoNameTestCase('3,4')]
64+
[AutoNameTestCase('5,6')]
65+
[Category('auto')]
66+
procedure TestAutoName(param1 : integer; param2 : integer);
67+
6068
[TestCase('Case 3','Blah,1')]
6169
procedure AnotherTestMethod(const a : string; const b : integer);
6270

@@ -220,6 +228,12 @@ procedure TMyExampleTests.AnotherTestMethod(const a: string; const b: integer);
220228
TDUnitX.CurrentRunner.Status(Format('AnotherTestMethod called with %s %d',[a,b]));
221229
end;
222230

231+
procedure TMyExampleTests.TestAutoName(param1, param2: integer);
232+
begin
233+
TDUnitX.CurrentRunner.Status(Format('TestAutoName called with %d %d',[param1,param2]));
234+
235+
end;
236+
223237
procedure TMyExampleTests.TestCaseWithStrings(const AInput, AResult: string);
224238
begin
225239
TDUnitX.CurrentRunner.Status(Format('TestCaseWithStrings called with %s %s',[AInput,AResult]));

Examples/ProviderExample.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ interface
3535
[TestFixture('ProviderExample1','Example using TestCaseProviders')]
3636
TProviderExample = class(TObject)
3737
public
38-
// [Test]
39-
// [TestCaseProvider('Demoprovider')]
38+
[Test]
39+
[TestCaseProvider(TSampleProvider)]
4040
Procedure Addtest(const v1,v2:integer;expected:integer);
4141
[Test]
4242
[TestCaseProvider(TSampleProvider)]

Source/DUnitX.Attributes.pas

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,28 @@ CustomTestCaseSourceAttribute = class abstract(TCustomAttribute)
269269
TestCaseAttribute = class(CustomTestCaseAttribute)
270270
protected
271271
FCaseInfo : TestCaseInfo;
272+
FValues : string;
272273
function GetCaseInfo : TestCaseInfo; override;
273274
function GetName: string;
274275
function GetValues: TValueArray;
276+
function GetValuesText : string;
275277
public
276278
constructor Create(const ACaseName : string; const AValues : string; const ASeparator : string = ','; const ATrimValues : boolean = false);
277279
property Name : String read GetName;
278280
property Values : TValueArray read GetValues;
281+
property ValuesText : string read GetValuesText;
282+
end;
283+
284+
/// <summary>
285+
/// The AutoTestCaseAttribute allows you to pass values to a test function.
286+
/// Each value is delimited in the string, by default the delimiter is ','
287+
/// The case name is generated by concatenating the test method name with (values)
288+
/// </summary>
289+
AutoNameTestCaseAttribute = class(TestCaseAttribute)
290+
protected
291+
public
292+
//Note : I wanted to do add this directly to testcase but can't add second constructor that made sense to overload resolution
293+
constructor Create(const AValues : string; const ASeparator : string = ','; const ATrimValues : boolean = false);
279294
end;
280295

281296
/// <summary>
@@ -382,6 +397,7 @@ constructor TestCaseAttribute.Create(const ACaseName: string; const AValues: str
382397
begin
383398
inherited Create;
384399
FCaseInfo.Name := ACaseName;
400+
FValues := AValues;
385401
lValues := SplitString(AValues,ASeparator);
386402
l := Length(lValues);
387403
SetLength(FCaseInfo.Values,l);
@@ -394,6 +410,8 @@ constructor TestCaseAttribute.Create(const ACaseName: string; const AValues: str
394410
end;
395411
end;
396412

413+
414+
397415
function TestCaseAttribute.GetCaseInfo: TestCaseInfo;
398416
begin
399417
Result := FCaseInfo;
@@ -409,6 +427,11 @@ function TestCaseAttribute.GetValues: TValueArray;
409427
Result := FCaseInfo.Values;
410428
end;
411429

430+
function TestCaseAttribute.GetValuesText: string;
431+
begin
432+
result := FValues;
433+
end;
434+
412435
{ MaxTimeAttribute }
413436

414437
constructor MaxTimeAttribute.Create(const AMaxTime : Cardinal);
@@ -443,4 +466,14 @@ constructor TestCaseProviderAttribute.Create(const AClass: TTestDataProviderClas
443466
FClass := AClass;
444467
end;
445468

469+
{ AutoTestCaseAttribute }
470+
471+
472+
{ AutoNameTestCaseAttribute }
473+
474+
constructor AutoNameTestCaseAttribute.Create(const AValues, ASeparator: string; const ATrimValues: boolean);
475+
begin
476+
inherited Create('', AValues, ASeparator, ATrimValues);
477+
end;
478+
446479
end.

Source/DUnitX.FixtureProviderPlugin.pas

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,11 @@ procedure TDUnitXFixtureProvider.GenerateTests(const context: IFixtureProviderCo
489489
begin
490490
for i := 1 to repeatCount do
491491
begin
492-
currentFixture.AddTestCase(method.Name, testCaseAttrib.CaseInfo.Name, FormatTestName(method.Name, i, repeatCount), category, method, testEnabled, testCaseAttrib.CaseInfo.Values);
492+
if testCaseAttrib is AutoNameTestCaseAttribute then
493+
caseName := '(' + AutoNameTestCaseAttribute(testCaseAttrib).ValuesText + ')'
494+
else
495+
caseName := testCaseAttrib.CaseInfo.Name;
496+
currentFixture.AddTestCase(method.Name, caseName, FormatTestName(method.Name, i, repeatCount), category, method, testEnabled, testCaseAttrib.CaseInfo.Values);
493497
end;
494498
end;
495499
// Add test case from test \case sources

Source/DUnitX.Test.pas

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,10 @@ implementation
146146
uses
147147
{$IFDEF USE_NS}
148148
System.Generics.Defaults,
149+
System.StrUtils,
149150
{$ELSE}
150151
Generics.Defaults,
152+
StrUtils,
151153
{$ENDIF}
152154
{$IFDEF MSWINDOWS}
153155
DUnitX.Timeout,
@@ -396,7 +398,12 @@ function TDUnitXTestCase.GetIsTestCase: boolean;
396398
function TDUnitXTestCase.GetName: string;
397399
begin
398400
if FCaseName <> '' then
399-
Result := FName + '.' + FCaseName
401+
begin
402+
if StartsText('(', FCaseName) then
403+
Result := FName + FCaseName
404+
else
405+
Result := FName + '.' + FCaseName;
406+
end
400407
else
401408
Result := FName;
402409

Source/DUnitX.TestFramework.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ interface
8989
CustomTestCaseAttribute = DUnitX.Attributes.CustomTestCaseAttribute;
9090
CustomTestCaseSourceAttribute = DUnitX.Attributes.CustomTestCaseSourceAttribute;
9191
TestCaseAttribute = DUnitX.Attributes.TestCaseAttribute;
92+
AutoNameTestCaseAttribute = DUnitX.Attributes.AutoNameTestCaseAttribute;
9293
TestCaseProviderAttribute = DUnitX.Attributes.TestCaseProviderAttribute;
9394

9495
TExceptionInheritance = DUnitX.Types.TExceptionInheritance;

0 commit comments

Comments
 (0)