Today I decided to show you how having a common base class for your unit tests might help you save some time and improve the readability of your tests.
The class I use simply wraps all non-deprecated methods from
cl_abap_unit_assert. This means they keep the exact same signatures and just pass them on like this:
METHOD assert_equals.
assertion_failed = cl_abap_unit_assert=>assert_equals(
act = act
exp = exp
tol = tol
level = level
msg = msg
quit = quit
ignore_hash_sequence = ignore_hash_sequence ).
ENDMETHOD.
Let's try to apply it to the test class used in my
previous unit testing blog. A
diff is worth a thousand words:
You can refactor existing test classes in two steps:
- Add inheritance.
- Use find/replace to get rid of cl_abap_unit_assert=> everywhere at once.
You can download the class from my blog's
git repository.
If you want to, you can take it and add your own often-used custom assert methods to have them accessible across your codebase.
Another advantage is easier testing of legacy code - you might not have certain assertion methods available on older releases, but you can
pretend you do by replacing the
cl_abap_unit_assert call in the base class with your downported version.
As always, hope this helps!
Frederik