CUnit与google test安装与使用

栏目: C++ · 发布时间: 6年前

内容简介:之前工作中使用的是CUnit,新工作使用的是与google test相似的自研框架,因此本文记录单独搭建CUnit与google test的方法,用于满足日常开发中demo编写过程中的测试。CUnit可以结合gcov lcov等使用,本文不介绍此部分。下载tools目录下的CUnit-2.1-2-src.tar.bz2,然后使用如下命令依次执行然后将CUnit官网的例子保存为example.c

之前工作中使用的是CUnit,新工作使用的是与google test相似的自研框架,因此本文记录单独搭建CUnit与google test的方法,用于满足日常开发中demo编写过程中的测试。CUnit可以结合gcov lcov等使用,本文不介绍此部分。

CUnit安装与使用

下载tools目录下的CUnit-2.1-2-src.tar.bz2,然后使用如下命令依次执行

$tar jxf CUnit-2.1-2-src.tar.bz2
$ cd CUnit-2.1-2
$ ./configure --prefix=/usr
$ make
$ make install

然后将CUnit官网的例子保存为example.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();
}

然后执行export LD_LIBRARY_PATH=/usr/lib/

LD_LIBRARY_PATH是 Linux 环境变量名,该环境变量主要用于指定查找共享库(动态链接库)时除了默认路径之外的其他路径。

因为libcunit.so等诸多库在/usr/lib目录下,不同系统可能有所不同,当然也可以在~/.bash_profile中添加/usr/lib,然后source .bash_profile,不用每次开机执行export命令。

然后执行命令:

gcc -g -o example example.c -lcunit

然后执行./example即可看到如下结果:
[root@centos-linux-7 cunit]# ./example


     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
[root@centos-linux-7 cunit]#

Google test安装与使用

安装方法:

git clone https://github.com/google/googletest
cd googletest
cmake -DBUILD_SHARED_LIBS=ON .
make
cd googlemock
sudo cp ./libgmock_main.so ./gtest/libgtest.so gtest/libgtest_main.so ./libgmock.so /usr/lib/
sudo ldconfig

测试代码:

#include <gtest/gtest.h>

int foobar(void)
{
    return 1;
}

TEST(foobar, test)
{
    ASSERT_EQ(1, foobar());
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

使用命令进行编译链接,结果如下:

[root@localhost fun]# g++ -c gtest.cpp
[root@localhost fun]# g++ gtest.o -lgtest
[root@localhost fun]# ls
a.out  gtest.cpp  gtest.o
[root@localhost fun]# ./a.out 

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from foobar
[ RUN      ] foobar.test
[       OK ] foobar.test (0 ms)
[----------] 1 test from foobar (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 1 test.
[root@localhost fun]#

以上所述就是小编给大家介绍的《CUnit与google test安装与使用》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

计算的本质

计算的本质

[英] Tom Stuart / 张伟 / 人民邮电出版社 / 2014-11 / 69.00元

《计算的本质:深入剖析程序和计算机》借助Ruby全面介绍计算理论和编程语言的设计。作者注重实用性,不仅尽量抛开复杂难懂的数学符号,而且特别选用简单快捷的编程语言Ruby,在读者熟知的背景知识下,以明晰的可工作代码阐明形式语义、自动机理论,以及通过lambda演算进行函数式编程等计算机科学知识,并为让其自行探索做足准备。 本书适合计算机科学系学生,以及熟知现代编程语言,想要系统地学习计算机科学......一起来看看 《计算的本质》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具