Skip to content
Sample files for clang + gtest + cmake with on-write automated test
Shell C++ CMake
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.


Sample project for clang + gtest + cmake + autotest


This is my first attempt to create a C++ development environment. I use a Mac, and I want to learn C++11, and so will be using clang. Also, all the cool kids seem to love cmake, so I'll try that too. Also, I want automated unit testing, and am trying googletest (gtest). Finally, I want a rapid test cycle, and therefore created a simple autotest script which runs the gtest unit tests every time I write a source file from the editor.

All the research work for this sample project is embodied in the shell scripts. I tried to make them as simple as possible for ease of leveraging my research.


Requires fswatch which I got on my Mac using homebrew ("brew install fswatch").

Requires cmake which I got on my Mac by downloading the .dmg file and doing the standard install. When I did it, the syn links didn't install in /usr/bin, so I did:

sudo /Applications/

to start the cmake app. Then I pulled down "Tools" and selected "Install For Command Line Use", etc.

Requires gtest, a snapshot of which is included in this sample project. Why? Because I couldn't get it working externally with cmake, and StackOverflow gave me the tip.


The main files in this sample are:

  • learn.cpp - the unit tests (in gtest form).
  • - script to remove all generated files.
  • - script to run cmake to re-configure and re-generate the Makefile, re-build gtest, and re-build learn.cpp.
  • - script to run make followed by the unit tests executable.
  • - script to monitor for updated source files and run
  • CMakeLists.txt - master file for cmake.
  • - compressed tar snapshot of gtest.


After downloading, get started with:


You'll need to re-run when you make changes to your project that require re-generating the Makefile. For exmaple, adding or removing a file, or making other changes to CMakeLists.txt.

Now you can run the automated tester in one window with:


while you edit source files in another window. Each time you save a source file, the autotest window should wake up, do a "make", and if successful, will do a ./

Subsequent to that, you can manually do a "minor" rebuild with:


By minor, I mean after you've made changes which do not require re-generating the Makefile. HOWEVER, if you add/remove files, or otherwise need to change the cmake list file, you'll have to run again.

You can manually run the unit tests with:


You can combine make and ./runUnitTests with:



One significant problem is that if a number of files are changed, autotest will loop that many times. It will be better if, when the first file change is detected, it waits a half second and reads the rest of the files from fswatch. But I can't think of an easy way to do that without a C program doing non-blocking reads, which sounds too much like work. Oh well, I'll get motivated to figure it out when I start being inconvenienced by the problem. :-)

You can’t perform that action at this time.