Skip to content
vishalbhardwaj26 edited this page Jul 30, 2016 · 7 revisions

Welcome to the UnitTestsUsingFakes wiki!

Using Google test and mock framework to unit test C++ libraries.

Using Visual Studio 2015

Source Code: https://github.com/vishalbhardwaj26/UnitTestsUsingFakes

CPlusPlusBusiness:

Person is a class using Logger to log information underneath. We are pushing logger as dependency injection to Person. Let’s test Person class with mocking the dependencies using google framework.

CPlusPlusUnitTest:

  • Install GoogleTest

  • Install gMock

  • If you got error: Unexpected end of file while looking for precompiled header

Set the Create/Use Precompiled Header property of source files to Not Using Precompiled Headers. To set this compiler option, follow these steps: 1. In the Solution Explorer pane of the project, right-click the project name, and then click Properties. 2. In the left pane, click the C/C++ folder. 3. Click the Precompiled Headers node. 4. In the right pane, click Create/Use Precompiled Header, and then click Not Using Precompiled Headers.

  • Mock the dependency
    1. Implement Logger as Mock_Logger.h
    2. Mock the required functions.
  • Write tests in test class using mocked dependencies and expectations
    1. Write Tests in MockLogger.cc with required expectations
  • Implement main function to run all the available tests in gtest framework.

`#include (gtest\gtest.h)

int main(int ac, char* av[]) {

testing::InitGoogleTest(&ac, av);

return RUN_ALL_TESTS();

}`

  • If you want to see test result on output on every build:

    1. Paste this in post build event command line: "$(TargetDir)$(TargetFileName)"
  • If you want to run tests using Test explorer, you need to add plugin Tools->Extension and Updates->Install “Google TestRunner for Visual Studio 2015”

Using MS VS unit test framework to add stubs for dependencies in C# applications

  • Add fake asmble for creating stubs