CPPUTest 单元测试框架

发表于:2014-10-24 10:04

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:wang_yb    来源:51Testing软件测试网采编

  运行可执行文件 test 就可以实施测试.
$ ./test    <-- 默认执行, 没有参数
test.c:34: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)
=================================================================================
$ ./test -c   <-- -c 执行结果加上颜色 (成功绿色, 失败红色)
test.c:34: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms) <-- bash中显示红色
=================================================================================
$ ./test -v  <-- -v 显示更为详细的信息
TEST(sample, ret_int_failed)
test.c:34: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
- 1 ms
TEST(sample, ret_int_success) - 0 ms
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)
=================================================================================
$ ./test -r 2   <-- -r 指定测试执行的次数, 这里把测试重复执行2遍
Test run 1 of 2
test.c:34: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 0 ms)
Test run 2 of 2
test.c:34: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)
=================================================================================
$ ./test -g sample    <-- -g 指定 TEST_GROUP, 本例其实只有一个 TEST_GROUP sample
test.c:34: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
..
Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 1 ms)
=================================================================================
$ ./test -n ret_int_success    <-- -s 指定执行其中一个 TEST, 名称为 ret_int_success
.
OK (2 tests, 1 ran, 1 checks, 0 ignored, 1 filtered out, 0 ms)
=================================================================================
$ ./test -v -n ret_int_success  <-- 参数也可以搭配使用
TEST(sample, ret_int_success) - 0 ms
OK (2 tests, 1 ran, 1 checks, 0 ignored, 1 filtered out, 0 ms)
  2.6 补充: setup and teardown
  上面 test.c 文件中 TEST_GROUP(sample) 中的代码是空的, 其实 CPPUTest 中内置了 2 个调用 setup 和 teardown.
  在 TEST_GROUP 中实现这2个函数之后, 每个属于这个 TEST_GROUP 的 TEST 在执行之前都会调用 setup, 执行之后会调用 teardown.
  修改 test.c 中的 TEST_GROUP 如下:
/* 定义个 TEST_GROUP, 名称为 sample */
TEST_GROUP(sample)
{
void setup()
{
printf ("测试开始......\n");
}
void teardown()
{
printf ("测试结束......\n");
}
};
  重新执行测试: (每个测试之前, 之后都多了上面的打印信息)
$ make clean
clean...
rm -f test sample
rm -f sample.o test.o
$ make test
g++ -c -o test.o test.c -g -Wall -I/home/wangyubin/Downloads/cpputest-3.6/include
gcc -c -o sample.o sample.c -g -Wall -std=c99 -D CPPUTEST
g++ -o test test.o sample.o -L/home/wangyubin/Downloads/cpputest-3.6/lib -lCppUTest
$ ./test -v
TEST(sample, ret_int_failed)测试开始......
test.c:44: error: Failure in TEST(sample, ret_int_failed)
expected <3>
but was  <4>
difference starts at position 0 at: <          4         >
^
  测试结束......
  - 0 ms
  TEST(sample, ret_int_success)测试开始......
  测试结束......
  - 0 ms
  Errors (1 failures, 2 tests, 2 ran, 2 checks, 0 ignored, 0 filtered out, 0 ms)
43/4<1234>
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号