Table of Contents

Separate test solution (TcUnit)

Use this layout when the tests are a normal TcUnit application that you maintain yourself: FB_TestSuite, TEST() / TEST_FINISHED(), and TcUnit.RUN() in MAIN. The test application lives in its own TwinCAT solution under tests/, next to the solution that builds the library or application.

The build tools compile this solution and activate it after the main solution has been built. DataRows, --test-framework, and Test Explorer belong to embedded tests. Run these suites by activating the test PLC.

Other layouts, result files, and CI reporting: Unit tests.


Repository layout

tests/.Zeugwerk/config.json is the marker. Its solution field points at the test .sln.

my-repo/
├── .Zeugwerk/
│   └── config.json                 ← library or application
├── MyLibrary/
│   └── MyLibrary.plcproj
├── MyLibrary.sln
└── tests/
    ├── .Zeugwerk/
    │   └── config.json             ← test solution
    ├── MyLibrary_Tests.sln
    └── MyLibrary_Tests/
        ├── MyLibrary_Tests.tsproj
        └── MyLibrary_Tests/
            ├── PlcTask.TcTTO
            └── MyLibrary_Tests.plcproj

The test PLC is an application: it has a PLC task. In tests/.Zeugwerk/config.json its type is Application. Reference the code under test and TcUnit as packages, so Twinpack restores them before the test compile.

{
  "fileversion": 1,
  "solution": "MyLibrary_Tests.sln",
  "projects": [
    {
      "name": "MyLibrary_Tests",
      "plcs": [
        {
          "name": "MyLibrary_Tests",
          "version": "1.0.0.0",
          "type": "Application",
          "packages": [
            {
              "name": "MyLibrary",
              "version": "1.0.0.0",
              "branch": "main",
              "target": "TC3.1",
              "configuration": "Release",
              "distributor-name": "My Company"
            },
            {
              "name": "TcUnit",
              "version": null,
              "distributor-name": "www.tcunit.org"
            }
          ],
          "references": {
            "*": [
              "Tc2_System=*",
              "Tc3_Module=*"
            ]
          }
        }
      ]
    }
  ]
}

TcUnit_Zeugwerk can stand in for TcUnit when the suites extend TcUnit_Zeugwerk.FB_TestSuite and MAIN calls TcUnit_Zeugwerk.RUN().

How a suite looks

Each suite is a function block that extends FB_TestSuite. Declare an instance in MAIN. MAIN only calls RUN().

FUNCTION_BLOCK MotionTest EXTENDS TcUnit.FB_TestSuite
METHOD Test_Clamp
VAR
  actual : LREAL;
END_VAR

TEST('Clamp');
actual := MyLibrary.Clamp(-1, 0, 1);
AssertEquals_LREAL(0, actual, 1E-9, message := '');
TEST_FINISHED();
PROGRAM MAIN
VAR
  motionTest : MotionTest;
END_VAR

TcUnit.RUN();

What the build does

  1. Builds the main solution from the repository .Zeugwerk/config.json and produces the library.
  2. Sees tests/.Zeugwerk/config.json and, because the main PLC has no embedded IUnitTest function blocks, keeps that test solution as it is.
  3. Restores the test solution's packages, compiles it, deploys the boot project, and reads TcUnit_xUnit_results.xml.

Embedded IUnitTest function blocks in the main PLC take priority. In that case the build recreates tests/ and writes the generated harness over this solution. Keep the library free of IUnitTest when this layout is the one you want.

An in-solution UnitTestApplication in the main config.json is skipped while tests/.Zeugwerk/config.json exists.