I am using Linux machine. I have download the googletest package from here
However, there is no installation guide or other blogs related on how to set it up properly The README file is no good that I can't understand what it is talking about?
Can anyone provide a simple example on how to test a simple function inside a .cc file with that gtest package?
Here's what I did and you can adjust as necessary. I downloaded gtest-1.6.0.zip (from the releases page) on my Linux box into ~/Downloads which typed out fully is /home/me/Downloads/
Unzip the contents of gtest-1.6.0.zip into ~/Downloads/gtest-1.6.0/
Build the gtest library because it's something you need to "include" in your test executable. Compile the object file gtest-all.o:
Then build the library archive libgtest.a:
Now you can create your test.cc file in ~/Downloads. Here is an example test file that I used to make sure it compiles.
To compile your own test and run it:
Then to execute it:
And it should run fine. Modify as necessary from there.
An addendum to James C's answer:
Note that building the library using
gtest-1.6.0/src/gtest-all.cc
will require you to provide with a main method yourself. If you want to avoid that altogether and use the default implementation of the main method provided by Googletest, build your library includinggtest_main.cc
.That is:
Also, keep in mind that implementing your own main method is not the recommended way of defining the
SetUp
andTearDown
behaviours; you should be using fixtures instead. Check the Googletest documentation on the topic.These instructions get the testing framework working for the Debug configuration.
Get Google C++ Testing Framework
1.Download the latest gtest framework
2.Unzip to
C:\gtest
Build the Framework Libraries
1.Open
C:\gtest\msvc\gtest.sln
in Visual Studio2.Set Configuration to "Debug"
3.Build Solution
Create and Configure Your Test Project
1.Create a new solution and choose the template Visual C++ > Win32 > Win32 Console Application
2.Right click the newly created project and choose Properties
3.Change Configuration to Debug.
4.Configuration Properties > C/C++ > General > Additional Include Directories: Add
C:\gtest\include
5.Configuration Properties > C/C++ > Code Generation > Runtime Library: If your code links to a runtime DLL, choose Multi-threaded Debug DLL (/MDd). If not, choose Multi-threaded Debug (/MTd).
6.Configuration Properties > Linker > General > Additional Library Directories: Add
C:\gtest\msvc\gtest\Debug
7.Configuration Properties > Linker > Input > Additional Dependencies: Add
gtestd.lib
Verifying Everything Works
1.Open the cpp in your Test Project containing the
main()
function.2.Paste the following code:
1.Debug > Start Debugging
If this works you should see the console window open with your test results.
take a look on the project home:
regarding available documentation.
If you have more specific questions you could also ask in the Google Group: