Skip to content

Instantly share code, notes, and snippets.

@mkamilov
Created November 13, 2017 15:39
Show Gist options
  • Save mkamilov/b4e67aefb1b930ee491c5290bfb7bca2 to your computer and use it in GitHub Desktop.
Save mkamilov/b4e67aefb1b930ee491c5290bfb7bca2 to your computer and use it in GitHub Desktop.
gtest sample
cmake_minimum_required(VERSION 2.6)
add_definitions( -DGTEST_HAS_PTHREAD=1 )
set(GMOCK_ROOT /home/miradham/cpp/gtest/googletest-master/googlemock/)
set(GTEST_ROOT /home/miradham/cpp/gtest/googletest-master/googlemock/gtest/)
# Locate GTest
find_package(GTest REQUIRED)
find_package(GMock REQUIRED)
#add_subdirectory(googletest-master)
include_directories(${GTEST_INCLUDE_DIRS})
include_directories(${GMOCK_INCLUDE_DIRS})
# Link runTests with what we want to test and the GTest and pthread library
add_executable(runTests test.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} ${GMOCK_BOTH_LIBRARIES} pthread)
// tests.cpp
#include "whattotest.cpp"
#include<gmock/gmock.h>
#include<gtest/gtest.h>
using ::testing::AtLeast; // #1
class MockCat: public Cat
{
public:
MOCK_METHOD0(show_tired_cat,void());
MOCK_METHOD0(show_energized_cat,void());
MOCK_METHOD0(show_cat, void());
};
TEST (catTest, show_cat){
//This is of course not working
/*Cat realCat;
realCat.energy = 3;
realCat.show_cat();*/
MockCat fakeCat;
cout << "made fakecat " << endl;
EXPECT_CALL(fakeCat,show_tired_cat()).Times(AtLeast(1));
cout << "expecting..." << endl;
fakeCat.energy = 3;
fakeCat.show_cat();
//EXPECT_CALL(fakeCat,show_energized_cat());
}
int main(int argc, char *argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
// whattotest.cpp
#include <iostream>
using namespace std;
class Cat{
public:
int energy;
virtual void show_cat()
{
if(energy < 10)
{
show_tired_cat();
}
else
{
show_energized_cat();
}
}
virtual void show_tired_cat(){ cout << "tired" << endl;}
virtual void show_energized_cat(){ cout << "energized" << endl; }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment