创建ABAP测试类
创立一个ABAP测量试验类以对CDS视图实行单元测量检验。有二个好的实施措施:为测量检验类起叁个和CUT一样/相似的名字,况兼拉长TEST的后缀。比方,对于CDS视图Salesorder_Items_By_TaxRate,测量检验类的名字能够是:Salesorder_Items_By_TaxRate_Test.
因为单元测量试验和CDS是例外的东西,一样/相似的名字可以扶持我们轻便的探究有关的测量试验。
CLASS Salesorder_Items_By_TaxRate_Test DEFINITION FINAL FOR TESTING
DURATION SHORT
RISK LEVEL HARMLESS.
PRIVATE SECTION.
...
...
ENDCLASS.
CLASS SO_ITEMS_BY_TAXRATE_TEST IMPLEMENTATION.
...
...
ENDCLASS.
CDS Test Double Framework
CDS Test Double Framework管理了以上的挑战,并且能够兑现CDS
entities测量检验的自动化:
- 在同样的Database
Schema中为每种依赖组件创建有的时候的可更新测验替身:- 复制注重组件表,但不复制任何数据。不复制注重数据库表的主键约束。那允许你轻轻巧松地插入测验数据,而不用忧虑数据的完整性。相似的,数据库索引也不会被复制。
- 为借助数据库视图创立数量库表。那几个表有着和依赖数据库视图一样的结构。
- 借助数据库functions(由带有参数的依赖CDS视图和依赖表function产生)会被复制,况兼function的测验替身达成会被修改为允许插入须要的测量试验数据。
- 在长期以来的Database Schema成立多少个CDS entity under
test(CUT)的不经常别本。从各类意义上来看,这些别本是为CUT服务的。在原来的CDS
entity中落到实处的逻辑在别本中平等存在,可是依据组件被替换为了呼应的测量检验替身。测验替身是由我们的CDS
Test Double Framework所创造的。
执行CDS
SELECT * FROM cdsfrwk_so_items_by_taxrate INTO TABLE @act_results.
Demo examples
您能够在那一个包里找到繁多单元测验的例子:
SABP_UNIT_DOUBLE_CDS_DEMO
依附组件是Table Function
Tables Function的测量检验替身的支配情势和别的的CDS视图同样。
运作CDS的单元测验
在ADT中档,展开满含全数CDS单元测量检验的ABAP测量检验类。右键选用Run
As->ABAP Unit
Test,或许利用ctrl+shift+f10组合键来运转单元测验。结果会在eclipse中的ABAP
Unit Runner视图中展现。
在意:到现在截止,还不可能在DDL源代码编辑器中一向运行单元测验。
动机
当今大家都理解单元测量试验对大家代码的益处。并且我们都承认它是付出进度中不能缺少的一片段。可是在把代码切换成数据库的格局下的时候,我们被粗鲁地打回了软件测量检验的乌黑年代…大家明日面前遇到着逻辑下推到ABAP
CDS entities后,代码要如何测量检验的难题。
CDS Test Double Framework允许开辟者们经过明显的ABAP Unit Test
Framework自动化地质度量试CDS entities。
本文链接:http://www.cnblogs.com/hhelibeb/p/7376232.html
俄文原著:Introduction to CDS Test Double Framework – How to write unit
tests for ABAP CDS
Entities?
概念单元测量试验方法
METHOD cuco_1_taxrate_1_item_1_ok.
ENDMETHOD.
概念固定格局
概念以下的设置拆卸方法。
运转方式cl_cds_test_environment=>create( i_for_entity =
‘<CDS under test>’
),隐式地在数据库中开创全数重视组件测量检验替身。那些点子在测量检验类中只应被调用三回。
"Fixture method class_setup is executed only once in the beginning of the execution of test class
METHOD class_setup.
"For parameter i_for_entity, specify the CDS view to be unit tested. This will create all the depended-on component Test doubles in the database.
environment = cl_cds_test_environment=>create( i_for_entity = 'Salesorder_Items_By_TaxRate' ).
ENDMETHOD.
METHOD class_teardown.
environment->destroy( ).
ENDMETHOD.
"Fixture method setup is executed once before each test method execution
<cod
METHOD setup.
environment->clear_doubles( ).
ENDMETHOD.
DCL对CUT的影响
你也足以打开/关闭给定的CDS的DCL。然而,在时下,假如您的CDS
DDL在测验时面对DCL影响的话,大家提出在运作测量试验时老是关闭DCL。在将来,使用DCL时会有大多挑选能够玩,何况能够应用剧中人物权限测量试验替身等。不过在当前的版本中,你须求小心在测验CDS
DDL时完全关闭DCL(假诺局地话)。在开荒DCL时些测量试验大概引致测量试验间断性失利,因为实际访谈调节剧中人物权限会被使用。因而,建议在你的生育测量检验中年天命之年是有四个:
DISABLE_DCL=ABAP_TRUE in the cl_cds_test_environment=>create(…)
含蓄NULL值的测量试验
为了在测量试验替身中插入null值,CDS Test Double Framework提供了章程:
cl_cds_test_data=>create( .. )->set_null_values( .. )
该方法可以显式地设定null值。
partners = VALUE #( ( client = sy-mandt bp_id = '1' ) ).
"Step 1 : define the list of columns into which NULL is inserted
i_null_vals = VALUE #( ( `address_guid` ) ).
"Step 2 : Create testdata and set the NULL value object
test_data = cl_cds_test_data=>create( i_data = partners )->set_null_values( i_null_vals ).
"Step 3 : Get test Double instance
DATA(partners_double) = environment->get_double( i_name = 'SNWD_BPA' ).
"Step 4 : Insert test data into test double
partners_double->insert( test_data ).
注脚输出——使用ABAP单元测量试验的预感
exp_results = VALUE #( ( so_id = 'ID' currency_code = 'EUR' sum_gross_amount = '1' tax_rate = '19.00' ) ).
cl_abap_unit_assert=>assert_equals(
act = lines( act_results )
exp = lines( exp_results ) ).
"The method looks as follows:
METHOD cuco_1_taxrate_1_item_1_ok.
"Step 1 : Insert testdata into the doubles
"Step 1.1 : create an instance of type snwd_so
sales_orders = VALUE #( ( client = sy-mandt node_key = '01' so_id = 'ID' ) ).
"Step 1.2 : Use the framework method CL_CDS_TEST_DATA=>create to create the test_data object
test_data = cl_cds_test_data=>create( i_data = sales_orders ).
"Step 1.3 : Use the framework method environment->get_double to the instance of the DOC double 'SNWD_SO'
DATA(sales_orders_double) = environment->get_double( i_name = 'SNWD_SO' ).
"Step 1.4 : Insert the testdata into the DOC double object
sales_orders_double->insert( test_data ).
"Repeat Step 1 for all the DOC doubles
sales_order_items = VALUE #( ( mandt = sy-mandt so_guid = '01' currency_code = 'EUR' gross_amount = '1' tax_rate = '19.00' ) ).
test_data = cl_cds_test_data=>create( i_data = sales_order_items ).
DATA(sales_order_items_double) = environment->get_double( i_name = 'CdsFrwk_DEMO_1' ).
sales_order_items_double->insert( test_data ).
"Step 2 : Execute the CDS
SELECT * FROM cdsfrwk_so_items_by_taxrate INTO TABLE @act_results.
"Step 3 : Verify Expected Output
exp_results = VALUE #( ( so_id = 'ID' currency_code = 'EUR' sum_gross_amount = '1' tax_rate = '19.00' ) ).
assert_so_items_by_taxrate( exp_results = exp_results ).
ENDMETHOD.
挑战
因为CDS entity中的逻辑运转在下层的数据库中(独立于abap
runtime),使用古板的ABAP注重注入实施方案以实现测量检验成为了不恐怕的作业。entity的借助组件急需在数据库中加上测验替身,况且大家亟须确认保障CDS
entity测量试验的时候数据库引擎调用/推行那么些测量试验替身(double)。
为了可以在CDS entity under test
(CUT)中可控地质衡量试逻辑,大家须求通过测量检验替身注射测验专项使用数据。那代表必得将测验数据插入到测量检验替身中,那样数据能够在CUT试行时被测验替身们回去。对于在ABAP
CDS上下文中具备固有的只读属性的信赖组件(比方数据库视图和数据库函数),那是一项特地的挑衅。
正文中首要的缩写:
CUT = CDS entity Under
Test
DOC = Depended-On
Component
受帮忙的测量试验场景
CDS Test Double framework支持为给定的CUT的以下DOC创制测量试验替身:
- DDIC tables
- DDIC views
- CDS views
- CDS views with Parameters
- External Views
- Table Functions
- CDS special functions. CURRENCY_CONVERSION and UNIT_CONVERSION
您能够张开/关闭给定CDS的DCL,更加多细节会在本文的前面提供。
更加多消息
报告bug的话,请为CSS组件BC-DWB-TOO-UT-CDS创建tickets。
小结:通过本文,你今后能够使用CDS Test Double
Framework高效地为你在CDS中落到实处的代码下推来写自动化的测量检验!
扩大阅读:浓密探討 Test Double、Dummy、Fake、Stub 、Mock 與
Spy
对特殊function的支持:CURRENCY_CONVERSION和UNIT_CONVERSION
CDS Test Double framework中得认为七个独辟蹊径的CDS function提供支撑:
"Step 1 : Create testdata using the special framework method create_currency_conv_data
test_data = cl_cds_test_data=>create_currency_conv_data( output = '399.21' )->for_parameters(
amount = '558.14'
source_currency = 'USD'
target_currency = 'EUR'
exchange_rate_date = '20150218'
).
"Step 2 : Get the double instance using the framework method get_double
DATA(curr_conv_data_double) = environment->get_double( cl_cds_test_environment=>currency_conversion ).
"Step 3 : Insert test_data into the double
curr_conv_data_double->insert( test_data ).
可用性
CDS Test Double Framework从NetWeaver AS ABAP 7.51
release开班可用。
有备无患输入——在测验替身中插入测量检验数据
METHOD cuco_1_taxrate_1_item_1_ok.
"Step 1 : Insert testdata into the doubles
"Step 1.1 : create an instance of type snwd_so. Note : CDS view Salesorder_Items_By_TaxRate depends on snwd_so.
sales_orders = VALUE #( ( client = sy-mandt node_key = '01' so_id = 'ID' ) ).
"Step 1.2 : Use the framework method CL_CDS_TEST_DATA=>create(..) to create the test_data object
test_data = cl_cds_test_data=>create( i_data = sales_orders ).
"Step 1.3 : Use the framework method environment->get_double(..) to create the instance of the double 'SNWD_SO'
DATA(sales_orders_double) = environment->get_double( i_name = 'SNWD_SO' ).
"Step 1.4 : Insert the testdata into the double depended-on component object
sales_orders_double->insert( test_data ).
"Repeat Step 1 for all the depended-on component doubles
sales_order_items = VALUE #( ( mandt = sy-mandt so_guid = '01' currency_code = 'EUR' gross_amount = '1' tax_rate = '19.00' ) ).
test_data = cl_cds_test_data=>create( i_data = sales_order_items ).
DATA(sales_order_items_double) = environment->get_double( i_name = 'CdsFrwk_DEMO_1' ).
sales_order_items_double->insert( test_data ).
...
ENDMETHOD.
如何使用CDS Test Double Framework写单元测量试验?
在下一些,大家会因而被分布应用的ABAP Unit Test
Framework为以下的CDS视图创设单元测量检验。
@AbapCatalog.sqlViewName: 'zSo_Items_By_1'
@EndUserText.label: 'Aggregations/functions in SELECT list'
@AbapCatalog.compiler.compareFilter: true
define view Salesorder_Items_By_TaxRate
as select from CdsFrwk_Sales_Order_Item
association [1] to snwd_so as _sales_order on so_guid = _sales_order.node_key
{
so_guid,
coalesce ( _sales_order.so_id, '9999999999' ) as so_id,
currency_code,
sum( gross_amount ) as sum_gross_amount,
tax_rate,
_sales_order
}
group by
so_guid,
_sales_order.so_id,
currency_code,
tax_rate
测量检验什么?
单元测量试验应当注意于由一个给定视图完结的有价值的功效定义。不是有所的CDS视图都亟待多个单元测量检验。在促成单元测量检验从前,提议辨别出entity中与测量试验有关的地方。
平日,供给为一些含有了代码下推的主意的entity进行单元测量检验。潜在的测量试验候选者包涵:
Calculations and/or filters, conversions, conditional expressions 比如
CASE…THEN…ELSE or COALESCE, type changing CAST operations, cardinality
changes or checks against NULL values, JOIN behavior, complex where
conditions 等.
单元测验不采纳于测量试验那多个更适用于静态检查、集成测验等技艺的CDS
entities属性。倘若无法从单元测量检验中获得其余价值的话,也不应进行它,比如对于那么些简单的CDS投影视图。
依赖组件是蕴含参数的CDS视图
CDS Test Double Framework提供了
cl_cds_test_data=>create( .. )->for_parameters( .. )
来为富含参数的体系的测验替身插入数据。
METHOD eur_tax_rate_19_found.
"Step 1 : Insert testdata into the doubles
open_items = VALUE #( ( mandt = sy-mandt so_guid = '0F' tax_rate = '19.00' so_id = '1' ) ).
i_param_vals = VALUE #( ( parm_name = `pCuCo` parm_value = `EUR` ) ).
"CdsFrwk_demo_3 is a CDS view with parameters. Use framework method ->for_parameters( ) to insert test data
test_data = cl_cds_test_data=>create( i_data = open_items )->for_parameters( i_param_vals ).
DATA(open_items_double) = environment->get_double( 'CdsFrwk_demo_3' ).
open_items_double->insert( test_data ).
...
...
ENDMETHOD.