Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Test your report dataset to verify that customers receive complete and accurate data. Dataset tests check the data output, not the visual layout.
Use streams to test report output in Dynamics 365 Business Central without depending on direct access to the server file system. Codeunit 131007, Library - Report Dataset, provides a high-level API for this approach. File-based report APIs remain relevant for supported on-premises and legacy test scenarios.
Run a report and verify its dataset
We recommend using RunReportAndLoad for new dataset tests. It runs the report and loads the dataset into Library - Report Dataset. Use AssertElementWithValueExists or AssertElementWithValueNotExist to verify values.
The following example also calls Report.RunRequestPage and registers a [RequestPageHandler] to accept the default request-page settings. If your test doesn't need to open a request page, you can supply the required parameter XML directly to RunReportAndLoad.
Before running the example:
- Make
Library - Report Datasetand its dependencies available to your test app. - Use a dedicated test company with company information, general ledger setup, and an existing vendor.
- Create one persisted, nonzero vendor payment journal line in the
GENERALjournal template and theREPORTTESTbatch. These are example fixture names. Set up that template and batch, or replace the names in the sample. - Use a test transaction model that permits
Commit(). Complete any setup writes before opening the request page, and arrange cleanup of your test data.
codeunit 50105 MyReportTesting
{
Subtype = Test;
[Test]
[HandlerFunctions('RemittanceAdviceJournalRequestPageHandler')]
procedure TestingReports()
var
XmlParameters: Text;
LibraryReportDataset: Codeunit "Library - Report Dataset";
GenJournalLine: Record "Gen. Journal Line";
begin
GenJournalLine.SetRange("Journal Template Name", 'GENERAL');
GenJournalLine.SetRange("Journal Batch Name", 'REPORTTEST');
GenJournalLine.SetRange("Document Type", GenJournalLine."Document Type"::Payment);
GenJournalLine.SetRange("Account Type", GenJournalLine."Account Type"::Vendor);
GenJournalLine.SetFilter(Amount, '<>0');
GenJournalLine.FindFirst();
GenJournalLine.TestField("Account No.");
GenJournalLine.SetRecFilter();
Commit(); // Complete any setup writes before opening the request page.
XmlParameters := Report.RunRequestPage(Report::"Remittance Advice - Journal");
LibraryReportDataset.RunReportAndLoad(Report::"Remittance Advice - Journal", GenJournalLine, XmlParameters);
LibraryReportDataset.AssertElementWithValueExists('Amt_GenJournalLine', GenJournalLine.Amount);
end;
[RequestPageHandler]
procedure RemittanceAdviceJournalRequestPageHandler(var RemittanceAdviceJournal: TestRequestPage "Remittance Advice - Journal")
begin
RemittanceAdviceJournal.OK().Invoke();
end;
}
Request-page options and filters set in the handler are returned in XmlParameters when the page is accepted and applied when the report runs. The OK().Invoke() call explicitly accepts the page. It doesn't complete setup transactions or replace Commit().
Examples of this testing pattern are in codeunit 133770, ERM Remittance Report UT, and codeunit 134141, ERM Bank Reconciliation.
Tip
To run the report separately and load its output from an input stream, use LoadFromInStream.
Choose methods for the dataset format
TestRequestPage.SaveAsXML writes the legacy, preview-compatible XML dataset format. Report.SaveAsXML and Report.SaveAs with ReportFormat::Xml produce the current XML dataset format.
TestRequestPage.SaveAsXML and Report.SaveAsXML are file-based APIs. Report.SaveAs writes to an OutStream. RunReportAndLoad uses this stream-based route and loads the dataset from a Temp Blob, avoiding a dataset file write/read round trip.
The library supports both XML formats. In the current format, Column elements identify columns with name attributes and contain the field values. Methods with Tag in their names target the legacy format. They aren't supported for current-format XML, whether it's loaded from a stream or a file.
Use assertions that support the current format, such as AssertElementWithValueExists and AssertElementWithValueNotExist. A method without Tag in its name isn't necessarily compatible. For example, SetRange, SetRangeWithTrimmedValues, and AssertParameterValueExists depend on legacy structures.
Tip
From the Business Central client, you can export report results as raw data to an XML file. Use the file to help validate that the report returns the expected data. To export a report dataset to XML, run the report and select the Send to > XML (data only) on the request page.