用glib來做單元測試。
setup, 創建受測對象,
teardown,釋放受測對象
#include <glib.h>
#include "dict.h"
typedef struct {
dictionary *dd;
} dfixture;
void dict_setup(dfixture *df, gconstpointer test_data){
df->dd = dictionary_new();
dictionary_add(df->dd, "key1", "val1");
dictionary_add(df->dd, "key2", NULL);
}
void dict_teardown(dfixture *df, gconstpointer test_data){
dictionary_free(df->dd);
}
void check_keys(dictionary const *d){
char *got_it = dictionary_find(d, "xx");
g_assert(got_it == dictionary_not_found);
got_it = dictionary_find(d, "key1");
g_assert_cmpstr(got_it, ==, "val1");
got_it = dictionary_find(d, "key2");
g_assert_cmpstr(got_it, ==, NULL);
}
void test_new(dfixture *df, gconstpointer ignored){
check_keys(df->dd);
}
void test_copy(dfixture *df, gconstpointer ignored){
dictionary *cp = dictionary_copy(df->dd);
check_keys(cp);
dictionary_free(cp);
}
void test_failure(){
if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)){
dictionary *dd = dictionary_new();
dictionary_add(dd, NULL, "blank");
}
g_test_trap_assert_failed();
g_test_trap_assert_stderr("NULL is not a valid key.\n");
}
int main(int argc, char **argv){
g_test_init(&argc, &argv, NULL);
g_test_add ("/set1/new test", dfixture, NULL,
dict_setup, test_new, dict_teardown);
g_test_add ("/set1/copy test", dfixture, NULL,
dict_setup, test_copy, dict_teardown);
g_test_add_func ("/set2/fail test", test_failure);
return g_test_run();
}
makefile 的寫法:
CFLAGS=-g -Wall -O3 pkg-config --cflags glib-2.0
LDADD=pkg-config --libs glib-2.0
dict_test: dict.o keyval.o
編譯後的訊息:
dict_test.c:47:5: warning: ‘g_test_trap_fork’ is deprecated (declared at /usr/include/glib-2.0/glib/gtestutils.h:198): Use 'g_test_trap_subprocess' instead [-Wdeprecated-declarations]
if (g_test_trap_fork(0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR)){
^
作者2010年寫的書,g_test_trap_fork 已經過期了,建議使用g_test_trap_subprocess。
而筆者寫裝的套件,glib2.0 (2.38.1-0ubuntu1) ,dev套件一起安裝。
試著改用g_test_trap_subprocess。網路上的中文資源相對較少,而整個測試框架的文件,範例不是很多。
使用這個單元測試庫有比較好嗎?