- Automated Testing in Microsoft Dynamics 365 Business Central
- Luc van Vugt
- 540字
- 2021-06-24 14:56:50
Pillar 3 – handler functions
Goal: Understand what handler functions are and learn how to build and apply them.
In our first test codeunit example, the Message statement results in the display of a message box. Unless we want to wait until a user presses the OK button, this message box stays there forever, halting the full execution of our test run. In order to be able to have a fully automated test run, we need a way to deal with any user interactions, such as a message box, a confirm dialog, a report request page, or a modal page.
For this, handler functions, also known as UI handlers, have been conceived. Handler functions are a special type of function that can only be created in test codeunits, and aim at handling UI interactions that exist in the code under test. Handler functions enable us to fully automate tests without the need of a real user to interact with them. As soon as specific UI interactions occur, and a handler has been provided for it, the platform takes care of calling the handler as a substitute for real user interactions.
Test function handler functions are defined by the FunctionType tag. The currently available values are shown in the following screenshot:

Each handler function addresses a different type of user interaction object and needs different parameters to let it interact adequately with the platform. Let VS Code and the AL extension be your guide in finding the right signature for a handler function. The following screenshot shows you the signature of a MessageHandler when you hover over the function name:

In case of a MessageHandler function, the signature is the text the message box would show to the user. Handing over this text to the MessageHandler enables you to determine if the right message was triggered.
So, to get the Message statement handled automatically in our first test codeunit, we should create a MessageHandler function:
[MessageHandler]
procedure MyMessageHandler(Message: Text[1024])
begin
end;
But this is only half of the job, as this handler needs to be linked to the test that will execute the code calling Message one way or another. The HandlerFunctions tag is used to do this. Each handler function needs to be called in a Test function and must be added to the HandlerFunctions tag as text. If multiple handlers are needed, these will make up a comma separated string:
HandlerFunctions('Handler1[,Handler2,…]')
Let's apply this to MyFirstTestFunction in a new codeunit and run it:
codeunit 60002 MyThirdTestCodeunit
{
Subtype = Test;
[Test]
[HandlerFunctions('MyMessageHandler')]
procedure MyFirstTestFunction()
begin
Message(MyFirstTestFunction);
end;
[MessageHandler]
procedure MyMessageHandler(Message: Text[1024])
begin
end;
}
Instantly, rather than showing a message box first, the resume message of the execution of the whole test codeunit is shown:

- 上級(jí)關(guān)照與員工信任:三種歸因理論分析
- 敏捷人才管理
- 策略管理:不懂設(shè)計(jì),你怎么帶團(tuán)隊(duì)?
- 企業(yè)風(fēng)險(xiǎn)管理數(shù)字化轉(zhuǎn)型:方法論與實(shí)踐
- 服務(wù)設(shè)計(jì)驅(qū)動(dòng)的革命:引發(fā)用戶追隨的秘密
- 成就:優(yōu)秀管理者成就自己,卓越管理者成就他人
- 中醫(yī)藥上市公司發(fā)展質(zhì)量與路徑研究:基于財(cái)務(wù)數(shù)據(jù)的分析
- 成就卓越:領(lǐng)導(dǎo)者的第一本高管教練書
- 產(chǎn)品經(jīng)理方法論
- 文秘人員“辦文”實(shí)務(wù):規(guī)范與方法
- 高管變更、政治關(guān)聯(lián)與財(cái)務(wù)環(huán)境恢復(fù)
- 超市賣場(chǎng)定價(jià)策略與品類管理
- 技術(shù)創(chuàng)業(yè):從創(chuàng)意到企業(yè)(第4版)
- 沃爾瑪零售業(yè)真經(jīng)
- 管理不“狠”,公司不穩(wěn)