CUnit 安装

CUnit 安装

本次安装的 Cunit 版本为 2.1.3 。

CUnit下载网址下载好 CUnit 2.1.3 源码后,依次输入下述命令构建安装 CUnit 。

sudo apt install automake
sudo apt install libtool

~/Downloads$ tar -jxvf CUnit-2.1-3.tar.bz2
~/Downloads$ cd CUnit-2.1-3
~/Downloads/CUnit-2.1-3$ aclocal
~/Downloads/CUnit-2.1-3$ autoheader
~/Downloads/CUnit-2.1-3$ libtoolize
~/Downloads/CUnit-2.1-3$ automake --add-missing
~/Downloads/CUnit-2.1-3$ autoconf
~/Downloads/CUnit-2.1-3$ automake
~/Downloads/CUnit-2.1-3$ ./configure --prefix <install directory>
~/Downloads/CUnit-2.1-3$ make
~/Downloads/CUnit-2.1-3$ sudo make install

以下都假设 <install directory> 为 /opt/CUnit 。

这个时候头文件和库文件分别在 /opt/CUnit/include 和 /opt/CUnit/lib 目录下,

/opt/CUnit/include$ ls
CUnit

/opt/CUnit/lib$ ls
libcunit.a  libcunit.la  libcunit.so  libcunit.so.1  libcunit.so.1.0.1  pkgconfig

不过此时不太方便使用,需要我们手动指定头文件搜索路径和库文件搜索路径。现在我们在系统的头文件和库文件搜索路径下创建个软连接解决这个问题。

tangjia@tangjia-VirtualBox:/usr/local/include$ sudo ln -s /opt/CUnit/include/CUnit/   CUnit
tangjia@tangjia-VirtualBox:/usr/local/include$ ls -l
lrwxrwxrwx 1 root root 25 6月  22 11:10 CUnit -> /opt/CUnit/include/CUnit/

tangjia@tangjia-VirtualBox:/usr/local/lib$ sudo ln -s /opt/CUnit/lib/libcunit.a libcunit.a
tangjia@tangjia-VirtualBox:/usr/local/lib$ ls -l
lrwxrwxrwx 1 root root    25 6月  22 11:11 libcunit.a -> /opt/CUnit/lib/libcunit.a

这样整个 CUnit 就安装好了,现在来测试一下。

创建一个 test.c 文件,输入下述官方例程

/*
 *  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();
}

编译链接执行:

tangjia@tangjia-VirtualBox:~$ gcc -o test test.c -lcunit
tangjia@tangjia-VirtualBox:~$ ./test

可以看见有如下结果:



     CUnit - A unit testing framework for C - Version 2.1-3
     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

大功告成。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容