Menu

How_to_use_NatUnit

Markus Wessjohann Stefan Macke

How to use NatUnit - the unit test framework

History and design

We couldn't find a unit test framework for Natural, so we decided to develop one ourselves.
The first version did the job, but not very well. So Stefan Macke developed it completely new in his Master's thesis about the development of NatUnit.

Using NatUnit

First you need a Natural module you want to test, e.g. a subprogram, a subroutine etc.

To test the module you write a TestCase for NatUnit that you create as a subprogram in the same library as the module to be tested. The prefix of the subprogram's name should be TC.

Here is a template for the TestCase:

************************************************************************
*
*  File: TCTEMPL
*
*  Template for an NUNIT TestCase.
*
*  Tags: NUNIT
*
*  Parameters:
*    NUTESTP - TestCase parameters.
*
*  Returns:
*    NUTESTP - TestCase parameters.
*
*  About:
*    Library       - NATUNIT
*    Created       - 17.09.2009
*    Author        - $Author$
*    Revision      - $LastChangedRevision$
*    Latest author - $LastChangedBy$
*    Modified      - $Date$
*    Id            - $Id$
************************************************************************
*
DEFINE DATA
PARAMETER USING NUTESTP
LOCAL USING NUCONST
LOCAL USING NUASSP
END-DEFINE
*
NUTESTP.FIXTURE := 'This is the fixture'
*
INCLUDE NUTCTEMP
INCLUDE NUTCSTUB
*
DEFINE SUBROUTINE TEST
*
**************************************************************
IF NUTESTP.TEST EQ 'This is the test'
**************************************************************
ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 1 1
END-IF
*
END-SUBROUTINE
*
END

Assign a meaningful description of your TestCase to NUTESTP.FIXTURE and change the Test's (the IF statement in the code above) name by changing the string 'This is the test'. To test your module, you now call it from within the Test. This may look like the following:

<Fill PDA>
CALLNAT '<module name>' <PDA>

To check the result add an Assertion like the following:

ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP <expected value> <actual value>

There are already quite a few different Assertions included in the framework. Simply take a look at the ASSERT- subroutines.

Now you can execute your TestCase by running NUSINGLE, which only takes a few parameters:

  • Library is the library where the TestCase is stored.
  • TestCase is the name of your TestCase (the module name).

Hit enter and the TestCase gets executed. After finishing the results will by shown:

Current TestCase: TCANRPBE   Last Error TestCase:                         
TestCases..: 1               Tests....: 5                 Asserts..: 17      
Failures...:                 Failures.:                   Start....: 09:05:05
Errors.....:                 Errors...:                   End......: 09:05:09

.    

If any errors occurred, they are shown in the following dialog.

Now you can add more Tests and Assertions to your TestCase e.g. to check all of the result parameters or to test a different behaviour of your module.

Have a look at the included internal TestCases for NatUnit to get a feeling for what and how to test your modules.

further Informations

[Break dependencies in Natural]


Related

Wiki: Break dependencies in Natural
Wiki: Home

MongoDB Logo MongoDB