General + Comments + Formatting: Clean ABAP
General + Comments + Formatting: Clean ABAP
Test publics, not private internals Use LOCAL FRIENDS to access the
CLASS unit_tests DEFINITION LOCAL FRIENDS
dependency-inverting constructor Assertions
Don’t obsess about coverage if it’s hidden away
60% -> all done! Few, focused assertions
Don’t misuse LOCAL FRIENDS to invade assert_not_initial( itab ).
the tested code assert_equals( act = itab exp = exp ).
CLASS unit_tests LOCAL FRIENDS cut.
cut->db_reader = stub_db_reader Use the right assert type
Test classes assert_equals( act = itab exp = exp ).
Don’t change the productive code to assert_true( itab = exp ).
Call local test classes by their purpose make the code testable
CLASS unit_tests Assert content, not quantity
IF in_test_mode = abap_true. assert_contains_message( key )
CLASS tests_for_the_class_under_test
assert_equals( act = lines( messages )
Don’t sub-class to mock methods exp = 3 ).
Put tests in local classes
Use test seams or OSQL_REPLACE or extract the
REPORT some_tests_for_this Assert quality, not content
methods to own class
assert_all_lines_shorter_than( … )
Don’t mock stuff that’s not needed
DATA unused_dependency Use FAIL to check for expected
Code under test exceptions
Don’t build test frameworks METHOD throws_on_empty_input.
Name the code under test setup( test_case_id = '4711' ) TRY.
" when
meaningfully, or default to CUT cut->do_something( '' ).
DATA switch cl_abap_unit_assert=>fail( ).
DATA cut CATCH /clean/some_exception.
Test Methods " then
Test interfaces, not classes ENDTRY.
DATA cut TYPE REF TO some_interface Test methods names: reflect what’s ENDMETHOD.
DATA cut TYPE REF TO some_class
given and expected Forward unexpected exceptions
Extract the call to the code under test METHODS accepts_emtpy_user_input
instead of catching and failing
METHODS test_1
to its own method METHODS throws RAISING EXCEPTION bad
to default unneeded parameters Use given-when-then
given_some_data( ). Write custom asserts to shorten code
do_the_good_thing( ). and avoid duplication
assert_that_it_worked( ). assert_table_contains( row )
READ TABLE itab
“When” is exactly one call assert_subrc( )
given_some_data( ).
do_the_good_thing( ).
and_another_good_thing( ).
assert_that_it_worked( ).