ALLINSIGHT

Home of the AlmostImplementedException

Your first TTCN-3 script

Before we start our first testscript, we’ll check a few pre-conditions:
– You have a working TTCN-3 compiler and know how to use it. I’ll use TTWorkbench.
– You have a strong stomach, a high frustration tolerance and a fair amount of programming experiance (yes you’ll need that too)

And now we can start. First we have to create a new file (in TTWorkbench you have to create it in a TTCN-3 Project) and name this file SimpleTest.ttcn3.
Now we create a TTCN-3 module in this file and name it SimpleTest too.

[ttcn3]
module SimpleTest{
}
[/ttcn3]

And here the first “fun-fact”: The module-name has nothing to do with the file name. Which means there is no way for others (or your self) to find a specific module if you use another name for your file -> Rule 1: always use the same file- and modulename.
Another thing is, you can use a module-name only once in your workspace / project.

Now we add a testcase:

[ttcn3]
module SimpleTest{
testcase simpleTest(){
}
}
[/ttcn3]

When you try to compile this, you’ll get an error (in TTWorkbench a very unhelpful one, don’t cry -> get used to it).
In TTCN-3 every testcase needs to define on which component it is running. In this component you can define the ports you want to use, but later more.
For a start we create an empty component, name it SimpleType and use it for our testcase.

[ttcn3]
module SimpleTest{

type component SimpleType {
}

testcase simpleTest() runs on SimpleType{
}
}
[/ttcn3]

Et voilĂ , you can compile it. After that you can run the testcase.
In TTWorkbench you have to create a testcampaign in order to use it. Just right click on the TTCN-3 file and choice “Generate Default Test Campaign” / “Recursively”. When its done you will find a new file called SimpleTest.clf. Double-click on it to load it into the execution manager. After that, just click on run and watch the result.

As we can see its finishing without any errors, but since we don’t do anything thats not unexpected. So lets add a little calculation to our test.

[ttcn3]
testcase simpleTest() runs on SimpleType{
var integer i:= 3 + 5 * 2;

if (match(i, 13)){
setverdict(pass);
}else{
setverdict(fail);
}
}
[/ttcn3]

First (at line 2) we add a variable of type integer and assign the result of 3 + 5 * 2 to it. Next (line 4) we match this variable with our expected result of 13. The match-command is very powerful as we’ll see in later posts, when we use our own codecs, templates and records.

Finally we set the result of our test at line 5 or 7 (depending on the result). I assume you know how to use a if-else-block.

Now try it yourself. Replace the old testcase with this new one, recompile the file, reload the testcampaign (you don’t need to create it again unless you add a new testcase) and execute it.
You should get a pass as result. In TTWorkbench you can use the Graphical-Logging to see it in a nice green.

To see what happens when a testcase fails, just change the expected result to something else.
And thats your first TTCN-3 testcase.

Share :

Leave a Reply

Your email address will not be published. Required fields are marked *