TwinCAT_UnitTestLibrary is a lightweight unit test library for testing TwinCAT3 source code - See A TwinCAT Unit Test Library
It has been written with the following goals in mind:
-
Minimum setup cost for a new test - a test is defined in a function block that extends T_UnitTestBase by implementing the RunTest method. Assertion methods are used to determine the correctness of the test and to output messages to the error window should they fail
-
Tests are run automatically on online-change to provide fast feedback during test-driven development.
-
Tests can be re-run by writing to the Retest flag
To create a test:
- Create a function block that inherits from T_UnitTestBase
- Add a RunTest method to perform the required test using assertion methods to verify correct operation
- Execute the unit test somewhere
The following is a recommended template for the RunTest method:
METHOD RunTest
IF Retest THEN
SUPER^.Init(); // this will clear the TestCompleted flag, causing the test to be executed again...
END_IF
IF TestCompleted THEN
RETURN; // execute no more test code...
END_IF
// Actual test code - for example:
CRC := F_CalculateCrc('Some Text');
AssertEquals_BYTE(Expected:= 16#3F, Actual:= CRC, Message:= 'Calculated CRC is incorrect');
TestCompleted := TRUE;
Code automatically formatted using