Skip to content
Andres Gomez Casanova edited this page May 6, 2020 · 10 revisions

This is a 5 minutes guide to get started with db2unit.

Downloading

You can download db2unit from Releases.

One you have download the file, you need to extract it. In Linux:

tar -xvf db2unit.tar.gz

You should also download log4db2 in order to install this framework. You can download it from Releases.

Once it is in your computer, you will need to extract it:

tar -xvf log4db2.tar.gz

For more information about installing, you can visit:

Preparing

First you need to connect to your database:

db2 connect to mydb

The output is:

   Database Connection Information

 Database server        = DB2/LINUXX8664 10.5.10
 SQL authorization ID   = ANGOCA
 Local database alias   = MYDB`

Then, you will need to install log4db2 if it is not installed yet.

cd log4db2
. ./install

The output is something like:

Installing utility for v10.1
/home/ext/angoca/workspace/install/log4db2/sql-pl/Tables.sql
DB20000I  The SQL command completed successfully.

DB20000I  The SQL command completed successfully.

...

DB20000I  The SQL command completed successfully.

DB20000I  The SQL command completed successfully.

log4db2 was installed successfully`

For more information about how to install log4db2 you can visit:

Installing

Then, you can install db2unit:

cd ../db2unit
. ./install

And the output:

. ./install
db2unit is licensed under the terms of the GNU General Public License v3.0
Checking prerequisites
/home/ext/angoca/workspace/install/db2unit/sql-pl/00-Prereqs.sql
DB20000I  The SQL command completed successfully.

Installing utility
/home/ext/angoca/workspace/install/db2unit/sql-pl/01-ObjectsAdmin.sql
DB20000I  The SQL command completed successfully.

...

DB20000I  The SQL command completed successfully.

DB20000I  The SQL command completed successfully.

Please visit the wiki to learn how to use and configure this utility
https://github.com/angoca/db2unit/wiki
To report an issue or provide feedback, please visit:
https://github.com/angoca/db2unit/issues

db2unit was successfully installed
Database: DB2UNIT           
Version: 2014-10-13 1                    
Schema: DB2UNIT_1

For more information how to install db2unit, you can visit:

Tests suite creation

Once the db2unit framework is installed, you can create the test. You just need to create a file and fill it with the tests. Let's suppose its name is myTests.sql and the content is like this:

--#SET TERMINATOR @

SET CURRENT SCHEMA MY_TESTS@

-- Tests <<<
CREATE OR REPLACE PROCEDURE TEST_my_first_test()
 BEGIN
  DECLARE EXPECTED VARCHAR(32);
  DECLARE ACTUAL VARCHAR(32);

  SET EXPECTED = 'MyValue';
  SET ACTUAL = 'My' || 'Value';

  CALL DB2UNIT.ASSERT_STRING_EQUALS('Message for the assertion', EXPECTED, ACTUAL);
 END @

CREATE OR REPLACE PROCEDURE TEST_my_second_test()
 BEGIN
  CALL DB2UNIT.REGISTER_MESSAGE('Message when exception');
 END @

CALL DB2UNIT.REGISTER_SUITE(CURRENT SCHEMA)@

Now, you can install it in the database:

db2 -tf myTests.sql

The output is:

DB20000I  The SQL command completed successfully.

DB20000I  The SQL command completed successfully.

DB20000I  The SQL command completed successfully.


  Return Status = 0

For more information how to create test, please visit:

Execution

And now you can call the execution of the tests suite.

db2 "CALL DB2UNIT.RUN_SUITE('MY_TESTS')"

And the output will be:

Result set 1
--------------

TEST             FINAL_STATE MICROSECONDS MESSAGE                                                         
---------------- ----------- ------------ ----------------------------------------------------------------
Before Suite     -                      - Starting execution                                              
TEST_MY_FIRST_TE Passed            165885 Executing TEST_MY_FIRST_TEST                                    
TEST_MY_SECOND_T Passed             70797 Executing TEST_MY_SECOND_TEST                                   
After Suite      -                      - Finishing execution                                             
                 -                      - 2 tests were executed                                           
                 -                      - 2 tests passed                                                  
                 -                      - 0 tests failed                                                  
                 -                      - 0 tests with errors                                             

8 record(s) selected.


Result set 2
--------------

TIME     EXECUTION_ID STATUS                MESSAGE                                                       
-------- ------------ --------------------- --------------------------------------------------------------
21:24:34        24939 Initialization        db2unit is licensed under the terms of the GPL v3             
21:24:34        24939 Initialization        Execution of MY_TESTS with ID 24939                           
21:24:34        24939 Prepare Report        The reports table created: MY_TESTS.REPORT_TESTS              
21:24:36        24939 Calculating time      Total execution time is: 2 seconds                            

4 record(s) selected.

Return Status = 0

For more information about how to run tests suites, you can visit:

Clone this wiki locally