GTest

  • Revision slug: GTest
  • Revision title: GTest
  • Revision id: 431371
  • Created:
  • Creator: bgirard
  • Is current revision? No
  • Comment

Revision Content

GTest (googletest) is Google's framework for writing C++ tests on a variety of platforms (Linux, Mac OS X, Windows, Cygwin, Windows CE, and Symbian). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, value- and type-parameterized tests, various options for running the tests, and XML test report generation.

Integration

GTest are ran part of the build job on Mac/Linux and coming soon to Windows (Bug 883339). Search for 'rungtest'.

Running tests

The Mozilla build process will build GTest on supported platforms. This will compile and link any GTEST_{CPP|C|CMM}SRCS to toolkit/library/{lib}gtest/{lib}xul{.so|.dll|}. To run the unit tests use 'mach gtest' when invoking Gecko. To run under a debug use the same steps to debug firefox but export 'MOZ_RUN_GTEST'

Don't forget to build + run 'mach gtest' to relink when using MOZ_RUN_GTEST since it's not part of a top level build!

Running selected tests

Tests can be selected using mach. You can also use environment variables support by GTest. See Running Test Programs: Running_a_Subset_of_the_Tests for more details.

mach gtest Moz2D.*

Configuring GTest

GTest can be controlled from other environment variables. See Running Test Programs: Advanced Options for more details.

Writing a GTest Unit Test

Most of the GTest documentation will apply here. The GTest primer is a recommended read.

GTest will run tests in parellel. Don't add unit tests that are not threadsafe such as tests that require focus or use specific sockets.

GTest will run without initializing mozilla services. Initialize and tear down any depenencies you have in your test fixtures. Avoid writing integration tests and focus on testing individual units.

See https://hg.mozilla.org/mozilla-central/rev/351462147f91 for an example of how to add a simple test.

Adding a test to the build system

Find a gtest directory appropriate for the module. If none exist create a directory using the following convention 'testt/gtest'. Create a moz.build file with a module declaration:

# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
# vim: set filetype=python:
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

MODULE = 'gfxtest'

Then create a Makefile with the following. Replace MODULE_NAME/LIBRARY_NAME/GTEST_CPPSRCS:

#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

DEPTH          = @DEPTH@
topsrcdir      = @top_srcdir@
srcdir         = @srcdir@
VPATH          = @srcdir@
relativesrcdir = @relativesrcdir@

include $(DEPTH)/config/autoconf.mk

# Create a GTest library
MODULE_NAME    = gfxtest
LIBRARY_NAME   = gfxtest
LIBXUL_LIBRARY = 1
IS_COMPONENT   = 1
EXPORT_LIBRARY = 1

GTEST_CPPSRCS = \
  <ListTestFiles> \
  $(NULL)

include $(topsrcdir)/config/rules.mk

Declare your new module to be linked into the GTest version of libxul. Open up toolkit/library/Makefile and look for the following section and append your module to the list:

ifdef LINK_GTEST

COMPONENT_LIBS += \
  gtest \
  gfxtest \
  $(NULL)
endif

Update the moz.build in the parent directory to build your new sub directory:

if CONFIG['ENABLE_TESTS']:
    DIRS += ['tests/gtest']

When adding tests to an exist Makefile add the following. That's it, there is no test manifest required. Your tests will be automatically registered using a static constructor.

GTEST_CPPSRCS = TestFoo.cpp

Revision Source

<p>GTest (googletest) is Google's framework for writing C++ tests on a variety of platforms (Linux, Mac OS X, Windows, Cygwin, Windows CE, and Symbian). Based on the xUnit architecture. Supports automatic test discovery, a rich set of assertions, user-defined assertions, death tests, fatal and non-fatal failures, value- and type-parameterized tests, various options for running the tests, and XML test report generation.</p>
<h3 id="Integration">Integration</h3>
<p>GTest are ran part of the build job on Mac/Linux and coming soon to Windows (Bug 883339). Search for 'rungtest'.</p>
<h3 id="Running_tests" name="Running_tests">Running tests</h3>
<p>The Mozilla build process will build GTest on supported platforms. This will compile and link any GTEST_{CPP|C|CMM}SRCS to toolkit/library/{lib}gtest/{lib}xul{.so|.dll|}. To run the unit tests use 'mach gtest' when invoking Gecko. To run under a debug use the same steps to debug firefox but export 'MOZ_RUN_GTEST'</p>
<div class="warning">
  <p>Don't forget to build + run 'mach gtest' to relink when using MOZ_RUN_GTEST since it's not part of a top level build!</p>
