<Cpp> google gtest使用示例

本文介绍了一个使用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

[ 60%] Built target ydlidar_sdk [ 68%] Built target etlidar_test [ 76%] Built target tof_test [ 84%] Built target ydlidar_test [ 92%] Built target lidar_c_api_test [ 96%] Building CXX object test/CMakeFiles/lidar_test.dir/lidar_test.cpp.o In file included from /usr/include/gtest/gtest-message.h:57, from /usr/include/gtest/gtest-assertion-result.h:46, from /usr/include/gtest/gtest.h:64, from /home/a/ROS2教程/源码/YDLidar-SDK-master/test/lidar_test.h:4, from /home/a/ROS2教程/源码/YDLidar-SDK-master/test/lidar_test.cpp:1: /usr/include/gtest/internal/gtest-port.h:279:2: error: #error C++ versions less than C++14 are not supported. 279 | #error C++ versions less than C++14 are not supported. | ^~~~~ /usr/include/gtest/gtest-assertion-result.h: In member function ‘void testing::AssertionResult::AppendMessage(const testing::Message&)’: /usr/include/gtest/gtest-assertion-result.h:207:48: error: ‘make_unique’ is not a member of ‘std’ 207 | if (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^~~~~~~~~~~ /usr/include/gtest/gtest-assertion-result.h:207:48: note: ‘std::make_unique’ is only available from C++14 onwards /usr/include/gtest/gtest-assertion-result.h:207:73: error: expected primary-expression before ‘>’ token 207 | f (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^ /usr/include/gtest/gtest-assertion-result.h:207:75: error: expected primary-expression before ‘)’ token 207 | f (message_ == nullptr) message_ = ::std::make_unique<::std::string>(); | ^ In file included from /usr/include/gtest/gtest-printers.h:122, from /usr/include/gtest/gtest-matchers.h:49, from /usr/include/gtest/internal/gtest-death-test-internal.h:47, from /usr/include/gtest/gtest-death-test.h:43, from /usr/include/gtest/gtest.h:65: /usr/include/gtest/internal/gtest-internal.h: At global scope: /usr/include/gtest/internal/gtest-internal.h:622:58: error: wrong number of template arguments (0, should be 1) 622 | typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap; | ^ In file included from /usr/include/c++/13/string:49, from /usr/include/c++/13/bits/locale_classes.h:40, from /usr/include/c++/13/bits/ios_base.h:41, from /usr/include/c++/13/iomanip:42, from /usr/include/gtest/gtest.h:54: /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/internal/gtest-internal.h:622:59: error: template argument 3 is invalid 622 | typedef ::std::map<std::string, CodeLocation, std::less<>> RegisteredTestsMap; | ^~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘bool testing::internal::TypedTestSuitePState::AddTestName(const char*, int, const char*, const char*)’: /usr/include/gtest/internal/gtest-internal.h:599:23: error: request for member ‘insert’ in ‘((testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘int’} 599 | registered_tests_.insert( | ^~~~~~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘bool testing::internal::TypedTestSuitePState::TestExists(const std::string&) const’: /usr/include/gtest/internal/gtest-internal.h:605:30: error: request for member ‘count’ in ‘((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘const testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘const int’} 605 | return registered_tests_.count(test_name) > 0; | ^~~~~ /usr/include/gtest/internal/gtest-internal.h: In member function ‘const testing::internal::CodeLocation& testing::internal::TypedTestSuitePState::GetCodeLocation(const std::string&) const’: /usr/include/gtest/internal/gtest-internal.h:609:40: error: qualified-id in declaration before ‘it’ 609 | RegisteredTestsMap::const_iterator it = registered_tests_.find(test_name); | ^~ /usr/include/gtest/internal/gtest-internal.h:610:5: error: ‘it’ was not declared in this scope; did you mean ‘int’? 610 | GTEST_CHECK_(it != registered_tests_.end()); | ^~~~~~~~~~~~ /usr/include/gtest/internal/gtest-internal.h:610:5: error: request for member ‘end’ in ‘((const testing::internal::TypedTestSuitePState*)this)->testing::internal::TypedTestSuitePState::registered_tests_’, which is of non-class type ‘const testing::internal::TypedTestSuitePState::RegisteredTestsMap’ {aka ‘const int’} 610 | GTEST_CHECK_(it != registered_tests_.end()); | ^~~~~~~~~~~~ /usr/include/gtest/internal/gtest-internal.h:611:12: error: ‘it’ was not declared in this scope; did you mean ‘int’? 611 | return it->second; | ^~ | int /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:725:75: error: wrong number of template arguments (0, should be 1) 725 | Matcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> { | ^ /usr/include/c++/13/bits/stl_function.h:373:12: note: provided for ‘template<class _Tp> struct std::equal_to’ 373 | struct equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~ /usr/include/gtest/gtest-matchers.h:725:76: error: template argument 3 is invalid 725 | Matcher : public ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::EqMatcher<Rhs>::EqMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:728:58: error: wrong number of template arguments (0, should be 1) 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:373:12: note: provided for ‘template<class _Tp> struct std::equal_to’ 373 | struct equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~ /usr/include/gtest/gtest-matchers.h:728:59: error: template argument 3 is invalid 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:728:61: error: expected ‘{’ before ‘(’ token 728 | : ComparisonBase<EqMatcher<Rhs>, Rhs, std::equal_to<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:734:67: error: wrong number of template arguments (0, should be 1) 734 | : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> { | ^ /usr/include/c++/13/bits/stl_function.h:383:12: note: provided for ‘template<class _Tp> struct std::not_equal_to’ 383 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:734:68: error: template argument 3 is invalid 734 | : public ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::NeMatcher<Rhs>::NeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:737:62: error: wrong number of template arguments (0, should be 1) 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:383:12: note: provided for ‘template<class _Tp> struct std::not_equal_to’ 383 | struct not_equal_to : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:737:63: error: template argument 3 is invalid 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:737:65: error: expected ‘{’ before ‘(’ token 737 | : ComparisonBase<NeMatcher<Rhs>, Rhs, std::not_equal_to<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:742:71: error: wrong number of template arguments (0, should be 1) 742 | s LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> { | ^ /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/gtest-matchers.h:742:72: error: template argument 3 is invalid 742 | s LtMatcher : public ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::LtMatcher<Rhs>::LtMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:745:54: error: wrong number of template arguments (0, should be 1) 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:403:12: note: provided for ‘template<class _Tp> struct std::less’ 403 | struct less : public binary_function<_Tp, _Tp, bool> | ^~~~ /usr/include/gtest/gtest-matchers.h:745:55: error: template argument 3 is invalid 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:745:57: error: expected ‘{’ before ‘(’ token 745 | : ComparisonBase<LtMatcher<Rhs>, Rhs, std::less<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:750:74: error: wrong number of template arguments (0, should be 1) 750 | tMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> { | ^ /usr/include/c++/13/bits/stl_function.h:393:12: note: provided for ‘template<class _Tp> struct std::greater’ 393 | struct greater : public binary_function<_Tp, _Tp, bool> | ^~~~~~~ /usr/include/gtest/gtest-matchers.h:750:75: error: template argument 3 is invalid 750 | tMatcher : public ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::GtMatcher<Rhs>::GtMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:753:57: error: wrong number of template arguments (0, should be 1) 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:393:12: note: provided for ‘template<class _Tp> struct std::greater’ 393 | struct greater : public binary_function<_Tp, _Tp, bool> | ^~~~~~~ /usr/include/gtest/gtest-matchers.h:753:58: error: template argument 3 is invalid 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:753:60: error: expected ‘{’ before ‘(’ token 753 | : ComparisonBase<GtMatcher<Rhs>, Rhs, std::greater<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:759:65: error: wrong number of template arguments (0, should be 1) 759 | : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> { | ^ /usr/include/c++/13/bits/stl_function.h:423:12: note: provided for ‘template<class _Tp> struct std::less_equal’ 423 | struct less_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:759:66: error: template argument 3 is invalid 759 | : public ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::LeMatcher<Rhs>::LeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:762:60: error: wrong number of template arguments (0, should be 1) 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:423:12: note: provided for ‘template<class _Tp> struct std::less_equal’ 423 | struct less_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:762:61: error: template argument 3 is invalid 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:762:63: error: expected ‘{’ before ‘(’ token 762 | : ComparisonBase<LeMatcher<Rhs>, Rhs, std::less_equal<>>(rhs) {} | ^ /usr/include/gtest/gtest-matchers.h: At global scope: /usr/include/gtest/gtest-matchers.h:768:68: error: wrong number of template arguments (0, should be 1) 768 | : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> { | ^ /usr/include/c++/13/bits/stl_function.h:413:12: note: provided for ‘template<class _Tp> struct std::greater_equal’ 413 | struct greater_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:768:69: error: template argument 3 is invalid 768 | : public ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>> { | ^~ /usr/include/gtest/gtest-matchers.h: In constructor ‘testing::internal::GeMatcher<Rhs>::GeMatcher(const Rhs&)’: /usr/include/gtest/gtest-matchers.h:771:63: error: wrong number of template arguments (0, should be 1) 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^ /usr/include/c++/13/bits/stl_function.h:413:12: note: provided for ‘template<class _Tp> struct std::greater_equal’ 413 | struct greater_equal : public binary_function<_Tp, _Tp, bool> | ^~~~~~~~~~~~~ /usr/include/gtest/gtest-matchers.h:771:64: error: template argument 3 is invalid 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^~ /usr/include/gtest/gtest-matchers.h:771:66: error: expected ‘{’ before ‘(’ token 771 | : ComparisonBase<GeMatcher<Rhs>, Rhs, std::greater_equal<>>(rhs) {} | ^ /usr/include/gtest/gtest.h: At global scope: /usr/include/gtest/gtest.h:302:30: error: ‘std::enable_if_t’ has not been declared 302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | ^~~~~~~~~~~ /usr/include/gtest/gtest.h:302:41: error: expected ‘>’ before ‘<’ token 302 | template <typename T, std::enable_if_t<std::is_convertible<T, int64_t>::value, | ^ make[2]: *** [test/CMakeFiles/lidar_test.dir/build.make:76:test/CMakeFiles/lidar_test.dir/lidar_test.cpp.o] 错误 1 make[1]: *** [CMakeFiles/Makefile2:449:test/CMakeFiles/lidar_test.dir/all] 错误 2 make: *** [Makefile:166:all] 错误 2
05-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值