1、sample1.h
#ifndef SAMPLE_1_H
#define SAMPLE_1_H
int Factorial(int n);
bool IsPrime(int n);
#endif
2、sample1.cpp
#include"sample1.h"
int Factorial(int n){
int result = 1;
for(int i = 1;i<=n;i++){
result *= i;
}
return result;
}
bool IsPrime(int n){
if(n<=1)return false;
if(n%2==0)return n==2;
for(int i=3;;i+=2){
if(i>n/i)break;
if(n%i==0)return false;
}
return true;
}
3、sample1_test.cpp
/*
一、平常怎么做C++开发
(1)整理需求文档
(2)构思具体方案(检车哪些模块,增加那些功能)
(3)功能实现
(4)编译代码,解决语法错误
(5)测试或者cs联调 修复bug
(6)测试人员测试修复 bug
(7)放在线上,出现bug 解决bug
不确定 新加模块 或者功能辅助函数 别人能否直接使用
单元测试 程序所有逻辑都跑到
(1)以函数为单位
(2)类进行测试
人为规定
二 gtest事件机制
(1)testcase
(2)testsuit
(3)global 事件
*/
#include<gtest/gtest.h>
#include<limits.h>
#include"sample1.h"
TEST(FactorialTest,Negative){
EXPECT_EQ(1,Factorial(-5));
EXPECT_EQ(1,Factorial(-1));
EXPECT_GT(Factorial(-10),0);
}
TEST(FactorialTest,Zero){
EXPECT_EQ(1,Factorial(0));
}
TEST(FactorialTest,Positive){
EXPECT_EQ(1,Factorial(1));
EXPECT_EQ(2,Factorial(2));
EXPECT_EQ(6,Factorial(3));
EXPECT_EQ(40320,Factorial(8));
}
//对多个参数化测试
//1添加类 继承自 public::testing::TestWithParam<int>
class IsPrimeParamTest:public::testing::TestWithParam<int>{};
//2获取参数并且测试
TEST_P(IsPrimeParamTest,ExpectTrueReturn)
{
int n = GetParam();
EXPECT_TRUE(IsPrime(n));
}
//3告诉gtest你的参数
INSTANTIATE_TEST_CASE_P(TrueReturn,IsPrimeParamTest,testing::Values(3,5,11,23));
//第三个参数还可以是
//Range(begin,end[,step]) testing::Range(3,10,2)
//ValuesInn(容器和c数组) ValuesIn(begin,end) testing::ValuesIn()
//Bool()分别取 false和true testing::Bool();
//combine(g1,g2,...,gn) 排列组合
//Step 3 Call RUN_ALL_TESTS() in main();
//如果没有main函数也可以在编译的时候如下:
//g++ sample1.cpp sample1_test.cpp -o sample1 -lgtest -lpthread -std=c++11 -lgtest_main
/*int main(int argc,char **argv)
{
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}*/
4、test.cpp
#include"gtest/gtest.h"
/**************事件global************/
//全局事件:所有案例执行前后
//可用于组合类行为测试
class GlobalTestSample:public testing::Environment
{
protected:
virtual void SetUp(){
//准备工作
}
virtual void TearDown(){
//清理工作
}
int a;//全局有效
};
int _tmain(int argc,char **argv)
{
//注册全局事件 GlobalTestSample
testing::AddGlobalTestEnvironment(new GlobalTestSample);
testing::InitGoogleTest(&argc,argv);
return RUN_ALL_TESTS();
}
/***************************事件suite********************/
//TestSuite事件:在一批案例中,第一个执行前到最后一个执行后
//一般用于类行为测试 或者 其他有联系的多个方法的测试
//多个函数的组合会产生不一样的效果,通过一个全局变量来保存整个类
class SuiteTestSample:public testing::Test
{
protected:
static void SetUpTestCase(){
shared_ = new T;
}
static void TearDownTestCase(){
delete shared_;
shared_ = NULL;
}
static T* shared_;
};
//执行前会调用 SetUpTestCase
TEST_F(SuiteTestSample,f1)
{
//这里可以引用 shared_
}
TEST_F(SuiteTestSample,f2)
{
//这里可以引用 shared_
}
TEST_F(SuiteTestSample,f3)
{
//这里可以引用 shared_
}
//执行后会调用 TearDownTestCase
/***********************事件case*********************/
//TestCase 事件:每个案例执行前后
//多次对类初始使用测试
//string 有很多方法
//一个类,多个行为,先后顺序相关
class CaseTestSample : public testing::Test
{
protected:
virtual void SetUp(){
temp_.Init();
}
virtual void TearDown(){
temp_.Finalize();
}
T temp_;
};
TEST_F(CaseTestSample,f1)
{
//在这里可以执行使用T以及它的内部方法
}