web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

D365BC Testing Automation - TestField AssertThat.ExpectedError

(0) ShareShare
ReportReport
Posted on by 30

I hope everyone is well! I'm working on Automated Testing for Business Central for a codeunit with a method that evaluates the No. Series parameter.

In this method, I use the TestField method to ensure that Default Nos. are enabled:

NoSeries.TestField("Default Nos.", true);

Now for the Test codeunit. Amongst my global variables, I define NonDefaultNoSeries as my No. Series record.

My OnRun procedure calls a local procedure that initializes my No. Series using Library - Utility with Default disabled:

local procedure CreateNoSeries()
    var
        LibraryUtil: Codeunit "Library - Utility";
        NoSeriesLine: Record "No. Series Line";
    begin
        LibraryUtil.CreateNoSeries(NonDefaultNoSeries, false, false, false);
        LibraryUtil.CreateNoSeriesLine(NoSeriesLine, NonDefaultNoSeries.Code, '10001', '99999');
    end;

Now that housekeeping is out of the way, I design a test to ensure the correct error arises with TestField method:

[Test]
[HandlerFunctions('ConfirmHandlerYes')]
procedure UPCMeth_NoSeriesValidation_NonDefault()
var
    ErrLabel: Label 'Default Nos. must be equal to ''Yes'' in No. Series: Code=%1. Current value is ''No''.';
begin
    // Using permissions that do not include Super.
    LibraryLowerPermissions.SetO365BusFull();
    // [GIVEN] A Postive No Series with Default disabled.
    // [WHEN] UPC No. Series Validation is run.
    // [THEN] Validation fails because No. Series Default is disabled.
    asserterror UPCMeth.ValidateUPCNoSeries(NonDefaultNoSeries, false);
    AssertThat.ExpectedError(StrSubstNo(ErrLabel, NonDefaultNoSeries.Code));
end;

Running my tests, I get an error for the Expect Error:

Assert.ExpectedError failed.
Expected: Default Nos. must be equal to 'Yes' in No. Series: Code=GU00000003. Current value is 'No'..
Actual: Default Nos. must be equal to 'Yes' in No. Series: Code=GU00000003. Current value is 'No'..

The text is virtually the same; so, I'm not sure what's going wrong here. Removing the Expected Error assertion resolves this problem.

I cannot drill into the TestField method to copy the error text label; so, I thought I would check with the community for feedback.

Is there anything I should do differently here? Is it even worth spending time to build tests around TestField?

I'm interested if there are any recommended resources describe best practices for automated testing in Business Central.

Thanks!

  • Suggested answer
    Marco Mels Profile Picture
    Microsoft Employee on at

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist.  If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 665 Super User 2026 Season 2

#2
YUN ZHU Profile Picture

YUN ZHU 418 Super User 2026 Season 2

#3
Teagen Boll Profile Picture

Teagen Boll 280 Super User 2026 Season 2

Last 30 days Overall leaderboard

Featured topics

Microsoft Training Manuals

Product updates

Dynamics 365 release plans