Mini Test Framework for Delphi Unit Testing
Hello All,
I was working last week on the TLabelledEnum project where I needed a very lightweight, and simple to use unit-test framework.
I have used Dunit and DunitX - both great but can be a little fiddly to configure and there are few versions around that might make it a little difficult to deploy reliably with the Delphi Enum repository to different users.
The tests I needed to do were not too complex so I simply started putting together a few simple functions like Check (following DUnit style) and CheckIsEqual, and CheckIsTrue.
At the end of the day, I had a pretty much fully functional unit test framework with DUnitX style output.
I thought some other people might get some use out of it, so I put it up on my GitHub, in the Delphi Tips repository Glens Delphi Tips Repo
The advantage is of this that you can simply include the MiniTestFrameWork.pas in a console project and start adding test cases.
For Example: Here is the TLabelledEnum test cases:
program TestTLabelledEnum;
{$APPTYPE
CONSOLE}
{$R *.res}
Uses
Enum in 'Enum.pas', << unit I am testing
TestEnum in 'TestEnum.pas', <<== tests cases unit
MiniTestFramework in 'MiniTestFramework.pas';
begin
try
// Set Title Here Title(Atitle: string);
Title('MiniTest - Test cases for TLabelledEnum'); <<== title for display
Label_of_static_Type_Passes;
Without_Labels_Labelled_Enums_match_Static_types;
Implicit_Static_Type_Assignment_Assigns_Values;
Implicit_String_Assignment_Assigns_Values;
Static_Class_Function_AsString_Works_as_expected;
// Set Labels
Assigning_Labels_assigns_alternate_labels;
Implicit_Labelled_String_Assignment_Label_or_Identifier_Assigns_Values;
Static_Class_Function_Labelled_AsString_returns_labels_as_expected;
TestSummary; <<== show the final results
if sameText(Paramstr(1),'/p') then ReadLn;
ExitCode := TotalErrors+TotalFailedTestCases; <<== return exit value for CI builders
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.
The actual test cases themselves are pretty simple too:
procedure Label_of_static_Type_Passes;
var StaticNames : TArray<String>;
begin
NewTestSet('Get Static Types using GetEnumNameList'); <<== new "set"
StaticNames :=Enum.GetEnumNameList(TypeHandle(TMyTYpe));
SetTestCase('GetEnumNameList returns 3 Values'); <<== new "case" in "set"
CheckIsEqual(3,length(StaticNames)); <<==test assertion
SetTestCase('GetEnumNameList Array elements match MyLabels');
CheckIsEqual(StaticNames[0],'MyOptionA'); <<= numbered test in case
CheckIsEqual(StaticNames[1],'MyOptionB');
CheckIsEqual(StaticNames[2],'MyOptionC');
end;
As you can see, there is not much to it. And the output is helpfully chromatic - so you can say that your Tests are "Green" or "Red" (or purple ...)
There is still a small amount of work to do - Backward compatibility testing - I have used so generics which I can probably remove to support Pre-Delphi 2009 code.
Anyway I hope you find it helpful.
The to-do work mentioned above has been completed (some time ago actually) and the framework now supports Exceptions, test runs and skipping.
ReplyDelete