csoltenborn / GoogleTestAdapter

Visual studio extension that adds support for the C++ testing framework Google Test.
Other
143 stars 100 forks source link
c-plus-plus cpp google-test visual-studio-extension

Build status Code coverage Visual Studio Marketplace downloads NuGet downloads

Looking for a Maintainer

As you might have noticed, this project is not maintained any more (I have moved to different technologies and are not even working as a developer any more). Since it still has a few features which the MS clone (which is still developed and delivered with Visual Studio) does not support, there still seems to be some interest in the project. Thus, if anybody wants to take over this project, please get in touch - I will certainly try to get you up and running!

Google Test Adapter

Google Test Adapter (GTA) is a Visual Studio extension providing test discovery and execution of C++ tests written with the Google Test framework.

Screenshot of Test Explorer

Features

History

Usage

Installation

Download from Visual Studio Marketplace Download from NuGet Download from GitHub

Google Test Adapter can be installed in three ways:

After restarting VS, your tests will be displayed in the Test Explorer at build completion time. If no or not all tests show up, have a look at the trouble shooting section.

Note that due to Microsoft requiring VS extensions to support asynchronous package loading, the last version of Google Test Adapter which supports Visual Studio 2012 is 0.14.4.

Configuration

GTA provides different ways of configuration:

The format of solution and user settings files is the same: a <GoogleTestAdapterSettings> node contains the solution settings and the (possibly empty) set of project settings and is itself contained in a <RunSettings> node (which in the case of user settings files might contain additional, e.g. VS specific settings). In contrast to solution settings, each set of project settings additionally has a regular expression to be evaluated at test discovery and execution time.

The final settings to be used are computed by merging the available settings, vaguely following Visual Studio's approach of configuration inheritance. Merging is done in two stages:

  1. The available global, solution file, and user file settings are merged into solution settings and a set of project settings. This is done in increasing priority, i.e., solution file settings override global settings, and user file settings override solution settings. Project settings of solution and user settings files are merged if they share the exact same regular expression.
  2. At test discovery and execution time, each test executable's full path is matched against the project settings' regular expressions; the first matching project settings are used for the particular test executable. If no project settings are found, the solution settings are used.

Overall, given a test executable mytests.exe, the following settings apply to that executable in decreasing priority:

  1. Project settings of a user settings file, the regular expression of which matches the full path of mytests.exe.
  2. Project settings of a solution settings file, the regular expression of which matches the full path of mytests.exe.
  3. Solution settings of a user settings file.
  4. Solution settings of a solution settings file.
  5. Global settings.

Note that due to the overriding hierarchy described above, you probably want to provide only a subset of the nodes in your configuration files. For instance, providing the node <DebugMode>true</DebugMode> in a shared solution settings file will make sure that all sharing developers will run GTA with debug output, no matter what the developer's individual settings at Tools/Options/Google Test Adapter are (and unless the developer has selected a test settings file via VS, which would override the solution setting).

For reference, see a settings file AllTestSettings.gta.runsettings containing all available settings, a more realistic solution settings file SampleTests.gta.runsettings as delivered with the SampleTests solution, and a user settings file NonDeterministic.runsettings as used by GTA's end-to-end tests. The syntax of the GTA settings files (excluding the <RunSettings> node) is specified by this schema.

Settings helper files

GTA does not provide direct access to VS project settings such as Project > Properties > Debugging > Environment. Additionally, when run as NuGet dependency, GTA does not have access to information such as solution dir or Platform/Configuration a test executable has been built with.

To overcome these problems, GTA supports so-called settings helper files which provide that information to GTA. Helper files are usually generated as part of the build, e.g. within a post-build event, which might look like this:

echo SolutionPath=$(SolutionPath)::GTA::SolutionDir=$(SolutionDir)::GTA::PlatformName=$(PlatformName)::GTA::ConfigurationName=$(ConfigurationName)::GTA::TheTarget=$(TargetFileName) > $(TargetPath).gta_settings_helper

This command generates a file $(TargetPath).gta_settings_helper (where $(TargetPath) is the path of the final test executable) containing key/value pairs separated by ::GTA::. At file generation time, the VS macros will be replaced by the actual values, which can then in turn be used as placeholders within the GTA settings. In particular, the helper file generated by the above command will

Note that the settings helper files need to be located within the same folder as their corresponding test executable, and must have the test executable's name concatenated with the .gta_settings_helper ending (make sure to ignore these files in version control if necessary).

Assigning traits to tests

GTA has full support for traits, which can be assigned to tests in two ways:

More precisely, traits are assigned to tests in three phases:

  1. Traits are assigned to tests which match one of the regular expressions specified in the traits before option. For instance, the expression .*///Size,Medium assigns the trait (Size,Medium) to all tests.
  2. Traits added to tests via test macros are assigned to the according tests, overriding traits from the first phase. For instance, the test declaration TEST_P_TRAITS(ParameterizedTests, SimpleTraits, Size, Small) will make sure that all test instances of test ParameterizedTest.SimpleTraits will be assigned the trait (Size,Small) (and override the Size trait assigned from the first phase).
  3. Traits are assigned to tests which match one of the regular expressions specified in the traits after option, overriding traits from phases 1 and 2 as described above. For instance, the expression .*\[1.*\]///Size,Large will make sure that all parameterized tests where the parameter starts with a 1 will be assigned the trait (Size,Large) (and override the traits assigned by phases 1 and 2).

