You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/faq.md
+45-17Lines changed: 45 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,6 +4,7 @@ title: F.A.Q.
4
4
---
5
5
6
6
## Frequently asked questions
7
+
7
8
Here you’ll find the most commonly asked questions and their answers.
8
9
If you don’t find what you are looking for here, you can look through the:
9
10
@@ -27,8 +28,8 @@ If you don’t find what you are looking for here, you can look through the:
27
28
28
29
---
29
30
30
-
31
31
### 1. How can I run a test across multiple PLC cycles?
32
+
32
33
This can be accomplished by keeping the function block under test as an instance variable of the test suite rather than the test method.
33
34
You can download an [example here](https://tcunit.org/temp/TimedTest_1x.zip).
34
35
In this example, the `FB_ToBeTested` is instantiated under the test suite (`FB_ToBeTested_Test`), and can thus be controlled over multiple cycles.
@@ -37,9 +38,10 @@ Then all that’s necessary to do is to set the condition for when the assertion
37
38
**Required TcUnit version:** 1.0 or later
38
39
39
40
### 2. How can I disable/ignore a test?
41
+
40
42
Add `DISABLED_` in front of the test name, for example:
41
43
42
-
```
44
+
```StructuredText
43
45
TEST('DISABLED_ThisTestWillBeIgnored');
44
46
45
47
AssertEquals(Expected := a,
@@ -48,9 +50,11 @@ AssertEquals(Expected := a,
48
50
49
51
TEST_FINISHED();
50
52
```
53
+
51
54
**Required TcUnit version:** 1.0 or later
52
55
53
-
### 3. Is there a way to test %I* or %Q* variables?
56
+
### 3. Is there a way to test `%I*` or `%Q*` variables?
57
+
54
58
In a number of scenarios, TwinCAT won't let you write directly to certain variables:
55
59
56
60
- Due to access restrictions (e.g. a variable in a FB's VAR)
@@ -59,29 +63,38 @@ In a number of scenarios, TwinCAT won't let you write directly to certain variab
59
63
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.
60
64
To support these cases, TcUnit provides helper functions like `WRITE_PROTECTED_BOOL()`, `WRITE_PROTECTED_INT()` (and so forth) for setting these type of variables.
61
65
For an example of how to use these, let's assume you have a test:
62
-
```
66
+
67
+
```StructuredText
63
68
METHOD PRIVATE TestCommsOkChannelsLow
64
69
VAR
65
70
EL1008 : FB_Beckhoff_EL1008;
66
71
END_VAR
67
72
```
73
+
68
74
Where the `FB_Beckhoff_EL1008` holds a variable:
69
-
```
75
+
76
+
```StructuredText
70
77
iChannelInput AT %I* : ARRAY[1..8] OF BOOL;
71
78
```
79
+
72
80
Now you might want to write a value to the first channel of the iChannelInput like:
Whereas afterwards you can make an assertion as usual:
78
-
```
88
+
89
+
```StructuredText
79
90
AssertFalse(Condition := EL1008.ChannelInput[1],
80
91
Message := 'Channel is not false');
81
92
```
93
+
82
94
**Required TcUnit version:** 1.0 or later
83
95
84
96
### 4. Is there a way to hide TcUnit in my libraries?
97
+
85
98
You can accomplish this by the [Hide reference](https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/18014402725266443.html&id=) option for referenced libraries.
86
99
This option lets you hide TcUnit from your other projects.
87
100
Let’s assume you’ve developed a library `MyLibrary`, which has tests written in TcUnit.
@@ -92,17 +105,17 @@ You can find it in the Properties tab:
92
105
93
106

94
107
95
-
96
108
**Required TcUnit version:** 1.0 or later
97
109
98
110
### 5. How do I do assertions on the BIT datatype?
111
+
99
112
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.
100
113
What do I do?
101
114
102
115
The reason a `AssertEquals_BIT()` does not exist is that TwinCAT does not allow a BIT-datatype as a variable input.
103
116
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()`.
### 6. When I run more than 100 tests in a single test-suite I get the wrong results, why?
131
+
118
132
When TcUnit is running it allocates memory in the PLC to store the test results.
119
133
The maximum number of tests for every test suite has been set to 100, which however is a configuration parameter for TcUnit and can be changed.
120
134
Parameters for TcUnit (and in fact any library references) are stored in your project, which means that this change will be persistent for your project/library.
@@ -124,8 +138,8 @@ To change this max amount, to say for instance 200 tests per test suite, go to t
124
138
125
139
**Required TcUnit version:** 1.0 or later
126
140
127
-
128
141
### 7. Is it possible to run test suites and/or tests in a sequence?
142
+
129
143
Yes.
130
144
By default TcUnit runs all the test suites and tests in parallel, in other words all test suites and tests are run at the same time.
131
145
Sometimes it is however desirable to run either the test suites or tests (or both) in a sequence, for example if you get exceed overruns while running tests.
@@ -134,7 +148,8 @@ Since TcUnit 1.2 it's possible to run test suites in sequence (one after another
134
148
To execute test suites in a sequence, simply replace `TcUnit.RUN()` with `TcUnit.RUN_IN_SEQUENCE()` in your main body of the test program.
135
149
This will execute the test suites in the order that they were declared.
136
150
So for example if we have defined the following test suites and test program:
This will first execute all tests defined in `fbDiagnosticMessageDiagnosticCodeParser_Test`, once all tests are finished in that function block, TcUnit will execute all tests in `fbDiagnosticMessageFlagsParser_Test`, and when that is done it will execute all tests in `fbDiagnosticMessageParser_Test`.
148
165
149
166
It's also possible to execute individual tests in order by simply replacing `TEST('TestName')` with `TEST_ORDERED('TestName')`.
150
167
This will execute the tests in the order that the `TEST_ORDERED()` is called for the various tests.
151
168
`TEST_ORDERED()` returns a boolean to indicate whether the TcUnit framework will run the test, so in order to only execute the code when it's time for that particular test, it makes sense to check if `TEST_ORDERED()` returns true, and only then do the execution of the function blocks and assertions, for example like this:
@@ -166,14 +183,14 @@ IF TEST_ORDERED('TestWithTimestampZeroTimeExpectCurrentTime') THEN
166
183
TEST_FINISHED();
167
184
END_IF
168
185
```
186
+
169
187
As usual, the `TEST_FINISHED()` will indicate that this test is finished, and the framework will go to the next test.
170
188
Note that you don't need to create any state machine for calling the different `TEST_ORDERED()` tests.
171
189
You can (and must!) call all `TEST_ORDERED()` at the same time.
172
190
The framework will make sure to only care about the assertions of the test that is currently running.
173
191
174
192
This means the following combinations can be used:
175
193
176
-
177
194
-`RUN()` with all tests as `TEST()` – means all tests suites and tests will run in parallel, this is the default behaviour.
178
195

179
196
-`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.
@@ -197,6 +214,7 @@ For a couple of TwinCAT projects that shows how to run both test suites in a seq
197
214
**Required TcUnit version:** 1.2 or later
198
215
199
216
### 8. Why is it taking so long to get the results from TcUnit?
217
+
200
218
If you have many test suites and/or tests, it can take some time for TcUnit to print all those results.
201
219
Since version 1.1 of TcUnit, much more data is printed to the ADS-logger as this data is used for the communication with TcUnit-Runner.
202
220
If you know that you will only run your tests locally and without integration to a CI/CD tool using TcUnit-Runner, you can set the parameter `LogExtendedResults` to `FALSE` (it is default `TRUE`).
@@ -207,6 +225,7 @@ To change this parameter, go to the library references and select TcUnit, then g
207
225
**Required TcUnit version:** 1.1 or later
208
226
209
227
### 9. Is it possible to have a time delay between the execution of the test suites?
228
+
210
229
Yes.
211
230
You can set the parameter `TimeBetweenTestSuitesExecution` to whatever delay you want to have.
212
231
To change this parameter, go to the library references and select TcUnit, then go to `GVLs` → `GVL_Param_TcUnit` → `TimeBetweenTestSuitesExecution`.
@@ -218,9 +237,11 @@ For example, in the below screenshot this is changed to 5 seconds.
218
237
**Required TcUnit version:** 1.2 or later
219
238
220
239
### 10. If I call ADSLOGSTR(), my messages don't show up in the correct sequence. Why?
240
+
221
241
If I call `Tc2_System.ADSLOGSTR()` during execution of a test, my messages don't arrive in the expected order.
222
242
Let's for example assume this very simple (always failing) test:
@@ -255,9 +276,11 @@ So if we replaced the call to `Tc2_System.ADSLOGSTR()` to `TCUNIT_ADSLOGSTR()` i
255
276
**Required TcUnit version:** 1.2 or later
256
277
257
278
### 11. How do I test functions?
279
+
258
280
It's done almost identical as in the introduction user guide, but simply replace the instance of the function block that you want to test with the call to the function instead.
### 12. I have problems running TcUnit on a ARMv7 controller, why?
317
+
290
318
When running TcUnit with a controller using ARMv7 you can run into issues, such as breakpoints not working.
291
319
This seems to be an issue with the limited memory of the controllers using an ARMv7 such as the CX8190 and CX9020. Please adjust the [parameters related to memory allocation](#6-when-i-run-more-than-100-tests-in-a-single-test-suite-i-get-the-wrong-results-why).
292
320
293
-
For more information on a set of working parameters, see [this issue on GitHub](https://github.com/tcunit/TcUnit/issues/148).
321
+
For more information on a set of working parameters, see [this issue on GitHub](https://github.com/tcunit/TcUnit/issues/148).
Copy file name to clipboardExpand all lines: docs/index.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,6 +6,8 @@ layout: home
6
6
list_title: ''
7
7
---
8
8
9
+
# TcUnit
10
+
9
11

10
12
11
13
TcUnit is an [xUnit](https://en.wikipedia.org/wiki/XUnit) type of framework specifically done for [Beckhoff’s TwinCAT 3](https://www.beckhoff.com/en-en/products/automation/twincat/) development environment.
@@ -17,18 +19,22 @@ Start by reading the [unit testing concepts](unit-testing-concepts.md) and then
17
19

18
20
19
21
## Easy to use
22
+
20
23
The framework is easy to use.
21
24
All that is needed is to download & install the library, and provide a reference to the TcUnit-library in your project, and you can start to write your test code.
22
25
For a complete set of instructions, start with [the concepts](unit-testing-concepts.md), continue with [the user guide](introduction-user-guide.md) and finish with [the programming example](programming-example.md).
23
26
24
27
## One library
28
+
25
29
All functionality is provided by one single library.
26
30
Add the library to your project and you are ready to go! You can either [download a precompiled](https://github.com/tcunit/TcUnit/releases) (ready to install) version of the library or [download the source code](https://www.github.com/tcunit/tcunit).
27
31
28
32
## MIT-license
33
+
29
34
The library and all the source code is licensed according to the MIT-license, which is one of the most relaxed software license terms.
30
35
The software is completely free and you can use the software in any way you want, be it private or for commercial use as long as you include the MIT license terms with your software.
31
36
32
37
## Automated test runs
38
+
33
39
With the additional TcUnit-Runner software, it’s possible to do integrate all your TcUnit tests into a CI/CD software toolchain.
34
-
With the aid of automation software such as [Jenkins](https://www.jenkins.io/) or [Azure DevOps](https://azure.microsoft.com/en-us/services/devops/), you can have your tests being run automatically and collect test statistics every time something is changed in your software version control (such as Git or Subversion).
40
+
With the aid of automation software such as [Jenkins](https://www.jenkins.io/) or [Azure DevOps](https://azure.microsoft.com/en-us/services/devops/), you can have your tests being run automatically and collect test statistics every time something is changed in your software version control (such as Git or Subversion).
Copy file name to clipboardExpand all lines: docs/introduction-user-guide.md
+14-8Lines changed: 14 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,9 +17,11 @@ The purpose of this user guide is to be a short tutorial where we will go throug
17
17
3. Create test suites and run the tests
18
18
19
19
## Download & install
20
+
20
21
The framework can either be downloaded as a [precompiled library](https://github.com/tcunit/TcUnit/releases), or you can download the [source code](https://www.github.com/TcUnit/TcUnit) and compile the library yourself.
21
22
22
23
### Install from library file
24
+
23
25
If you’ve downloaded the library, you should have a file called **TcUnit.library** in your computer.
24
26
Start your TwinCAT XAE (Visual Studio).
25
27
In the menu of Visual Studio select **PLC** and then **Library Repository...**
@@ -30,6 +32,7 @@ Click on **Install...**, locate the **TcUnit.library** file and double-click on
30
32
Now it will install to your TwinCAT-folder, more specifically C:\TwinCAT\3.1\Components\Plc\Managed Libraries\www.tcunit.org\TcUnit\.
31
33
32
34
### Install from source
35
+
33
36
If you want to install it from source, make sure that you have a TwinCAT XAE installed.
34
37
Next do a GIT-clone on the repository.
35
38
Open the folder where you cloned the repo, and open the solution by double-clicking on the TcUnit.sln file in the root of the folder, which will open the project in your TwinCAT XAE environment.
@@ -43,6 +46,7 @@ This will install the library on your computer.
43
46
Once the library is installed, the file that you saved on the desktop can be removed.
44
47
45
48
### Reference the library in project
49
+
46
50
In order to use TcUnit you need to add a reference to the library in your project.
47
51
Open your TwinCAT project, and right-click on the **References** under the PLC-project and click on **Add library...**
48
52
@@ -51,6 +55,7 @@ Open your TwinCAT project, and right-click on the **References** under the PLC-p
51
55
Next go to the TcUnit-group, select TcUnit and click **OK**.
52
56
53
57
### Create test suites and run them
58
+
54
59
For every function block (or free function) that you have defined we want to create a test function block (test suite), which has the responsibility to:
55
60
56
61
- Instantiate the FB under test
@@ -78,7 +83,7 @@ Each test suite is responsible of testing one FB or function, and can have one o
78
83
Let’s assume we want to create the simplest possible FB that takes two unsigned integers and sums them.
79
84
We can create the header for the FB, but the actual implementation can (and should) wait after we’ve done the unit tests.
@@ -108,7 +113,7 @@ For them well-named tests are invaluable.
108
113
We’ll be creating two tests called `TwoPlusTwoEqualsFour` and `ZeroPlusZeroEqualsZero`.
109
114
The `TwoPlusTwoEqualsFour` will look like this:
110
115
111
-
```
116
+
```StructuredText
112
117
METHOD TwoPlusTwoEqualsFour
113
118
VAR
114
119
Sum : FB_Sum;
@@ -136,7 +141,7 @@ This gives the flexibility to have tests that span over more than one PLC-cycle.
136
141
137
142
For `ZeroPlusZeroEqualsZero` it’s more or less the same code.
138
143
139
-
```
144
+
```StructuredText
140
145
METHOD ZeroPlusZeroEqualsZero
141
146
VAR
142
147
Sum : FB_Sum;
@@ -156,7 +161,8 @@ TEST_FINISHED();
156
161
```
157
162
158
163
Next we need to update the body of the test suite (`FB_Sum_Test`) to make sure these two tests are being run.
159
-
```
164
+
165
+
```StructuredText
160
166
TwoPlusTwoEqualsFour();
161
167
ZeroPlusZeroEqualsZero();
162
168
```
@@ -168,7 +174,7 @@ Being part of the library project we only want a convenient way to test all the
168
174
`PRG_TEST` needs to instantiate all the test suites, and only execute one line of code.
169
175
In this case we only have one test suite.
170
176
171
-
```
177
+
```StructuredText
172
178
PROGRAM PRG_TEST
173
179
VAR
174
180
fbSum_Test : FB_Sum_Test; // This is our test suite
@@ -191,7 +197,7 @@ As we can see, the test `TwoPlusTwoEqualsFour` failed, which means that the one
191
197
The reason this succeeds is that the default return value for an output-parameter is zero, and thus it means that even if we haven’t written the body of `FB_Sum` the test will succeed.
192
198
Let’s finish by implementing the body of `FB_Sum`.
193
199
194
-
```
200
+
```StructuredText
195
201
FUNCTION_BLOCK FB_Sum
196
202
VAR_INPUT
197
203
one : UINT;
@@ -217,4 +223,4 @@ Obviously this is a very simple example and the purpose of this was to show how
217
223
Simple functionality that does not require any state would be better suited to be implemented as a function, or in this case just using the "+" operator.
218
224
For a real-world example see the [programming example](programming-example.md).
219
225
220
-
The source code for this example is [available on GitHub](https://github.com/tcunit/ExampleProjects/tree/master/SimpleExampleProject).
226
+
The source code for this example is [available on GitHub](https://github.com/tcunit/ExampleProjects/tree/master/SimpleExampleProject).
0 commit comments