- you know what parametrized tests are
- you know how you can add further test cases to parametrized tests
- you've seen how parametrized tests are look like
- you can add further test cases to parametrized tests
- you're able to handle a failed test
- you've can use explicit type conversion
-
In some cases you want to test a functions or method with different input parameters. This can be solved in this way:
USING Axunit.Assert; USING Simatic.Ax.Tutorial; NAMESPACE Tutorial.Test {TestFixture} CLASS Test_Add {Test} METHOD PUBLIC Test_values VAR_TEMP result : DINT; END_VAR Add(v1 := 1, v2 := 1 , result => result); Equal(2, result); Add(v1 := -1, v2 := -1 , result => result); Equal(DINT#-2, result); Add(v1 := 1, v2 := -1 , result => result); Equal(DINT#0, result); Add(v1 := -1, v2 := 1 , result => result); Equal(DINT#0, result); END_METHOD END_CLASS END_NAMESPACE
you'll find the code in the file
test/Test_Add.st
In this case, the Add function will be tested with the input parameter:
Test# v1 v2 expected result 1. 1 1 2 2. -1 -1 -2 3. 1 -1 0 3. -1 1 0 Another solution are
parametrized tests
-
Open the file
Test_Add_Parametrized.st
-
Uncomment all the code by pressing
STRG + a
(select all) and thenSTRG + #
(uncomment all)Here you see, that the test pragma can also have some parameters. The parameters
i1
,i2
,res
will be forwarded to the input variablesi1
,i2
,res
of the methodTest_values
-
Execute the tests
In the test explorer you can see, that the test is executed four times with different parameters.
-
If not, open the file
Test_Add_Parametrized.st
-
Extend the parametrized test by another test case with the values
i1 := 32000
,i2 := 32000
, andres := DINT#32000
Your test configuration should be look like:
-
Execute the test
Result: you'll get a failed test.
The test shows you, that your expected value
64000
does not match the acual value-1536
-
hover over the failed test in the explorer and click on
Go to Test
. The test file will be opened. -
Press
CTRL
hover over theAdd
function. The Add method will be underlined. Click left to jump theAdd
function
-
The problem for our failed test was obviously an integer overflow. Why can I say this? Because I know it.
A look in line 13 will explain this:
result := v1 + v2;
v1
andv2
are of the typeINT
. When we add 32000 + 32000, the addition will be done as an integer operation, where the maximum value is 32767. To solve this, we've to convert theINT
toDINT
explicitly. -
Replace the line 13 in the file by
result := TO_DINT(v1) + TO_DINT(v2);
The integer values will be converted to double integer values
-
Run the tests again
The tests should now run without errors
Goal reached? Check yourself...
- you know what parametrized tests are ✔
- you know how you can add further test cases to parametrized tests ✔
- you've seen how parametrized tests are look like ✔
- you can add further test cases to parametrized tests ✔
- you're able to handle a failed test ✔
- you've can use explicit type conversion ✔