Custom asserts - Manual - AxUnit-ST - AxUnit ST documentation package - AxUnit-ST,

AxUnit-ST CLI Tool (test)

Portfolio
SIMATIC AX
Product
AxUnit-ST
Software version
8.3.9
Edition
07/2025
Language
English (original)
Package Name
@ax/axunitst-docs

If you need a specific assertion, which is not covered in the AxUnit-ST library, AxUnit-ST provides you the ability to create your own assertion. The following example demonstrates how to use this feature.

Given is the following class representing a motor:

CLASS Motor
    VAR PRIVATE
        _inRun : BOOL;
    END_VAR
    METHOD PUBLIC Run
        _inRun := TRUE;
    END_METHOD
    METHOD PUBLIC IsRunning : BOOL 
        IsRunning := _inRun; 
    END_METHOD
    METHOD PUBLIC HasFailure : BOOL 
        _inRun := FALSE;
    END_METHOD
END_CLASS

You want to check if the motor is running with an explicit assert. Import the AxUnit.ResultFunctions namespace, and declare a PUBLIC FUNCTION. In the VAR_INPUT section, add the two parameter file and line, and annotate them with the pragmas {CallerFilePath} and {CallerLineNumber}. Those are required to display the assert messages.

Your assert code should branch into two paths, the success path and the failure path:

  • For the success path, call axunit_Succeed at the end to indicate that the assert passed.
  • For the failure path, call axunit_Fail and pass an assertion message, file and line parameter. The message will be shown in the execution environment (IDE or CLI).

Note

If the message is a string, you may use the functionality of the System.Strings library.

USING AxUnit.ResultFunctions;

NAMESPACE MyTests.Assert
    FUNCTION PUBLIC MotorIsRunning
        VAR_IN_OUT
            motor : Motor;
        END_VAR

        VAR_INPUT
            {CallerFilePath}
            file : WSTRING[1024];
            {CallerLineNumber}
            line : INT;
        END_VAR

        IF motor.IsRunning() THEN
            axunit_Succeed();
        ELSE
            axunit_Fail('Expected the motor to be running but it is stopped.', file, line);
        END_IF;

    END_FUNCTION
END_NAMESPACE

Later on, you can use your custom assert in tests:

NAMESPACE MyTests

    {TestFixture}
    CLASS PUBLIC MotorTests
        VAR
            _testee : Motor;
        END_VAR

        {Test}
        METHOD PUBLIC MotorShouldRun
            _testee.Run();
            Assert.MotorIsRunning(_testee);
        END_METHOD
    END_CLASS

END_NAMESPACE

WARNING

Do not call an assert function outside your tests.