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!
However, if you want, NatUnit can be integrated into NaturalONE and even in a Continuous Integration build process, e.g. with Jenkins.
CALCPREM
that calculates a premium for an insurance contract, you could create a TestCase TCCALCPR
that tests whether the premium is calculated correctly. In most cases, there are many different scenarios that have to be tested, e.g. calculating the premium for an adult vs. for a child. Therefore, TestCases can contain multiple Tests that check these different requirements.AGE
of the person in the first Test may be 35
and 17
in the second Test. And of course the result (the premium for the two people) will be different, too. Tests are written as IF
statements inside the subroutine TEST
inside the TestCase.@TestData
) inside comments in the TestCase's code. Each individual Test is run in isolation and can therefore fail without affecting the other test runs.ASSERT-NUM-GREATER-THAN #PREMIUM-ADULT #PREMIUM-CHILD
).TSINSURA
to be able to execute them all at once with a single command.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
NUTESTP
so the framework can call it and get the test results back.NUCONST
(constants, e.g. for the test results) and the PDA NUASSP
are used for internal processing and calling the assertions.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.TEST
is used to define the individual Tests of this TestCase.IF
statement to be able to describe it with plain text (including punctuation etc.) like the fixture above.#Z
) to the expected value (5 + 6 = 11
) and fails, if the two don't match.You can run a single TestCase with NUSINGLE
.
If all Tests are successful, the result looks like this:
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):
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
.
You can download NatUnit for free (please respect the license: LGPL) from GitHub: AlteOldenburger / NatUnit.
The following User Exits are used by NatUnit and have to be present.
USR0020N
: Read any error message from FNAT or FUSERUSR1040N
: Get or set profile parameter UDBUSR1057N
: Read a Natural source code into an arrayUSR2013N
: Get SYSPROF informationUSR4208N
: Read or write a Natural resourceUSR6006N
: Get path to system file (optional, only on Linux)The repository contains a working project for NaturalONE that you can directly import into your workspace.
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.
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!