11 Star 66 Fork 12

zhuangbo / MiniUnit

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 2.17 KB
一键复制 编辑 原始数据 按行查看 历史

MiniUnit -- A mini unit test framework for C/C++.

README: English | 中文

This project is inspired by MinUnit, which is a minimal unit testing framework for C. MiniUnit provides more flexible assertions and presents test results with better readability, including display the file name and line number in color text.

MiniUnit is

  • Simple: Assert, test, show result, no extra code.
  • Flexible: Flexible assertions, optional messages, even with arguments.
  • Pretty: Display file name and line number, support color text.
  • Small: Only one header file, about 120 lines.

Features

Assertions

  • mu_assert(expr) assertion fail if expr false
  • mu_assert(expr, message) assertion with message
  • mu_assert(expr, message, args...) assertion and message with no more than 16 args

Test function

A test is a function without parameters and return int. It return 0 only if all assertions passed. For example,

int test_something()
{
  mu_assert(1 + 1 == 2);
  return 0;  // 0 表示测试通过
}

Run tests

  • mu_run_test(test) to run a test function int test() which return 0 if passed

Show results

  • mu_test_results() to display the test results

No color

#define MU_NOCOLOR
#include "miniunit.h"

Example

For example,

#include "miniunit.h"

int test_one()
{
  mu_assert(2 + 2 == 4);
  return 0; // 0 means test passed
}

int test_two()
{
  int a = 3, b = 5;
  mu_assert(a == 3);
  mu_assert(b == 5, "b is 5");
  mu_assert(a + b == 7, "should be %d", a + b); // fail
  return 0;
}

int main()
{
  mu_run_test(test_one);
  mu_run_test(test_two);

  mu_test_results();

  return 0;
}

Output in color mode:

Output in no-color mode:

|- test_one ./mu_example.c:21 ... ✔
|- test_two ./mu_example.c:22 ... ✘
|\_[FAIL] at ./mu_example.c:15 for 'a + b == 7' ✘ should be 8
\_________________________________
1 ✔ and 1 ✘ in 2 TEST(S)
#### 1 TEST(S) FAILED ####
C
1
https://gitee.com/zhuangbo/MiniUnit.git
git@gitee.com:zhuangbo/MiniUnit.git
zhuangbo
MiniUnit
MiniUnit
master

搜索帮助