本博客(http://blog.csdn.net/livelylittlefish )贴出作者(三二一@小鱼)相关研究、学习内容所做的笔记,欢迎广大朋友指正!
Content
1. What is CUnit?
2. What are the interfaces?
3. How to use CUnit framework?
4. An example
Appendix installation of CUnit
(1) install curses/ncurses
(2) install CUnit
1. Wha t is CUnit?
(1) CUnit is a system for writing, administering, and running unit tests in C.
(2) It is built as a static library which is linked with the user's testing code.
(3) CUnit uses a simple framework for building test structures, and provides a rich set of assertions for testing common data types.
2. What are the interfaces?
There are some kinds of interfaces of CUnit for running tests and reporting results.
(1) Automated interface with xml output
(2) Basic interface with non-interactive output to stdout
(3) Interactive console interface
(4) Interactive console interface (*nix)
(5) Windows interface (not yet implemented)
3. How to use CUnit framework?
The following is the typical of steps for using CUnit framework.
(1) Write functions for tests (and suite init/cleanup if necessary)
(2) Initialize the test registry, by calling CU_initialize_registry()
(3) Add suites to the test registry, by calling CU_add_suite()
(4) Add tests to the suites, by calling CU_add_test()
(5) Run tests using an appropriate interface, e.g. by calling CU_console_run_tests()
(6) Cleanup the test registry, by calling CU_cleanup_registry()
4. test, suite, registry
Registry 由 1 个或多个 suite 构成;
suite 由 1 个或多个 test case 构成;
suite 可以有 setup/teardown 方法,并在执行 suite 中的 tests 前 / 后被自动调用;
所有的 suites/tests 可由一个相应函数触发执行,例如, CU_console_run_tests() ,也可以选择某个 / 些 suites/tests 执行;
5 . An example
the platform can be cgywin (win32) or Linux . 该例子就是其官方文档中的例子,http://cunit.sourceforge.net/example.html ,代码如下。
/* * Simple example of a CUnit unit test. * * This program (crudely) demonstrates a very simple "black box" * test of the standard library functions fprintf() and fread(). * It uses suite initialization and cleanup functions to open * and close a common temporary file used by the test functions. * The test functions then write to and read from the temporary * file in the course of testing the library functions. * * The 2 test functions are added to a single CUnit suite, and * then run using the CUnit Basic interface. The output of the * program (on CUnit version 2.0-2) is: * * CUnit : A Unit testing framework for C. * http://cunit.sourceforge.net/ * * Suite: Suite_1 * Test: test of fprintf() ... passed * Test: test of fread() ... passed * * --Run Summary: Type Total Ran Passed Failed * suites 1 1 n/a 0 * tests 2 2 2 0 * asserts 5 5 5 0 */ #include <stdio.h> #include <string.h> #include "CUnit/Basic.h" /* Pointer to the file used by the tests. */ static FILE* temp_file = NULL; /* The suite initialization function. * Opens the temporary file used by the tests. * Returns zero on success, non-zero otherwise. */ int init_suite1(void) { if (NULL == (temp_file = fopen("temp.txt", "w+"))) { return -1; } else { return 0; } } /* The suite cleanup function. * Closes the temporary file used by the tests. * Returns zero on success, non-zero otherwise. */ int clean_suite1(void) { if (0 != fclose(temp_file)) { return -1; } else { temp_file = NULL; return 0; } } /* Simple test of fprintf(). * Writes test data to the temporary file and checks * whether the expected number of bytes were written. */ void testFPRINTF(void) { int i1 = 10; if (NULL != temp_file) { CU_ASSERT(0 == fprintf(temp_file, "")); CU_ASSERT(2 == fprintf(temp_file, "Q/n")); CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1)); } } /* Simple test of fread(). * Reads the data previously written by testFPRINTF() * and checks whether the expected characters are present. * Must be run after testFPRINTF(). */ void testFREAD(void) { unsigned char buffer[20]; if (NULL != temp_file) { rewind(temp_file); CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file)); CU_ASSERT(0 == strncmp(buffer, "Q/ni1 = 10", 9)); } } /* The main() function for setting up and running the tests. * Returns a CUE_SUCCESS on successful running, another * CUnit error code on failure. */ int main() { CU_pSuite pSuite = NULL; /* initialize the CUnit test registry */ if (CUE_SUCCESS != CU_initialize_registry()) return CU_get_error(); /* add a suite to the registry */ pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1); if (NULL == pSuite) { CU_cleanup_registry(); return CU_get_error(); } /* add the tests to the suite */ /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */ if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) || (NULL == CU_add_test(pSuite, "test of fread()", testFREAD))) { CU_cleanup_registry(); return CU_get_error(); } /* Run all tests using the CUnit Basic interface */ CU_basic_set_mode(CU_BRM_VERBOSE); CU_basic_run_tests(); CU_cleanup_registry(); return CU_get_error(); }
makefile文件内容
test: test.o
gcc -g -o $@ $^ -lcunit -lcurses
test.o: test.c
gcc -g -c $^
clean:
rm test *.o
注:使用CUnit框架的单元测试,需要curses库的支持。
编译运行如下,适合于Linux、Cygwin和Win32平台。
# make
gcc -c -g test.c
gcc -g -o test test.o -lcunit
# ./test
CUnit - A unit testing framework for C - Version 2.1-2
http://cunit.sourceforge.net/
Suite: Suite_1
Test: test of fprintf() ...passed
Test: test of fread() ...passed
Run Summary: Type Total Ran Passed Failed Inactive
suites 1 1 n/a 0 0
tests 2 2 2 0 0
asserts 5 5 5 0 n/a
Elapsed time = 0.000 seconds
Detailed information and programmers guide, please refer the reference.
Reference
http://cunit.sourceforge.net
http://cunit.sourceforge.net/doc/index.html
Appendix : installation of CUnit
(1) install curses/ncurses
Since CUnit will depend curses/ncurses, then, before install CUnit, curses/ncurses must be installed . Otherwise, there will be errors during compiling CUnit.
$ cd /usr/src
$ wget http://ftp.gnu.org/pub/gnu/ncurses/ncurses-5.9.tar.gz
$ tar -zxvf ncurses-5.9.tar.gz
$ ./configure
$ make
$ make install
The following links will show you the detail of curses/ncurses.
http://www.gnu.org/software/ncurses
http://en.wikipedia.org/wiki/Ncurses
http://en.wikipedia.org/wiki/Curses_%28programming_library%29
http://blog.csdn.net/dai_weitao/archive/2007/07/26/1708712.aspx
(2) install CUnit
CUnit must be installed in cygwin, since some header files will be used in the unit test.
# wget http://sourceforge.net/projects/cunit/files/CUnit/2.1-2/CUnit-2.1-2-src.tar.bz2
# bzip2 -d CUnit-2.1-2-src.tar.bz2
# tar -xvf CUnit-2.1-2-src.tar
# cd CUnit-2.1-2
# ./configure -h // 查看配置帮助
//enable debugging, and compile CUnit example programs
# ./configure --enable-debug=yes --enable-curses=yes --enable-examples=yes
Then, the header files and library of CUnit will be installed in cygwin. And you can run the examples for some basic concept.
The following links will show you the detailed information.
http://cunit.sourceforge.net
http://cunit.sourceforge.net/doc/index.html
http://cunit.sourceforge.net/doxdocs/index.html