NatUnit 2.0

NatUnit is an open-source unit testing framework for Software AG's programming language Natural.

It's written in 100% Natural and can be used in any Natural environment (LUW, Mainframe). There's no need to have NaturalONE, a web server, or any other additional component. It's all plain Natural!

NatUnit test results

However, if you want, NatUnit can be integrated into NaturalONE and even in a Continuous Integration build process, e.g. with Jenkins.

NatUnit test results inside NaturalONE

NatUnit test results in a Jenkins build

Features

Getting started

Nomenclature

A basic TestCase with one Test and one Assertion

NatUnit's TestCases are simply Natural subprograms that follow certain conventions (i.e. use a special PDA, define some inline subroutines etc.).

The subprogram's name should start with TC to distinguish them from other modules, but this is only a convention. NatUnit itself doesn't care about names and only checks the aforementioned conventions like PDA usage etc.

Here's a basic TestCase that should be pretty self-explanatory.

/* File: TCARITHM
DEFINE DATA

PARAMETER USING NUTESTP /* (1)

LOCAL USING NUCONST /* (2)
LOCAL USING NUASSP /* (2)

LOCAL /* (3)
01 #X (N4)
01 #Y (N4)
01 #Z (N4)

END-DEFINE

NUTESTP.FIXTURE := 'Basic arithmetic operations' /* (4)

INCLUDE NUTCTEMP /* (5)

DEFINE SUBROUTINE TEST /* (6)

IF NUTESTP.TEST EQ 'two numbers should be added' /* (7)
  #X := 5
  #Y := 6
  #Z := #X + #Y
  ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 11 #Z /* (8) 
END-IF

END-SUBROUTINE

END
  1. Every TestCase needs to use the PDA NUTESTP so the framework can call it and get the test results back.
  2. The LDA NUCONST (constants, e.g. for the test results) and the PDA NUASSP are used for internal processing and calling the assertions.
  3. These are plain old local variables for the "business logic". You can use whatever you want/need here.
  4. The Fixture describes what the TestCase is actually testing and is used as a human-readable description when tests fail. It's plain text so you can write anything you want here including punctuation, so you're not forced to the 8 characters of the subprogram's name.
  5. The CopyCode NUTCTEMP defines the basic program flow of every TestCase (e.g. call sequence, error handling). TEMP stands for template method, an object-oriented design pattern.
  6. The inline subroutine TEST is used to define the individual Tests of this TestCase.
  7. This is an actual Test. Every Test is defined as an IF statement to be able to describe it with plain text (including punctuation etc.) like the fixture above.
  8. The Assertion in this line compares the actual result of the business logic (the sum in variable #Z) to the expected value (5 + 6 = 11) and fails, if the two don't match.

Execute a single TestCase

You can run a single TestCase with NUSINGLE.

Running a single TestCase with NUSINGLE

If all Tests are successful, the result looks like this:

Successful result of a single TestCase in NUSINGLE

Every dot (.) stands for a successful Test. If you change the Assertion to this:

ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 12 #Z

The Test will fail and produce a different output (F for failure):

Failed TestCase in NUSINGLE

And after you press Enter, the following message explaining the failure including actual (11) and expected (12) values and the Natural line number (26) of the assertion in TCARITHM will be shown:

(F) two numbers should be added TCARITHM (26) failed: <11> should be <12>

You can now correct your code and run the same TestCase over and over again simply by pressing Enter.

Installing NatUnit

Download

You can download NatUnit for free (please respect the license: LGPL) from GitHub: AlteOldenburger / NatUnit.

Prerequisites

The following User Exits are used by NatUnit and have to be present.

Installation

The repository contains a working project for NaturalONE that you can directly import into your workspace.

NatUnit project in NaturalONE

If you want to integrate the framework into your Natural environment, simply copy the source files to a library of your choice and catalog them on the server.

Self-test

You can run TESTNU to execute NatUnit's internal tests. If they all pass, the framework works as expected and you're good to go!

Successful results of NatUnit's internal tests