using google test

5,893 views
Skip to first unread message

i.dell'oro

unread,
May 3, 2011, 10:12:57 AM5/3/11
to Google C++ Testing Framework
Hi Everybody,
I'm trying to use GTest and I tryed to search for some guide on the
web but i can't solve my problem:
i don't understand how-to compile and link my example program and the
program test:

one.cpp (in folder: /home/zen/workspace/testing/gtest-1.6.0):

#include <iostream>

int double(int x){
return 2*x;

}



int main(int argc, char** argv){
void testing::InitGoogleTest(&argc, argv);
int x=2;
int n=double(x);
std::cout<< n << " double of " << x << std::endl;
return RUN_ALL_TESTS();

}

and finally try.cpp ( in folder /home/zen/workspace/testing/
gtest-1.6.0/test ):

#include <gtest/gtest.h>
#include "one.cpp"
#include <iostream>

TEST(DoubleTest, positivo){
EXPECT_EQ(2,double(1));
EXPECT_EQ(4,double(2));
EXPECT_EQ(0,double(0));

}

and I'm not sure where to I shuold write the main()...

please help me!

ps- my os is ubuntu 11.04 with compliler g++

Vlad Losev

unread,
May 3, 2011, 11:46:29 AM5/3/11
to i.dell'oro, Google C++ Testing Framework
The project's README file has detailed build instructions. Check it out.
- Vlad

i.dell'oro

unread,
May 3, 2011, 11:53:18 AM5/3/11
to Google C++ Testing Framework
Hi, i've done what you told me, and i'm one step closer to the end but
when i'm going to link the files (i've renamed one.cpp -> uno.cpp ,
try.cpp -> prova.cpp and the function double -> doppio like a simple
translation in italian)...i'll show you the files modified and the
shell commands i used:
prova.cpp

#include <gtest/gtest.h>
#include "uno.h"
#include <iostream>
TEST(DoubleTest, positivo){
EXPECT_EQ(2,doppio(1));
EXPECT_EQ(4,doppio(2));
EXPECT_EQ(0,doppio(0));

}

int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

uno.cpp:

#include <iostream>

int doppio(int x){
return 2*x;

}

