Technical Articles
Do Unit Tests Exist?
This is not a philsophical question… π
For internal audits or just for curiosity reasons you might want to know if there are unit tests defined in a class. Of course you can open the class and check but you might not want to regulary check several classes again and again.
The following code selects all desired classes and displays the local test classes and their unit tests:
REPORT.
TYPES: BEGIN OF _test_class,
classname TYPE seoclsname,
testclass TYPE seoclsname,
unit_test TYPE seocpdname,
END OF _test_class,
_test_classes TYPE STANDARD TABLE OF _test_class WITH EMPTY KEY.
DATA test_classes TYPE _test_classes.
DATA test_class TYPE _test_class.
SELECT-OPTIONS clsnam FOR test_class-classname DEFAULT 'CL_AUNIT*' OPTION CP OBLIGATORY.
START-OF-SELECTION.
"read classes to check
SELECT * FROM seoclass
INTO TABLE @DATA(clt)
WHERE clsname IN @clsnam.
LOOP AT clt INTO DATA(cls).
test_class-classname = cls-clsname.
"get test classes and their unit tests
DATA(hlp) = NEW cl_aunit_factory( ).
DATA(res) = hlp->get_test_class_handles(
EXPORTING
obj_type = 'CLAS'
obj_name = CONV #( cls-clsname ) ).
LOOP AT res INTO DATA(tcl).
test_class-testclass = tcl->get_class_name( ).
LOOP AT tcl->get_test_methods( ) INTO test_class-unit_test.
APPEND test_class TO test_classes.
ENDLOOP.
ENDLOOP.
ENDLOOP.
TRY.
"display found classes
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(grid) " Basis Class Simple ALV Tables
CHANGING
t_table = test_classes ).
grid->get_functions( )->set_all( ).
grid->display( ).
CATCH cx_salv_msg INTO DATA(msg).
MESSAGE msg TYPE 'I'.
ENDTRY.
Thank you for the information!
Also it could be helpful check ATC-resuls like described here
Hey Oleg thanks for you feedback.
Yes, I also thought about this but firstly wanted to show how finding unit tests could work in general.
I also was kinda afraid that this KPI might already exist in an ATC check... π¬
you can also try https://github.com/larshp/abapOpenChecks/blob/main/src/utils/zaoc_count_classes_with_tests.prog.abap
ππ
Another way to go is using myΒ ABAP Object Search + ππ
That's really cool Ludwig!! π
Nice, thanks!
To take a step in the philosophical direction:
What is your experience: How many UnitTests exist in SAP customer systems that you have seen?
Edo von Glan for the total number of unit test a 1-byte integer variable is more than sufficient... π
But we are working on it...!
It seems to me, in a customer system, unit tests are only a good investment in the case of central (really reused) global classes. Which make up less than 5% of the total coding, in my experience.
And maybe in some special cases, where writing and maintaining a unit test takes about the same time as manual testing.
And maybe because it implicitly leads to a better design (an argument that I have heard from reliable non-ABAP sources).
Would be nice to have an undogmatic discussion about this.
There are a lot of discussions around here about testing.
In my opinion it's hard to unit test in an ABAP environment because there is so much business logic involved. and business logic often comes from customizing and customizing can change... Often only the positive ways are described. but it's not defined, what to do if an assertion fails.
Some of those points can be compensated by better design. If implementing unit tests leads to the question "how to react if there is an error?" it's not the worst thing... π
Maybe we should discuss more details in the coffee corner?
I haven't used the Coffee Corner yet. But I would be happy to join in an open (undogmatic) discussion about this topic (UniTests in an SAP customer environment)
the discussions mostly start in such blog posts... π
therefore I started a unit test coffee corner. β