• Install CUnit test framework on ubuntu


         CUnit is a lightweight system for writing, administering, and running unit tests in C.  It provides C programmers a basic testing functionality with a flexible variety of user interfaces.
         CUnit is built as a static library which is linked with the user's testing code.  It uses a simple framework for building test structures, and provides a rich set of assertions for testing common data types.   In addition, several different interfaces are provided for running tests and reporting results. 

    its official webset is:CUnit Home

    now we start to install CUnit environment on Ubuntu18.04

    1.download the CUint source code

    download CUnit from below website:

    C Unit Testing Framework - Browse /CUnit at SourceForge.net

    now we use the latest version 2.1-3.

    2: extract

    3.configuration & compile & install

    the CUint test environment use jam compile default, you can install jame use command:

    sudo apt install jam

    then execute the configuration with bootstrap in top dir.

    1. $ ./bootstrap
    2. $ ./configure --prefix=/home/czl/Workspace/CUint/prefix

    then execute make to compile and install

    make && make install

    install dir:

    Test

    download the default test case from below website:

    CUnit Example

    1. /*
    2. * Simple example of a CUnit unit test.
    3. *
    4. * This program (crudely) demonstrates a very simple "black box"
    5. * test of the standard library functions fprintf() and fread().
    6. * It uses suite initialization and cleanup functions to open
    7. * and close a common temporary file used by the test functions.
    8. * The test functions then write to and read from the temporary
    9. * file in the course of testing the library functions.
    10. *
    11. * The 2 test functions are added to a single CUnit suite, and
    12. * then run using the CUnit Basic interface. The output of the
    13. * program (on CUnit version 2.0-2) is:
    14. *
    15. * CUnit : A Unit testing framework for C.
    16. * http://cunit.sourceforge.net/
    17. *
    18. * Suite: Suite_1
    19. * Test: test of fprintf() ... passed
    20. * Test: test of fread() ... passed
    21. *
    22. * --Run Summary: Type Total Ran Passed Failed
    23. * suites 1 1 n/a 0
    24. * tests 2 2 2 0
    25. * asserts 5 5 5 0
    26. */
    27. #include <stdio.h>
    28. #include <string.h>
    29. #include "CUnit/Basic.h"
    30. /* Pointer to the file used by the tests. */
    31. static FILE* temp_file = NULL;
    32. /* The suite initialization function.
    33. * Opens the temporary file used by the tests.
    34. * Returns zero on success, non-zero otherwise.
    35. */
    36. int init_suite1(void)
    37. {
    38. if (NULL == (temp_file = fopen("temp.txt", "w+"))) {
    39. return -1;
    40. }
    41. else {
    42. return 0;
    43. }
    44. }
    45. /* The suite cleanup function.
    46. * Closes the temporary file used by the tests.
    47. * Returns zero on success, non-zero otherwise.
    48. */
    49. int clean_suite1(void)
    50. {
    51. if (0 != fclose(temp_file)) {
    52. return -1;
    53. }
    54. else {
    55. temp_file = NULL;
    56. return 0;
    57. }
    58. }
    59. /* Simple test of fprintf().
    60. * Writes test data to the temporary file and checks
    61. * whether the expected number of bytes were written.
    62. */
    63. void testFPRINTF(void)
    64. {
    65. int i1 = 10;
    66. if (NULL != temp_file) {
    67. CU_ASSERT(0 == fprintf(temp_file, ""));
    68. CU_ASSERT(2 == fprintf(temp_file, "Q\n"));
    69. CU_ASSERT(7 == fprintf(temp_file, "i1 = %d", i1));
    70. }
    71. }
    72. /* Simple test of fread().
    73. * Reads the data previously written by testFPRINTF()
    74. * and checks whether the expected characters are present.
    75. * Must be run after testFPRINTF().
    76. */
    77. void testFREAD(void)
    78. {
    79. unsigned char buffer[20];
    80. if (NULL != temp_file) {
    81. rewind(temp_file);
    82. CU_ASSERT(9 == fread(buffer, sizeof(unsigned char), 20, temp_file));
    83. CU_ASSERT(0 == strncmp(buffer, "Q\ni1 = 10", 9));
    84. }
    85. }
    86. /* The main() function for setting up and running the tests.
    87. * Returns a CUE_SUCCESS on successful running, another
    88. * CUnit error code on failure.
    89. */
    90. int main()
    91. {
    92. CU_pSuite pSuite = NULL;
    93. /* initialize the CUnit test registry */
    94. if (CUE_SUCCESS != CU_initialize_registry())
    95. return CU_get_error();
    96. /* add a suite to the registry */
    97. pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
    98. if (NULL == pSuite) {
    99. CU_cleanup_registry();
    100. return CU_get_error();
    101. }
    102. /* add the tests to the suite */
    103. /* NOTE - ORDER IS IMPORTANT - MUST TEST fread() AFTER fprintf() */
    104. if ((NULL == CU_add_test(pSuite, "test of fprintf()", testFPRINTF)) ||
    105. (NULL == CU_add_test(pSuite, "test of fread()", testFREAD)))
    106. {
    107. CU_cleanup_registry();
    108. return CU_get_error();
    109. }
    110. /* Run all tests using the CUnit Basic interface */
    111. CU_basic_set_mode(CU_BRM_VERBOSE);
    112. CU_basic_run_tests();
    113. CU_cleanup_registry();
    114. return CU_get_error();
    115. }

    compile&test

    gcc -o cunit test.c -I/home/czl/Workspace/CUint/prefix/include -L/home/czl/Workspace/CUint/prefix/lib -lcunit -static

     if you need not install CUnit from source code, you can use the apt install manner to install CUnit packet directly.

    sudo apt install libcunit1-dev

    another CPP Unit test framwork are called gtest, you can refer this artical:

    CSDNhttps://mp.csdn.net/mp_blog/creation/editor/127637639


    End

  • 相关阅读:
    windows 驱动与内核调试 学习2
    CY3/CY5/CY7荧光标记甜菊糖Stevioside,三氯蔗糖Sucralose,D-(+)-海藻糖D-(+)-Trehalose Dihydrate
    华为机试 - 字符串重新排列
    基于Springboot+Vue的校园在线打印预约系统
    tcp/ip协议2实现的插图,数据结构
    [python]使用pyinstaller打包带界面的Pytorch程序的多个问题
    JAVA设计模式-工厂模式(Factory Pattern)
    Babel AST代码转换、生成
    Java线程池-异步任务编排
    文本识别论文CRNN
  • 原文地址:https://blog.csdn.net/tugouxp/article/details/127798123