官术网_书友最值得收藏!

Pillar 2 – asserterror

Goal: Understand what the asserterror keyword means and learn how to apply it.

A substantial part of the business logic we implement specifies conditions under which a user action or a process should fail or stop to continue its execution. Testing the circumstances that lead to this failure are as important as testing the successful conclusion of an action or process. The second pillar allows us to write tests that are focused on checking whether errors do occur; a so called positive-negative or rainy path test. For example, that posting errors out because a posting date has not been provided, or that, indeed, a negative line discount percentage cannot be entered on a sales order line. To achieve this, the asserterror keyword should be applied in front of the calling statement:

asserterror <calling statement>

Let's use it in a new codeunit and run it:

codeunit 60001 MySecondTestCodeunit
{
Subtype = Test;

[Test]
procedure MyNegativeTestFunction()
begin
Error('MyNegativeTestFunction');
end;

[Test]
procedure MyPostiveNegativeTestFunction()
begin
asserterror Error('MyPostiveNegativeTestFunction');
end;
}

The MyPostiveNegativeTestFunction function is reported as a SUCCESS, and, consequently, no error message is recorded:

If the calling statement following the asserterror keyword throws an error, the system will continue executing the following statements. However, if the calling statement does not throw an error, the asserterror statement will cause one:

An error was expected inside an asserterror statement.

Where asserterror enables the test to continue with the next statement, it will not check the error as such. As we will see later, it is up to you to verify whether the expected did occur or not. If there is no verification on the specific error following the asserterror, any error will make your test pass.

If a successful positive-negative test does not report the error, this does not mean that the error did not occur. It is thrown, and, therefore, when a write transaction was performed, a rollback will happen. Any data modifications will disappear.
主站蜘蛛池模板: 乌拉特后旗| 沙雅县| 崇州市| 忻城县| 福州市| 安吉县| 年辖:市辖区| 牟定县| 辽源市| 洞口县| 宜君县| 克什克腾旗| 芮城县| 宁晋县| 始兴县| 尼玛县| 门头沟区| 昌平区| 陈巴尔虎旗| 永顺县| 青海省| 土默特左旗| 嫩江县| 莱州市| 修武县| 修武县| 珠海市| 宁陵县| 安溪县| 清流县| 大英县| 仙游县| 景宁| 伊吾县| 肃南| 商都县| 苗栗县| 平江县| 巴林右旗| 福泉市| 辽源市|