fpcunit
Overview
fpcunit is a unit testing framework a la DUnit/JUnit/SUnit SUnit. This allows you to quickly write a set of test for a (logical) unit of code (not necessarily the same as a Pascal unit, though often it is).
Development methodologies like Test Driven Design use this to make sure you code your expectations/specifications in your unit tests first, then write your main code, then run your tests and improve the code until all tests pass.
Use in FPC/Lazarus
FPCUnit tests are used in the FPC database test framework: Databases#Running_FPC_database_tests
There are also tests for the FPC compiler/core packages, but these presumably predate fpcunit and use a simpler approach.
Use
It's easiest to use Lazarus to set up a new test project for you.
Setup
This procedure is present in all FPCUnit tests. It sets up the test environment - once at the beginning or for each test? cannot remember. You can use this to e.g. fill a database with test data.
Teardown
This procedure is present in all FPCUnit tests. It cleans up the test environment - once at the beginning or for each test? cannot remember. You can use this to e.g. clear test data from a database.
Tests
You write your own tests as published procedures in the test class. You can use AssertEquals etc to specify what should be tested. The order in which the tests are run is: cannot remember - alphabet? order of procedures?
Example test:
Ttestexport1 = class(Ttestcase)
...
published
procedure TestOutput;
...
procedure Ttestexport1.TestOutput;
const
OutputFilename='output.csv';
begin
TestDataSet.Close;
if FileExists(OutputFilename) then DeleteFile(OutputFileName);
TestDataset.FileName:=OutputFileName;
TestDataset.Open;
// Fill test data
TestDataset.Append;
TestDataset.FieldByName('ID').AsInteger := 1;
// Data with quotes
TestDataset.FieldByName('NAME').AsString := 'J"T"';
TestDataset.FieldByName('BIRTHDAY').AsDateTime := ScanDateTime('yyyymmdd', '19761231', 1);
TestDataset.Post;
TestDataset.Last;
TestDataset.First;
TestDataset.First;
AssertEquals('Number of records in test dataset', 1, TestDataset.RecordCount);
TestDataset.Close;
end;
Alternatives
There is DUnit2 (IIRC an improvement of the original DUnit), originally written for Delphi, which is in use in the tiOPF framework
Lazarus
Lazarus has the consoletestrunner and GUI test runner units, which can be installed by installing the FPCUnitTestRunner package. This will help you create and run your unit tests using a GUI (or console, if you want to).