bcquality/microsoft/knowledge/testing/use-generateguid-for-unique-test-fixture-values.md
Michael Dieringer 1028aacd4f Add 3 more AL/BC patterns from CURABIS Academy testing course material
Third batch from CURABIS ApS: item-ledger-entry document-no lookup after Ship-and-Invoice posting, TestPage.Visible()/.Enabled() as the mechanism for verifying field UI state, and LibraryUtility.GenerateGUID() for collision-free test fixture values.
2026-09-21 22:34:40 +02:00

1.3 KiB

bc-version domain keywords technologies countries application-area
all
testing
generateguid
library-utility
test-fixtures
uniqueness
copystr
maxstrlen
al
w1
all

Generate unique test fixture values with LibraryUtility.GenerateGUID()

Description

A fixture helper that assigns a hardcoded literal to a primary-key or descriptive field collides the moment two tests, or two runs of the same test, create that fixture without cleanup, and a literal longer than the field allows raises a truncation or insert error. LibraryUtility.GenerateGUID() returns a value that is unique per call and long enough to guarantee no collision; paired with CopyStr(..., 1, MaxStrLen(Field)) it fits any fixed-length Code or Text field safely.

Best Practice

For a fixture field that must be unique across test runs, assign CopyStr(LibraryUtility.GenerateGUID(), 1, MaxStrLen(TargetField)) rather than a literal string.

See sample: use-generateguid-for-unique-test-fixture-values.good.al.

Anti Pattern

Hardcoding a fixture value such as 'TEST001' or a short descriptive literal. It collides across parallel or repeated test runs, and a value longer than the field's length limit is either silently truncated or raises an insert error.

See sample: use-generateguid-for-unique-test-fixture-values.bad.al.