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 個字符。