Introducing ABAP Sencha
If you imagine that ABAP Sencha is a tea infuser driver running on Raspberry Pi, well, it's not. However, if you're curious about writing tests in ABAP in a slightly different form, keep reading.
If you program in JavaScript or have had contact with SAP's CAP, you have almost
certainly come across chai.js
- a library for writing tests that provides
various styles for writing tests:
In each of the styles, you can use methods and properties called "chainers",
which allow you to write test code that resembles prose and can be understood even
by non-developers (probably). I like this approach (not only in test code) and I have become
very fond of chai.js. I have never delved into or had projects
where test readability is taken even further with tools
like Cucumber.
I also don't like shortcuts like eq
instead of equals
, etc. chai.js
with
the expect
style is my choice - both for efficient writing and
reading of tests.
And I don't like assertions - I never use assert
unless I'm forced to...
And here we come to ABAP where the assertion style is imposed.
We have a choice of cl_abap_unit_assert=>assert_...
- or
a shorthand form using tools like
assert>.
I think about 7 years ago, I thought it would be cool to have chai.js
in ABAP.
I started working on it, then abandoned it, then started again,
then I didn't have time. But the idea was lingering in the back of my mind
and during the migration of things from the old NW Trial to the Dockerized
ABAP Platform 1909, the idea somehow revived and slowly but surely progressed.
ABAP Sencha has finally brewed.
foo.should.equal('bar'); // chai.js
the( foo )->should->equal( 'bar' ). //ABAP Sencha
expect(foo).to.equal('bar'); // chai.js
expect( foo )->to->equal( 'bar' ). // ABAP Sencha
assert.equal(foo, 'bar'); // chai.js
assert->equal( actual = foo expected = 'bar' ) // ABAP Sencha
assert( foo )->equals( 'bar' ). // ABAP Sencha
As you can see, the goal was to enable the use of all 3 styles in ABAP.
Of course, ABAP is not JavaScript and it is not possible to do it 1:1,
especially for the should
style, but I was able to make it look
similar - there are connective words, variations, additional methods
inspired by those from chai.js
, and additional features.
" use styles as you like
expect( 1 )->to->be->one_of( some_values ).
expect( bonus-periodic )->is->true( )->and( bonus-amount )->equals( 70 ).
the( |def| )->should->be->contained_in( some_string_table ).
assert( lines )->equals_to( 8 ).
" some shortcuts
DATA(td) = get_test_double_for( 'ZMY_INTERFACE' ).
" or if you prefer to call them mocks
DATA(mock) = mock_for( 'ZMY_INTERFACE' )
" use additional methods for documenting
given( 'the user uses the CET timezone' ).
timezone_mocked_for( cet ).
when( 'we request the header' ).
DATA(header) = cut->get_user_header( ).
then( 'it should be prepended by CET phrase' ).
the( header )->should->cover_pattern( '*CET user*' ).
" or even use them to run actual test code (see ZCL_ABAP_SENCHA_SAMPLES for how-to)
given( 'factors are set to non-standard' ).
when( 'calculation is requested' ).
then( 'modification for standard is applied' ).
Regarding the last example - check out the library Cacamber which is fully dedicated to this approach.
You can find a detailed description on the project page at https://github.com/wozjac/abap-sencha.
Happy testing!🪳