PageRenderTime 630ms CodeModel.GetById 111ms app.highlight 306ms RepoModel.GetById 107ms app.codeStats 0ms

#! | 318 lines | 249 code | 69 blank | 0 comment | 0 complexity | 544f6637850e2816f5faddc3f3228da7 MD5 | raw file
  1Google C++ Testing Framework
  7Google's framework for writing C++ tests on a variety of platforms (Linux, Mac
  8OS X, Windows, Windows CE, Symbian, and etc). Based on the xUnit architecture.
  9Supports automatic test discovery, a rich set of assertions, user-defined
 10assertions, death tests, fatal and non-fatal failures, various options for
 11running the tests, and XML test report generation.
 13Please see the project page above for more information as well as mailing lists
 14for questions, discussions, and development. There is also an IRC channel on
 15OFTC ( #gtest available. Please join us!
 19Google Test is designed to have fairly minimal requirements to build
 20and use with your projects, but there are some. Currently, we support
 21building Google Test on Linux, Windows, Mac OS X, and Cygwin.  We will
 22also make our best effort to support other platforms (e.g. Solaris and
 23IBM z/OS).  However, since core members of the Google Test project
 24have no access to them, Google Test may have outstanding issues on
 25these platforms.  If you notice any problems on your platform, please
 26notify (patches for fixing them
 27are even more welcome!).
 29### Linux Requirements ###
 30These are the base requirements to build and use Google Test from a source
 31package (as described below):
 32  * GNU-compatible Make or "gmake"
 33  * POSIX-standard shell
 34  * POSIX(-2) Regular Expressions (regex.h)
 35  * A C++98 standards compliant compiler
 37Furthermore, if you are building Google Test from a VCS Checkout (also
 38described below), there are further requirements:
 39  * Automake version 1.9 or newer
 40  * Autoconf version 2.59 or newer
 41  * Libtool / Libtoolize
 42  * Python version 2.4 or newer
 44### Windows Requirements ###
 45  * Microsoft Visual Studio 7.1 or newer
 47### Cygwin Requirements ###
 48  * Cygwin 1.5.25-14 or newer
 50### Mac OS X Requirements ###
 51  * Mac OS X 10.4 Tiger or newer
 52  * Developer Tools Installed
 53  * Optional: Xcode 2.5 or later for univeral-binary framework; see note below.
 55Getting the Source
 57There are two primary ways of getting Google Test's source code: you can
 58download a source release in your preferred archive format, or directly check
 59out the source from a Version Control System (VCS, we use Google Code's
 60Subversion hosting). The VCS checkout requires a few extra steps and some extra
 61software packages on your system, but lets you track development, and make
 62patches to contribute much more easily, so we highly encourage it.
 64### VCS Checkout: ###
 65The first step is to select whether you want to check out the main line of
 66development on Google Test, or one of the released branches. The former will be
 67much more active and have the latest features, but the latter provides much
 68more stability and predictability. Choose whichever fits your needs best, and
 69proceed with the following Subversion commands:
 71  svn checkout gtest-svn
 73or for a release version X.Y.*'s branch:
 75  svn checkout \
 76    gtest-X.Y-svn
 78Next you will need to prepare the GNU Autotools build system, if you
 79are using Linux, Mac OS X, or Cygwin. Enter the target directory of
 80the checkout command you used ('gtest-svn' or 'gtest-X.Y-svn' above)
 81and proceed with the following command:
 83  autoreconf -fvi
 85Once you have completed this step, you are ready to build the library. Note
 86that you should only need to complete this step once. The subsequent `make'
 87invocations will automatically re-generate the bits of the build system that
 88need to be changed.
 90If your system uses older versions of the autotools, the above command will
 91fail. You may need to explicitly specify a version to use. For instance, if you
 92have both GNU Automake 1.4 and 1.9 installed and `automake' would invoke the
 931.4, use instead:
 95  AUTOMAKE=automake-1.9 ACLOCAL=aclocal-1.9 autoreconf -fvi
 97Make sure you're using the same version of automake and aclocal.
 99### Source Package: ###
100Google Test is also released in source packages which can be downloaded from
101its Google Code download page[1]. Several different archive formats are
102provided, but the only difference is the tools used to manipulate them, and the
103size of the resulting file. Download whichever you are most comfortable with.
105  [1] Google Test Downloads:
107Once downloaded expand the archive using whichever tools you prefer for that
108type. This will always result in a new directory with the name "gtest-X.Y.Z"
109which contains all of the source code. Here are some examples in Linux:
111  tar -xvzf gtest-X.Y.Z.tar.gz
112  tar -xvjf gtest-X.Y.Z.tar.bz2
113  unzip
115Choosing a TR1 Tuple Library
117Some Google Test features require the C++ Technical Report 1 (TR1)
118tuple library, which is not yet widely available with all compilers.
119The good news is that Google Test implements a subset of TR1 tuple
120that's enough for its own need, and will automatically use this when
121the compiler doesn't provide TR1 tuple.
123Usually you don't need to care about which tuple library Google Test
124uses.  However, if your project already uses TR1 tuple, you need to
125tell Google Test to use the same TR1 tuple library the rest of your
126project uses (this requirement is new in Google Test 1.4.0, so you may
127need to take care of it when upgrading from an earlier version), or
128the two tuple implementations will clash.  To do that, add
132to the compiler flags while compiling Google Test and your tests.
134If you don't want Google Test to use tuple at all, add
138to the compiler flags.  All features using tuple will be disabled in
139this mode.
141Building the Source
143### Linux, Mac OS X (without Xcode), and Cygwin ###
144There are two primary options for building the source at this point: build it
145inside the source code tree, or in a separate directory. We recommend building
146in a separate directory as that tends to produce both more consistent results
147and be easier to clean up should anything go wrong, but both patterns are
148supported. The only hard restriction is that while the build directory can be
149a subdirectory of the source directory, the opposite is not possible and will
150result in errors. Once you have selected where you wish to build Google Test,
151create the directory if necessary, and enter it. The following steps apply for
152either approach by simply substituting the shell variable SRCDIR with "." for
153building inside the source directory, and the relative path to the source
154directory otherwise.
156  ${SRCDIR}/configure  # Standard GNU configure script, --help for more info
157  make  # Standard makefile following GNU conventions
158  make check  # Builds and runs all tests - all should pass
160Other programs will only be able to use Google Test's functionality if you
161install it in a location which they can access, in Linux this is typically
162under '/usr/local'. The following command will install all of the Google Test
163libraries, public headers, and utilities necessary for other programs and
164libraries to leverage it:
166  sudo make install  # Not necessary, but allows use by other programs
168Should you need to remove Google Test from your system after having installed
169it, run the following command, and it will back out its changes.  However, note
170carefully that you must run this command on the *same* Google Test build that
171you ran the install from, or the results are not predictable.  If you install
172Google Test on your system, and are working from a VCS checkout, make sure you
173run this *before* updating your checkout of the source in order to uninstall
174the same version which you installed.
176  sudo make uninstall  # Must be run against the exact same build as "install"
178Your project can build against Google Test simply by leveraging the
179'gtest-config' script. This script can be invoked directly out of the 'scripts'
180subdirectory of the build tree, and it will be installed in the binary
181directory specified during the 'configure'. Here are some examples of its use,
182see 'gtest-config --help' for more detailed information.
184  gtest-config --min-version=1.0 || echo "Insufficient Google Test version."
186  g++ $(gtest-config --cppflags --cxxflags) -o foo.o -c foo.cpp
187  g++ $(gtest-config --ldflags --libs) -o foo foo.o
189  # When using a built but not installed Google Test:
190  g++ $(../../my_gtest_build/scripts/gtest-config ...) ...
192### Windows ###
193The msvc\ folder contains two solutions with Visual C++ projects. Open the
194gtest.sln or gtest-md.sln file using Visual Studio, and you are ready to
195build Google Test the same way you build any Visual Studio project. Files
196that have names ending with -md use DLL versions of Microsoft runtime
197libraries (the /MD or the /MDd compiler option). Files without that suffix
198use static versions of the runtime libraries (the /MT or the /MTd option).
199Please note that one must use the same option to compile both gtest and his
200test code. If you use Visual Studio 2005 or above, we recommend the -md
201version as /MD is the default for new projects in these versions of Visual
204### Mac OS X (universal-binary framework) ###
205Open the gtest.xcodeproj in the xcode/ folder using Xcode. Build the "gtest"
206target. The universal binary framework will end up in your selected build
207directory (selected in the Xcode "Preferences..." -> "Building" pane and
208defaults to xcode/build). Alternatively, at the command line, enter:
210  xcodebuild
212This will build the "Release" configuration of gtest.framework in your
213default build location. See the "xcodebuild" man page for more information about
214building different configurations and building in different locations.
216To test the gtest.framework in Xcode, change the active target to "Check" and
217then build. This target builds all of the tests and then runs them. Don't worry
218if you see some errors. Xcode reports all test failures (even the intentional
219ones) as errors. However, you should see a "Build succeeded" message at the end
220of the build log. To run all of the tests from the command line, enter:
222  xcodebuild -target Check
224Installation with xcodebuild requires specifying an installation desitination
225directory, known as the DSTROOT. Three items will be installed when using
228  $DSTROOT/Library/Frameworks/gtest.framework
229  $DSTROOT/usr/local/lib/libgtest.a
230  $DSTROOT/usr/local/lib/libgtest_main.a
232You specify the installation directory on the command line with the other
233xcodebuild options. Here's how you would install in a user-visible location:
235  xcodebuild install DSTROOT=~
237To perform a system-wide inistall, escalate to an administrator and specify
238the file system root as the DSTROOT:
240  sudo xcodebuild install DSTROOT=/
242To uninstall gtest.framework via the command line, you need to delete the three
243items listed above. Remember to escalate to an administrator if deleting these
244from the system-wide location using the commands listed below:
246  sudo rm -r /Library/Frameworks/gtest.framework
247  sudo rm /usr/local/lib/libgtest.a
248  sudo rm /usr/local/lib/libgtest_main.a
250It is also possible to build and execute individual tests within Xcode. Each
251test has its own Xcode "Target" and Xcode "Executable". To build any of the
252tests, change the active target and the active executable to the test of
253interest and then build and run.
255Individual tests can be built from the command line using:
257  xcodebuild -target <test_name>
259These tests can be executed from the command line by moving to the build
260directory and then (in bash)
262  export DYLD_FRAMEWORK_PATH=`pwd`
263  ./<test_name>  # (e.g. ./gtest_unittest)
265To use gtest.framework for your own tests, first, install the framework using
266the steps described above. Then add it to your Xcode project by selecting
267Project->Add to Project... from the main menu. Next, add libgtest_main.a from
268gtest.framework/Resources directory using the same menu command. Finally,
269create a new executable target and add gtest.framework and libgtest_main.a to
270the "Link Binary With Libraries" build phase.
272### Using GNU Make ###
273The make/ directory contains a Makefile that you can use to build
274Google Test on systems where GNU make is available (e.g. Linux, Mac OS
275X, and Cygwin).  It doesn't try to build Google Test's own tests.
276Instead, it just builds the Google Test library and a sample test.
277You can use it as a starting point for your own Makefile.
279If the default settings are correct for your environment, the
280following commands should succeed:
282  cd ${SRCDIR}/make
283  make
284  ./sample1_unittest
286If you see errors, try to tweak the contents of make/Makefile to make
287them go away.  There are instructions in make/Makefile on how to do
290### Using Your Own Build System ###
291If none of the build solutions we provide works for you, or if you
292prefer your own build system, you just need to compile
293src/ into a library and link your tests with it.  Assuming
294a Linux-like system and gcc, something like the following will do:
296  cd ${SRCDIR}
297  g++ -I. -I./include -c src/
298  ar -rv libgtest.a gtest-all.o
299  g++ -I. -I./include path/to/ libgtest.a -o your_test
301Regenerating Source Files
303Some of Google Test's source files are generated from templates (not
304in the C++ sense) using a script.  A template file is named FOO.pump,
305where FOO is the name of the file it will generate.  For example, the
306file include/gtest/internal/gtest-type-util.h.pump is used to generate
307gtest-type-util.h in the same directory.
309Normally you don't need to worry about regenerating the source files,
310unless you need to modify them (e.g. if you are working on a patch for
311Google Test).  In that case, you should modify the corresponding .pump
312files instead and run the 'pump' script (for Pump is Useful for Meta
313Programming) to regenerate them.  We are still working on releasing
314the script and its documentation.  If you need it now, please email such that we know to make it
316happen sooner.
318Happy testing!