The steps below show how to setup Google Test Framework for Test Driven Development (TDD). The steps have been tested on Windows 10, Ubuntu and Mac, but should work on other platforms too. Set it up, explore google test for unit testing and enjoy TDD. 

 

Required tools:

  • Python 2.7 or earlier (SCons does not support 3 yet)
  • SCons - Build tool
  • Google Test Framework
  • MinGW  (on Windows)

Step 1: Install necessary tools

Tools can be installed on Unix/Linux systems using the appropriate get/install commands. 

Make sure Python and SCons are installed properly by executing the following

 

Step 2: Create SCons build file

  • Go to your working directory
  • Create a new file name SConstruct - this will contain the instructions to make your build
  • We will add contents to it in the later steps

 

Step 3: Build gtest library

3a:

  • Go to your working directory
  • Under googletest folder (copied in step #1a) create new folder named build

 

  • Under googletest\build, create a new file SConscript  (any name should work, we will use the filename in our main SCons script)
  • Copy and paste the below lines in this SConscript file
# scons file for gtest library

env = Environment()
env.Replace(CC = 'g++')
env.VariantDir('$(BUILDPATH)', '..', duplicate = 0)
env.Append(LINKFLAGS = '--static')
env.Append(CPPPATH = ['../include', '../'])
env.Library('gtest_lib', ['../src/gtest-all.cc', '../src/gtest_main.cc'])
  • Save file and exit

3b:

  • Open SConstruct file from the parent folder
  • Call SConscript for googletest
    • Copy and paste below lines in SConstruct file  (if you have some other name for the file, make sure you use that)
# scons file for build
gtest_lib = SConscript('googletest/build/SConscript')
  • Save file and exit
  • Open command window in the parent folder
  • Run scons - this should build gtest library

 

  • Gtest library under build folder

 

Congratulations, you have your gtest library!

 

Step 4: The fun with your code

  • I have created two folders in the parent working directory
    • \src - all source files go here (.h/.c). Code under test.
    • \test - all test files go here (.cpp)
  • Under \src
    • For this tutorial purpose, I have a factorial code (can be downloaded from location below)
    • factorial.c and factorial.h
    • Note that there is no main()
  • Under \test
    • Create a test file: test_factorial.cpp (can be downloaded from location below)
    • The main() goes in this file
    • All the test code for your factorial function will go here.
    • This is your test bench

 

Step 5: Building your code with gtest

  • Just like we created SConscript for gtest library, we need to create one for our test code
  • Under \test, create a new folder named build
  • Create a new file SConscript
  • Copy and paste below lines in the file
# scons file for test

Import('env')
env.Program('test_factorial', [Glob('../*.cpp'), Glob('../../src/*.c')])
  • Open SConstruct file again from the parent folder
  • Call SConscript for your test code
    • Append below lines (Note that we are setting up the environment and exporting it to test\build\SConscript. Not necessary to do this but good option to know)

 

env = Environment()

env.Replace(CC = 'g++')
env.Append(CCFLAGS = '-g')
env.Append(LIBS = ['gtest_lib', 'pthread'])
env.Append(LIBPATH = ['#googletest/build'])
env.Append(CPPPATH = ['#googletest/include', '#src/'])
env.VariantDir('$(BUILDPATH)/build', '..', duplicate = 0)

SConscript('test/build/SConscript', exports=['env'])
  • Run scons - this should build your gtest library (if not previously done) and your source/test code

 

Step 6: Run the tests

 

 

If you see the factorial code, it is very simple and will fail at corner cases. Give it a try, and see if you can make it better using TDD.

  

Issues that you could run into:

  • Install the tools as administrator on Windows machine
  • Python version and path
    • Make sure you have version 2.7 or earlier and the path is updated correctly
  • pthread - On windows machine, if you are running MinGW, you may need to install mingw32-pthreads-w32
    • Install mingw32-pthreads-w32 from MinGW Installation Manager -> All Packages -> (mingw32-pthreads-w32)
  • libgcc and libstdc++ - The SConstruct will add these to the environment based on your platform.

 

File location:

https://github.com/basuhiremath/googletest

 

Further reading:

https://github.com/google/googletest/blob/master/googletest/docs/Primer.md

https://github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md

https://www.ibm.com/developerworks/aix/library/au-googletestingframework.htm

 

Happy GTesting!

Add comment


Security code
Refresh