and the command lines:
zen@ubuntu:~/workspace/testing$ g++ -I gtest-1.6.0/include/ -c
gtest-1.6.0/prova.cpp
zen@ubuntu:~/workspace/testing$ g++ -I gtest-1.6.0/ -c gtest-1.6.0/
uno.cpp
zen@ubuntu:~/workspace/testing$ g++ -I gtest-1.6.0/ uno.o prova.o -o
test
prova.o: In function `doppio(int)':
prova.cpp:(.text+0x0): multiple definition of `doppio(int)'
uno.o:uno.cpp:(.text+0x0): first defined here
/usr/lib/i386-linux-gnu/gcc/i686-linux-gnu/4.5.2/../../../crt1.o: In
function `_start':
(.text+0x18): undefined reference to `main'
prova.o: In function `DoubleTest_positivo_Test::TestBody()':
prova.cpp:(.text+0x9b): undefined reference to
`testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type,
char const*, int, char const*)'
prova.cpp:(.text+0xad): undefined reference to
`testing::internal::AssertHelper::operator=(testing::Message const&)
const'
prova.cpp:(.text+0xb8): undefined reference to
`testing::internal::AssertHelper::~AssertHelper()'
prova.cpp:(.text+0x15c): undefined reference to
`testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type,
char const*, int, char const*)'
prova.cpp:(.text+0x16e): undefined reference to
`testing::internal::AssertHelper::operator=(testing::Message const&)
const'
prova.cpp:(.text+0x179): undefined reference to
`testing::internal::AssertHelper::~AssertHelper()'
prova.cpp:(.text+0x225): undefined reference to
`testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type,
char const*, int, char const*)'
prova.cpp:(.text+0x237): undefined reference to
`testing::internal::AssertHelper::operator=(testing::Message const&)
const'
prova.cpp:(.text+0x242): undefined reference to
`testing::internal::AssertHelper::~AssertHelper()'
prova.cpp:(.text+0x272): undefined reference to
`testing::internal::AssertHelper::~AssertHelper()'
prova.cpp:(.text+0x2af): undefined reference to
`testing::internal::AssertHelper::~AssertHelper()'
prova.cpp:(.text+0x2ec): undefined reference to
`testing::internal::AssertHelper::~AssertHelper()'
prova.o: In function `__static_initialization_and_destruction_0(int,
int)':
prova.cpp:(.text+0x37c): undefined reference to
`testing::internal::GetTestTypeId()'
prova.cpp:(.text+0x3b8): undefined reference to
`testing::internal::MakeAndRegisterTestInfo(char const*, char const*,
char const*, char const*, void const*, void (*)(), void (*)(),
testing::internal::TestFactoryBase*)'
prova.o: In function
`DoubleTest_positivo_Test::DoubleTest_positivo_Test()':
prova.cpp:
(.text._ZN24DoubleTest_positivo_TestC2Ev[_ZN24DoubleTest_positivo_TestC5Ev]
+0xd): undefined reference to `testing::Test::Test()'
prova.o: In function
`testing::internal::scoped_ptr<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >
>::reset(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >*)':
prova.cpp:
(.text._ZN7testing8internal10scoped_ptrISsE5resetEPSs[testing::internal::scoped_ptr<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >
>::reset(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >*)]+0x19): undefined reference to
`testing::internal::IsTrue(bool)'
prova.o: In function
`testing::internal::scoped_ptr<std::basic_stringstream<char,
std::char_traits<char>, std::allocator<char> >
>::reset(std::basic_stringstream<char, std::char_traits<char>,
std::allocator<char> >*)':
prova.cpp:
(.text._ZN7testing8internal10scoped_ptrISt18basic_stringstreamIcSt11char_traitsIcESaIcEEE5resetEPS6_[testing::internal::scoped_ptr<std::basic_stringstream<char,
std::char_traits<char>, std::allocator<char> >
>::reset(std::basic_stringstream<char, std::char_traits<char>,
std::allocator<char> >*)]+0x18): undefined reference to
`testing::internal::IsTrue(bool)'
prova.o: In function `testing::AssertionResult
testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int
const&, int const&)':
prova.cpp:
(.text._ZN7testing8internal11CmpHelperEQIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_[testing::AssertionResult
testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int
const&, int const&)]+0x1f): undefined reference to
`testing::AssertionSuccess()'
prova.cpp:
(.text._ZN7testing8internal11CmpHelperEQIiiEENS_15AssertionResultEPKcS4_RKT_RKT0_[testing::AssertionResult
testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int
const&, int const&)]+0x8d): undefined reference to
`testing::internal::EqFailure(char const*, char const*,
testing::internal::String const&, testing::internal::String const&,
bool)'
prova.o:(.rodata._ZTV24DoubleTest_positivo_Test[vtable for
DoubleTest_positivo_Test]+0x10): undefined reference to
`testing::Test::SetUp()'
prova.o:(.rodata._ZTV24DoubleTest_positivo_Test[vtable for
DoubleTest_positivo_Test]+0x14): undefined reference to
`testing::Test::TearDown()'
prova.o: In function
`DoubleTest_positivo_Test::~DoubleTest_positivo_Test()':
prova.cpp:
(.text._ZN24DoubleTest_positivo_TestD2Ev[_ZN24DoubleTest_positivo_TestD5Ev]
+0x16): undefined reference to `testing::Test::~Test()'
prova.o:(.rodata._ZTI24DoubleTest_positivo_Test[typeinfo for
DoubleTest_positivo_Test]+0x8): undefined reference to `typeinfo for
testing::Test'
collect2: ld returned 1 exit status


i'm sorry, i've tryed understanding the README file but the examples
are just pieces of program and testing code and not easy for who is
new on c++...

Keith Ray

unread,
May 3, 2011, 12:01:45 PM5/3/11
to i.dell'oro, Google C++ Testing Framework
Try building one of the examples provided by gTest. Does that compile, link, and run?

Try modifying that example... 

