<Cpp> google gtest使用示例

环境搭建参考我上条博客

#include <iostream>
#include <string>
#include "gtest/gtest.h"

//#ifdef _DEBUG  
//#pragma comment(lib, "gtestd.lib")  
//#pragma comment(lib, "gtest_maind.lib")  
//#else  
//#pragma comment(lib, "gtest.lib")  
//#pragma comment(lib, "gtest_main.lib")   
//#endif

////////////////////////////////////////////////////
int Power(int value)
{
    return value * value;
}

const char *GetBuffer(std::string &str)
{
    return str.c_str();
}

std::string g_str("global string");
std::string g_upstr("GLOBAL sTring");

void Foo()
{
    int *pInt = NULL;
    fprintf(stderr, "Foo death at: %d", __LINE__);
    *pInt = 42;
    _exit(1);
}


TEST(ExpectTest, Expect)
{
    // expect equal
    EXPECT_EQ(4, Power(2)); 
    // expect not equal
    EXPECT_NE(5, Power(-3));
    // expect greater than
    EXPECT_GT(17, Power(4));
    // expect greater or equal
    EXPECT_GE(16, Power(4));
    // expect less or equal
    EXPECT_LE(15, Power(4));
    // expect less
    EXPECT_LT(14, Power(4));


    // expect boolean true
    EXPECT_TRUE(Power(-5) > 0);
    // expect boolean false
    EXPECT_FALSE(3 == Power(3));

    // expect string equal
    EXPECT_STREQ(g_str.c_str(), GetBuffer(g_str));
    // expect string not equal
    EXPECT_STRNE(NULL, GetBuffer(g_str));
    // expect string case equal
    EXPECT_STRCASEEQ(g_str.c_str(), GetBuffer(g_str));
    // expect string case not equal
    EXPECT_STRCASEEQ(g_str.c_str(), GetBuffer(g_upstr));

}
// 系统优化运行test_case_name中包含DeathTest的测试实例
TEST(FooDeathTest, Demo)
{
    EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");
    /* 
     "    Result: died but not with expected exit code:\n"
     "            " << ExitSummary(status()) << "\n"
     "Actual msg:\n" << FormatDeathTestOutput(error_message);
    */
    //EXPECT_DEATH(Foo(), "Foo death at: .*");
}

////////////////////////////////////////////////////
class FixtureTest : public testing::Test 
{
protected:
    FixtureTest() : m_a(0), m_b(1) {}

    void SetUp() {
        m_a++;  
        m_b--;
        m_strStatus = "setup";
    }

    int m_a;
    int m_b;
    std::string m_strStatus;
};

TEST_F(FixtureTest, TestSetup)
{
    EXPECT_EQ(m_a, 1);
    EXPECT_FALSE(m_a == m_b);
    EXPECT_STREQ("setup", m_strStatus.c_str());
}
///////////////////////////////////////////////

int main(int argc, char* argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    RUN_ALL_TESTS();

    getchar();
    return 0;
}

我的GitHub: https://github.com/Lulixue/GTestTest.git

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值