Google Test测试框架自带Sample案例注释翻译

本文翻译了Google Test(gtest)框架自带的sample测试样例的注释,详细介绍了测试流程、方法和设计。项目工程可在附件中下载,适用于Visual Studio 2005环境。对gtest感兴趣的读者可以参考金山公司前辈的技术博客进行深入学习。提供工程结构图表,包括多个sample的源码和对应的unittest文件。
摘要由CSDN通过智能技术生成

      有一段时间没写博客了,前些时间闲下来看了下google test(gtest)测试框架自带的sample测试样例,感觉还不错,就对里面的注释进行了相关翻译,在此做个标录,对测试流程、方法和设计的理解会有些帮助,方便日后重新查阅,有兴趣的同志不妨看一下

相关说明:

     1、项目工程可到附件中下载

     2、项目工程在visual studio2005环境下编译运行的

     3、对于想对gtest有进一步了解的通知,可参考金山公司一个前辈的gtest技术博客http://www.cnblogs.com/coderzh/archive/2009/03/31/1426758.html(玩转Google开源C++单元测试框架Google Test系列(gtest))

     4、转载请保留该博客地址:http://mzhx-com.iteye.com/

    

正所谓,外行看热闹,内行看门道,在此就不多做解释了,注释和源码相信应该是最好的老师吧

 

工程结构图表:

sample1.h

//一个用来展示如何应用Google C++测试框架的简单程序示例

#ifndef GTEST_SAMPLES_SAMPLE1_H_
#define GTEST_SAMPLES_SAMPLE1_H_

// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
//返回n!(n的阶乘),对于负数n,n!约定等于1
int Factorial(int n);

// Returns true iff n is a prime number.
//如果n是素数返回true
bool IsPrime(int n);

#endif  // GTEST_SAMPLES_SAMPLE1_H_

 

sample1.cc

//一个用来展示如何应用Google C++测试框架的简单程序示例
#include "stdafx.h"
#include "sample1.h"
// Returns n! (the factorial of n).  For negative n, n! is defined to be 1.
//返回n!(n的阶乘),对于负数n,n!约定等于1
int Factorial(int n) {
	int result = 1;
	for (int i = 1; i <= n; i++) {
		result *= i;
	}
	return result;
}
// Returns true iff n is a prime number.
//如果n是素数返回true
bool IsPrime(int n) {
	// Trivial case 1: small numbers
	//情况1:小于等于1的数字
	if (n <= 1) return false;
	// Trivial case 2: even numbers
	//情况2:整数
	if (n % 2 == 0) return n == 2;
	// Now, we have that n is odd and n >= 3.
	//现在,对于那些大于3的奇数
	// Try to divide n by every odd number i, starting from 3
	//从3开始,用每个奇数i去除n
	for (int i = 3; ; i += 2) {
		// We only have to try i up to the squre root of n
		if (i > n/i) break;
		// Now, we have i <= n/i < n.
		// If n is divisible by i, n is not prime.
		if (n % i == 0) return false;
	}
	// n has no integer factor in the range (1, n), and thus is prime.
	return true;
}

 

sample1_unittest.cc

// This sample shows how to write a simple unit test for a function,
// using Google C++ testing framework.
//这个示例用来展示如何应用Google C++测试框架来写一个简单的函数测试单元
// Writing a unit test using Google C++ testing framework is easy as 1-2-3:
//应用Google C++写一个单元测试很简单,只需要1-2-3共三个步骤


// Step 1. Include necessary header files such that the stuff your
// test logic needs is declared.
//第一步:引入一些必须的头文件
// Don't forget gtest.h, which declares the testing framework.
//不要忘了引入声明测试框架的gtest.h头文件
#include "stdafx.h"
#include <limits.h>
#include "sample1.h"
#include <gtest/gtest.h>


// Step 2. Use the TEST macro to define your tests.
//第二步:应用TEST宏来定义你的测试

