Archive

Posts Tagged ‘Google’

Add Google test directly to your Visual Studio project

November 9th, 2019 Comments off

Why?

While it is easy(ish) enough to add google test using various packages, you might want to add it directly to your project, (to debug your tests and so on).

I also found that packages are not always as often as you might want, so it might be because you need a new feature or something added to your test.

Before we start

– I am using Visual Studio 2019, but it should work fine with 2017 and even 2015
– I am using the latest Google Test (v1.10 at the time of writing this, but the principle is the same for almost all versions and should work fine for a few versions.

Getting started

  • Go to Google test on Github and get the latest release, make sure you get the ‘source code’
  • Don’t get it from anywhere else, you don’t know what has been changed and who changed it!
  • Remember that google test is written to a test application, not your main application!
  • Create a new console application, (File > New > Project …) and select “Console App”

Now add Google Test

  • In your project, create a new ‘folder’, I normally prefer to have it mirror what it looks like in my actual windows source code directory, but you could call it whatever you want, (like I did below).
  • Right-click on the ‘src’ folder and select “Add > Existing Items…”
  • Navigate to the folder you extracted, and find the ‘googletest > src’ folder
  • And then select ‘gtest_main.cc’ and ‘gtest-all.cc’, nothing else.
  • Finally, right-click your project and select “properties” and edit the “Additional Include Directories”
  • Add the directory relative to the location of your solution
    You could enter the full path if you want, (but that … blah), or you could use macros …
    Either way, make sure that the path is valid.
  • Make sure that the changes you make apply to all your build configuration.
    In the screenshot above I have x64 and Release but make sure that you choose all the configurations you will be using.
  • Compile your code … nothing funny should happen, (’cause you did nothing really).
    If you get an error, double check your paths and permissions.

Write and run a test

Last but not least, write a very simple test in your main and run a test

You do the rest

Now that a simple test is running, you can start creating tests for your own project.

Example?

Have a look at my directory watcher project and see my own tests.

As an aside, I am alway looking for comments, reviews and so on, please have a look at the project and comment!

Include Google test to your vs2015 project

November 21st, 2015 Comments off

To include Google test to your visual studio project you just need to follow the steps below

  1. Download Googletest from Github and copy all the file in it’s own folder.
    You only need the folder called “googletest” other files are for github and so on.
  2. Create a library
    1. Add a new empty project
      Right click the solution, Add > New Project …, “Win 32 project”
    2. Give it a name “googletest”
    3. Make sure that the directory is relative to your project, (the default is somewhere weird and wonderful in your %appdata% I think).
    4. Select the option “Static Library”, “empty project” and it should have no files in it.
    5. Uncheck the “Precompiled header” and press “Finish”
    6. Add gtest_main.cc and gtest-all.cc to that project, (they are located in the googletest\src\ folder of the files you just downloaded.
    7. Compile that project, and note where the googletest.lib file is created.
  3. In your test project,
    1. Right click > Properties > C++ > General
    2. In the part for “Additional include libraries” add the path to the google test folder and include folder.
      For example, “..\googletest\include;..\googletest\”.
    3. There might already be a folder called “%(AdditionalIncludeDirectories)”.
    4. Right click > Properties > Linker > General
    5. In the part for “”Additional Library directories” add the directory where the googletest.lib file was created, (the library, not the file).
    6. Finaly, add a reference, (Right Click the project > References and add googletest).

Be sure to set the references and include libraries for all your configurations, “Release”, “Debug”, x32, x64 and so on.

NB: Of course you can just build the lib and include it as a library, but that way when Googletest does an update, you just need to replace the code, and life is good again!

Categories: development Tags: , ,