ABAP单元测试之osql

OSQL可以在ABAP单元测试运行时mock up特定的数据库表,使得单元测试独立于特定的运行系统。

其使用的工具类为:

  • CL_OSQL_TEST_ENVIRONMENT

示例代码 (在UT运行时,mock up数据库表BKPF) :

CLASS ltc_abc_fun02 DEFINITION FOR TESTING FINAL
                                   DURATION SHORT
                                   RISK LEVEL HARMLESS.
  PUBLIC SECTION.

  PRIVATE SECTION.
    CLASS-DATA:
      mo_osql_obj TYPE REF TO if_osql_test_environment.
    DATA:
      mo_cut TYPE REF TO cl_abc_fun02.  "class under test

    CLASS-METHODS: class_setup.
    CLASS-METHODS: class_teardown.
    METHODS: setup.
    METHODS: teardown.
    METHODS test_method_01   FOR TESTING.

ENDCLASS.

CLASS ltc_abc_fun02 IMPLEMENTATION.

  METHOD class_setup.

    DATA: lt_bkpf   TYPE TABLE OF bkpf.
    mo_osql_obj = cl_osql_test_environment=>create(
      i_dependency_list = VALUE #(
        ( 'BKPF' ) ) ).

    " Mock up: BKPF
    lt_bkpf = VALUE #( ( bukrs = 'CFP1' belnr = '0100000118' gjahr = '2018' ) ).
    mo_osql_obj->insert_test_data( i_data = lt_bkpf ).

  ENDMETHOD.

  METHOD class_teardown.
    mo_osql_obj->destroy( ).
  ENDMETHOD.
  
  METHOD: setup.
 	 " you could mock up DB table here as well
  ENDMETHOD.
  
  METHOD teardown.
    mo_osql_obj->clear_doubles( ).
  ENDMETHOD.

  METHOD test_method_01.
	" code to be tested 
	" xxx
	" xxx  
  ENDMETHOD.

ENDCLASS.

你可能感兴趣的:(ABAP,UT)