File: WriteTestsInApps.rst

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (372 lines) | stat: -rw-r--r-- 9,214 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
How To: Write Tests in Apps
---------------------------

TOC

This page describes how to write tests for components of apps. These
tests are located within the app folder

Each test program defines a *Test Suite*, a collection of related
*Tests*.

Initial Setup
~~~~~~~~~~~~~

Of course, we first need an app to write the tests for. We create a
simple example sandbox and app, but of course this will also work for
your own app.

::

    #ShellBox
    seqan-trunk # ./util/bin/skel.py repository sandbox/my_sandbox
    ...
    seqan-trunk # ./util/bin/skel.py app my_app sandbox/my_sandbox
    ...

Next, create a header for the code you want to test.

::

    #ShellBox
    seqan-trunk # ./util/bin/skel.py header my_module.h sandbox/my_sandbox/apps/my_app
    ...

Next, add the following function declaration to your header:

::

    #cpp
    // Returns the square of the given parameter.
    int mySquare(int x);

Then, create a file ``sandbox/my_sandbox/apps/my_app/my_module.cpp``
(thus in the same directory as ``module.h``):

::

    #cpp
    #include "my_module.h"

    int mySquare(int x)
    {
        return x * x;  // your code here
    }

To link your application binary against the new module, you have to
replace the ``add_executable()`` call in your
``sandbox/my_sandbox/apps/my_app/CMakeLists.txt``. In the
``CMakeLists.txt``, you should find a line that reads:

::

    add_executable (my_app my_app.cpp)

You have to change this line and add the new module as follows:

::

    add_executable (my_app
                    my_app.cpp
                    my_module.cpp
                    my_module.h)

Next, call CMake (in the build directory) to update your Makefiles or
IDE project files:

::

    #ShellBox
    seqan-build # cmake .
    ...

Creating the Test Program
~~~~~~~~~~~~~~~~~~~~~~~~~

Next, we create the test program and register it in the
``CMakeLists.txt``.

Create a new C++ file at
``sandbox/my_sandbox/apps/my_app/test_my_module.cpp`` with the following
contents:

::

    #cpp

    #undef SEQAN_ENABLE_TESTING
    #define SEQAN_ENABLE_TESTING 1

    #include <seqan/basic.h>

    #include "my_module.h"

    SEQAN_DEFINE_TEST(test_my_module_my_sqr)
    {
        SEQAN_ASSERT_EQ(mySquare(0), 0);
        SEQAN_ASSERT_EQ(mySquare(1), 1);
        SEQAN_ASSERT_EQ(mySquare(2), 4);
        SEQAN_ASSERT_EQ(mySquare(3), 9);
    }

    SEQAN_BEGIN_TESTSUITE(test_suite_name)
    {
        SEQAN_CALL_TEST(test_my_module_my_sqr);
    }
    SEQAN_END_TESTSUITE

Next, create a new executable entry in ``CMakeLists.txt``. We need a new
``add_executable()`` and a new ``target_link_libraries()`` entry for
your new tests.

Add the following below the ``target_link_libraries()`` for ``my_app``:

::

    add_executable (test_my_module
                    test_my_module.cpp
                    my_module.h
                    my_module.cpp)
    target_link_libraries (test_my_module ${SEQAN_LIBRARIES})

Note how similar these lines are to the corresponding lines for
``my_app``.

Next, run CMake in the build directory again to see the
``test_my_module`` program and run it:

::

    #ShellBox
    seqan-build # cmake .
    ...
    seqan-build # make test_my_module && ./bin/test_my_module

Test Suite Skelleton / Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Let us now consider the test program further.

``SEQAN_BEGIN_TESTSUITE(...)`` and ``SEQAN_END_TESTSUITE`` are macros
that expand to book-keeping code for running a test suite.
``SEQAN_DEFINE_TEST(...)`` expands to the definition of a function that
runs a test.

Note that when adding new tests then you have to add them to the
dependencies of the test target in
*sandbox/my\_sandbox/tests/my\_module/CMakeLists.txt*.

Test Macros
~~~~~~~~~~~

Inside your tests, you can use the ``SEQAN_ASSERT*`` and
``SEQAN_ASSERT_*_MSG`` macros to check for assertions. Other useful
macros are seqan:Macro.SEQAN\_PATH\_TO\_ROOT and
seqan:Macro.SEQAN\_TEMP\_FILENAME.

The macros themselves are documented on the `Macro page of the SeqAn
docs <https://www.seqan.de/dddoc/html_devel/INDEXPAGE_Macro.html>`__.

Best Practices
~~~~~~~~~~~~~~

**Rules are there to make you think before you break them.** The
following is not written into stone, but should be good guidelines.
Improvements to the best practices is welcome.

Be Consistent
^^^^^^^^^^^^^

Whatever you do: Be consistent. If the one has read one part of your
code then one should not have to adjust to different variable and
function naming, comment style etc.

