C Language Unit Test

Source: Internet
Author: User

 

 

CUnit:
1. CUnit introduction.
1.1 describes how to use CUnit.

CUnit is a unit testing framework for C-language programs. Online documents say that it is linked to the user's test code as a static Link Library. It provides a simple framework to build a test architecture and provides a wide range of Assertion to test common data types. In addition, it provides many different structures to run test cases and report test results.

(1) CUnit Architecture
Test Registry
|
------------------------------
|
Suite '1'... Suite 'n'
|
------------------------------
|
Test '11'... Test '1m' Test 'n1 '... Test 'nm'

This framework provides a foundation for the use of CUnit.
Let's first introduce this framework. There are two statements from the bottom layer:
(1) Each test case is packaged in a test package (suite,
(2) Each Test suite is registered in a valid Test Registry.

For CUnit, it runs in a single thread, so there can only be one valid Test Registry at a time. The Test Registry Unit can contain multiple Test packages ), each test package can have multiple test cases. The rules for dividing a test package (suite) can be freely agreed. For example, the test cases of a module are centralized into a test package (suite ). Test Cases are used to test functions in a module. The test case function calls the tested function through various input provided, returns the execution result, and then uses the assertions provided by CUnit to determine whether the tested function is correct.

(2) Test Mode

The following are four test modes:
1 automatic Output to xml file Non-interactive
2 Basic Flexible programming interface Non-interactive
3 Console interface (ansi C) Interactive
4 Curses Graphical interface (Unix) Interactive
The first mode is to output the results to the XML document for easy report generation. The second mode is to display the test results in standard output after each running, and the test result data cannot be retained. The third mode is the console mode, which enables human-computer interaction. The first two modes are non-interactive. The fourth type is only used in Unix.

(3) Basic test process
1) write unit test functions (write the init/cleanup function of suite if necessary ). Write functions for tests (and suite init/cleanup if necessary ).
2) Call the CU_initialize_registry () function to initialize the Test Registry ). Initialize the test registry-CU_initialize_registry ()
3) Call the CU_add_suite () function to add the Test package (suite) to the Test Registry. Add suites to the test registry-CU_add_suite ()
4) Call the CU_add_test () function to add the test case to the test package (suite. Add tests to the suites-CU_add_test ()
5) Use appropriate interfaces to run test cases. Run tests using an appropriate interface, e.g. CU_console_run_tests
6) Call the CU_cleanup_registry function to clear the Test Registry ). Cleanup the test registry-CU_cleanup_registry ()



××××××××××××××××××××××××××××××××××××××××××× ×××××××××××× × Code
/*File:test.c *Auth:sjin *Date:2014-03-20 *Mail:413977243@qq.com */#include test.h#include 
 
  int sum(int a,int b){    return a + b;}
 
/*File:test.h *Auth:sjin *Date:2014-03-20 *Mail:413977243@qq.com */#ifndef __TEST_H_#define __TEST_H_int sum(int a,int b);#endif

/* File: testmain. c * Auth: sjin * Date: 2014-03-20 * Mail: 413977243@qq.com */# include test. h # include
 
  
# Include
  
   
# Include int InitSuite () {return 0;} int EndSuite () {return 0;} int Test_Is_Equal (int a, int B, int real) {int result; result = sum (a, B); if (result = real) {return 1;} return 0;} int Test_Is_Not_Equal (int a, int B, int real) {int result; result = sum (a, B); if (result! = Real) {return 1;} return 0;} void Test1 () {CU_ASSERT (Test_Is_Equal (3, 4, 7);} void Test2 () {CU_ASSERT (Test_Is_Not_Equal (4, 5, 10);}/* 0: sucessful; 1: failed */int AddTestMain () {# if 1 CU_pSuite pSuite = NULL; /******************* 1. CU_add_suite adds a Suite * 2. suite name: testSuite * 3. initSuite EndSuite is the initial and release functions of the test unit. if NULL */pSuite = CU_add_suite (testSuite, InitSuite, EndSuite) is not required, // if (NULL! = PSuite) {if (NULL = CU_add_test (pSuite, Test1, Test1) | NULL = CU_add_test (pSuite, Test2, Test2) {return 1 ;} ///}/*********** another test method ***********/# else CU_TestInfo testcases [] ={{ Test1, test1}, {Test2, Test2}, CU_TEST_INFO_NULL}; CU_SuiteIndo Suites [] ={{ Test the functon sum:, InitSuite, EndSuite, testcases}, Region}; if (CUE_SUCCESS! = CU_register_suites) {return 1 ;} /*****************************/# endif return 0 ;}
  
 
/* File: CuintRunTest. c * Auth: sjin * Date: 2014-03-20 * Mail: 413977243@qq.com */# include
 
  
# Include
  
   
Extern int AddTestMain (); int main () {// initialize if (CUE_SUCCESS! = CU_initialize_registry () {return CU_get_error ();} // return the assert (NULL! = CU_get_registry); // checks whether assert (! CU_is_test_running (); // call the test module to complete the test case if (0! = AddTestMain () {CU_cleanup_registry (); return CU_get_error ();}/* use the console to control the function entry of the interactive interface */CU_console_run_tests (); /* use the automatic XML file generation mode */CU_set_output_filename (TestMax); CU_list_tests_to_file (); CU_automated_run_tests ();/* clear registration information after calling */CU_cleanup_registry ();}
  
 

Makefile
INC=-I/usr/local/includeLIB=-L/usr/local/liball:test.c CUnitRunTest.c testMain.c        gcc $^ -o test $(INC) $(LIB) -lcunitclean:        rm -f test


When the library cannot be loaded during compilation
export LD_LIBRARY_PATH=/usr/local/lib


References: 1 http://www.cnblogs.com/linux-sir/archive/2012/08/25/2654557.html 2 'HTTP: // blog.csdn.net/colin719/article/details/1420583


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.