用CL_AUNIT_ASSERT 类进行ABAP单元测试 ABAP Unit TEST
2011-08-07 14:46
525 查看
转自:http://www.saptechnical.com/Tutorials/OOPS/ABAPUnit/Index.htm
Understanding "ABAP Unit"
By Nithya Murugesan, YASH TechnologiesIntroduction:
It is a best practice to modularize our programs as much as we can for better programming. If we want to check one particular module like subroutines, function modules or classes for bugs then we can do it using ABAP Unit. ABAP Unit is a tool for unit
testing of ABAP programs.
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.
Example Program:
Let us consider an example for ABAP unit test using the method ASSERT_EQUALS to check the equality of two data objects. In this program, we have two methods divide and factorial in a local class MATH. We want to test the factorial method. So we have
created one class and one method MYTEST for testing. In the test method implementation we have called the factorial method and so the data object RESULT is populated. Now we are going to compare the actual data object (RESULT) with the expected result.
For that we are calling the ASSERT_EQUALS from the global class passing the expected result.
REPORT ZWTEST1 .
class math DEFINITION.
PUBLIC SECTION.
METHODS divide
IMPORTING opr1 type i
opr2 type i
EXPORTING result type f
RAISING cx_sy_arithmetic_error.
methods factorial
IMPORTING n type i
RETURNING VALUE(fact) type i.
ENDCLASS.
class math implementation.
method divide.
result = opr1 / opr2.
ENDMETHOD.
method factorial.
fact = 1.
if n = 0.
return.
else.
do n times.
fact = fact * sy-index.
enddo.
endif.
endmethod.
ENDCLASS.
start-of-SELECTION.
data w_obj type ref to math.
data exc type ref to cx_sy_arithmetic_error.
DATA res TYPE f.
DATA result TYPE i.
DATA text TYPE string.
CREATE OBJECT w_obj.
TRY.
w_obj->divide( EXPORTING opr1 = 32 opr2 = 4
IMPORTING result = res ).
WRITE : res.
text = res.
CATCH cx_sy_arithmetic_error INTO exc.
text = exc->get_text( ).
MESSAGE text TYPE 'I'.
ENDTRY.
CREATE OBJECT w_obj.
COMPUTE result = w_obj->factorial( 4 ).
WRITE :/ 'The result for factorial is:',result.
*----------------------------------------------------------------------*
* CLASS mytest DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mytest DEFINITION "#AU Risk_Level Harmless
FOR TESTING. "#AU Duration Short
PRIVATE SECTION.
METHODS mytest FOR TESTING.
ENDCLASS. "mytest DEFINITION*----------------------------------------------------------------------*
* CLASS mytest IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS mytest IMPLEMENTATION.
METHOD mytest.
CREATE OBJECT w_obj.
result = w_obj->factorial( 4 ).
cl_aunit_assert=>assert_equals( act = result
exp = '240'
msg = 'Factorial Not calculated Correctly'
level = '0'
quit = '2'
tol = '0.999'
).
ENDMETHOD. "mytest
ENDCLASS.
注释: 要激活程序 在左上角菜单 program----> test---->unit test
使用code inspector在program ---> check--->code inspector
要ABAP Unit出来 需要自己创建一个默认的entry 在code inspector
相关文章推荐
- VS2012 Unit Test —— 我对接口进行单元测试使用的技巧
- iOS开发 简述使用OCUnit对程序进行单元测试(UnitTest)
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- python 单元测试(assert 或者 unittest.TestCase)
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- VS2012 Unit Test —— 我对接口进行单元测试使用的技巧
- 用flask开发个人博客(28)—— 利用unittest进行单元测试
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- VS2012 Unit Test(Void, Action, Func) —— 对无返回值、使用Action或Func作为参数、多重载的方法进行单元测试
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码(详解)
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码(详解)
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码 (实例) (轉載)
- Python+Unittest+自动化:使用Python进行单元测试
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码
- (1)写一个程序,用于分析一个字符串中各个单词出现的频率,并将单词和它出现的频率输出显示。(单词之间用空格隔开,如“Hello World My First Unit Test”); (2)编写单元测试进行测试; (3)用ElcEmma查看代码覆盖率,要求覆盖率达到100%。
- python使用unittest进行单元测试
- 使用 Visual Studio 2005 Team System 进行单元测试并生成用于 Unit Test Framework 的源代码 (实例)