How to setup the solution (Visual studio 2008) to include Google test and Google mock

754 views
Skip to first unread message

frank

unread,
Nov 25, 2010, 2:09:14 PM11/25/10
to Google C++ Mocking Framework
Hello everyone!

My application is built from a win32 type project. Obviously this
means there are windows and all the graphical stuff. How can I
integrate Google test and Google mock to test it. I will expose you
what I already did and you will complete/correct it.

I need a test executable so I added a console project
(unittests.vcproj) in the solution of my application. I added a header
file in unittests to contain the declaration of the mock class. In the
main source file of unittests, I added the main function, the
expectations macros, etc (see below):


#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "mock-CUser.h"

using ::testing::AtLeast;

TEST(ConnectionManagerTest, CanReadUser) {
MockCUser user;
EXPECT_CALL(user, CompareName(_))
.Times(AtLeast(1));
CConnectionManager connectionMgr(&user);

EXPECT_TRUE(connectionMgr.Traverse(_));
}


int main(int argc, char **argv)
{
// The following line must be executed to initialize Google Mock
// (and Google Test) before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}


I think I am ready to launch the test. But I don’t understand how the
code of my application will be run?

Thank you!
Frank

Manuel Klimek

unread,
Nov 25, 2010, 4:10:41 PM11/25/10
to frank, Google C++ Mocking Framework

I'm not really sure what your question is here... does the setup work
for you or not? Which application are you talking about? Do you mean
the CConnectionManager class that you reference in the test? If it's
not working, what are you expecting and how does it behave?

Cheers,
/Manuel

leefw

unread,
Nov 26, 2010, 2:53:46 AM11/26/10
to Google C++ Mocking Framework
Hi Frank

If I understand your question correctly you need to look down the
lines of building a project structure that will support your existing
application and let you also run unit tests. You might consider
spilting your existing solution in to two projects:

- One which will build the bulk code of your application's logic in to
a library (or DLL)
- One which will build an executable, containing the UI, which will
make calls in to your library (project depends on the library).

When you have this working you can then add yet another project to the
solution which canl make unit test calls to the library. You can then
define your unit tests and mocks here or create yet another project to
separate them. This will let you test the functionality in your
library (but not your UI). All in all you will end up with 3
components: an library, an exe for your application and an exe for
your tests.

You may want to check out my recent blog entry on setting up GTest for
Visual Studio for more details on how I did this:

http://leefw.wordpress.com/2010/11/17/google-test-gtest-setup-with-microsoft-visual-studio-2008-c/

Hope this is of some use to you.

Regards
Lee Francis Wilhelmsen
Reply all
Reply to author
Forward
0 new messages