NUNIT: A Unit-Test-Framework for Natural

Update (10.04.2012): NUNIT (now NatUnit) is now available via SourceForge: NUNIT/NatUnit at SourceForge.

As part of my Master’s thesis about Test Driven Development (TDD) I developed a Unit-Test-Framework for the programming language Natural. This 4GL language from Software AG is in use for over 30 years now but the notion of automated tests has not been spread throughout the developer community. Perhaps now, with a framework for tests available, this will change 😉

The Unit-Tests that can be run by NUNIT are subprograms and need to have the following structure. As you can see, the fixture’s and tests’ names are simply plain text to allow strings that are longer than 32 characters (the longest possible name for a Natural module) to provide meaningful names as in Behaviour Driven Development. The TestCases are parsed by NUNIT for this structure to make sure that all the needed parameters, subroutines etc. are available. To create a new TestCase all you have to do is copy the example source code and change the names and the implementation of the tests in the IF-branches.

DEFINE DATA PARAMETER USING NUTESTP /* parameters for a single test LOCAL USING NUCONST /* NUNIT constants (error code etc.) LOCAL USING NUASSP /* parameters for the assertions END-DEFINE * NUTESTP.FIXTURE := 'Example TestCase 1' /* this TestCase's fixture * INCLUDE NUTCTEMP /* the template method (SETUP -> TEST -> TEARDOWN) INCLUDE NUTCSTUB /* stub implementations of SETUP and TEARDOWN * DEFINE SUBROUTINE TEST /* the main test routine * ************************************************************** IF NUTESTP.TEST EQ 'comparing two equal numbers should pass' /* name of first test ************************************************************** ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 2 2 END-IF * ************************************************************** IF NUTESTP.TEST EQ 'comparing two different numbers should fail' /* name of second test ************************************************************** ASSERT-LINE := *LINE; PERFORM ASSERT-NUM-EQUALS NUASSP 1 2 END-IF * END-SUBROUTINE * END

The following diagram shows NUNIT’s architecture. I tried to make the adaption of NUNIT to a different platform than ours (Solaris with Natural modules saved in the file system) as easy as possible and encapsulated the OS-specific implementation into two modules that need to be changed accordingly. I have also included some basic assertions like ASSERT-NUM-EQUALS, ASSERT-STRING-EQUALS and ASSERT-ARRAY-EQUALS and some example TestCases in the source code you can download below.

Architecture of NUNIT

The TestCases’ names are defined in a user-specific workfile in the home directory (in the current configuration, but this may be changed e.g. to a DB READ). A run of all defined TestCases is as simple as calling the main program NUNIT and produces an output like the following (even with a “red bar” 🙂 ).

NUNIT test run (red bar)
NUNIT detailed test results

Notice the nice failure/error messages including the module names, line numbers etc. 😀

I developed NUNIT by applying TDD which means the framework tests itself. After you “installed” it (which means importing the NUNIT modules into a library of your choice, preferably a steplib if you plan on using it throughout your application) you can run a simple TESTNU to make sure NUNIT behaves as expected.

NUNIT self tests

All NUNIT modules include a detailed header comment so they should be pretty self-explanatory. But in fact, all you need to start are the programs TESTNU, NUNIT and NUSINGLE (with which you can run a single TestCase) and a TestCase like TCEXMPL1. Just try it out and let me know if you like it 🙂

Downloads

Über Stefan

Polyglot Clean Code Developer

6 Kommentare

  1. Haha, ja, im Prinzip eine leichte Aufgabe. Frohes neues Jahr.

  2. Hallo,

    zunächst ersteinmal “Hut ab” für diesen wirklich gelungenen Versuch, in Natural Unit-Tests möglich zu machen. Im Gegensatz zu dem Poster @herbs finde ich es eine nicht leichte Aufgabe dieser spröden, uralten und ungelenken Dame solche Konzepte zu entlocken. Natural ist nicht tot – es riecht nur so 🙂

    Aber … der Umgang mit dieser “Sprache” muß doch eigentlich weh getan haben, oder? Jedenfalls dann, wenn – was ich mal unterstelle – Sie Java, C# und/oder andere Objektorientierte Sprachen kennen und anwenden können. Von anderen heutigen Konzepten in der Softareentwicklung mal abgesehen.

    Grüße aus Berlin
    KlaBueBaer

    P.S.: meine ersten Natural-Programme habe ich 1983 geschrieben, heute nicht mehr so oft, aber doch manchmal. time flys like an arrow.

  3. @KlaBueBaer: Danke! Ich finde es eigentlich sogar recht interessant, die “modernen” Konzepte anderer Sprachen in Natural anzuwenden. Aber leider sind einige Sachen wirklich schwierig umzusetzen. Eine Möglichkeit, Programme hinter einer Art “Interface” zu verstecken, wäre z.B. seeehr hilfreich gewesen 😉

  4. Hallo Stefan,

    ich bin Schüler eines Gymnasiums und wird programmieren in Informatik mit Delphi. Da du dich ja anscheinend sehr gut auskennst, wollte ich dich fragen, ob man Delphi und Pascal heutzutage eigentlich nochverwendet und wielange du gebraucht hast, um richtig Programmieren zu lernen.

    Viele Grüße,
    Fabian.

  5. @Fabian: Delphi und Pascal werden häufig in Schulen eingesetzt, um programmieren zu lernen. In der freien Wirtschaft gibt es durchaus auch Einsatzgebiete für Delphi, aber häufiger kommen sicherlich Java oder C# zum Einsatz.

    Die Ausbildung zum Fachinformatiker Anwendungsentwicklung dauert 3 Jahre. Ich denke, das ist ein guter Richtwert um programmieren zu lernen. Sicherlich kann man schneller Fortschritte machen, aber die Erfahrung kommt natürlich erst mit der Zeit.

  6. @Fabian: Delphi und Pascal werden häufig in Schulen eingesetzt, um programmieren zu lernen. In der freien Wirtschaft gibt es durchaus auch Einsatzgebiete für Delphi, aber häufiger kommen sicherlich Java oder C# zum Einsatz.

    Die Ausbildung zum Fachinformatiker Anwendungsentwicklung dauert 3 Jahre. Ich denke, das ist ein guter Richtwert um programmieren zu lernen. Sicherlich kann man schneller Fortschritte machen, aber die Erfahrung kommt natürlich erst mit der Zeit.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax