xref: /curl/tests/unit/README.md (revision 4f198c85)
1<!--
2Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3
4SPDX-License-Identifier: curl
5-->
6
7# Unit tests
8
9The goal is to add tests for *all* functions in libcurl. If functions are too
10big and complicated, we should split them into smaller and testable ones.
11
12## Build Unit Tests
13
14`./configure --enable-debug` is required for the unit tests to build. To
15enable unit tests, there will be a separate static libcurl built that will be
16used exclusively for linking unit test programs. Just build everything as
17normal, and then you can run the unit test cases as well.
18
19## Run Unit Tests
20
21Unit tests are run as part of the regular test suite. If you have built
22everything to run unit tests, to can do 'make test' at the root level. Or you
23can `cd tests` and `make` and then invoke individual unit tests with
24`./runtests.pl NNNN` where `NNNN` is the specific test number.
25
26## Debug Unit Tests
27
28If a specific test fails you will get told. The test case then has output left
29in the %LOGDIR subdirectory, but most importantly you can re-run the test again
30using gdb by doing `./runtests.pl -g NNNN`. That is, add a `-g` to make it
31start up gdb and run the same case using that.
32
33## Write Unit Tests
34
35We put tests that focus on an area or a specific function into a single C
36source file. The source file should be named `unitNNNN.c` where `NNNN` is a
37previously unused number.
38
39Add your test to `tests/unit/Makefile.inc` (if it is a unit test). Add your
40test data file name to `tests/data/Makefile.am`
41
42You also need a separate file called `tests/data/testNNNN` (using the same
43number) that describes your test case. See the test1300 file for inspiration
44and the `tests/FILEFORMAT.md` documentation.
45
46For the actual C file, here's a simple example:
47~~~c
48
49    #include "curlcheck.h"
50
51    #include "a libcurl header.h" /* from the lib dir */
52
53    static CURLcode unit_setup( void )
54    {
55      /* whatever you want done first */
56      return CURLE_OK;
57    }
58
59    static void unit_stop( void )
60    {
61      /* done before shutting down and exiting */
62    }
63
64    UNITTEST_START
65
66      /* here you start doing things and checking that the results are good */
67
68      fail_unless( size == 0 , "initial size should be zero" );
69      fail_if( head == NULL , "head should not be initiated to NULL" );
70
71      /* you end the test code like this: */
72
73    UNITTEST_STOP
74