Adding a Unit Test
For low level operations, it is important to write unittests. They are
usually directly included at the end of the file that has the code to
test. It is possible to create a new file in regress/src
or
bacula/src/tools
.
The C code should be compiled with the appropriate Makefile rule (see
alist_test
) for example.
mytest_test: Makefile libbac.la mytest.c unittests.o
$(CXX) -DTEST_PROGRAM $(DEFS) $(DEBUG) -c $(CPPFLAGS) -I$(srcdir) -I$(basedir) $(DINCLUDE) $(CFLAGS) mytest.c -o mytest_test.o
$(LIBTOOL_LINK) $(CXX) $(LDFLAGS) -L. -o $@ mytest_test.o unittests.o $(DLIB) -lbac -lm $(LIBS) $(OPENSSL_LIBS)
$(LIBTOOL_INSTALL) $(INSTALL_PROGRAM) $@ $(DESTDIR)$(sbindir)/
The Bacula Unittest framework is a simple set of functions that helps to write a unittest. It has functions to
Test variables
Log messages
Configure/Unconfigure Bacula tools such as mempool, lock manager
Do some high level filesystem functions (mkdir, rmdir, stat)
Ease some string operations
The following code explains how to use the Bacula Unittest framework:
int function_to_test(int parameter1, int parameter2)
{
/* some interesting code */
return 1;
}
/* In this case, the test procedure is at the end of the file,
* it can be also in a separated file
*/
#ifdef TEST_PROGRAM
#include "lib/unittests.h"
int main(int argc, char **argv)
{
Unittests tests("app_test");
tests.set_nb_tests(4); /* Plan the number of expected tests */
tests.configure(TEST_QUIET|TEST_PRINT_LOCAL); /* Configure */
log("Test a the function_to_test()");
is(function_to_test(1,2), 1, "Test function_to_test(1,2)");
ok(function_to_test(1,2) == 1, "Test function_to_test(1,2)");
isnt(function_to_test(3,4), 2, "Test function_to_test(3,4)");
nok(function_to_test(3,4) == 2, "Test function_to_test(3,4)");
return report();
}
#endif /* TEST_PROGRAM */
The current options in the unittest library are:
- TEST_QUIET
Display only test in error
- TEST_PRINT_LOCAL
Display the local variables after an error
These options can be set via an environment variable (UNITTEST_TEST_QUIET, UNITTEST_TEST_PRINT_LOCAL).
See also
Possible Next Steps
Go to Bacula MD5 Algorithm.
Go back to Bacula Regression Testing.
Go back to Developer Guide.