gtest

简单的介绍

TEST(测试套,测试名) 测试套随意起名即可
TEST_p(测试套,)

说明

本地自己配置gtest很麻烦...直接用visual studio 2017+版本,直接有gtest
教程

ubuntu上配置gtest更简单,教材

hello world

test.cpp

#include "pch.h"

TEST(TestCaseName, TestName) {
  EXPECT_EQ(1, 1);
  EXPECT_EQ(3, add(1, 2));
  EXPECT_EQ(4, add(2, 2));
  EXPECT_TRUE(true);
}

pch.h

//
// pch.h
//

#pragma once

#include "gtest/gtest.h"
int add(int a, int b);

pch.cpp

#include "pch.h"
int add(int a, int b)
{
	return a + b;
}

完整覆盖大部分特性的大例子---gtest

#include "pch.h"  // 这里面实现一下 需要测试的函数

#include <gtest/gtest.h>
#include <iostream>
#include <tuple>
using namespace std;
using ll = long long int;
ostream& operator << (ostream& os, tuple<ll, ll, ll> const& rhs)
{
    return os << "[" << get<0>(rhs) << "," << get<1>(rhs) << "," << get<2>(rhs) << "]";
}
struct IsPrimeParamTest : testing::TestWithParam<tuple<ll,ll,ll>>
{
    string cmp;
    IsPrimeParamTest()
    {
        cmp = "123123";
    }
    static void SetUpTestCase()
    {
        
        //cout << "\n\n>>>测试套开始" << endl;
    }
    static void TearDownTestCase()
    {
        //cout <<">>>测试套结束\n\n" << endl;
    }
    virtual void SetUp()
    {
        cout << "debug +  " << GetParam() << endl;
        //cout << "\n>>>测试用例开始" << endl;
    }
    virtual void TearDown()
    {
        //cout << ">>>测试用例结束\n" << endl;
    }
};
TEST_P(IsPrimeParamTest, HandleTrueReturn)
{
    auto tmp = get<0>(GetParam());
    EXPECT_TRUE(tmp);
    EXPECT_EQ("123123",cmp) << "show how to add infomation = " << GetParam() << endl;
}
TEST(IsPrimeParamTest,myHandleTrueReturn)
{
    EXPECT_FALSE(0 == 1);
}
vector<tuple<ll, ll, ll>> v = { {7,8,9},{10,11,12} };
vector < tuple < ll, ll, ll >>::iterator myBegin = v.begin(), myEnd = v.end();
INSTANTIATE_TEST_CASE_P(TrueReturn1, IsPrimeParamTest, testing::Values(make_tuple(1, 2, 3), make_tuple(4,5,6)));
INSTANTIATE_TEST_CASE_P(TrueReturn2, IsPrimeParamTest, testing::ValuesIn(v.begin(),v.end()));
INSTANTIATE_TEST_CASE_P(TrueReturn3, IsPrimeParamTest, testing::ValuesIn(v));
INSTANTIATE_TEST_CASE_P(TrueRetuen4, IsPrimeParamTest, testing::Combine(testing::Values(10,11), testing::Values(10), testing::Values(12, 15)));
void Foo()
{
    int* p = 0;
    *p = 42;
}
TEST(FooDeathTest, Demo)
{
    EXPECT_DEATH(Foo(),"");
}
struct GlobalSetting : public testing::Environment
{
    virtual void SetUp()
    {
        cout << "\n>>>begin testing" << endl;
    }
    virtual void TearDown()
    {
        cout << ">>>end testing\n" << endl;
    }
};
int main(int argc,char * argv[])
{
    testing::AddGlobalTestEnvironment(new GlobalSetting());
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

gmock 示范



命令行参数



参数

INSTANTIATE_TEST_CASE_P(TrueReturn, IsPrimeParamTest, testing::Values(make_tuple(1, 2, 3), make_tuple(4,5,6)));
第三个参数:

死亡测试




posted @ 2022-01-22 15:43  XDU18清欢  阅读(88)  评论(0)    收藏  举报