Google Test Primer(四)——简单测试

Simple Tests
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


测试例子


To create a test:

创建一个测试


    1、Use the
TEST() macro to define and name a test function, These are ordinary C++ functions that don't return a value.

         使用TEST()宏定义个和命名一个测试函数,这些函数是普通无返回值的C++函数。

    2、In this function, along with any valid C++ statements you want to include, use the various Google Test assertions to check values.
        
这些函数中,可以使用任何合法的C++语句,可以使用各类Google Test断言检测值。

    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()
arguments go from general to specific. The first argument is the name of the test case, and the second argument is the test's name within the test case. Remember that a test case can contain any number of individual tests. A test's full name consists of its containing test case and its individual name. Tests from different test cases can have the same individual name.


TEST()
参数名从一般到特殊:第一个参数是测试案例名字,第二个参数是测试案例中的测试名字。记住,一个测试案例可以包含任何一个独立的测试。一个测试的全名由包含测试案例和独立测试名字组成。来自不同的测试案例的测试可以有相同的独立测试名字。


For example, let's take a simple integer function:

以简单的整数函数为例:

int Factorial(int n); // Returns the factorial of n


A test case for this function might look like:

这个函数的测试一个测试案例可能如下:

// Tests factorial of 0.

TEST(FactorialTest, HandlesZeroInput) {

  EXPECT_EQ(1, Factorial(0));

}

 

// Tests factorial of positive numbers.

TEST(FactorialTest, HandlesPositiveInput) {

  EXPECT_EQ(1, Factorial(1));

  EXPECT_EQ(2, Factorial(2));

  EXPECT_EQ(6, Factorial(3));

  EXPECT_EQ(40320, Factorial(8));

}


Google Test groups the test results by test cases, so logically-related tests should be in the same test case; in other words, the first argument to their
TEST() should be the same. In the above example, we have two tests, HandlesZeroInput and HandlesPositiveInput, that belong to the same test case FactorialTest.


Google Test
组合了测试案例的测试结果,因此逻辑上相关的测试应该在同一个测试案例中。此外,相关测试的TEST()的第一个参数名应该相同,在上面的例子中,我们有两个测试HandlesZeroInputHandlesPositiveInput,但属于同一个测试案例FactorialTest


Availability
: Linux, Windows, Mac.

转载请注明来自ubunoon[http://www.cnblogs.com/ubunoon]

转载于:https://www.cnblogs.com/ubunoon/archive/2008/11/08/GoogleTestPrimerTestCase.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值