/Unittests/googletest/README

http://unladen-swallow.googlecode.com/ · #! · 318 lines · 249 code · 69 blank · 0 comment · 0 complexity · 544f6637850e2816f5faddc3f3228da7 MD5 · raw file

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