Unit Test in ABAP

公司上CRM项目后,在Netweaver平台上ABAP终于支持Unit Test,相对于JUnit相比,ABAP Unit有自己鲜明的特点:

1、ABAP Unit是语言级的支持,不像JUnit是以第三方类库的形式出现。

2、ABAP Unit可以和主程序写在一个文件中,而且是推荐这样做的。

3、在TestCase里面可以定义两个pseudo comments,其中Risk_Level是必须定义的。

SDN上面有一个不错的教程:https://www.sdn.sap.com/irj/sdn/weblogs?blog=/pub/wlg/1088

下面是一个具体的例子:

*&———————————————————————*
*& Report
Z_UNIT_TEST1
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

REPORT
z_unit_test1.

TABLES: but000.

DATA: it_but000 LIKE TABLE OF but000 WITH HEADER LINE.

PARAMETERS: price TYPE p.

PERFORM minus_ten_percent CHANGING price.

WRITE price.

SELECT *
INTO TABLE it_but000
FROM but000.

*&———————————————————————*
*&

Form
minus_ten_percent
*&———————————————————————*
*

text
*———————————————————————-*
*

–>FPRICE

text
*———————————————————————-*
FORM minus_ten_percent CHANGING fprice TYPE p.
*
price = fprice * '0.9'.
fprice = fprice * '0.9'.
ENDFORM.

"minus_ten_percent

*———————————————————————-*
*

CLASS test
DEFINITAION FO
*———————————————————————-*
*
*———————————————————————-*
CLASS test DEFINITION FOR TESTING. "#AU Risk_Level Harmless
PRIVATE SECTION.

METHODS setup.

METHODS test_minus_ten_percent FOR TESTING.

METHODS test_internal_table_equal FOR TESTING.

METHODS teardown.
ENDCLASS.

"test
DEFINITAION FO

*———————————————————————-*
*

CLASS test IMPLEMENTATION
*———————————————————————-*
*
*———————————————————————-*
CLASS test IMPLEMENTATION.
METHOD setup.

WRITE AT /1 'setup is running!'.
ENDMETHOD.

"setup

METHOD test_minus_ten_percent.

WRITE AT /1 'test1 is running!'.

DATA: testprice TYPE p VALUE 200.

PERFORM minus_ten_percent

CHANGING testprice.

cl_aunit_assert=>assert_equals( act = testprice exp = 120

msg = 'ninety percent not calculated correctly').
ENDMETHOD.

"test_minus_ten_percent

METHOD test_internal_table_equal.

WRITE AT /1 'test2 is running!'.

DATA: matnr(18) TYPE c.

DATA: i_mara1 LIKE TABLE OF matnr,

i_mara2 LIKE TABLE OF matnr.

matnr = 'C075'. APPEND matnr TO i_mara1.

matnr = 'C076'. APPEND matnr TO i_mara1.

matnr = 'C077'. APPEND matnr TO i_mara1.

i_mara2[] = i_mara1[].
*

matnr = 'B088'. APPEND matnr to i_mara2.

cl_aunit_assert=>assert_equals( act = i_mara1 exp = i_mara2

msg = 'internal table1 not equals internal table 2!').
ENDMETHOD.

"test_internal_table_equal

METHOD teardown.

WRITE AT /1 'teardown is running!'.
ENDMETHOD.

"teardown
ENDCLASS.

"test IMPLEMENTATION

Comments are closed.