//TEST has two parameters: the test case name and the test name.
//TEST宏包含两个参数:一个案例【TestCaseName】名,一个测试【TestName】名

// After using the macro, you should define your test logic between a  pair of braces.  
//在应用了宏之后,你应该定义这两者之间的测试逻辑

//You can use a bunch of macros to indicate the success or failure of a test.
//你应该使用一些宏命令去指出测试是否成功

//EXPECT_TRUE and EXPECT_EQ are  examples of such macros. 
//其中EXPECT_TRUE和EXPECT_EQ就是这样的宏的例子

// For a complete list, see gtest.h.
//要查看完整的列表,可以去看看gtest.h头文件

// <TechnicalDetails>
//
// In Google Test, tests are grouped into test cases.  
//在gtest中,测试都被分组到测试案例中

// This is how we keep test code organized. 
//这就是我们有效组织测试代码的方式

//  You should put logically related tests into the same test case.
//你应该将这些逻辑上相关的测试应用到相似的测试案例中去

// The test case name and the test name should both be valid C++ identifiers.  
//测试案例名和测试名应该都是有效的C++标识符

// And you should not use underscore (_) in the names.
//并且你不应该应用强调符号(_)到命名中

// Google Test guarantees that each test you define is run exactly
// once, but it makes no guarantee on the order the tests are
// executed.  
//gtest能够保证你定义的每个测试都能准确的运行一次,但是它并不能保证这些测试执行的顺序

//Therefore, you should write your tests in such a way
// that their results don't depend on their order.
//所以你应该依输出结果不依赖自身顺序的方式来写这些测试案例

// </TechnicalDetails>


// Tests Factorial().
//测试阶乘函数Factorial().

// Tests factorial of negative numbers.
//测试传入负数的情况下函数的运行情况

TEST(FactorialTest, Negative) {
  // This test is named "Negative", and belongs to the "FactorialTest"
  // test case.
  //这个测试本身被命名为“Negative”且从属于"FactorialTest"测试案例

  EXPECT_EQ(1, Factorial(-5));
  EXPECT_EQ(1, Factorial(-1));
  EXPECT_TRUE(Factorial(-10) > 0);

  // <TechnicalDetails>
  //<技术细节>

  // EXPECT_EQ(expected, actual) is the same as
  //
  //   EXPECT_TRUE((expected) == (actual))
  //
  // except that it will print both the expected value and the actual
  // value when the assertion fails.  
  //EXPECT_EQ(expected, actual)除了在断言失败时会打印出期望值和相应的真实值外,其他的情况都和EXPECT_TRUE((expected) == (actual))相同

  //This is very helpful for debugging.
  //这对调试来说很有帮助

  //   Therefore in this case EXPECT_EQ is preferred.
  //因此这样说来EXPECT_EQ是很有用的

  // On the other hand, EXPECT_TRUE accepts any Boolean expression,and is thus more general.
  // 在另一方面,EXPECT_TRUE会接受任何的Boolean表达式,所以他的用法更普通一些

  //
  // </TechnicalDetails>
}

// Tests factorial of 0.
//使用数字0测试阶乘

TEST(FactorialTest, Zero) {
  EXPECT_EQ(1, Factorial(0));
}

// Tests factorial of positive numbers.
//使用正数测试阶乘

TEST(FactorialTest, Positive) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}


// Tests IsPrime()
//测试素数检测函数

// Tests negative input.
//测试负数输入

TEST(IsPrimeTest, Negative) {
  // This test belongs to the IsPrimeTest test case.
  //这个测试依赖于IsPrimeTest测试案例

  EXPECT_FALSE(IsPrime(-1));
  EXPECT_FALSE(IsPrime(-2));
  EXPECT_FALSE(IsPrime(INT_MIN));
}

// Tests some trivial cases.
//测试一些平常的案例

