This can be accomplished by keeping the function block under test as an instance variable of the test suite rather than the test method. You can download an example here. In this example, the FB_ToBeTested is instantiated under the test suite (FB_ToBeTested_Test), and can thus be controlled over multiple cycles. Then all that’s necessary to do is to set the condition for when the assertion should be made in the test itself, which in the example is when the TestSuiteTimer has elapsed (TestSuiteTimer.Q).
Add DISABLED_ in front of the test name, for example:
TEST('DISABLED_ThisTestWillBeIgnored'); AssertEquals(Expected := a, Actual := b, Message := 'A does not equal B'); TEST_FINISHED();
In a number of scenarios, TwinCAT won’t let you write directly to certain variables:
- Due to access restrictions (e.g. a variable in a FB’s VAR)
- The variable being set as I/O (i.e. AT %I* or AT %Q*)
Writing to these variables wouldn’t make sense and should be prevented in the normal PLC code, so having special privileges during testing is a must.
To support these cases, TcUnit provides helper functions like
WRITE_PROTECTED_INT (and so forth) for setting these type of variables. For an example of how to use these, let’s assume you have a test:
METHOD PRIVATE TestCommsOkChannelsLow VAR EL1008 : FB_Beckhoff_EL1008; END_VAR
Where the FB_Beckhoff_EL1008 holds a variable:
iChannelInput AT %I* : ARRAY[1..8] OF BOOL;
Now you might want to write a value to the first channel of the iChannelInput like:
TcUnit.WRITE_PROTECTED_BOOL(Ptr := ADR(EL1008.iChannelInput), Value := FALSE);
Whereas afterwards you can make an assertion as usual:
AssertFalse(Condition := EL1008.ChannelInput, Message := 'Channel is not false');
You can accomplish this by the Hide reference option for referenced libraries. This option lets you hide TcUnit from your other projects.
Let’s assume you’ve developed a library
MyLibrary, which has tests written in TcUnit. You make a PLC project
MyProject, which references
If you use Hide reference on TcUnit in
MyLibrary, then TcUnit won’t show up in the imports list of
MyProject. You can find it in the Properties tab:
I want to do an assertion on two variables both declared with the BIT-datatype, but I have noticed that a AssertEquals_BIT does not exist. What do I do?
The reason a AssertEquals_BIT does not exist is that TwinCAT does not allow a BIT-datatype as a variable input. If you have data declared with the BIT-type, the easiest way to do an assertion on these is to do a
BIT_TO_BOOL conversion and use the AssertEquals_BOOL.
TEST('Testing_of_BIT_Type'); AssertEquals_BOOL(Expected := BIT_TO_BOOL(VariableDeclaredAsBit_A), Actual := BIT_TO_BOOL(VariableDeclaredAsBit_B), Message := 'The variables differ'); TEST_FINISHED();