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

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.
主站蜘蛛池模板: 柞水县| 翁源县| 达日县| 肇庆市| 龙胜| 宁乡县| 吕梁市| 新沂市| 西乌珠穆沁旗| 依安县| 宣城市| 靖西县| 辛集市| 吉水县| 鹿邑县| 武鸣县| 屏山县| 小金县| 卓资县| 沭阳县| 义马市| 万全县| 鄂州市| 宁南县| 广安市| 老河口市| 湖口县| 济宁市| 丹凤县| 礼泉县| 延安市| 舟山市| 社旗县| 四会市| 乐安县| 康定县| 石棉县| 高唐县| 泸州市| 高台县| 鹰潭市|