Note that traits are assigned in an additive manner within each phase, and in an overriding manner between phases. For instance, if a test is assigned the traits (Author,Foo) and (Author,Bar) in phase 1, the test will have both traits. If the test is also assigned the trait (Author,Baz) in phases 2 or 3, it will only have that trait. See test code for examples.

Evaluating the test executable's exit code

If option Exit code test case is non-empty, an additional test case will be generated per text executable (referred to as exit code test in the following), and that exit code test will pass if the test executable's exit code is 0. This allows to reflect some additional result as a test case; for instance, the test executable might be built such that it performs memory leak detection at shutdown (see below for example); the result of that check can then be seen within VS as the result of the according additional test.

A couple of tokens can used as part of a test executable's output; if GTA sees theses tokens, it will act accordingly:

Note that a test executable might be run more than once by GTA (e.g., if tests are run in parallel, or if the selection of tests to be run results in command lines too long for a single test run). In this case, the exit codes and respective outputs of a test exectutable are aggregated as follows:

Example usage: Memory leak detection

An example usage of the Exit code test case can be found as part of the SampleTests solution: Project MemoryLeakTests makes use of MS' memory leak detection facilities and reports the results to VS via an exit code test. The approach can easily be re-used for other Google Test projects:

However, note that Google Test as of V1.8.1 uses some memory allocation which is recognized by MS' leak detection mechanism as a leak (although it isn't), in particular for failing assertions. Some of these "false positives" have been fixed with the linked issue, making leak detection useful as long as all tests are green; however, and until this problem is fixed, the memory leak detection provided by GTA will result in a skipped exit code test in case RUN_ALL_TESTS() does not return 0, but will report the leak in the test's error message. If you run into such problems, please report them against the Google Test repository if appropriate.

Known issues

Running tests from command line with VSTest.Console.exe

GTA can be used to run tests from the command line, which can be done making use of VS's VSTest.Console.exe. GTA supports all the tool's command line options, including /UseVsixExtensions and /TestAdapterPath.

Note, however, that VSTest.Console.exe will not make use of GTA solution settings (if the solution containing the tests happens to use such settings). All settings to be used by VSTest.Console.exe need to be passed using the /Settings command line option. Note also that the $(SolutionDir) placeholder is neither available in the Test setup/teardown batch file options nor in the Additional test execution parameters option.

The tests to be run can be selected via the /TestCaseFilter option. Filters need to follow the syntax as described in this blog entry. GTA supports the following test properties:

Additionally, traits can be used in test case filters. E.g., all tests having a Duration of short can be executed by means of the filter /TestCaseFilter:"Duration=short".

Parallelization

Tests are run sequentially by default. If parallel test execution is enabled, the tests will be distributed to the available cores of your machine. To support parallel test execution, additional command line parameters can be passed to the Google Test executables (note that this feature is not restricted to parallel test execution); they can then be parsed by the test code at run time and e.g. be used to improve test isolation.

GTA remembers the durations of the executed tests to improve test scheduling for later test runs. The durations are stored in files with endings .gta.testdurations - make sure your version control system ignores these files.

Note that since VS 2015 update 1, VS allows for the parallel execution of tests (again); since update 2, Test Explorer has an own Run tests in parallel button, and VsTest.Console.exe suppports a new command line option /Parallel. Neither button nor command line option has any effect on test execution with GTA.

Test setup and teardown

If you need to perform some setup or teardown tasks in addition to the setup/teardown methods of your test code, you can do so by configuring test setup/teardown batch files, to which you can pass several values such as solution directory or test directory for exclusive usage of the tests.

Feature availability

GTA runs in three different environments:

For technical reasons, not all features are available in all environments by default; refer to the table below for details.

Feature VS/VSIX VS/NuGet VsTest.Console
Test discovery yes yes yes
Test execution yes yes yes
Test debugging yes no -
Configuration via
- VS Options yes no -
- VS Toolbar yes no -
- Solution test config file yes no no
- User test config file yes1 yes1 yes2
Placeholders
- $(SolutionDir) yes yes3, 4 yes3
- $(PlatformName) yes yes3 yes3
- $(ConfigurationName) yes yes3 yes3
- $(ExecutableDir) yes yes yes
- $(Executable) yes yes yes
- $(TestDir)5 yes yes yes
- $(ThreadId)5 yes yes yes
- Additional placeholders yes3 yes3 yes3
- Environment variables yes yes yes

1: Via Test/Test Settings/Select Test Settings File
2: Via /Settings option
3: If settings helper files are provided
4: During test execution, placeholders are available even without settings helper files
5: Only during test execution; placeholders are removed in discovery mode

External resources

Trouble shooting

General advice
None or not all of my tests show up

In general, you can identify issues with your test executables by debugging them within VS: make the according test project the startup project of your solution, add command line arguments as desired, place breakpoints at points of interest and launch your test executable with F5.

No source locations and traits are found for my tests
Test discovery is slow/does not seem to finish

You might consider using GTA's project settings to switch off symbol parsing and binary scanning for problematic test executables only, thus compromising between speed of test discovery and build maintainability.

The Test Explorer window can not be opened after installing GTA
The Google Test Adapter extension is disabled when I start Visual Studio

Development of Google Test Adapter

Please refer to our wiki.

Credits

People

Google Test Adapter is written and maintained by Christian Soltenborn.

The first version of GTA was a slightly enhanced C# port of the F# Google Test Runner, written by Markus Lindqvist. We have also learned a lot from the JavaScript test runner Chutzpah, written by Matthew Manela.

GTA has benefited from all kinds of contributions, be it feature requests, bug reports, code snippets, testing of new features and bugfixes, or even pull requests:

Organizations

Tools