</div>
<h4 id="Running_selected_tests">Running selected tests</h4>
<p>Tests can be selected using mach. You can also use environment variables support by GTest. See <a href="http://code.google.com/p/googletest/wiki/AdvancedGuide#Running_a_Subset_of_the_Tests" title="/en-US/docs/">Running Test Programs: Running_a_Subset_of_the_Tests</a> for more details.</p>
<pre>
mach gtest Moz2D.*</pre>
<h4 id="Configuring_GTest">Configuring GTest</h4>
<p>GTest can be controlled from other environment variables. See <a href="https://code.google.com/p/googletest/wiki/AdvancedGuide#Running_Test_Programs:_Advanced_Options" title="/en-US/docs/">Running Test Programs: Advanced Options</a> for more details.</p>
<h3 id="Writing_a_GTest_Unit_Test">Writing a GTest Unit Test</h3>
<p>Most of the <a href="http://code.google.com/p/googletest/wiki/Documentation" title="http://code.google.com/p/googletest/wiki/Documentation">GTest documentation</a> will apply here. The <a href="http://code.google.com/p/googletest/wiki/Primer" title="http://code.google.com/p/googletest/wiki/Primer">GTest primer</a> is a recommended read.</p>
<div class="warning">
  <p>GTest will run tests in parellel. Don't add unit tests that are not threadsafe such as tests that require focus or use specific sockets.</p>
</div>
<div class="warning">
  <p>GTest will run without initializing mozilla services. Initialize and tear down any depenencies you have in your test fixtures. Avoid writing integration tests and focus on testing individual units.</p>
</div>
<p>See <a href="https://hg.mozilla.org/mozilla-central/rev/351462147f91" title="https://hg.mozilla.org/mozilla-central/rev/351462147f91">https://hg.mozilla.org/mozilla-central/rev/351462147f91</a> for an example of how to add a simple test.</p>
<h4 id="Adding_a_test_to_the_build_system">Adding a test to the build system</h4>
<p>Find a gtest directory appropriate for the module. If none exist create a directory using the following convention 'testt/gtest'. Create a moz.build file with a module declaration:</p>
<div class="note">
  <p># -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-<br />
    # vim: set filetype=python:<br />
    # This Source Code Form is subject to the terms of the Mozilla Public<br />
    # License, v. 2.0. If a copy of the MPL was not distributed with this<br />
    # file, You can obtain one at http://mozilla.org/MPL/2.0/.<br />
    <br />
    MODULE = 'gfxtest'</p>
</div>
<p>Then create a Makefile with the following. Replace MODULE_NAME/LIBRARY_NAME/GTEST_CPPSRCS:</p>
<div class="note">
  <p>#<br />
    # This Source Code Form is subject to the terms of the Mozilla Public<br />
    # License, v. 2.0. If a copy of the MPL was not distributed with this<br />
    # file, You can obtain one at http://mozilla.org/MPL/2.0/.<br />
    <br />
    DEPTH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = @DEPTH@<br />
    topsrcdir&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = @top_srcdir@<br />
    srcdir&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = @srcdir@<br />
    VPATH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; = @srcdir@<br />
    relativesrcdir = @relativesrcdir@<br />
    <br />
    include $(DEPTH)/config/autoconf.mk<br />
    <br />
    # Create a GTest library<br />
    MODULE_NAME&nbsp;&nbsp;&nbsp; = gfxtest<br />
    LIBRARY_NAME&nbsp;&nbsp; = gfxtest<br />
    LIBXUL_LIBRARY = 1<br />
    IS_COMPONENT&nbsp;&nbsp; = 1<br />
    EXPORT_LIBRARY = 1<br />
    <br />
    GTEST_CPPSRCS = \<br />
    &nbsp; &lt;ListTestFiles&gt; \<br />
    &nbsp; $(NULL)<br />
    <br />
    include $(topsrcdir)/config/rules.mk</p>
</div>
<p>Declare your new module to be linked into the GTest version of libxul. Open up toolkit/library/Makefile and look for the following section and append your module to the list:</p>
<div class="note">
  <p>ifdef LINK_GTEST<br />
    <br />
    COMPONENT_LIBS += \<br />
    &nbsp; gtest \<br />
    &nbsp; gfxtest \<br />
    &nbsp; $(NULL)<br />
    endif</p>
</div>
<p>Update the moz.build in the parent directory to build your new sub directory:</p>
<div class="note">
  <p>if CONFIG['ENABLE_TESTS']:<br />
    &nbsp;&nbsp;&nbsp; DIRS += ['tests/gtest']</p>
</div>
<p>When adding tests to an exist Makefile add the following. That's it, there is no test manifest required. Your tests will be automatically registered using a static constructor.</p>
<pre>
<span class="difflineplus">GTEST_CPPSRCS = TestFoo.cpp</span></pre>
Revert to this revision