Not on 756? No function module test framework? No problem!

Not on 756? No function module test framework? No problem!

TLDR: Use it standalone on GitHub actions, move the unit tests into the system when its upgraded to 756+


The Function Module Test Framework allows replacing/doubling functionality in function modules during unit test execution, this is sometimes very useful when testing logic utilizing function modules.

Its also possible to manually wrap the function module in a class + interface + factory for dependency injection, but it easily becomes a lot of scaffolding to call a single function module.


How does it work?

The below example outlines how it works overall inside a unit test. First setup the function modules to be doubled, when called during unit test, the function module is redirected to "METHOD if_ftd_invocation_answer~answer"

  METHOD test.

    DATA lt_deps    TYPE if_function_test_environment=>tt_function_dependencies.
    DATA li_env     TYPE REF TO if_function_test_environment.
    DATA lv_message TYPE string.

    INSERT 'ABC' INTO TABLE lt_deps.
    li_env = cl_function_test_environment=>create( lt_deps ).

    li_env->get_double( 'ABC' )->configure_call( )->ignore_all_parameters( )->then_answer( me ).

    CALL FUNCTION 'ABC'
      EXPORTING
        integer = 2
      IMPORTING
        message = lv_message.

    cl_abap_unit_assert=>assert_equals(
      act = lv_message
      exp = gc_hello_world ).

    li_env->clear_doubles( ).

  ENDMETHOD.

  METHOD if_ftd_invocation_answer~answer.

    DATA ref TYPE REF TO data.

    FIELD-SYMBOLS <fs> TYPE any.

    ref = arguments->get_importing_parameter( 'INTEGER' ).
    ASSIGN ref->* TO <fs>.

    cl_abap_unit_assert=>assert_equals(
      act = <fs>
      exp = 2 ).

    result->get_output_configuration( )->set_exporting_parameter(
      name  = 'MESSAGE'
      value = gc_hello_world ).

  ENDMETHOD.        

What's the problem?

The framework is delivered as part of 756, not everyone is upgraded to that version yet. But every developer needs to test functionality in the system in an easy way.


What's the solution?

I suggest writing and running tests outside of the real system, and when the system is upgraded, then move the test cases into the system.

open-abap can run ABAP code standalone, and editing ABAP files is made easy through the Standalone ABAP Development Extension Pack.

The above example is a testcase in open-abap, which runs on GitHub actions for free, without access to any internal corporate network.


??


要查看或添加评论,请登录

Lars Hvam Petersen的更多文章

  • ABAP and Excel, its never been easier

    ABAP and Excel, its never been easier

    abap2xlsx is an open source project making it easy to read and write Excel files with ABAP. In ABAP its always been a…

    9 条评论
  • ABAP Cloud: Getting rid of those pesky macros

    ABAP Cloud: Getting rid of those pesky macros

    The ABAP Cloud Programming Model does not allow using macros, however lots of old code still use macros, so how do we…

    9 条评论
  • Happy Birthday abapGit, 10 years!

    Happy Birthday abapGit, 10 years!

    The Past On a dark and lonely summer morning, 10 years ago, I did the first commit of something that ended up being…

    19 条评论
  • Scrutinizing ABAP GenAI example from SAP Sapphire keynote

    Scrutinizing ABAP GenAI example from SAP Sapphire keynote

    Below is the code example from yesterdays SAP Sapphire Keynote, ABAP GenAI example At the point of writing, its not…

    46 条评论
  • Stuff I'd like removed from the #ABAP roadmap

    Stuff I'd like removed from the #ABAP roadmap

    SAP provides roadmap information at https://help.sap.

    21 条评论
  • Running abap2UI5 backend in browser

    Running abap2UI5 backend in browser

    abap2UI5 by Lars Kaldewey et al. allows to easily write ui5 applications with backend ABAP code.

    3 条评论
  • Running Scrypt in Rust on ABAP on Raspberry PI Zero

    Running Scrypt in Rust on ABAP on Raspberry PI Zero

    Running ABAP code on a Raspberry PI Zero has been possible for some time. Executing JavaScript on it is slow, running…

    15 条评论

社区洞察

其他会员也浏览了