PLCs and test driven development

PLCs and test driven development

This is a crosspost from my blog https://www.alltwincat.com/

The user base for TcUnit, the free open-source PLC/TwinCAT unit testing framework, has grown enormously. Current estimates are that there are over 100 installations of TcUnit worldwide. Running an open-source project has for some moments taken all my spare-time to maintain the framework, and answer all the questions from the users (I’ve added an FAQ since). The most asked-for functionality of TcUnit that was missing was the possibility to run unit tests in a sequence/in order. Up to now, all tests defined in TcUnit were running in parallel, while in many use cases it was desirable to run the tests in sequence. Thanks to several fantastic contributors from the TcUnit community on GitHub, this is now available in TcUnit starting from version 1.2.

The use cases for being able to run test suites or individual tests in a sequence rather than in parallel have been many, but the two most common ones are:

  1. Being able to test function blocks which include sequences and/or timers, and thus are dependent on the tests being able to finish in time
  2. Running tests on lower performance CPUs (like the Beckhoff CX8100-series of PLCs, or upcoming CX7000), which if many tests were defined meant that not all tests would finish in time and we would get exceed counts, which could affect certain tests

There are two ways to run tests in a sequence, which are also possible to combine.

  1. Run the test suites in a sequence
  2. Run the individual tests in a sequence

Running the test suites in a sequence is achieved by simply replacing the standard call to TcUnit.RUN() with TcUnit.RUN_IN_SEQUENCE(). The order of execution of the test suites will be according to in which order they are declared. This means that if you already have some TcUnit tests, you only need to change one line of code to replace your parallel tests with tests that run in a sequence.

For even higher flexibility, it’s also possible to determine in which order each individual test will be executed/evaluated. This is accomplished by replacing the call to your TEST() with TEST_ORDERED(). The order of execution of the tests will be in what order the tests are called.

This means the following combinations can be used:

  • RUN() with all tests as TEST() – means all tests suites and tests will run in parallel, this is the default behavior in all previous versions of TcUnit
No alt text provided for this image
  • RUN_IN_SEQUENCE() with all tests as TEST() – means all test suites will run in sequence, but the tests in every test suite will run in parallel
No alt text provided for this image
  • RUN() with all tests as TEST_ORDERED() – means all test suites will run in parallel, but the tests in every test suite will run in sequence
No alt text provided for this image
  • RUN_IN_SEQUENCE() with all tests as TEST_ORDERED() – means all test suites will run in sequence, as will every test
No alt text provided for this image

To download the latest version of TcUnit, go to the releases on GitHub (either ready-to-use precompiled library or complete source code). For documentation on how to run tests in sequence visit the TcUnit.org website. There are some example TwinCAT projects added to showcase how test suites and/or test cases can be run in a sequence. All of them are available free to download on GitHub: https://github.com/tcunit/ExampleProjects/tree/master/RunTestsInSequenceExampleProjects

With this release, the TcUnit community hope you will find these new features useful so that we as a community can continue bringing unit testing and test driven development to the world of automation!

While on the topic of open-source initiatives for industrial automation, I'd highly recommend you to look at TcOpen, a great open-source initiative for TwinCAT developers!

Rafael Silva

Strategic Development Manager | Excellence in Software ALM | Product Security | Product Quality | AI-Enabler | Innovation

3 年
Nicolas Veilleux, P.Eng, MASc

Manufacturing Automation Scale Up Expert | Vision Builder | ex-Tesla Battery Division

3 年

Great work! This is very useful.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了