bcquality/custom/knowledge/testing/test-setup-must-use-library-codeunit.md
Michael Dieringer c4a9f8e88e update
2026-06-13 13:46:40 +02:00

1.6 KiB

bc-version domain keywords technologies countries application-area
all
testing
test
library
setup
initialize
suppresscommit
asserterror
al
w1
all

Description

In CURABIS test apps, all test setup is centralized in a dedicated Test Library codeunit (e.g. SV Test Library). Individual test procedures must not call BC standard library codeunits (LibrarySales, LibraryInventory, etc.) directly.

Additionally, two rules apply to every test that calls a posting codeunit:

  1. SetSuppressCommit(true) must be called before Run() to prevent data from leaking between tests.
  2. asserterror must always be followed by Assert.ExpectedErrorCode() or Assert.ExpectedError() — a naked asserterror passes on any error, not just the expected one.

Anti Pattern

// WRONG: inline setup bypassing the test library
procedure MyTest()
var
    Item: Record Item;
begin
    LibraryInventory.CreateItem(Item);   // do not call directly
    // ...
end;
// WRONG: posting without SuppressCommit
SVPost.Run(SVHeader);   // commits to test database
// WRONG: naked asserterror
asserterror SVPost.Run(SVHeader);
// no assertion follows — passes on any error

Best Practice

// CORRECT: delegate to test library
procedure MyTest()
var
    Item: Record Item;
begin
    SVLib.GivenScrapItem(Item);   // test library owns setup
    // ...
end;
// CORRECT: SuppressCommit before Run
SVPost.SetSuppressCommit(true);
SVPost.Run(SVHeader);
// CORRECT: asserterror followed by assertion
asserterror SVPost.Run(SVHeader);
Assert.ExpectedErrorCode('Dialog');