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

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.
主站蜘蛛池模板: 台南县| 九龙县| 平和县| 嘉峪关市| 台南市| 班玛县| 临湘市| 繁昌县| 奎屯市| 武鸣县| 武穴市| 正镶白旗| 玉龙| 凯里市| 鹤岗市| 鄂尔多斯市| 康保县| 卫辉市| 温宿县| 长汀县| 天津市| 广汉市| 慈溪市| 南投县| 确山县| 石柱| 临清市| 同德县| 绍兴县| 西城区| 阳东县| 甘谷县| 苏尼特左旗| 高密市| 海口市| 比如县| 剑川县| 城固县| 镇坪县| 玛沁县| 湟中县|