Markdown 語法學習


codecov
codecov

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
Screenshot of Test Explorer

Features

  • stend
  • sea
  1. dae

History

Usage

Installation

Google Test Adapter can be installed in three ways:

  • Install through the Visual Studio Gallery at Tools/Extensions and Updates - search for Google Test Adapter. This will make sure that the extension is updated automatically
  • Download and launch the VSIX installer (which can also be downloaded from the Visual Studio Gallery)
  • Add a NuGet dependency to the Google test adapter nuget package to your Google Test projects. Note, however, that Visual Studio integration is limited this way: VS can discover and run tests, but no debugging, options or toolbar will be available; configuration is only possible through solution config files (see below).

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.

<a name="gta_configuration"></a>Configuration

GTA is configured following Visual Studio's approach of configuration inheritance. It provides different ways of configuration:

  • <a name="global_settings"></a>The Google Test Adapter section of Visual Studio's Tools/Options (not available if installed via NuGet). These options are referred to as global options in the following.
  • <a name="toolbar"></a>The GTA toolbar (not available if installed via NuGet). The most important runtime options (i.e., Parallel test execution, Break on failure, Catch exceptions, and Print test output) can also be set via a toolbar; this is equivalent to setting the according options via Tools/Options/Google Test Adapter.
  • <a name="solution_settings"></a>Solution settings files (not available if run via VsTest.Console.exe). They are provided by means of an XML configuration file; this allows sharing of settings via source control. The configuration file must be placed in the same folder as the solution's .sln file, and must have the same name as that file, but with extension .gta.runsettings. E.g., if the solution file's name is Foo.sln, the settings file must be named Foo.gta.runsettings.
  • Visual Studio user settings files. VS allows for the selection of test settings files via the Test/Test Settings menu.

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 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.

Assigning traits to tests

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

  • <a name="trait_macros"></a>You can make use of the custom test macros provided in GTA_Traits_1.8.0.h (or GTA_Traits_1.7.0.h if you are still on Google Test 1.7.0), which contain macros for all test types of the Google Test framework. The macros do not change behavior of the tests; they only add some information to the generated test code which encodes the traits assigned to the respective test. All GTA provided macros follow the same naming schema <Google Test macro>_TRAITS, where, obviously, <Google Test macro> is the name of the according macro in Google Test. Each test can be assigned up to 8 traits.
  • <a name="trait_regexes"></a>Combinations of regular expressions and traits can be specified under the GTA options: If a test's name matches one of these regular expressions, the according trait is assigned to that test.
  1. Te
  2. ABC

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

  1. Te
  2. ABC
  3. 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.
  4. 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).
  5. 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.

<a name="vstest_console"></a>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.

<a name="test_case_filters"></a>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:

  • DisplayName
  • FullyQualifiedName
  • Type
  • Author
  • TestCategory
  • Source (i.e., binary containing the test)
  • CodeFilePath (i.e., source file containing the test)
  • Class
  • LineNumber
  • Id
  • ExecutorUri

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".

<a name="parallelization"></a>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.

<a name="test_setup_and_teardown"></a>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.

<a name="trouble_shooting"></a>Trouble shooting

None or not all of my tests show up!

  • <a name="test_discovery_regex"></a>Switch on Debug mode at Tools/Options/Google Test Adapter/General, which will show on the test console whether your test executables are found by GTA. If they are not, you have two options:
    • Configure a Test discovery regex at the same place. In case of GTA installation via NuGet, do not forget to add the regex to the solution config file (which might be a good idea anyways).
    • If your test executable is ..\FooTests.exe, make sure that a file ..\FooTests.exe.is_google_test exists.
  • Your test executable can not run with command line option --gtest_list_tests, e.g. because it crashes. Make sure that your tests can be listed via command line; if they do not, debug your test executable, e.g. by making the according test project the startup project of your solution, and placing a breakpoint at the main method of your test executable.
  • If your project configuration contains references to DLLs which do not end up in the build directory (e.g. through Project/Properties/Linker/Input/Additional Dependencies), these DLLs will not be found when running your tests. Use option PATH extension to add the directories containing these DLLs to the test executables' PATH variable.
  • If your project happens to be a makefile project, there's a pitfall which will prevent GTA from discovering your tests: It appears that when importing a makefile project into VS, the Output setting of the project is populated from the makefile's content. However, if the makefile is edited later on such that the location of the generated test executable changes, VS does not find the test executable any more. One symptom of this is that your project can not be launched any more with F5. Make sure that the Output setting of the project is consistent with its makefile to avoid this problem.

