Technology Blogs by SAP
Learn how to extend and personalize SAP applications. Follow the SAP technology blog for insights into SAP BTP, ABAP, SAP Analytics Cloud, SAP HANA, and more.
cancel
Showing results for 
Search instead for 
Did you mean: 
thomas_jung
Developer Advocate
Developer Advocate

This blog is part of the larger series on all new developer features in SAP HANA SPS 09:http://scn.sap.com/community/developer-center/hana/blog/2014/12/02/sap-hana-sps-09-new-developer-fea...

Unit testing and test driven development have increasingly become more popular in the industry in general and specifically within the SAP customer base.  To meet this demand, SAP introduces both an Unit Test framework and a table/view Mock framework in SPS 09.  Its important to note that this test framework is not automatically installed into SPS 09 however. The delivery unit for this framework is delivered as non-automatic content. This means its sitting on the file system of your HANA system and must be imported by an administrator. This is done because we assume customers will want these tools in their development systems probably not production systems.  Therefore we allow you, the customer, to decide if you want the tools installed.

Contents of the HANA_TEST_TOOLS Delivery Unit

  1. XSUnit a unit test framework based on Jasmine, which is a open source JavaScript Test Framework. With XSUnit you can test both JavaScript code and database content. Various extensions have been made to the standard Jasmine library to make it easier to test database content. (unit.jasminexs)
  2. XSUnit Test Coverage for XS JavaScript (unit.codecoverage)
  3. Test Isolation Framework, namely Mockstar, which allows you to test a view or a stored procedure in isolation. (mockstar)
  4. The results of the XSUnit can be persisted in the Test Result Logger
  5. Test Helper (unit.util)
  6. Developer Documentation (doc)
  7. Demos which are a good starting point for how to test various HANA content. (demo)

How to create an XSUnit Test

First of all there is one common tool for testing XS JavaScript and database content such as SQLScript and Views.  It is all based upon the open source libraries detailed in the above section; several of which have been extended by custom assertions and test utilities specific to the HANA environment.

Test code and test data are developed beside your productive code in the same HANA instance as just another XSJSLIB development object.  Test themselves are implemented in XSJS syntax in SAP HANA Studio or the Web-based Development Workbench.

The Unit tests can than be ran standalone in the Web browser via the URL:
/sap/hana/testtools/unit/jasminxs/TestRunner.xsjs?package=<package>

Where package = the repository package your test are located within.

These unit tests can also be ran directly from the SAP Web-based Development Workbench.

This has the advantage of displaying the code coverage directly within the editor of the SAP Web-based Development Workbench.

Code coverage can also be displayed in the standalone web tool as well:

By integrating the JavaScript line code coverage library BlanketJS into the XSUnit Test framework, it’s now possible to see, to which extent the JavaScript code is covered (percentage, line based) by the executed test run.

Note that all .xsjslib files which match pattern parameter (default “*Test”) are excluded per default.

Example 1: The coverage is measured for each imported .xsjslib file
http://<host>:<port>/sap/hana/testtools/unit/jasminexs/TestRunner.xsjs?package=sap.hana.testtools.mockstar&coverage=true

Example 2: The coverage is measured for each .xsjslib file located in the mockstar package
http://<host>:<port>/sap/hana/testtools/unit/jasminexs/TestRunner.xsjs?package=sap.hana.testtools&coverage=sap.hana.testtools.mockstar

Example 3: If you would like to exclude test helpers located in the tests package you can specify them via the “exclude” parameter
http://<host>:<port>/sap/hana/testtools/unit/jasminexs/TestRunner.xsjs?package=sap.hana.testtools&coverage=true&exclude=sap.hana.testtools.mockstar.tests

However the test tool can also be ran as a service call.  This way if you like to execute the test on a regular basis or trigger by code changes you can use Jenkins as a continuous integration server.  The unit tests are called remotely from Jenkins and the results are stored and displayed within Jenkins. This is a process that we use internally at SAP.

Mocking

To write self-contained unit tests that are executable in any system, you have to test the HANA model in an isolated way.

An HANA View for example has typically dependencies to other HANA Views or to database tables. These dependencies pass data to the “View under test" and cannot be controlled and overwritten by the test. This means you need the possibility to mock dependencies of the “View under test".

The Mocking Framework, namely Mockstar helps you to test your models with specific test data that resides in dedicated test tables.

Therefore it creates you a copy of your origin view/procedure where the dependent views/tables are replaced by test tables.

These (temporary) tables can be prepared, controlled and filled by the test.

The advantages of mocking are:

  • Write self-contained unit tests that are executable in any system
  • Tests can be executed by several users in parallel
20 Comments
Former Member
0 Kudos

where to find a hana_test_tools delivery unit, It looks like it is not shipped with SP09 Rev 95

Former Member

Hello Xavier,

you're right, the HANA TEST TOOLS are not being shipped by default. You need to download the corresponding Delivery Unit from the SAP Support Portal. It's free. Please follow this path:

Support Packages and Patches > H > SAP HANA PLATFORM EDITION > SAP HANA PLATFORM EDIT 1.0 > Entry by Component > HANA XS Testtools content > HANA TEST TOOLS 1.0

Regards

Johannes

Former Member
0 Kudos

Thank you !!!.  I have downloaded from the service market place.

Regards,

Xavier

BenPatterson
Participant
0 Kudos

Hello Thomas,

I was wondering if there exists, or if there are plans to have, an ability to create Unit Tests like this for SQL procedures of top down (ABAP -> HANA) implementations (CDS/AMDB/Open SQL)?

Kind regards,

Ben.

thomas_jung
Developer Advocate
Developer Advocate

You can already write XSUnit tests for SQLScript procedures.  There are examples in the SHINE content. As far as ABAP on HANA test tools you would have to ask someone from the ABAP side of the house.  This test suite (and consequently blog) is focused on the HANA native tools only.

abe_letkeman
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Thomas and developers.

It appears that this works via repository objects.  Can it work if there are only catalog objects?   I know that's not the recommended way to create HANA objects, but that is out of my control. 

I'll give it a try and share my findings if the question hasn't already been answered.  I suspect others may have the same question.  Thank you!

Former Member
0 Kudos

Hi, Thomas

I'm having a .hdbprocedure which using a .hdbsequence to generate an ID automatically, I want to know how to mock up the .hdbsequence which being used in the SP I'm testing on with Jasmine Unit Test for Hana?

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

While we can mock tables and their usage, I don't know of anything in the current framework that would mock using the hdbsequence. That would require replacing the logic of the INSERT statement itself within the implementation of the procedure. That would be quite difficult and potentially could violate the integrity of the object you are testing.


Are you just concerned about the test cases using numbers from the sequence? Is this really a concern in a development system? Unit tests shouldn't really be executed in production systems anyway.

Former Member
0 Kudos

Thanks Thomas for your quick respond, turns out it doesn't need to have a mock up of the .hdbsequence to run unit test for the .hdbprocedure. Hope this will answer ppl's question if they have similar with mine.

0 Kudos
Hi Thomas,
if an expectation fails the jasmine reporter outputs the stack of the failure which clutters up the test report unnecessarily, for example:



Is it somehow possible to supress the stack output in the test report?
 
Former Member
0 Kudos
Hi Thomas,

I am unable to run the above JavaScript code for Scripted Calculation Views. Though it is working for modeled Calculation views. What can be the reason?

The functionality is only for Modeled views in SPS09?

Regards,
Aditya Trivedi
Former Member
0 Kudos
Hi Thomas,

I am unable to use the above method of testing for Scripted Views. Is it only meant for modeled views?
Former Member
0 Kudos
Hi, Thomas,

Does HANA SP12 have any bugs that not working with the latest testtool? unit tests used to work with SP11 now failing however it is working fine by manual tests in our app. Looks like the new SP12 having some bugs with testtool.
david_bussiere
Explorer
0 Kudos
Hi Thomas, with regards to the Mockstar environment if we have tests where we want to test language or region specific formatting , is there a way to manipulate the session language in the tests? We have models exposed as .xsodata services where we are using the Accept-Language header to determine the language specific content to return but would like to be able to test that logic?
anton_samoilik2
Discoverer
0 Kudos
Hello,

 

Could you please elaborate more on how these unit tests can be called from Jenkins?

Thanks
thomas_jung
Developer Advocate
Developer Advocate
All unit tests can be triggered via URL as described in the blog:

The Unit tests can than be ran standalone in the Web browser via the URL:
/sap/hana/testtools/unit/jasminxs/TestRunner.xsjs?package=<package>

Any tool which can be scripted to call a URL can trigger the tests.
abhijeetgc
Advisor
Advisor
Hi Thomas,

 

Is it possible to use this for HANA 2.0 SP3 in HANA as a Service as well?
thomas_jung
Developer Advocate
Developer Advocate
0 Kudos
This blog describes functionality for XS Classic and the SAP HANA Web-Based Development Workbench.  In HANA As A Service there is no XS Classic and you use the SAP Web IDE Fullstack as a development tool.  There is no need for Mockstar as you have HDI containers, however you can use the non-sap Node.js modules Gulp and Jasmine for building unit tests.  There are examples of this from the HANA7 openSAP course: https://github.com/SAP/hana-xsa-opensap-hana7/tree/master/srv/tests
former_member685329
Discoverer
0 Kudos

Hi Thomas,

Could you please provide us steps how to trigger unit test written in SAP Web IDE for SAP HANA (SPS 04 Patch 6). We want to trigger in CI/CD pipeline as well so any CLI is available to trigger the unit test?

former_member685329
Discoverer
0 Kudos

Hi Thomas,

Could you please provide us an example of alternate way of mocking data using HDI containers in SAP Web IDE for SAP HANA(SPS 04 Patch 6)?