Project

General

Profile

Test Guidelines » History » Version 3

Hans-Martin Haase, 08/28/2015 09:17 AM
Remove integration test section.

1 1 Hans-Martin Haase
h1. Test Guidelines
2
3
Tests are an important part of the development work to ensure the functionality of the code. This wiki page covers the guideline regarding unit tests and states difficulties to define integration tests for new devices.
4
5
h2. Unit Tests
6
7
Unit tests are used to test the correct behavior of functions. The Open eCard project uses for test creation the "TestNG":http://testng.org/doc/index.html Framework which is inspired by JUnit and NUnit but provides new functionalities and is easier to use.
8
9
Test files have to be placed into the separate source tree @src/test@ relative to the root directory of the project or module. The Java files should be placed into @src/test/java@ while supporting files like images, XML files or whatever the test requires has to be located in @src/test/resources@. Most Java IDEs have integrated support for this structure and create it automatically but if you just use a editor without project support you should know this. In the @java@ directory the same package structure as in the source tree should be applied.
10
11
There is no strict rule which methods have to be tested. Here we give a brief overview of thing which should be tested and which not.
12
13
* Do not test private methods.
14
* Do not test getters or setters except they contain a lot of logic for input validation or something similar.
15
* At least one unit test per non trivial public method.
16
* Methods with parameters should be tested with correct and incorrect input data to ensure a well defined error handling.
17
* Do not specify tests which require a specific infrastructure e.g. a working network connection or a special kind  of smart cards attached to the test executing machine. 
18
* The unit test should be as small as possible.
19
* The name of the test file should be as descriptive as possible, e.g. if you test a specific method than chose something like WrongInputInFooBarTest.java or if you test all method of an object use something like ObjectNameTest.java
20
* Every unit test should be documented so that everyone is able to understand whats the expected result.
21
* Every unit test should contain an Assert.assert... call but also the annotation with @@Test(expectedExceptions=...)@ is legit in case of a test with wrong data.
22
23 2 Hans-Martin Haase
h2. Usage of TestNG
24
25
Here we provide a short introduction into the creation of unit tests with TestNG. For detailed information visit the "TestNG Documntation":http://testng.org/doc/index.html.
26
27
Specifying a test works the following way:
28
29
<pre>
30
<code class="Java">
31
package de.foo.bar
32
33
import org.testng.annotations.Test;
34
35
public class foo {
36
37
    @Test
38
    public void foobarTest() {
39
40
    }
41
}
42
</code>
43
</pre> 
44
45
So all you have to do is to annotate a public method with the @@Test@ annotation which instructs the TestNG Framework to interpret the function as test. But currently the test is always evaluated to @passed@ because no real rest method is called. So lets add some more detail to the test.
46
47
48
<pre>
49
<code class="Java">
50
package de.foo.bar
51
52
import org.testng.annotations.Test;
53
import org.testng.Assert;
54
55
public class foo {
56
57
    @Test
58
    public void foobarTest() {
59
        BarObject bar = new BarObject();
60
        bar.open(true);
61
        Assert.assertNotNull(bar);
62
        Assert.assertTrue(bar.isOpen());
63
    }
64
}
65
</code>
66
</pre>
67
68
69
The test creates now a @BarObject@ and sets the bar to open. Now the @assertNotNull()@ method checks that the bar is not @NULL@ that's obviously true. Furthermore a check is performed which test whether the bar is open and the bar is so the conditions is also true and the test is passed. If a condition in an Assert statement is not fulfilled the test is evaluated to failed.
70
71
Another feature of TestNG is to evaluate methods which throw exception in certain cases. 
72
73
74
<pre>
75
<code class="Java">
76
package de.foo.bar
77
78
import org.testng.annotations.Test;
79
80
public class foo {
81
82
    public static class Beer {
83
84
        public boolean open(int power) throws BottleBrokenExcepion {
85
86
            if (power > 75) {
87
                throw new BottleBrokenException();
88
            } else if (power < 25) {
89
                return false;
90
            } else {
91
                return true;
92
            }
93
        } 
94
95
    }
96
97
    @Test(expectedExceptions = BottleBrokenException.class)
98
    public void beerTest() {
99
        Beer beer = new Beer();
100
        bee.open(80);
101
    }
102
}
103
</code>
104
</pre>
105
106
In some case it might be necessary to create an object with a lot of methods if we would do that in the test method the method body would grow and grow. TestNG provides an annotation for methods to do such things before the test is launched.
107
108
<pre>
109
<code class="Java">
110
package de.foo.bar
111
112
import org.testng.annotations.Test;
113
import org.testng.Assert;
114
115
public class foo {
116
117
    private Cocktail cocktail;
118
119
    @BeforeClass
120
    public void init() {
121
        cocktail.add(new Limejuice());
122
        cocktail.add(new Vodka());
123
        cocktail.add(new Beer());
124
        cocktail.add(new Wine());
125
        cocktail.shake();
126
    }
127
128
    @Test
129
    public void cocktailTest() {
130
        Assert.assertFalse(cocktail.tastsGood());
131
    }
132
}
133
</code>
134
</pre>
135
136
*Note:* There are several other @@Before*@ annotations available see the TestNG documentation for the exact meaning.