Skip to content

Commit

Permalink
Added AutoNameTestCase attribute
Browse files Browse the repository at this point in the history
Will auto generate the test case name in the form MethodName(parameters)
  • Loading branch information
vincentparrett committed Feb 12, 2025
1 parent 5102f48 commit 7dcc37c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 4 deletions.
14 changes: 14 additions & 0 deletions Examples/DUnitX.Examples.General.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ TMyExampleTests = class
[TestCase('Case 3','5,6')]
procedure TestOne(param1 : integer; param2 : integer);


[Test]
[AutoNameTestCase('1,2')]
[AutoNameTestCase('3,4')]
[AutoNameTestCase('5,6')]
[Category('auto')]
procedure TestAutoName(param1 : integer; param2 : integer);

[TestCase('Case 3','Blah,1')]
procedure AnotherTestMethod(const a : string; const b : integer);

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

procedure TMyExampleTests.TestAutoName(param1, param2: integer);
begin
TDUnitX.CurrentRunner.Status(Format('TestAutoName called with %d %d',[param1,param2]));

end;

procedure TMyExampleTests.TestCaseWithStrings(const AInput, AResult: string);
begin
TDUnitX.CurrentRunner.Status(Format('TestCaseWithStrings called with %s %s',[AInput,AResult]));
Expand Down
4 changes: 2 additions & 2 deletions Examples/ProviderExample.pas
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ interface
[TestFixture('ProviderExample1','Example using TestCaseProviders')]
TProviderExample = class(TObject)
public
// [Test]
// [TestCaseProvider('Demoprovider')]
[Test]
[TestCaseProvider(TSampleProvider)]
Procedure Addtest(const v1,v2:integer;expected:integer);
[Test]
[TestCaseProvider(TSampleProvider)]
Expand Down
33 changes: 33 additions & 0 deletions Source/DUnitX.Attributes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,28 @@ CustomTestCaseSourceAttribute = class abstract(TCustomAttribute)
TestCaseAttribute = class(CustomTestCaseAttribute)
protected
FCaseInfo : TestCaseInfo;
FValues : string;
function GetCaseInfo : TestCaseInfo; override;
function GetName: string;
function GetValues: TValueArray;
function GetValuesText : string;
public
constructor Create(const ACaseName : string; const AValues : string; const ASeparator : string = ','; const ATrimValues : boolean = false);
property Name : String read GetName;
property Values : TValueArray read GetValues;
property ValuesText : string read GetValuesText;
end;

/// <summary>
/// The AutoTestCaseAttribute allows you to pass values to a test function.
/// Each value is delimited in the string, by default the delimiter is ','
/// The case name is generated by concatenating the test method name with (values)
/// </summary>
AutoNameTestCaseAttribute = class(TestCaseAttribute)
protected
public
//Note : I wanted to do add this directly to testcase but can't add second constructor that made sense to overload resolution
constructor Create(const AValues : string; const ASeparator : string = ','; const ATrimValues : boolean = false);
end;

/// <summary>
Expand Down Expand Up @@ -382,6 +397,7 @@ constructor TestCaseAttribute.Create(const ACaseName: string; const AValues: str
begin
inherited Create;
FCaseInfo.Name := ACaseName;
FValues := AValues;
lValues := SplitString(AValues,ASeparator);
l := Length(lValues);
SetLength(FCaseInfo.Values,l);
Expand All @@ -394,6 +410,8 @@ constructor TestCaseAttribute.Create(const ACaseName: string; const AValues: str
end;
end;



function TestCaseAttribute.GetCaseInfo: TestCaseInfo;
begin
Result := FCaseInfo;
Expand All @@ -409,6 +427,11 @@ function TestCaseAttribute.GetValues: TValueArray;
Result := FCaseInfo.Values;
end;

function TestCaseAttribute.GetValuesText: string;
begin
result := FValues;
end;

{ MaxTimeAttribute }

constructor MaxTimeAttribute.Create(const AMaxTime : Cardinal);
Expand Down Expand Up @@ -443,4 +466,14 @@ constructor TestCaseProviderAttribute.Create(const AClass: TTestDataProviderClas
FClass := AClass;
end;

{ AutoTestCaseAttribute }


{ AutoNameTestCaseAttribute }

constructor AutoNameTestCaseAttribute.Create(const AValues, ASeparator: string; const ATrimValues: boolean);
begin
inherited Create('', AValues, ASeparator, ATrimValues);
end;

end.
6 changes: 5 additions & 1 deletion Source/DUnitX.FixtureProviderPlugin.pas
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,11 @@ procedure TDUnitXFixtureProvider.GenerateTests(const context: IFixtureProviderCo
begin
for i := 1 to repeatCount do
begin
currentFixture.AddTestCase(method.Name, testCaseAttrib.CaseInfo.Name, FormatTestName(method.Name, i, repeatCount), category, method, testEnabled, testCaseAttrib.CaseInfo.Values);
if testCaseAttrib is AutoNameTestCaseAttribute then
caseName := '(' + AutoNameTestCaseAttribute(testCaseAttrib).ValuesText + ')'
else
caseName := testCaseAttrib.CaseInfo.Name;
currentFixture.AddTestCase(method.Name, caseName, FormatTestName(method.Name, i, repeatCount), category, method, testEnabled, testCaseAttrib.CaseInfo.Values);
end;
end;
// Add test case from test \case sources
Expand Down
9 changes: 8 additions & 1 deletion Source/DUnitX.Test.pas
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ implementation
uses
{$IFDEF USE_NS}
System.Generics.Defaults,
System.StrUtils,
{$ELSE}
Generics.Defaults,
StrUtils,
{$ENDIF}
{$IFDEF MSWINDOWS}
DUnitX.Timeout,
Expand Down Expand Up @@ -396,7 +398,12 @@ function TDUnitXTestCase.GetIsTestCase: boolean;
function TDUnitXTestCase.GetName: string;
begin
if FCaseName <> '' then
Result := FName + '.' + FCaseName
begin
if StartsText('(', FCaseName) then
Result := FName + FCaseName
else
Result := FName + '.' + FCaseName;
end
else
Result := FName;

Expand Down
1 change: 1 addition & 0 deletions Source/DUnitX.TestFramework.pas
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ interface
CustomTestCaseAttribute = DUnitX.Attributes.CustomTestCaseAttribute;
CustomTestCaseSourceAttribute = DUnitX.Attributes.CustomTestCaseSourceAttribute;
TestCaseAttribute = DUnitX.Attributes.TestCaseAttribute;
AutoNameTestCaseAttribute = DUnitX.Attributes.AutoNameTestCaseAttribute;
TestCaseProviderAttribute = DUnitX.Attributes.TestCaseProviderAttribute;

TExceptionInheritance = DUnitX.Types.TExceptionInheritance;
Expand Down

0 comments on commit 7dcc37c

Please sign in to comment.