c - 如何在 C 中构建静态功能测试?

标签 c unit-testing unix

我在编译任何类型的合理结构以对模块的辅助函数/静态函数进行单元测试时遇到问题。几乎所有这个模块都是静态函数,而且它有很多,所以我尽量不把我所有的测试都放在同一个文件中。具体(大量)错误是: /usr/bin/ld:/usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): 重定位 0 具有无效的符号索引 11

但我对可以编译的通用方法很感兴趣。

从命令行,首先安装 Cunit:

# Install cunit
sudo apt-get install libcunit1 libcunit1-doc libcunit1-dev

module_a.c 中:

#include <stdio.h>

int main(void)
{
  // Do the real thing
  printf("The number 42: %d\n", get_42());
  printf("The number 0: %d\n", get_0());

  return 0;
}

static int32_t get_42(void)
{
  return 42;
}

static int32_t get_0(void)
{
  return 42;
}

module_a_tests.c 中:

#define UNIT_TEST
#include "module_a.c"
#include "CUnit/Basic.h"
#ifdef UNIT_TEST

int set_up(void)
{
  return 0;
}

int tear_down(void)
{
  return 0;
}

void run_good_fn(void)
{
  CU_ASSERT(42 == get_42());
}

void run_bad_fn(void)
{
  CU_ASSERT(0 == get_0());
}

int main(void)
{
  CU_pSuite p_suite = NULL;

  // Initialize
  if (CU_initialize_registry() != CUE_SUCCESS) {
    return CU_get_error();
  }

  p_suite = CU_add_suite("First Suite", set_up, tear_down);
  if (p_suite == NULL) {
    goto exit;
  }
  CU_basic_set_mode(CU_BRM_VERBOSE);

  // Add tests
  if (CU_add_test(p_suite, "Testing run_good_fn", run_good_fn) == NULL) {
    goto exit;
  }

  if (CU_add_test(p_suite, "Testing run_bad_fn", run_bad_fn) == NULL) {
    goto exit;
  }

  // Run the tests
  CU_basic_run_tests();

  exit:
    CU_cleanup_registry();

  return CU_get_error();
}
#endif

相关:

How to test a static function

最佳答案

这有点 hacky,但解决此问题的一种方法是在正确的位置(在所有静态函数的前向声明之后)使用 #include 作为原始文本替换。对位置有依赖性,但如果您遵循约定,则很容易理解:

module_a.c 中:

#include <stdio.h>

// Comment this macro in and out to enable/disable unit testing
#define UNIT_TEST

static int32_t get_42(void);
static int32_t get_0(void);

#ifndef UNIT_TEST
int main(void)
{
  // Do the real thing
  printf("The number 42: %d\n", get_42());
  printf("The number 0: %d\n", get_0());

  return 0;
}
#else
#include "module_a_tests.c"
#endif

static int32_t get_42(void)
{
  return 42;
}

static int32_t get_0(void)
{
  return 42;
}

module_a_tests.c 中:

// Add a #include guard
#ifndef MODULE_A_TESTS_C
#define MODULE_A_TESTS_C

#include "CUnit/Basic.h"

int set_up(void)
{
  return 0;
}

int tear_down(void)
{
  return 0;
}

void run_good_fn(void)
{
  CU_ASSERT(42 == get_42());
}

void run_bad_fn(void)
{
  CU_ASSERT(0 == get_0());
}

int main(void)
{
  CU_pSuite p_suite = NULL;

  // Initialize
  if (CU_initialize_registry() != CUE_SUCCESS) {
    return CU_get_error();
  }

  p_suite = CU_add_suite("First Suite", set_up, tear_down);
  if (p_suite == NULL) {
    goto exit;
  }
  CU_basic_set_mode(CU_BRM_VERBOSE);

  // Add tests
  if (CU_add_test(p_suite, "run_good_fn", run_good_fn) == NULL) {
    goto exit;
  }

  if (CU_add_test(p_suite, "run_bad_fn", run_bad_fn) == NULL) {
    goto exit;
  }

  // Run the tests
  CU_basic_run_tests();

  exit:
    CU_cleanup_registry();

  return CU_get_error();
}
#endif

关于c - 如何在 C 中构建静态功能测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30588529/

相关文章:

c++ - LNK2019:VS 单元测试中未解析的外部符号

c++ - 当要测试的类很复杂时,如何编写单元测试?

linux - 用 bash 设置参数

python - 如何在 blender 中使用环境变量

c - 8 位循环的内联程序集大小不匹配

c - *ptr++ 对下一个内存位置有副作用吗?

c++ - 在 C++ 中对数组执行随机排列

java - 如何开始为我的 Android 应用程序进行单元测试?

java - 读取 Dockerfile 命令 : Is it possible to cd into a jar file? 时遇到问题,tar -cf 有何作用?

c++ - 为什么要在定义宏之前取消定义它们?