bcquality/microsoft/knowledge/testing/transactionmodel-attribute-governs-test-transactions.md
Jesper Schulz-Wedde 5706959e4a
Fix lifecycle compatibility guidance (#93)
* Fix lifecycle compatibility guidance

Correct high-confidence Business Central guidance and samples for upgrade tags, collectible errors, trigger semantics, obsoletion, events, interfaces, API contracts, and test transactions.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e05a43e7-6448-4d67-9c73-798523f5d945

* Address guidance review findings

Gate SecretText guidance to BC23 and clarify that the collectible-error sample intentionally emits a message-only blocking aggregate.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: e05a43e7-6448-4d67-9c73-798523f5d945

---------

Co-authored-by: Jesper Schulz-Wedde <jesper.schulzwedde@microsoft.com>
2026-07-14 11:26:16 +02:00

2.3 KiB

bc-version domain keywords technologies countries application-area
all
testing
transactionmodel
attribute
test
autorollback
autocommit
testisolation
al
w1
all

Match TransactionModel to the commit behavior of the code under test

Description

[TransactionModel(...)] declares how a test method interacts with the database's write transaction. The attribute applies only to methods inside a codeunit with SubType = Test and takes one of three values: AutoRollback, AutoCommit, or None. The choice must match the code being exercised — in particular, whether that code calls Commit(). Per the platform reference, "if the code that you test includes calls to the COMMIT Method, then set the TransactionModel property on the test method to AutoCommit." Applying AutoRollback to a test that drives code which calls Commit produces a runtime error on the first Commit, not a meaningful assertion failure — the test does not complete, and the reviewer sees an infrastructure error instead of a business-logic verdict.

Best Practice

Default to AutoRollback: it opens a write transaction at the start of the test, runs the test body, and rolls back at the end, leaving the database in its original state. Pick AutoCommit only when the code under test genuinely calls Commit — posting routines, job-queue handlers, integration flows — and make the test exercise that commit path. Pair the test codeunit with a TestIsolation-enabled test runner so committed changes are reverted at a higher scope. Pick None only for read-only tests or tests that drive UI code without writing from the test method itself.

See sample: transactionmodel-attribute-governs-test-transactions.good.al.

Anti Pattern

Applying AutoRollback to every test method without checking whether the tested business logic calls Commit. The test throws at the first Commit, leaving no verdict on the behavior it intended to verify; in a CI run this looks like a flake or a setup bug, not a specification mismatch. The mirror-image anti-pattern is defaulting to AutoCommit across the suite "to avoid the error" — without a TestIsolation runner this permanently dirties the test database between runs and produces order-dependent test outcomes.

See sample: transactionmodel-attribute-governs-test-transactions.bad.al.