Chinaunix首页 | 论坛 | 博客
  • 博客访问: 101763
  • 博文数量: 20
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-21 11:18
文章分类

全部博文(20)

文章存档

2011年(1)

2009年(3)

2008年(16)

我的朋友

分类: C/C++

2009-04-03 20:49:24

2009/03/27
gtest 编译
gtest 可以用于windows,linux等,
也可以使用 自己的构建,读README中的说明,
cd ${SRCDIR}
  g++ -I. -I./include -c src/gtest-all.cc
  ar -rv libgtest.a gtest-all.o
  g++ -I. -I./include path/to/your_test.cc libgtest.a -o your_test

  可以修改gtest-all.cc中,啬src/gtest_main.cc进行编译,运行,测试生成的类库是否
能正确运行。

2009/03/27
gtest 的使用

基本概念
assertions 检查条件是否为真的程序语句,一个assertions 结果可以是: success, nonfatal failure, or fatal failure.
如是fatal failure,则中断当前函数。

Tests
使用assertions来验证被测试代码的行为。是测试的基本单元。

test case
test case 包括一个或多个tests, 你必须把tests组织到test case里,test case反射测试代码的结构。
当在一个test case 中包括多的tests需要共享objects and subroutines,你就需要把tests加到test fixture
类中。?

test program
一个测试程序包括多个test case

SUCCEED();
FAIL();

Basic Assertions
These assertions do basic true/false condition testing.

Fatal assertion             Nonfatal assertion      Verifies
ASSERT_TRUE(condition);     EXPECT_TRUE(condition);     condition is true
ASSERT_FALSE(condition);     EXPECT_FALSE(condition);     condition is false

Binary Comparison

This section describes assertions that compare two values.

Fatal assertion                 Nonfatal assertion     Verifies
ASSERT_EQ(expected, actual);    EXPECT_EQ(expected, actual);    expected == actual
ASSERT_NE(val1, val2);             EXPECT_NE(val1, val2);     val1 != val2
ASSERT_LT(val1, val2);             EXPECT_LT(val1, val2);     val1 < val2
ASSERT_LE(val1, val2);             EXPECT_LE(val1, val2);     val1 <= val2
ASSERT_GT(val1, val2);             EXPECT_GT(val1, val2);     val1 > val2
ASSERT_GE(val1, val2);             EXPECT_GE(val1, val2);     

String Comparison

The assertions in this group compare two C strings. If you want to compare two string objects, use EXPECT_EQ, EXPECT_NE, and etc instead.

Fatal assertion                        Nonfatal assertion     Verifies
ASSERT_STREQ(expected_str, actual_str); EXPECT_STREQ(expected_str, actual_str);
                                    the two C strings have the same content

ASSERT_STRNE(str1, str2);                 EXPECT_STRNE(str1, str2);     
                                   the two C strings have different content
ASSERT_STRCASEEQ(expected_str, actual_str);    EXPECT_STRCASEEQ(expected_str, actual_str);     
                                   the two C strings have the same content, ignoring case
ASSERT_STRCASENE(str1, str2);    EXPECT_STRCASENE(str1, str2);     
                                    the two C strings have different content, ignoring case

出错时,打印信息
EXPECT_EQ(x[i], y[i]) << "Vectors x and y differ at index " << i;

Advanced Options

--help flag
--gtest_list_tests //list the available tests in a program
--gtest_filter //flag to a filter string --gtest_filter=*Name?  ":"separated list of wildcard patterns
--gtest_repeat=1000
--gtest_repeat=1000 --gtest_filter=FooBar
--gtest_output flag to the string "xml:_path_to_output_file_", which will create the file at the given location.
--gtest_break_on_failure command line flag.

Temporarily Disabling Tests
If you have a broken test that you cannot fix right away, you can add the DISABLED_ prefix to its name

定义tests
   1. Use the TEST() macro to define and name a test function, These are ordinary C++ functions that don't return a value.
   2. In this function, along with any valid C++ statements you want to include, use the various Google Test assertions to check values.
   3. The test's result is determined by the assertions; if any assertion in the test fails (either fatally or non-fatally), or if the test crashes, the entire test fails. Otherwise, it succeeds.

TEST(test_case_name, test_name) {
 ... test body ...
}

Test Fixtures: Using the Same Data Configuration for Multiple Tests

To create a fixture, just:

   1. Derive a class from testing::Test . Start its body with protected: or public: as we'll want to access fixture members from sub-classes.
   2. Inside the class, declare any objects you plan to use.
   3. If necessary, write a default constructor or SetUp() function to prepare the objects for each test. A common mistake is to spell SetUp() as Setup() with a small u - don't let that happen to you.
   4. If necessary, write a destructor or TearDown() function to release any resources you allocated in SetUp() . To learn when you should use the constructor/destructor and when you should use SetUp()/TearDown(), read this FAQ entry.
   5. If needed, define subroutines for your tests to share.

When using a fixture, use TEST_F() instead of TEST() as it allows you to access objects and subroutines in the test fixture:

TEST_F(test_case_name, test_name) {
 ... test body ...
}

Invoking the Tests

After defining your tests, you can run them with RUN_ALL_TESTS() 
阅读(2057) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~