tests/unit/README: describe how to unit test static functions

UNITTEST explained

Closes #21018
This commit is contained in:
Daniel Stenberg 2026-03-19 23:05:05 +01:00
parent 98d8e82c74
commit 2b3332e749
No known key found for this signature in database
GPG Key ID: 5CC908FDB71E12C2

View File

@ -95,3 +95,35 @@ Here's an example using optional initialization and cleanup:
UNITTEST_END(t9999_stop())
}
~~~
## Testing static functions
Lots of internal functions are made static, and they *should* be static if
they are private within a single source file.
The curl build system provides a way to write unit tests that let us properly
test these functions while keeping them static in release builds.
A function that is static in the build but should be provided for unit testing
needs to replace its `static` keyword with `UNITTEST` and it needs to have a
prototype provided immediately above it.
An example `add_two_integers()` function for unit testing:
~~~c
UNITTEST int add_two_integers(int a, int b);
UNITTEST int add_two_integers(int a, int b)
{
return a + b;
}
~~~
Since the function is static and is private for this source file, it should
not have its prototype in any header file.
When building unit tests, the build system automatically generates the
`lib/unitprotos.h` header file with all the prototypes for `UNITTEST`
functions provided in any libcurl C source code files. (This is done by the
`scripts/extract-unit-protos` script.)