TEST(IsPrimeTest, Trivial) {
  EXPECT_FALSE(IsPrime(0));
  EXPECT_FALSE(IsPrime(1));
  EXPECT_TRUE(IsPrime(2));
  EXPECT_TRUE(IsPrime(3));
}

// Tests positive input.
//测试正数输入

TEST(IsPrimeTest, Positive) {
  EXPECT_FALSE(IsPrime(4));
  EXPECT_TRUE(IsPrime(5));
  EXPECT_FALSE(IsPrime(6));
  EXPECT_TRUE(IsPrime(23));
}

// Step 3. Call RUN_ALL_TESTS() in main().
//第三步:在main()函数中调用 RUN_ALL_TESTS()函数

// We do this by linking in src/gtest_main.cc file, which consists of
// a main() function which calls RUN_ALL_TESTS() for us.
//在src/gtest_main.cc文件中有一个调用RUN_ALL_TESTS()函数的main()函数,我们可以引入该文件来实现第三步的要求

// This runs all the tests you've defined, prints the result, 
//在这里可以运行所有你定义的测试,打印输出结果

//and returns 0 if successful, or 1 otherwise.
//并且如果成功返回0,其他情况返回1

// Did you notice that we didn't register the tests?  
//你有没有发现我们并没有注册这些测试

//The RUN_ALL_TESTS() macro magically knows about all the tests we defined.
//这个RUN_ALL_TESTS()宏函数能神奇的知道我们定义的所有测试

//Isn't this convenient?
//这样是不是很方便呢,嘿嘿

 

sample2.h

//一个用来展示如何应用Google C++测试框架的简单程序示例

#ifndef GTEST_SAMPLES_SAMPLE2_H_
#define GTEST_SAMPLES_SAMPLE2_H_

#include <string.h>


// A simple string class.
//一个简单的string字符串相关类
class MyString {
private:
	const char * c_string_;
	const MyString& operator=(const MyString& rhs);

public:

	// Clones a 0-terminated C string, allocating memory using new.
	//克隆一个0结尾的C string字符串,用new关键字分配空间
	static const char * CloneCString(const char * c_string);

	
	//
	// C'tors

	// The default c'tor constructs a NULL string.
	//为c_string_默认赋NULL值的构造函数

	MyString() : c_string_(NULL) {}

	// Constructs a MyString by cloning a 0-terminated C string.
	explicit MyString(const char * c_string) : c_string_(NULL) {
		Set(c_string);
	}

	// Copy c'tor
	MyString(const MyString& string) : c_string_(NULL) {
		Set(string.c_string_);
	}

	
	//
	// D'tor.  MyString is intended to be a final class, so the d'tor
	// doesn't need to be virtual.
	//析构函数:MyString类想定义为final类,所以析构函数没必要定义为virtual

	~MyString() { delete[] c_string_; }

	// Gets the 0-terminated C string this MyString object represents.
	//返回MyString对象的c_string_
	const char * c_string() const { return c_string_; }

	size_t Length() const {
		return c_string_ == NULL ? 0 : strlen(c_string_);
	}

	// Sets the 0-terminated C string this MyString object represents.
	//为MyString对象的c_string赋值
	void Set(const char * c_string);
};


#endif  // GTEST_SAMPLES_SAMPLE2_H_

 

sample2.cc

#include "stdafx.h"
#include "sample2.h"

#include <string.h>

// Clones a 0-terminated C string, allocating memory using new.
//克隆复制一个以0结尾的C字符串,用new关键字申请空间
const char * MyString::CloneCString(const char * c_string) {
	if (c_string == NULL) return NULL;

	const size_t len = strlen(c_string);
	char * const clone = new char[ len + 1 ];
	memcpy(clone, c_string, len + 1);

	return clone;
}

// Sets the 0-terminated C string this MyString object
// represents.
//设置以0结尾的字符串用MyString实体表示
void MyString::Set(const cha
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值