mirror of
https://github.com/microsoft/BCQuality.git
synced 2026-10-05 14:46:55 +01:00
Fix remaining correctness issues from Jesper's 2026-09-15 re-review
- commit-shared-test-fixture-inside-lazy-initialize: three sub-issues.
Recommended TestIsolation = Codeunit instead of listing Disabled as an
equal option - Disabled never rolls back at all ("tests are not
isolated from each other" per the property's own docs), so a fixture
this pattern commits under Disabled is permanent database
contamination unless something else tears it down; Disabled is now
only mentioned alongside that explicit teardown requirement. Added
precedence in al-testing-review.md so the deliberate end-of-test
asserterror Error(...) rollback sentinel isn't also flagged by the
generic asserterror-needs-expectederror-and-code rule. Rewrote both
fixtures to actually demonstrate the pattern: persisted fixture data
(an Item record) instead of an empty comment, a second [Test] method
that depends on the fixture surviving into it, and an explicit
Subtype = TestRunner / TestIsolation = Codeunit runner codeunit.
- table-relation-test-exclude-known-invalid-relations-via-event:
the length/type rule was stated as one global requirement. Verified
ValidateFieldRelation in codeunit 134926 directly (BCApps reference
clone) and split it into the two branches the source actually has:
a field with any unconditional relation needs exact length and exact
resolved type; a field whose relations are all conditional only fails
on being shorter (longer is fine) than the largest related field, and
when the required type is specifically Code, a Text source passes too
- a tolerance that does not apply on the unconditional side and does
not extend to a required Text.
Rebased onto upstream/main (one conflict in
transactionmodel-attribute-governs-test-transactions.md - upstream had
already linked its sample references via the READ convention, ours
added a Source section; merged both). Also converted the 3 remaining
plain-backtick sample references in this PR to the READ-convention
markdown-link form, same fix as #156/#157/#158.
This commit is contained in:
parent
1392521a8e
commit
69b09db3a6
6 changed files with 100 additions and 16 deletions
|
|
@ -4,6 +4,7 @@ codeunit 50142 "Sample Test Library"
|
|||
|
||||
var
|
||||
Initialized: Boolean;
|
||||
SharedItemNo: Code[20];
|
||||
RollBackMsg: Label 'Revert back the tables to their original state.';
|
||||
|
||||
local procedure Initialize()
|
||||
|
|
@ -12,23 +13,62 @@ codeunit 50142 "Sample Test Library"
|
|||
exit;
|
||||
|
||||
CreateSharedFixtureData();
|
||||
// BUG: no Commit() here. The fixture below is still inside this
|
||||
// test method's own transaction.
|
||||
Initialized := true;
|
||||
end;
|
||||
|
||||
local procedure CreateSharedFixtureData()
|
||||
var
|
||||
Item: Record Item;
|
||||
begin
|
||||
// insert master/setup data shared across every test in this codeunit
|
||||
Item.Init();
|
||||
Item."No." := 'SAMPLE-SHARED';
|
||||
Item.Insert(true);
|
||||
SharedItemNo := Item."No.";
|
||||
end;
|
||||
|
||||
[Test]
|
||||
procedure FirstTestUsesSharedFixture()
|
||||
var
|
||||
Item: Record Item;
|
||||
begin
|
||||
Initialize();
|
||||
|
||||
// exercise/verify against the shared fixture, then make scratch changes of its own
|
||||
Item.Get(SharedItemNo);
|
||||
Item.Description := 'Scratch change this test makes and does not need to keep.';
|
||||
Item.Modify();
|
||||
|
||||
asserterror Error(RollBackMsg);
|
||||
// the deliberate rollback above also erases the never-committed fixture;
|
||||
// Initialized still reads true on the next test, but the rows are gone
|
||||
// The deliberate rollback above also erases the never-committed
|
||||
// fixture from CreateSharedFixtureData(). Initialized still reads
|
||||
// true on the next test, but the row it points at is gone.
|
||||
end;
|
||||
|
||||
[Test]
|
||||
procedure SecondTestStillFindsSharedFixture()
|
||||
var
|
||||
Item: Record Item;
|
||||
begin
|
||||
Initialize();
|
||||
|
||||
// Fails here: Initialize() saw Initialized = true and returned
|
||||
// immediately, so it never recreated the fixture - and the first
|
||||
// test's rollback took the original row with it.
|
||||
Item.Get(SharedItemNo);
|
||||
end;
|
||||
}
|
||||
|
||||
codeunit 50143 "Sample Test Runner"
|
||||
{
|
||||
// Codeunit isolation alone does not save this fixture: TestIsolation
|
||||
// only controls whether committed changes survive between methods, and
|
||||
// this fixture was never committed in the first place.
|
||||
Subtype = TestRunner;
|
||||
TestIsolation = Codeunit;
|
||||
|
||||
trigger OnRun()
|
||||
begin
|
||||
Codeunit.Run(Codeunit::"Sample Test Library");
|
||||
end;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ codeunit 50142 "Sample Test Library"
|
|||
|
||||
var
|
||||
Initialized: Boolean;
|
||||
SharedItemNo: Code[20];
|
||||
RollBackMsg: Label 'Revert back the tables to their original state.';
|
||||
|
||||
local procedure Initialize()
|
||||
|
|
@ -17,17 +18,55 @@ codeunit 50142 "Sample Test Library"
|
|||
end;
|
||||
|
||||
local procedure CreateSharedFixtureData()
|
||||
var
|
||||
Item: Record Item;
|
||||
begin
|
||||
// insert master/setup data shared across every test in this codeunit
|
||||
Item.Init();
|
||||
Item."No." := 'SAMPLE-SHARED';
|
||||
Item.Insert(true);
|
||||
SharedItemNo := Item."No.";
|
||||
end;
|
||||
|
||||
[Test]
|
||||
procedure FirstTestUsesSharedFixture()
|
||||
var
|
||||
Item: Record Item;
|
||||
begin
|
||||
Initialize();
|
||||
|
||||
// exercise/verify against the shared fixture, then make scratch changes of its own
|
||||
Item.Get(SharedItemNo);
|
||||
Item.Description := 'Scratch change this test makes and does not need to keep.';
|
||||
Item.Modify();
|
||||
|
||||
asserterror Error(RollBackMsg);
|
||||
// Rolls back the Modify() above, but not the fixture: that was
|
||||
// already committed inside Initialize().
|
||||
end;
|
||||
|
||||
[Test]
|
||||
procedure SecondTestStillFindsSharedFixture()
|
||||
var
|
||||
Item: Record Item;
|
||||
begin
|
||||
// Runs after FirstTestUsesSharedFixture's deliberate rollback.
|
||||
// Initialize() sees Initialized = true and does nothing, but the
|
||||
// committed fixture it created earlier is still there to Get().
|
||||
Initialize();
|
||||
|
||||
Item.Get(SharedItemNo);
|
||||
end;
|
||||
}
|
||||
|
||||
codeunit 50143 "Sample Test Runner"
|
||||
{
|
||||
// Codeunit isolation: everything this codeunit's tests commit,
|
||||
// including the shared fixture, survives from one test method to the
|
||||
// next, and rolls back only once every method in the codeunit has run.
|
||||
Subtype = TestRunner;
|
||||
TestIsolation = Codeunit;
|
||||
|
||||
trigger OnRun()
|
||||
begin
|
||||
Codeunit.Run(Codeunit::"Sample Test Library");
|
||||
end;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,19 +15,19 @@ A test method with no `[TransactionModel(...)]` attribute defaults to `AutoCommi
|
|||
|
||||
What an early `Commit()` inside `Initialize()` actually guards against is the test method's *own later, deliberate* rollback — the BCApps cleanup idiom of ending a test with `asserterror Error(SomeLabel)` to undo demo-data mutations that method made, so the run doesn't permanently dirty the database. Per the documented `Codeunit.Run` transaction semantics, changes are committed at the end of an execution "unless an error occurs" — an unhandled error rolls back whatever wasn't already committed. `Commit()` closes out the fixture's own transaction immediately, so it is unaffected by whatever the rest of that method does afterward, including that end-of-test error. Without the early `Commit()`, the same deliberate rollback wipes out the fixture too, even though `IsInitialized` still reads `true` on the next test, since it's a plain variable, not persisted data. BCApps' `codeunit 134915 "ERM Online Mapping Setup"` shows exactly this shape: no `TransactionModel` attribute, `Commit()` inside a lazy `Initialize()`, and the test itself ends with `asserterror Error(RollBackMessage)`.
|
||||
|
||||
Protecting the fixture from that same-method rollback is necessary but not sufficient for the fixture to reach a *later* test method — that also depends on the executing test runner's `TestIsolation`. Under `Disabled` (the property's own documented default) or `Codeunit` (used by BCApps' own `TestRunner`, `CLITestRunner`, and `SnapTestRunner` codeunits), nothing rolls back until the whole test codeunit finishes, so the already-committed fixture survives across every method run before then. Under `Function`, the runner rolls back all database changes — explicitly including ones already committed via `Commit()` — after every single test method; no amount of committing inside `Initialize()` makes a fixture shared across methods survive that regime, because the whole premise of a lazy, once-per-codeunit fixture doesn't hold when every method is isolated from every other.
|
||||
Protecting the fixture from that same-method rollback is necessary but not sufficient for the fixture to reach a *later* test method — that also depends on the executing test runner's `TestIsolation`. Under `Disabled` (the property's own documented default) or `Codeunit` (used by BCApps' own `TestRunner`, `CLITestRunner`, and `SnapTestRunner` codeunits), nothing rolls back until the whole test codeunit finishes, so the already-committed fixture survives across every method run before then. These two are not interchangeable, though: `Codeunit` rolls back everything once the codeunit's last method completes, so the environment is clean afterward; `Disabled` never rolls back anything at all — "tests are not isolated from each other" is the property's own description — so a fixture this pattern commits stays in the database permanently unless something else explicitly deletes it. Under `Function`, the runner rolls back all database changes — explicitly including ones already committed via `Commit()` — after every single test method; no amount of committing inside `Initialize()` makes a fixture shared across methods survive that regime, because the whole premise of a lazy, once-per-codeunit fixture doesn't hold when every method is isolated from every other.
|
||||
|
||||
## Best Practice
|
||||
|
||||
When a test method's own cleanup relies on ending in a deliberate error to roll back its scratch changes, call `Commit()` once, inside the lazy `Initialize()` guard, right after the shared fixture is created — before that cleanup-triggering error can run. This pattern only delivers a fixture shared across test methods when the executing runner's `TestIsolation` is `Disabled` or `Codeunit`; do not recommend it, or pair it with, a `Function`-isolated runner — that configuration undoes the committed fixture after every method regardless.
|
||||
When a test method's own cleanup relies on ending in a deliberate error to roll back its scratch changes, call `Commit()` once, inside the lazy `Initialize()` guard, right after the shared fixture is created — before that cleanup-triggering error can run. This pattern only delivers a fixture shared across test methods when the executing runner's `TestIsolation` is `Disabled` or `Codeunit`; do not recommend it, or pair it with, a `Function`-isolated runner — that configuration undoes the committed fixture after every method regardless. Recommend `TestIsolation = Codeunit`: it gives every method in the codeunit the same shared, committed fixture and still leaves the database clean once the codeunit finishes. Recommend `Disabled` only alongside an explicit, verified teardown step that removes the fixture data at the end of the run — without one, the committed fixture is permanent contamination, not a controlled trade-off.
|
||||
|
||||
See sample: `commit-shared-test-fixture-inside-lazy-initialize.good.al`.
|
||||
See sample: [`commit-shared-test-fixture-inside-lazy-initialize.good.al`](commit-shared-test-fixture-inside-lazy-initialize.good.al).
|
||||
|
||||
## Anti Pattern
|
||||
|
||||
A shared `Initialize()` guarded by `IsInitialized` that creates fixture records without committing, in a test method that ends with a deliberate `asserterror Error(...)` to undo its own scratch changes, run under a `Disabled`- or `Codeunit`-isolated test runner. That rollback also erases the never-committed fixture; the next test still finds `IsInitialized = true` but the rows it depends on are gone. (Under a `Function`-isolated runner the fixture is lost regardless of `Commit()`, for the unrelated reason above — that is a runner-configuration problem, not this anti-pattern.)
|
||||
|
||||
See sample: `commit-shared-test-fixture-inside-lazy-initialize.bad.al`.
|
||||
See sample: [`commit-shared-test-fixture-inside-lazy-initialize.bad.al`](commit-shared-test-fixture-inside-lazy-initialize.bad.al).
|
||||
|
||||
## Source
|
||||
|
||||
|
|
|
|||
|
|
@ -11,19 +11,24 @@ application-area: [all]
|
|||
|
||||
## Description
|
||||
|
||||
Codeunit 134926 "Table Relation Test" (shipped in BCApps' test app — only consumers that depend on the BC test libraries can subscribe to it) reads Table Relations Metadata tenant-wide across every installed app, not just the current one, and fails the moment a related field's type or length doesn't match what the relation requires — the related field must match the largest related field's length, and its type must match (except a field may relate to both `Code` and `Text`, which resolves to `Text`). A field with a legitimate, intentional relation shape has no per-field override in its own object definition; the check runs with no built-in escape hatch. The validation test method itself is `[Scope('OnPrem')]`: it only runs from an on-premises test surface, not from a cloud-targeted test app, so this whole exception mechanism — and the check it works around — is only reachable where that test can actually execute.
|
||||
Codeunit 134926 "Table Relation Test" (shipped in BCApps' test app — only consumers that depend on the BC test libraries can subscribe to it) reads Table Relations Metadata tenant-wide across every installed app, not just the current one, and validates each field's type and length against what its relations require — but the exact rule depends on whether that field has an *unconditional* relation (a `Table Relations Metadata` row with `Condition Field No. = 0`) among its relations, or only *conditional* ones:
|
||||
|
||||
- If any relation is unconditional, the field's length must equal *exactly* the largest related field's length, and its type must exactly match the required type — resolved to `Text` when the related fields themselves mix `Code` and `Text`.
|
||||
- If every relation for that field is conditional, the requirement relaxes: the field only needs to be *at least* as long as the largest related field (longer is accepted; only shorter fails), and when the required type is specifically `Code`, both a `Code` and a `Text` source field pass. That `Code`/`Text` tolerance is conditional-only — it does not apply on the unconditional side, and it does not extend to a required type of `Text` (a `Code` source field does not satisfy a required `Text`).
|
||||
|
||||
A field with a legitimate, intentional relation shape outside both of these tolerances has no per-field override in its own object definition; the check runs with no built-in escape hatch beyond them. The validation test method itself is `[Scope('OnPrem')]`: it only runs from an on-premises test surface, not from a cloud-targeted test app, so this whole exception mechanism — and the check it works around — is only reachable where that test can actually execute.
|
||||
|
||||
## Best Practice
|
||||
|
||||
Subscribe to `OnAfterRemoveTableRelation` and call the codeunit's own `RemoveTableRelation(TableRelationsMetadata, TableID, FieldID, RelatedTableID, RelatedFieldID)` to strike the one known-valid relation before the test evaluates it, scoped as narrowly as the exception actually is. Because the test itself is `[Scope('OnPrem')]`, do not recommend subscribing to it as a way to guard a cloud-targeted app's test suite — the subscription has no effect where the test never runs.
|
||||
|
||||
See sample: `table-relation-test-exclude-known-invalid-relations-via-event.good.al`.
|
||||
See sample: [`table-relation-test-exclude-known-invalid-relations-via-event.good.al`](table-relation-test-exclude-known-invalid-relations-via-event.good.al).
|
||||
|
||||
## Anti Pattern
|
||||
|
||||
Excluding an entire table's relations (or disabling the whole test codeunit) to work around one known exception. This discards the check's coverage for every other relation on that table, or in the app, not just the one that needed an exception.
|
||||
|
||||
See sample: `table-relation-test-exclude-known-invalid-relations-via-event.bad.al`.
|
||||
See sample: [`table-relation-test-exclude-known-invalid-relations-via-event.bad.al`](table-relation-test-exclude-known-invalid-relations-via-event.bad.al).
|
||||
|
||||
## Source
|
||||
|
||||
|
|
|
|||
|
|
@ -17,13 +17,13 @@ application-area: [all]
|
|||
|
||||
When the code under test returns a `Boolean` rather than raising an error, assert the value directly with `Assert.IsFalse(SomeFunc(), Msg)` (or `Assert.IsTrue` for the positive case). Reserve `asserterror` for statements expected to actually raise an error.
|
||||
|
||||
See sample: `use-assert-isfalse-not-asserterror-for-boolean-checks.good.al`.
|
||||
See sample: [`use-assert-isfalse-not-asserterror-for-boolean-checks.good.al`](use-assert-isfalse-not-asserterror-for-boolean-checks.good.al).
|
||||
|
||||
## Anti Pattern
|
||||
|
||||
`asserterror Assert.IsTrue(SomeFunc(), Msg);` to verify `SomeFunc()` is `false`. It passes today because `Assert.IsTrue` happens to raise an error on failure, but it verifies the assertion helper's error-raising behavior, not the value under test.
|
||||
|
||||
See sample: `use-assert-isfalse-not-asserterror-for-boolean-checks.bad.al`.
|
||||
See sample: [`use-assert-isfalse-not-asserterror-for-boolean-checks.bad.al`](use-assert-isfalse-not-asserterror-for-boolean-checks.bad.al).
|
||||
|
||||
## Source
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue