四种ABAP单元测试隔离(test isolation)技术

简介: 四种ABAP单元测试隔离(test isolation)技术

Hi friends,

As far as I know test isolation is widely used in SAP internal to build unit test code, at least in my team. Test isolation in unit test means that in your production code you make use of some API(class method/Function module) which are developed by other team, and you would not like to really consume them during your unit test, since you would not accept that most red lights in your unit test report are caused by the bugs in those external API,right? Instead, you expect that the call of those external API during your unit test execution will be replaced by some dummy code written by yourself.

I will show you four different ways to achieve that.

The example comes from one productive class in my project. For simplicity reasons I don’t list unrelevant code here. The class is ZCL_DESTRUCTION_IMG_TOOL_S1.

image.png

method RUN.

   DATA: lv_social_post_id TYPE string.

   fill_worklist( ).

   LOOP AT mt_worklist INTO lv_social_post_id.

      IF dependent_object_existed( lv_social_post_id ) = abap_false.

         delete( lv_social_post_id ).

      ENDIF.

   ENDLOOP.

 endmethod.

The worklist is just hard-coded:

method FILL_WORKLIST.

    CLEAR: mt_worklist.

    APPEND '20130001' TO mt_worklist.

    APPEND '20130002' TO mt_worklist.

    APPEND '20130003' TO mt_worklist.

 endmethod.

The reason why test isolation is used in our unit test is because in method dependent_object_existed, we call an API provided by another team, and we don’t want that API to be really executed during our unit test execution. For demonstration reason, I use the following code to simulate the production code.

It means during the unit test on this class, the following code is NOT expected to be executed at all.

 

 method DEPENDENT_OBJECT_EXISTED.

    WRITE: / 'Productive code to check dependent object existence for ID: ' , iv_social_post_id.

 endmethod.

Approach1: test subclass instead

(1) Change the visibility of method DEPENDENT_OBJECT_EXISTED from private to protected. The idea is in this approach, we create a sub class ( a local test class) which inherits from ZCL_DESTRUCTION_IMG_TOOL_S1. Since the DEPENDENT_OBJECT_EXISTED is now protected, we have the chance to redefine it in the local test class.


image.pngimage.pngimage.pngwhen execution comes into run method, since the subclass only redefines the very method dependent_object_existed, so the execution of the left methods are still using the code of ZCL_DESTRUCTION_IMG_TOOL_S1. That means all methods of ZCL_DESTRUCTION_IMG_TOOL_S1 except dependent_object_existed will be covered by unit test.

image.pngimage.png

Approach2 – interface extraction + optional argument

The idea is to extract the logic written in method dependent_object_existed and put its implementation into method dependent_object_existed of a new interface ZIF_SOC_DEPENDENCY_DETECTOR instead. Via such abstraction, the loose coupling of dependency detection call and its implementation is achieved.


Necessary change on production class ZCL_DESTRUCTION_IMG_TOOL_S2 source code:


(1) Private method dependent_object_existed can be removed now.


image.pngimage.png

Approach3 – Dynamic detector initialization

Interface extraction is also necessary for this solution.

Necessary change on production class ZCL_DESTRUCTION_IMG_TOOL_S3’s source code:


(1) Create a local class to implement the productive logic of dependency detection just the same as approach2.

(2) Add a new static member attribute for technical name of dependency class name. Default value is local class name created in step1:

image.pngimage.pngimage.pngNo new attribute or method interface change is required in this solution. This solution should be used for test isolation whenever possible.

目录
相关文章
|
28天前
|
安全 测试技术
现代软件测试中的自动化技术应用及挑战
在当今数字化时代,软件测试的重要性日益凸显。本文探讨了现代软件测试中自动化技术的应用现状和挑战,分析了自动化测试在提高效率、降低成本、增强可靠性等方面的优势,同时也提出了自动化测试所面临的挑战和解决方案。
|
1月前
|
BI
如何使用动态 ABAP 程序生成技术,对 ABAP 系统标准的报表行为进行微调试读版
如何使用动态 ABAP 程序生成技术,对 ABAP 系统标准的报表行为进行微调试读版
13 0
|
27天前
|
人工智能 测试技术 虚拟化
现代软件测试中的自动化工具与技术
随着信息技术的迅猛发展,现代软件开发和测试领域也在不断创新。本文将探讨现代软件测试中自动化工具与技术的应用。通过分析自动化测试的优势、挑战以及最佳实践,帮助读者更好地理解当前软件测试领域的发展趋势,并为实际工作提供参考。
16 1
|
28天前
|
敏捷开发 供应链 测试技术
深入理解与应用软件测试中的Mock技术
【2月更文挑战第30天】 在现代软件开发过程中,单元测试是保证代码质量的重要手段。然而,对于高度依赖外部系统或服务的应用来说,传统的单元测试方法往往难以实施。Mock技术应运而生,它通过模拟外部依赖的响应,使开发者可以在隔离的环境中测试目标代码。本文将探讨Mock技术的概念、应用场景以及如何在软件测试中有效地使用Mock对象,以增强测试的灵活性和可靠性。
|
1月前
SAP ABAP ALV 的分组显示和 Subtotal 显示实现的技术步骤试读版
SAP ABAP ALV 的分组显示和 Subtotal 显示实现的技术步骤试读版
29 0
|
2月前
|
调度
【技术探讨】无线通信模块拉距测试,是否一定要带笔记本电脑?
对于Sub-G的无线模块通常通信距离较远可以达到公里级甚至数公里之远,而笔记本的续航时间通常是2-3个小时,很多用户测试到一半,不得不提前终止测试,回去给笔记本电脑充电
|
2月前
|
人工智能 测试技术 API
你知道哪些新兴的测试技术?
你知道哪些新兴的测试技术?
|
3月前
|
测试技术 Shell Android开发
随机测试 Monkey Test
随机测试 Monkey Test
|
3月前
|
安全 测试技术 数据库
常见测试技术都有哪些?
常见测试技术都有哪些?
|
3月前
|
人工智能 运维 搜索推荐
测试技术大会可以给我们带来什么?
测试技术大会可以给我们带来什么?