来自 Arch Linux 中文维基

Faction 是一个用于测试驱动软件开发的 C 语言库。

安装

安装 libfactionAUR 软件包。

用法

该库提供了几个 C 宏,使编写测试变得更快。

  • FI 表示 Faction Initialization
  • FT 表示 Faction Test
  • FC 表示 Faction Close

使用 FT 宏时,需要填写三个字段。

  • AUTHORS() takes a comma-separated list of double-quotation surrounded author names
  • SPEC() takes a single double-quotation surrounded test specification description
  • A C boolean expression (just like when using C assert macros)

按照惯例,Faction Test 应写在包含测试代码的源文件的底部。Test 应由 FACTION 宏保护(见下面的示例)包围,以便在编译时启用或禁用。C 编译器(如 GNU C 编译器 (GCC))m提供了一种在命令行上启用宏的方法(即 -D 标志)。

示例

/* 这就是要测试的函数 */
int
increment(int input)
{
   return (input + 1);
}

#ifdef FACTION
#include <faction.h>
#include <limits.h>
FI

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 1 when given 0" ),
    increment(0) == 1
  );

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 0 when given the largest integer value" ),
    increment(INT_MAX) == 0
  );

FC
#endif

可以使用 gcc filename.c -D FACTION 对其进行编译。

模式

Faction 有两种编译模式:最小模式和扩展模式。

上述示例以最小模式编译 Faction。最小化编译只依赖三个库:stdlib、stdio 和 getopt。扩展编译则需要额外的依赖库,包括一些只能通过 GNU 功能测试宏使用的函数。

因此,要在扩展模式下编译,只需在文件顶部定义 GNU 功能测试宏即可。例如,将前面的示例修改为在扩展模式下编译,就会变成这样:

#ifdef FACTION
#define _GNU_SOURCE
#endif
/* 这就是要测试的函数 */
increment(int input)
{
  return (input + 1);
}

#ifdef FACTION
#include <faction.h>
#include <limits.h>
FI

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 1 when given 0" ),
    increment(0) == 1
  );

  FT(
    AUTHORS( "timetoplatypus" ),
    SPEC( "increment() returns 0 when given the largest integer value" ),
    increment(INT_MAX) == 0
  );

FC
#endif

扩展模式功能

在扩展模式下,

  • 可以在运行时使用 -l 标记将输出镜像到用户指定的日志文件。
  • 结果表将根据所使用终端的宽度动态调整大小。否则,默认宽度为 78 个字符。

参见