See https://github.com/AlexandreBrown/GTestSetupRemoteDownload (recommended) to setup GTest without having to manually clone the repository of GTest.
- Inside the project folder (eg:
GTestSetup/GTestSetup
), create a directory called "lib".
GTestSetup/GTestSetup/lib
-
Inside this newly created directory (lib), put the GTest repository https://github.com/google/googletest.
- Simply clone the repo using
git clone https://github.com/google/googletest
or download the zip by visiting the repo page. - Ex: from
GTestSetup/GTestSetup/lib
rungit clone https://github.com/google/googletest
- Simply clone the repo using
-
Create build directory from the root of the nested GTestSetup folder (eg: GTestSetup/GTestSetup/build).
mkdir build
cd build
- Try to cmake and make to ensure your project is well setup
- In a terminal at the project build location, do the following commands:
cmake ..
make
- If no error occured then everything should be setup correctly.
-
Inside the project folder, create a directory called "lib".
-
Inside this newly created directory (lib), put the googletest content form the official repository https://github.com/google/googletest.
- Simply clone the repo using
git clone https://github.com/google/googletest
or download the zip by visiting the repo page.
- Simply clone the repo using
-
Inside the
CMakeLists.Txt
- that is located insise your Clion project - you have to put:
add_subdirectory(lib/googletest)
include_directories(lib/googletest/googletest/include)
include_directories(lib/googletest/googlemock/include)
target_link_libraries(GTestSetup_test gtest gtest_main pthread)
-
Reload your CMakeLists (IDE should propose you to reload it)
-
Create build directory from the root of the nested GTestSetup folder (eg:
GTestSetup/GTestSetup/build
).
mkdir build
cd build
- Try to cmake and make to ensure your project is well setup
- In a terminal at the project build location, do the following commands:
cmake ..
make
- If no error occured then everything should be setup correctly.
- Start writing unit tests using GTest and make sure to add the test files to the test executable in your
CMakeLists.txt
- See
MyClassTest.cpp
for a test class sample - See the
CMakeLists
for more details on how to define your test executable
Note: Some other config is needed like setting executable for your app etc, take a look at the CMakeLists for more details.
- To run the all the tests inside
MyClassTest
you can use the run button at the test fixture or test name- You can also right click a folder with tests and run all the tests or you can run individual tests using the green run button
- You can also run tests from the bottom pane that opens up after runnign tests initially
- Using the IDE to run tests might provide better feedback than other alternatives (subjective)
- go into
/build
and runmake && make test
, this will recompile your poject and run all the tests using CTest and give you a command-line print of the result.
- Delete
/build
folder and recreate/populate it usingmkdir build
+cmake ..
+make
- CLion : File->Invalidate cache & restart
- Try to right click your test class from the file explorer (inside CLion) and click "Run all in YourClassTest.cpp"
- If it doesn't work try deleting the
/build
folder and recreating it and repopulating it using steps describe in Global fixes to try - If it doesn't work try running your tests using
make test
from/build
folder
- Try deleting
/build
and recreating/populating it using steps describe in Global fixes to try
- Try re-running the test, maybe it was ran with outdated code or try
make && make test
from/build
- Try File->Invalidate cache & restart