Sep 27

Setting Up The Test Windows Project

Setting Up The Test Windows Project


Example diagram:



Theoretical Background:

In the previous articles we learned about gmock and gtest frameworks and set up the windows gmock and gtest projects.

In this article we will set up the Windows test project, This project will take dependency on:


The gmock project (which takes dependency upon the gtest project)

As we will use these frameworks as our test frameworks.


The CrossPlatformProjectExampleCore project (which is the project that compiles our core cross platform code)

We need to take dependency on this project as this is the business logic and the actual code we will test.


Setting up the project:


1. Create a new console app project named: CrossPlatformProjectTests.

We will create the project under the folder:



2. Set the project to take dependency upon the gmock and CrossPlatformProjectExampleCore projects:



3. Set the project to use PCH file:

We will set to use a new PCH file:




In this testpch.h we will add the following includes:


#include "gtest/gtest.h"
#include "gmock/gmock.h"

//Including the basic common includes file
#include <CommonIncludes.h>


The CommonIncludes.h file is a header file that is included by the pch.h file of the CrossPlatformProjectExampleCore and by our testpch.h file. It is located in the code folder:


As our test project will use the real code of the CrossPlatformProjectExampleCore project, in order to avoid duplicity in the pch.h file and testpch.h file – not to add each include in both these files, we are creating a CommonIncludes.h header file that has all the includes the core project and the test project need in order to build.


4. Adding the include directories:

We need to add to following directories to the Additional Include Directories:


The inc folder of our test code.




The gmock and gtest include folder.



The inc folder of our core code.



The src folder of our core code. Since the test project will test internal classes and mock them it will need to know the header files that will be in the src folder of the core project.


5. Set up the main.cpp file:


int main(int argc, char **argv) {
    std::cout << "Running main()";

    //No need to call InitGoogleTest, It is being called from InitGoogleMock
    //::testing::InitGoogleTest(&argc, argv);

    ::testing::InitGoogleMock(&argc, argv);

    return RUN_ALL_TESTS();


In the main method we initialize the google mock and call RUN_ALL_TESTS();

This will auto run all tests we compiled in this project


Creating a sample test:

Now that we have the project set up, we can create a sample test:


1. Create a MockAdder class:

We will create the new class in the folder: CrossPlatformProjectExample\XPlat\Code\test\inc\Mocks


namespace LIOR_TEST
    namespace Test
        namespace Mock
            class MockAdder : public Adder

                MOCK_METHOD2(Add, int(int, int));


2. Create a new test class – CalculatorTests.cpp:

We will create the new class in the folder:



#include "Calculator.hpp"

#include <Mocks/MockAdder.hpp>

namespace LIOR_TEST {

    namespace Test {

        // The fixture for testing class Foo.
        class CalculatorTest : public ::testing::Test {

            shared_ptr<MockAdder> m_mockAdder;

            // You can remove any or all of the following functions if its body
            // is empty.

            CalculatorTest() {
                // You can do set-up work for each test here.

            virtual ~CalculatorTest() {
                // You can do clean-up work that doesn't throw exceptions here.

            // If the constructor and destructor are not enough for setting up
            // and cleaning up each test, you can define the following methods:

            virtual void SetUp() {
                // Code here will be called immediately after the constructor (right
                // before each test).

                m_mockAdder = make_shared<MockAdder>();

                EXPECT_CALL(*m_mockAdder, Add(::testing::_, ::testing::_))


        TEST_F(CalculatorTest, AdderTestExampleUsingTheMockAdder) {
            Calculator calculator;

            //The MockAdder always returns the result 5.

            EXPECT_CALL(*m_mockAdder, Add(::testing::_, ::testing::_)).Times(2);

            int result = calculator.AddTwice(1, 1);

            //Since we use the MockAdder we will get the result 10 and not the expected result of 4
            EXPECT_EQ(4, result) << "Expected result 4 but using the mock got: " << result;


Running the tests:

We can either run by simply setting the CrossPlatformProjectTests as the startup project and press F5 / ctrl+F5.

We can also run specific tests and use the TestExplorer window:




That is all, up to this point we set up all we need for the windows platform:

Created the core project

Create a Core console app project for testing as playground

Create the gtest and gmock projects

Created the test project


Link to download the example project


In the following articles I will go over to the Android framework and we will see how we can integrate our core code to compile in the android framework.


Good luck !

Leave a Reply

Your email address will not be published.

אתם יכולים להשתמש באפשרויות ותגי ה-HTMLהבאים: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>