Skip to content
Wei-ju Wu edited this page Dec 17, 2016 · 16 revisions

Welcome to the chibitest.c wiki!

General Usage

The simplest case is to define a test suite using chibi_suite_new(), define a test case using the CHIBI_TEST macro and add it to the suite using chibi_suite_add_test(). The standard runner function is chibi_suite_run(), and chibi_suite_summary() will print a simple report to the console. Free up the resources associated with the test suite using chibi_suite_delete().

#include "chibi.h"

CHIBI_TEST(MyTest)
{
  ...
}

int main(int argc, char **argv)
{
  chibi_suite *suite = chibi_suite_new("MySuite");
  chibi_suite_add_test(suite, MyTest);
  chibi_suite_run(suite, NULL);
  chibi_suite_delete(suite);

  return 0;
}

Currently supported assertions

void chibi_assert_not_null(void *ptr);
void chibi_fail(const char *msg);
void chibi_assert(int cond);
void chibi_assert_msg(int cond, const char *msg);
void chibi_assert_eq_int(int expected, int value);
void chibi_assert_eq_cstr(const char *expected, const char *value);

Test fixtures

You can define test suites that will call user-defined setup()/teardown() functions using chibi_suite_new_fixture():

struct { int a; } mydata;
void mysetup(void *userdata) {...}
void myteardown(void *userdata) {...}
CHIBI_TEST(mytest)
{
  /* access userdata object through the TC_USERDATA value */
  do_something_with(TC_USERDATA);
}

int main(int argc, char **argv)
{
  ...
  chibi_suite *suite = chibi_suite_new_fixture("MySuite", mysetup, myteardown, &mydata);
  chibi_summary_data summary;
  ...
  chibi_suite_run(suite, &summary);
  ...
}

Nested suites

You can use chibi_suite_add_suite() to create groups of test cases and run them together as part of a top level test suite. This is particularly useful if you want to group related tests into suites and/or define sets of test cases with certain fixtures. When running the TAP runner, the tests are reported as if they were part of a single larger suite.

int main(int argc, char **argv)
{
  ...
  chibi_suite *toplevel = chibi_suite_new("ToplevelSuite");
  chibi_summary_data summary;
  chibi_suite *group1 = chibi_suite_new("ChildSuite1");
  chibi_suite *group2 = chibi_suite_new("ChildSuite2");

  chibi_suite_add_suite(toplevel, group1);
  chibi_suite_add_suite(toplevel, group2);
  ...
  chibi_suite_run(toplevel, &summary);
  ...
}

How to use with automake

chibi_test provides a simple test runner that supports the TAP protocol. If you are using automake, using this runner can help providing more details about your tests.

For more information about the TAP protocol: automake: Using the TAP test protocol

Below are some examples how to set up the respective project files to support TAP protocol tests.

mytest.c

#include "chibi.h"

...
int main(int argc, char **argv)
{
  chibi_suite *suite = chibi_suite_new("MySuite");
  chibi_summary_data summary;
  chibi_suite_add_test(suite, MyTest1);
  chibi_suite_add_test(suite, MyTest2);
  ...
  chibi_suite_run_tap(suite, &summary);
  chibi_suite_delete(suite);
  return summary.num_failures;
}

configure.ac

AM_INIT_AUTOMAKE([subdir-objects])
...
AC_REQUIRE_AUX_FILE([tap-driver.sh])

Makefile.am

TESTS = test/alltests
...
LOG_DRIVER = env AM_TAP_AWK='$(AWK)' $(SHELL) \
              $(top_srcdir)/tap-driver.sh
...
check_PROGRAMS = $(TESTS)

test_alltests_SOURCES = test/mytest.c test/chibi.c