Also Google is your friend: search for "g++ undefined reference to "typeinfo"

And "man". Try "man g++" on the command line.

Lots of information out there. Try searching for an introduction to C++ programming on Linux.
--
C. Keith Ray

Coach, Trainer, and Developer at Industrial logic, Inc.
http://industriallogic.com  "Amplify Your Agility"
Coaching and Live- and Web-based Training

Vlad Losev

unread,
May 3, 2011, 12:04:27 PM5/3/11
to i.dell'oro, Google C++ Testing Framework
You are not linking you tests with gtest the library. Try building the makefile-based sample in the make/ directory. You can then modify that makefile to build your files.

On Tue, May 3, 2011 at 8:53 AM, i.dell'oro <i.del...@gmail.com> wrote:

i.dell'oro

unread,
May 4, 2011, 5:54:28 AM5/4/11
to Google C++ Testing Framework
Still have problem with gtest, searching on the web i can't find a
step-by-step guide of gtest, and not understanding how it works (takes
test from the test/ folder? takes the test from the src/ folder? what
is libgtest.a ?)...
i think google test is not builded for beginner users like me and
doesn't help to test simple c++ programs...
thank's for the help
happy testing
> > (.text._ZN7testing8internal10scoped_ptrISsE5resetEPSs[testing::internal::sc oped_ptr<std::basic_string<char,
> > std::char_traits<char>, std::allocator<char> >
> > >::reset(std::basic_string<char, std::char_traits<char>,
> > std::allocator<char> >*)]+0x19): undefined reference to
> > `testing::internal::IsTrue(bool)'
> > prova.o: In function
> > `testing::internal::scoped_ptr<std::basic_stringstream<char,
> > std::char_traits<char>, std::allocator<char> >
> > >::reset(std::basic_stringstream<char, std::char_traits<char>,
> > std::allocator<char> >*)':
> > prova.cpp:
>
> > (.text._ZN7testing8internal10scoped_ptrISt18basic_stringstreamIcSt11char_tr aitsIcESaIcEEE5resetEPS6_[testing::internal::scoped_ptr<std::basic_stringst ream<char,
> > std::char_traits<char>, std::allocator<char> >
> > >::reset(std::basic_stringstream<char, std::char_traits<char>,
> > std::allocator<char> >*)]+0x18): undefined reference to
> > `testing::internal::IsTrue(bool)'
> > prova.o: In function `testing::AssertionResult
> > testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int
> > const&, int const&)':
> > prova.cpp:
>
> > (.text._ZN7testing8internal11CmpHelperEQIiiEENS_15AssertionResultEPKcS4_RKT _RKT0_[testing::AssertionResult
> > testing::internal::CmpHelperEQ<int, int>(char const*, char const*, int
> > const&, int const&)]+0x1f): undefined reference to
> > `testing::AssertionSuccess()'
> > prova.cpp:
>
> > (.text._ZN7testing8internal11CmpHelperEQIiiEENS_15AssertionResultEPKcS4_RKT _RKT0_[testing::AssertionResult

Keith Ray

unread,
May 4, 2011, 8:31:22 AM5/4/11
to i.dell'oro, Google C++ Testing Framework
If you don't know what a ".a" file is, then you don't know the basics of programming in your environment. (Which is Linux? Mac? Windows?)

You need to learn the basics of your development environment, and C++. Tests in this framework are using macros and templates -- they are just code. They will not "search" for tests in any directory unless you set that up in the makefile or IDE.

Google test for c++ is the best-documented framework that I've seen and I've worked with most of them, but it does require you to know how to build libraries and applications and link them together.

Web-search for "how to build and link to a static library in Linux" (or whatever platform you are using.)

Maybe try testing in C -- get James Grenning's book on TDD in Embedded C -- it's useful for normal C programming/testing too.

Consider writing your own test framework from scratch, using "#include <assert.h>", guided by "xUnit Patterns".

C. Keith Ray

Amplify Your Agility
Coaching | Training | Assessment | eLearning
http://industriallogic.com

Reply all
Reply to author
Forward
0 new messages