- Automated Testing in Microsoft Dynamics 365 Business Central
- Luc van Vugt
- 413字
- 2021-06-24 14:56:49
Test functions
A Test function is defined by the FunctionType tag:
[Test]
procedure MyFirstTestFunction()
begin
end;
This makes it different from a standard function:
- It has to be global
- It cannot have arguments
- It yields a result, which is either SUCCESS or FAILURE
When SUCCESS is returned by a test, it means that no error occurred in the execution of the test. Consequently, when FAILURE is returned, the test execution did throw an error. This error could be due to various reasons, such as the following:
- Code execution hitting a TestField, FieldError, or Error call
- Data modifications not being fulfilled because of version conflicts, primary key conflicts, or locks
The latter, a Test function returning FAILURE, brings us to another typicality of a test codeunit—when a test fails, the execution of a test codeunit doesn't halt. It continues to execute the next Test function.
Let's build two simple tests, one returning SUCCESS and the other FAILURE:
codeunit 60000 MyFirstTestCodeunit
{
Subtype = Test;
[Test]
procedure MyFirstTestFunction()
begin
Message('MyFirstTestFunction');
end;
[Test]
procedure MySecondTestFunction()
begin
Error('MySecondTestFunction');
end;
}
Now you can run them.
As test functions are executed from top to bottom, the message thrown by MyFirstTestFunction will show the following screenshot first:

After that, the following message is shown, being a resume message of the execution of the whole test codeunit:

To be able to run the test codeunit, I built a simple page, MyTestsExecutor, with an action calling MyFirstTestCodeunit:
page 60000 MyTestsExecutor
{
PageType = Card;
ApplicationArea = All;
UsageCategory = Tasks;
Caption = 'My Test Executor';
actions
{
area(Processing)
{
action(MyFirstTestCodeunit)
{
Caption = 'My First Test Codeunit';
ToolTip = 'Executes My First Test Codeunit';
ApplicationArea = All;
Image = ExecuteBatch;
RunObject = codeunit MyFirstTestCodeunit;
}
}
}
}
If you are following me using the code on GitHub and have a hard time opening the MyTestsExecutor page, use any of the following options:
- Set startupObjectType to Page and startupObjectId to 60000 in the launch.json
- Add ?page=6000 to the web client URL in the address bar of your browser: http://localhost:8080/BC130/?page=6000
- Use Alt + Q, Tell me what you want, in the web client and search for My Test Executor
- Launch the page directly from the VS Code, making use of a VS Code AL extension such as CRS AL Language Extension