Write focused AL tests
The framework controls execution, but test quality depends on how clearly each procedure expresses a business rule. In the review-policy scenario, focused tests create their own record, invoke one public operation, and assert the result that matters to the user.
Separate production and test responsibilities
The production app owns the review record and policy. Its codeunit validates the comment, asks for confirmation, and updates the record.
table 71000 "Review Policy Entry"
{
DataClassification = CustomerContent;
fields
{
field(1; "Entry No."; Integer)
{
AutoIncrement = true;
DataClassification = SystemMetadata;
}
field(2; "Review Comment"; Text[100])
{
DataClassification = CustomerContent;
}
field(3; Reviewed; Boolean)
{
DataClassification = CustomerContent;
}
}
keys
{
key(PK; "Entry No.")
{
Clustered = true;
}
}
}
codeunit 71001 "Customer Review Policy"
{
procedure MarkReviewed(var ReviewEntry: Record "Review Policy Entry")
begin
if ReviewEntry."Review Comment" = '' then
Error(MissingCommentErr);
if not Confirm(ConfirmReviewQst, false) then
exit;
ReviewEntry.Reviewed := true;
ReviewEntry.Modify();
end;
var
MissingCommentErr: Label 'Enter a review comment before completing the review.';
ConfirmReviewQst: Label 'Complete this review?';
}
The test app depends on this app and contains the test codeunit. This separation prevents test-only dependencies from entering the production package.
Implement positive and negative cases
The following test codeunit covers successful review, validation failure, and a declined confirmation. Initialize resets codeunit state, and CreateReviewEntry gives every test explicit data.
using System.TestLibraries.Utilities;
codeunit 71050 "Review Policy Tests"
{
Subtype = Test;
TestPermissions = Disabled;
var
LibraryAssert: Codeunit "Library Assert";
ReviewPolicy: Codeunit "Customer Review Policy";
MissingCommentErr: Label 'Enter a review comment before completing the review.';
[Test]
[TransactionModel(TransactionModel::AutoRollback)]
[HandlerFunctions('AcceptReviewHandler')]
procedure ReviewWithCommentSucceeds()
var
ReviewEntry: Record "Review Policy Entry";
begin
Initialize();
CreateReviewEntry(ReviewEntry, 'Checked against the source document.');
ReviewPolicy.MarkReviewed(ReviewEntry);
ReviewEntry.Get(ReviewEntry."Entry No.");
LibraryAssert.IsTrue(ReviewEntry.Reviewed, 'The entry must be reviewed.');
end;
[Test]
[TransactionModel(TransactionModel::AutoRollback)]
procedure ReviewWithoutCommentFails()
var
ReviewEntry: Record "Review Policy Entry";
begin
Initialize();
CreateReviewEntry(ReviewEntry, '');
ClearLastError();
AssertError ReviewPolicy.MarkReviewed(ReviewEntry);
LibraryAssert.AreEqual(
MissingCommentErr,
GetLastErrorText(),
'The policy returned an unexpected error.');
end;
[Test]
[TransactionModel(TransactionModel::AutoRollback)]
[HandlerFunctions('DeclineReviewHandler')]
procedure DeclinedReviewRemainsOpen()
var
ReviewEntry: Record "Review Policy Entry";
begin
Initialize();
CreateReviewEntry(ReviewEntry, 'Ready for review.');
ReviewPolicy.MarkReviewed(ReviewEntry);
ReviewEntry.Get(ReviewEntry."Entry No.");
LibraryAssert.IsFalse(ReviewEntry.Reviewed, 'The entry must remain open.');
end;
local procedure Initialize()
begin
Clear(ReviewPolicy);
end;
local procedure CreateReviewEntry(var ReviewEntry: Record "Review Policy Entry"; Comment: Text[100])
begin
ReviewEntry.Init();
ReviewEntry."Entry No." := 0;
ReviewEntry."Review Comment" := Comment;
ReviewEntry.Insert();
end;
[ConfirmHandler]
procedure AcceptReviewHandler(Question: Text[1024]; var Reply: Boolean)
begin
LibraryAssert.AreEqual('Complete this review?', Question, 'Unexpected confirmation.');
Reply := true;
end;
[ConfirmHandler]
procedure DeclineReviewHandler(Question: Text[1024]; var Reply: Boolean)
begin
LibraryAssert.AreEqual('Complete this review?', Question, 'Unexpected confirmation.');
Reply := false;
end;
}
The success and declined-confirmation cases reload the record before asserting its state. This checks persisted data rather than only the var record's in-memory value. The negative case verifies the intended error; don't claim an additional database rollback assertion that it doesn't perform.
Each test requests an automatically generated entry number instead of assuming that entry number 1 is unused. The assertions don't depend on the assigned value. Explicit AutoRollback returns the test data to its starting state; auto-increment values can have gaps after rollback, so don't assert consecutive numbers. Keep tests independent of their execution order.
Keep failures diagnostic
One test should fail for one clear reason. A test that creates unrelated master data, posts documents, opens pages, and checks several outcomes makes root-cause analysis difficult. Extract setup helpers, but keep expected values visible in the test procedure.
Use boundary and regression cases where they carry risk. For this policy, a comment at the field's maximum length is a useful boundary. A corrected defect gets a test that fails with the old implementation and passes with the correction. This pattern turns defect history into durable release evidence.
Avoid calling test procedures from other test procedures. Shared helpers arrange data or perform narrow assertions. Test methods remain independently discoverable and runnable.
For additional examples, see Testing the application and AL test code example.