Testing classes - Manual - AxUnit - AxUnit - AxUnit,

AxUnit (legacy) CLI Tool (test)

Portfolio
SIMATIC AX
Product
AxUnit
Software version
3.0.4
Edition
08/2025
Language
English (original)
Package Name
@ax/axunit-docs

Until now the instances of classes are possible in VAR_GLOBAL section or in VAR section of a class. In order to test a class the developer would need to declare an instance of it in VAR_GLOBAL section and refer to it in VAR_EXTERNAL section of the test function. AxUnit now simplifies this procedure by introducing {TestFixture} feature for classes.

The below code defines the class NumberGenerator and tests it in the class NumberGeneratorTest.

CONFIGURATION PLC_1
    TASK Main(Interval := T#1000ms, Priority := 1);
END_CONFIGURATION

CLASS NumberGenerator
    VAR PUBLIC
        number : int;
    END_VAR

    METHOD PUBLIC GetNext  : Int
        GetNext := number + 1;
        number := GetNext;
    END_METHOD

    METHOD PUBLIC GetCurrent  : Int
        GetCurrent := number;
    END_METHOD
END_CLASS

{TestFixture}
CLASS NumberGeneratorTest
    VAR
        defaultNumberGenerator : NumberGenerator;
        numberGenerator100 : NumberGenerator := (number := 100);
        getnextInstance : NumberGenerator;
    END_VAR

    {Test}
    METHOD PUBLIC Number_is_set_to_0_by_default_initializer
        AxUnit.Assert.Equal(0, defaultNumberGenerator.number);
    END_METHOD

    {Test}
    METHOD PUBLIC Number_is_set_by_explicitInitializer
        AxUnit.Assert.Equal(100, numberGenerator100.number);
    END_METHOD

    {Test}
    METHOD PUBLIC GetNext_increments_the_number
        AxUnit.Assert.Equal(0, getnextInstance.number);
        AxUnit.Assert.Equal(1, getnextInstance.GetNext());
        AxUnit.Assert.Equal(1, getnextInstance.number);
    END_METHOD
END_CLASS

Warning

Before each Test, the TestFixture is reinitialized. This means all the data stored in member variables is reset.