Tests Should Compile Without Warnings
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

::

    #comment
    TODO(holtgrew): This belongs somewhere else, right?

Make sure that your tests compile without warnings. A common warning is
"comparison of signed and unsigned integer".

In many places, the problematic line looks like this

::

    #cpp
    SEQAN_ASSERT_LT(length(ndl), 30);

The ``length`` function returns an unsigned integer while the string
literal ``30`` represents a (signed) ``int``. You can fix this by
changing the type of the number literal:

::

    #cpp
    SEQAN_ASSERT_LT(length(ndl), 30u);

Break Your Tests Down
^^^^^^^^^^^^^^^^^^^^^

Each test should isolate target an as small as possible and/or feasible
unit of your code. Having short test functions makes them easier to read
and maintain.

Another advantage is that bogus state does not leak into other tests:
Imagine, you have a test that tests a function
``assign_if_positive(a, b)`` that assigns b to a if b is positive.

::

    #cpp
    SEQAN_DEFINE_TEST(test_assign)
    {
        int x = 0;

        assign_if_positive(x, 5);
        SEQAN_ASSERT_EQ(x, 5);

        assign_if_positive(x, -7);
        SEQAN_ASSERT_EQ(x, 5);
    }

Now, what happens if ``assign_if_positive(...)`` has a bug and *never*
assigns a value to its first parameter or always assigns 1? Both of your
assertions will fail. This means you do not really know in which case
the function works well and in which case it does not work well.

Splitting the test make it more robust:

::

    #cpp
    SEQAN_DEFINE_TEST(test_assign_positive)
    {
        int x = 0;
        assign_if_positive(x, 5);
        SEQAN_ASSERT_EQ(x, 5);
    }

    SEQAN_DEFINE_TEST(test_assign_negative)
    {
        int x = 0;
        assign_if_positive(x, -7);
        SEQAN_ASSERT_EQ(x, 0);
    }

Use Helper Functions For Setup/TearDown
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

If you need to initialize the same state for multiple tests, then the
code for this should only exist once. This makes it easier to maintain
since we do not have to change it in multiple places at once. This is
especially useful when following the best practice *Break Your Tests
Down*.

Example:

Instead of

::

    #cpp
    SEQAN_DEFINE_TEST(test_grep)
    {
        char *contents = loadFile("corpus.txt");

        int pos = doGrep(contents, "nonexisting pattern");
        SEQAN_ASSERT_EQ(pos, -1);

        pos = doGrep(contents, "existing pattern");
        SEQAN_ASSERT_EQ(pos, 3);

        delete contents;
    }

do

::

    #cpp

    // Set-up for test_grep_{success, failure}.
    void testGrepSetUp(const char *filename, char *outContents)
    {
        outContents = loadFile(filename);
    }

    // Tear-down for test_grep_{success, failure}.
    void testGraphTearDown(char *contents)
    {
        delete contents;
    }

    // Test greping for existing patterns.
    SEQAN_DEFINE_TEST(test_grep_success)
    {
        // corpus.txt contains the string "1234existing pattern567".
        char *contents;
        testGrepSetUp("corpus.txt", contents);

        int pos = doGrep(contents, "existing pattern");
        SEQAN_ASSERT_EQ(pos, 3);

        testGrepTearDown(contents);
    }

    // Test greping for non-existing patterns.
    SEQAN_DEFINE_TEST(test_grep_failure)
    {
        // corpus.txt contains the string "1234existing pattern567".
        char *contents;
        testGrepSetUp("corpus.txt", contents);

        int pos = doGrep(contents, "nonexisting pattern");
        SEQAN_ASSERT_EQ(pos, -1);

        testGrepTearDown(contents);
    }

Comment Your Tests
^^^^^^^^^^^^^^^^^^

Tests can complement examples from the documentation in that they
illustrate each call to your code's API. Thus, make sure that your tests
are well-documented. Not only for users who look up how to use your code
but also for the next maintainer.

There should be a documentation of the test itself and also inline
comments. In your comments, you should focus on the maintainer and not
so much the user. Even if some things are obvious, you might want to
illustrate why you call a function with the given parameters, e.g.
describe the corner cases.

Example:

::

    #cpp

    // Test abs() function with 1, a representant for positive values.
    SEQAN_DEFINE_TEST(test_abs_with_one)
    {
        SEQAN_ASSERT_EQ(abs(1), 1);
    }

    // Test abs() function with 0, the only corner case here.
    SEQAN_DEFINE_TEST(test_abs_with_zero) {
        SEQAN_ASSERT_EQ(abs(0), 0);
    }

    // Test abs() function with -1, a representant for negative values.
    SEQAN_DEFINE_TEST(test_abs_with_minus_one)
    {
        SEQAN_ASSERT_EQ(abs(-1), 1);
    }

.. raw:: mediawiki

   {{TracNotice|{{PAGENAME}}}}