No source locations and traits are found for my tests!

  • The test adapter is not able to find the pdb of your test executable, e.g. because it has been deleted or moved (and indicates that with a warning in the test output window). Rebuilding your solution should regenerate the pdb at an appropriate location.
  • The test executable's project settings result in a pdb file not containing the information necessary to resolve source locations and traits (see #46). Change the setting as indicated below and rebuild your solution.
    • VS 2015 and earlier: Yes or Optimize for debugging (/DEBUG)
    • VS 2017: Generate debug information optimized for sharing and publishing (/DEBUG:FULL)
  • Option Parse symbol information is set to false, making GTA not parse that information out of the pdb file intentionally. The actual set of options used is potentially composed from VS options, a solution settings file, and a user settings file; the resulting set of options will be logged to the test output window if the Print debug info option is set to true.

The Google Test Adapter extension is disabled when I start Visual Studio!

  • Your MEF cache might have been corrupted. Please refer to this issue for help.

Development of Google Test Adapter

Please refer to our wiki.

External links

Basic tutorial for using Google Test with GTA in Visual Studio

Credits

People

Tools

  • Google Test Runner - the starting point for GTA. We have ported the Google Test Runner extension from F# to C# and implemented various enhancements and refactorings.
  • ReSharper - awesome VS extension for .NET development, including refactoring, static analysis etc.
    • thanks to JetBrains for providing free licenses for our developers!
    • note that JetBrains' Resharper C++ can also run tests written using Google Test
  • AppVeyor - awesome .NET CI build services
    • thanks for providing free services and great support for open source projects!
  • Codecov - code coverage visualization facilities
    • thanks for providing free services for open source projects!
  • CommonMark.NET - open source Markdown to HTML converter
  • NRegFreeCom - open source library for accessing unregistered COM objects
  • NamedPipeWrapper - open source library for easily working with Named Pipes
  • OpenCover - open source .NET code coverage
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末妹沙,一起剝皮案震驚了整個濱河市掷空,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌饱搏,老刑警劉巖返吻,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件勋又,死亡現(xiàn)場離奇詭異玄柏,居然都是意外死亡畸写,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進店門腊敲,熙熙樓的掌柜王于貴愁眉苦臉地迎上來击喂,“玉大人,你說我怎么就攤上這事碰辅《海” “怎么了?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵没宾,是天一觀的道長凌彬。 經常有香客問我沸柔,道長,這世上最難降的妖魔是什么铲敛? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任褐澎,我火速辦了婚禮,結果婚禮上伐蒋,老公的妹妹穿的比我還像新娘工三。我一直安慰自己,他們只是感情好先鱼,可當我...
    茶點故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布俭正。 她就那樣靜靜地躺著,像睡著了一般焙畔。 火紅的嫁衣襯著肌膚如雪掸读。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天宏多,我揣著相機與錄音寺枉,去河邊找鬼。 笑死绷落,一個胖子當著我的面吹牛姥闪,可吹牛的內容都是我干的。 我是一名探鬼主播砌烁,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼筐喳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了函喉?” 一聲冷哼從身側響起避归,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎管呵,沒想到半個月后梳毙,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡捐下,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年账锹,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片坷襟。...
    茶點故事閱讀 38,100評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡奸柬,死狀恐怖,靈堂內的尸體忽然破棺而出婴程,到底是詐尸還是另有隱情廓奕,我是刑警寧澤,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布,位于F島的核電站桌粉,受9級特大地震影響蒸绩,放射性物質發(fā)生泄漏。R本人自食惡果不足惜铃肯,卻給世界環(huán)境...
    茶點故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一患亿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧缘薛,春花似錦窍育、人聲如沸卡睦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽表锻。三九已至恕齐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間瞬逊,已是汗流浹背显歧。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留确镊,地道東北人士骤。 一個月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像蕾域,于是被迫代替她去往敵國和親拷肌。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,834評論 2 345

推薦閱讀更多精彩內容