README.md (2955B)
1 <!-- 2 Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 3 4 SPDX-License-Identifier: curl 5 --> 6 7 # Unit tests 8 9 The goal is to add tests for *all* functions in libcurl. If functions are too 10 big 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 15 enable unit tests, there is a separate static libcurl built that is used 16 exclusively for linking unit test programs. Just build everything as normal, 17 and then you can run the unit test cases as well. 18 19 ## Run Unit Tests 20 21 Unit tests are run as part of the regular test suite. If you have built 22 everything to run unit tests, to can do 'make test' at the root level. Or you 23 can `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 28 If a specific test fails you get told. The test case then has output left in 29 the %LOGDIR subdirectory, but most importantly you can re-run the test again 30 using gdb by doing `./runtests.pl -g NNNN`. That is, add a `-g` to make it 31 start up gdb and run the same case using that. 32 33 ## Write Unit Tests 34 35 We put tests that focus on an area or a specific function into a single C 36 source file. The source file should be named `unitNNNN.c` where `NNNN` is a 37 previously unused number. 38 39 Add your test to `tests/unit/Makefile.inc` (if it is a unit test). Add your 40 test data filename to `tests/data/Makefile.am` 41 42 You also need a separate file called `tests/data/testNNNN` (using the same 43 number) that describes your test case. See the test1300 file for inspiration 44 and the `tests/FILEFORMAT.md` documentation. 45 46 For the actual C file, here's a simple example: 47 ~~~c 48 #include "unitcheck.h" 49 50 #include "a libcurl header.h" /* from the lib dir */ 51 52 static CURLcode test_unit9998(char *arg) 53 { 54 UNITTEST_BEGIN_SIMPLE 55 56 /* here you start doing things and checking that the results are good */ 57 58 fail_unless( size == 0 , "initial size should be zero" ); 59 fail_if( head == NULL , "head should not be initiated to NULL" ); 60 61 /* you end the test code like this: */ 62 63 UNITTEST_END_SIMPLE 64 } 65 ~~~ 66 67 Here's an example using optional initialization and cleanup: 68 ~~~c 69 #include "unitcheck.h" 70 71 #include "a libcurl header.h" /* from the lib dir */ 72 73 static CURLcode t9999_setup(void) 74 { 75 /* whatever you want done first */ 76 return CURLE_OK; 77 } 78 79 static void t9999_stop(void) 80 { 81 /* done before shutting down and exiting */ 82 } 83 84 static CURLcode test_unit9999(char *arg) 85 { 86 UNITTEST_BEGIN(t9999_setup()) 87 88 /* here you start doing things and checking that the results are good */ 89 90 fail_unless( size == 0 , "initial size should be zero" ); 91 fail_if( head == NULL , "head should not be initiated to NULL" ); 92 93 /* you end the test code like this: */ 94 95 UNITTEST_END(t9999_stop()) 96 } 97 ~~~