WinCC Open Architecture (OA) has supported Unit Tests for quite a while now, however, when Siemens released version 3.16, one of the new features was High-Speed Programming. This new feature set included a tool for project documentation and the ability to auto-generate templates for faster creation of code. In this blog, we are going to look deeper at the new way to run and write Unit Tests inside of the graphical editor (GEDI).
Step 1: Create a Script
The first thing we need to do is create a script, or class, that we would like to unit test. I made a very simple script with two functions inside of it. If we were using a Test-Driven Development (TDD) methodology writing the functions would come later. Learn more about TDD
Step 2: Auto-generating Testing Script
Once you have the script created, all you will need to do is right-click that script and select the Open Unit Test option. This will autogenerate a WinCC OA Test class which is used to run the unit tests.
Here you can see the created script and libs.
Step 3: Define your Tests
When you open the autogenerated script you will see a group of code already written. The parts that will need to be added to are on line 28 and inside the startTestCase() function. Inside the makeDynString on line 28 you will need to add in names of the tests you would like to run. These tests should be small and only test a small unit of your code. I’ve added “add” and “getDataPoint”.
Step 4: Write the Tests
Now that the tests are defined it’s time to actually write them. Because we’ve added two entries above we need to add two cases to the switch statement. Inside of the “add” case, I did a simple assertEqual(6,result); . This means I am testing that 6 equals the result of 2 plus 3. This test will fail because this is not true. I also wrote a simple test that reads a datapoint and asserts that the value is what I expect it to be.
Here is the entire script.
Step 5: Run the Test
Now that the tests are written we can now run them by right-clicking the test script and selecting Start Tests from the options. This will cause the script and tests to run and display the results.
Step 6: View the Results
After we ran the test, from inside the GEDI we can see the results. We have two fail and one success judging by the thumbs up or thumbs down. The two that failed were the “add” and the “UnitTestDemo_”, which is a default autogenerated test that can be deleted.
For those that have been using Unit Tests inside of WinCC OA, none of this is to new because of the long support of Unit Tests in the platform but the new High-Speed Programming set of tools will make it easier for others to begin seeing the benefits of Unit Testing.