Installing GTest on Ubuntu and OSX, A Test Framework for C++

[Last update at: 201-02-18]

Nowadays, there is no talk about development without mentioning TESTS, if you develop with languages ​​like Ruby, Python, Scala ... you should already be familiar with the subject. But, what about making a Coding Dojo in C++ .

Google's framework for writing C++ tests on a variety of platforms (Linux, Mac OS X, Windows, Cygwin, Windows CE, and Symbian). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, value- and type-parameterized tests, various options for running the tests, and XML test report generation.
UBUNTU

Step 1 - It's simple, just install (in Terminal):

apt-get install libgtest-dev

Now a little detail! By reason of a small rule definition of C++ language "Definition of Single Classes" the framework is not compiled at installation time, so you must compile it.

Step 2 - Find and Build (in Terminal):

# Go to your home
cd ~
# Compiling the lib
g++ -I /usr/include/gtest -I /usr/src/gtest/ -c /usr/src/gtest/src/gtest-all.cc
# Archiving the generated file to a static library
ar -rv libgtest.a gtest-all.o
# Move to a place that the GCC Linker can find
mv libgtest.a /usr/local/lib/

OSX

Step 1 - Download the latest version

Enter in the oficial framework site GTest Code, download the latest version, generally it comes in .zip and .tar.gz, decompress it in wherever folder you want.

Step 2 - Find and Build (in Terminal):

# Inside from decompress folder, create a new folder
mkdir mybuild
# change to the new folder
cd mybuild
# run the cmake command
cmake -G"Unix Makefiles" ..
# run the make
make
# and then
make install
# move to a place that the GCC Linker can find
mv ../libgtest.a /usr/local/lib/

Ok, now the framework is ready for use, see this example:

Create the code of Person call: Person.h


Create the code of implementation of Person class: Person.cpp


Create the code of the test: person-test.cpp


To run this test is simple, there are only two considerations to be made: 
1 - If the linker can not find the GTeste, add "/usr/lib/local/libgtest.a" in command
2 - Always add the "-lpthread" in command


# makefile
all:
 gcc -c ./*.cpp
 g++ ./person-test.cpp Person.o /usr/lib/local/libgtest.a -o test.bin -fPIC -O2 -g -Wall -lpthread

clean:
 rm *.o
 rm ./person-test.bin

The output will be:

 To go deeper, visit this link and save: Documentation
Installing GTest on Ubuntu and OSX, A Test Framework for C++ Installing GTest on Ubuntu and OSX, A Test Framework for C++ Reviewed by AJ Alves on segunda-feira, outubro 15, 2012 Rating: 5

Nenhum comentário:

Tecnologia do Blogger.