一.概述
在项目中我们将常会碰到逻辑十分复杂的程序, 但我们只修改复杂程序的一小部分,对于修改部分的测试会很难进行。首先我们可能不知道程序全部的逻辑,编写测试case会十分的复杂,需要浪费大量的时间。因此ABAP Unit Test则显现出了它的优势。
二.实现方式
*Unit test code========================================================
*----------------------------------------------------------------------*
* CLASS mytest DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mytest DEFINITION "#AU Risk_Level Harmless
FOR TESTING. "#AU Duration Short
PRIVATE SECTION.
METHODS mytest_factorial FOR TESTING.
METHODS mytest_divide FOR TESTING.
METHODS mytest_divide_0 FOR TESTING.
METHODS mytest_frm_test FOR TESTING.
ENDCLASS. "mytest DEFINITION
*----------------------------------------------------------------------*
* CLASS mytest IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mytest IMPLEMENTATION.
METHOD mytest_factorial.
CREATE OBJECT w_obj.
result = w_obj->factorial( 4 ).
cl_aunit_assert=>assert_equals( act = result
exp = '24'
msg = 'Factorial Not calculated Correctly'
level = '0'
quit = '2'
tol = '0.999'
).
ENDMETHOD. "mytest
METHOD mytest_divide.
CREATE OBJECT w_obj.
DATA: lv_res TYPE f,
lv_res_exp TYPE f,
lv_res_str TYPE string.
lv_res_exp = 4 / 32.
lv_res_str = lv_res_exp.
w_obj->divide( EXPORTING opr1 = 32
opr2 = 4
IMPORTING result = lv_res ).
cl_aunit_assert=>assert_equals( act = lv_res
exp = lv_res_str
msg = 'Divide Not calculated Correctly'
level = '0'
quit = '2'
tol = '0.999'
).
ENDMETHOD. "mytest
METHOD mytest_divide_0.
CREATE OBJECT w_obj.
DATA: lv_res TYPE f,
lv_res_exp TYPE f,
lv_res_str TYPE string.
TRY.
w_obj->divide( EXPORTING opr1 = 0
opr2 = 4
IMPORTING result = lv_res ).
lv_res_str = res.
CATCH cx_sy_arithmetic_error INTO exc.
lv_res_str = exc->get_text( ).
ENDTRY.
cl_aunit_assert=>assert_equals( act = lv_res_str
exp = 'Division by zero'
msg = 'Divide Not calculated Correctly'
level = '0'
quit = '2'
tol = '0.999'
).
ENDMETHOD. "mytest
METHOD mytest_frm_test.
DATA: lv_res TYPE i.
PERFORM frm_test USING 1 2
CHANGING lv_res.
cl_aunit_assert=>assert_equals( act = lv_res
exp = 4
msg = 'PERFORM frm_test calculated Correctly'
level = '0'
quit = '2'
tol = '0.999'
).
ENDMETHOD. "mytest
ENDCLASS. "mytest IMPLEMENTATION
执行Unit test:
Report:
如果测试通过,会出现成功的message
如果单元测试中的代码运行不通过,会出现以下界面
通过方法的名称,可以查看测试方法的哪些部分在测试期间导致了错误:
参考文档:
How to write these tests:
ABAP unit is based on ABAP objects. The global class CL_AUNIT_ASSERT contains methods which can be used for testing .Tests are implemented in local classes. Inside the local class the necessary method from the global class can be called for testing. These test classes can be written inside the program for which the test is to be done. It will not affect our production code in anyways.
Difference between Ordinary class and Test class:
Both the test class and test method should have FOR TESTING addition.
Ex:
CLASS mytest DEFINITION FOR TESTING.
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS.
Methods in CL_AUNIT_ASSERT for Testing:
ASSERT_EQUALS
ASSERT_DIFFERS
ASSERT_BOUND
ASSERT_NOT_BOUND
ASSERT_INITIAL
ASSERT_NOT_INITIAL
ASSERT_CHAR_CP
ASSERT_CHAR_NP
ASSERT_EQUALS_F
FAIL
ABORT
ASSERT_EQUALS - Checks the equality of two data objects.
ASSERT_DIFFERS - Checks for the difference of two data objects.
ASSERT_BOUND - checks for the validity of the reference of a reference variable.
ASSERT_INITIAL - checks whether the reference of a reference variable is invalid.
ASSERT_NOT_INITIAL - checks whether the data object is not having its initial value.
ASSERT_SUBRC - checks for the specific value of SY-SUBRC.
ASSERT_EQUALS:
ASSERT_EQUALS is one of the methods in the class CL_AUNIT_ASSERT. This method can be used for checking equality of two data objects.
The parameters of the method:
ACT - Actual result
EXP - Expected Result
MSG - Message to be displayed in the result
LEVEL - Error level (Tolerable/Critical/fatal)
QUIT - If the test fails, flow level is controlled using this
(NO/METHOD/CLASS/PROGRAM)
TOL - Tolerance level for F
Levels:
0 - Tolerable
1 - Critical
2 - Fatal
Quit:
No ( 0 ) – It will continue the current test Method.
Method ( 1 ) – It will interrupt the current test method
Class ( 2 ) – It will interrupt the current test class.
Program ( 3 ) – abandon execution of all test classes for the tested program.
Tolerance:
If the tolerance limit specified is exceeded then error is shown.
Ex:
Actual result – 24.
Expected Result – 25.
Tolerance – 0.9999.
Difference = Expected Result - Actual result.
= 1 > tolerance.
Therefore displays an error.