Questions tagged [googletest]

Google's C++ testing framework based on xUnit that runs on multiple platforms.

GoogleTest is an open source unit testing framework developed by Google. It also supports unit testing of C code.

GTest (as it is commonly known) supports various platforms including Linux, OS X, Windows and Symbian. It provides basic unit testing features in the xUnit model in the form ASSERT_EQ() or EXPECT_EQ(), with automatic test registration. It also provides advanced features such as death tests, shuffling and sharding of tests, and Hudson/Jenkins-compatible XML output.

GTest integrates well with gmock, a C++ mocking framework.

There are three GTest related open source projects which can enhance the GTest user experience:

  1. Google Test UI - provides a GUI for running gtest binary and displaying the results.
  2. GTest TAP listener - Event listener for GTest based on TAP, for test result output.
  3. gtpp - Google Test Pretty Printer, offering shorter and clearer console test output
2773 questions
25
votes
2 answers

How to specify consecutive returns in gmock?

In Mockito we can specify multiple returns like (taken from here): //you can set different behavior for consecutive method calls. //Last stubbing (e.g: thenReturn("foo")) determines the behavior of further consecutive calls. …
Paymahn Moghadasian
  • 9,301
  • 13
  • 56
  • 94
25
votes
2 answers

How can I get code coverage using gtest on Windows?

I am using gtest for testing my code in C++ with Visual studio 2010. But I could not able to makeout that I have reached 100% code coverage. To make sure that I have covered 100% code coverage, I would like to know that, is there any way to find out…
24
votes
4 answers

pass method with template arguments to a macro

I am unable to use Google Test's ASSERT_THROW() macro in combination with multiple template arguments. Consider that I want to make sure that construction of Matrix<5,1> throws: ASSERT_THROW(Matrix<5,1>(), std::runtime_error); (this example doesn't…
Philipp
  • 11,549
  • 8
  • 66
  • 126
23
votes
1 answer

How to run tests and debug Google Test project in VS Code?

I want to run the sample tests and debug the Google Test project. I am using VS Code on Ubuntu 16.04 LTS. I cloned the project locally at /home/user/Desktop/projects/cpp/googletest, created a new directory called mybuild at…
Stupid Man
  • 885
  • 2
  • 14
  • 31
23
votes
4 answers

How to mock method with optional parameter in Google Mock?

How to mock a method with optional parameter in Google Mock? For example: class A { public: void set_enable( bool enabled = true ); }; class MockA : public A { MOCK_METHOD1( set_enable, void( bool ) ); // this is not working };
nyarlathotep108
  • 5,275
  • 2
  • 26
  • 64
23
votes
2 answers

Google Test Fixtures

I'm trying to understand how the Google Test Fixtures work. Say I have the following code: class PhraseTest : public ::testing::Test { protected: virtual void SetUp() { phraseClass * myPhrase1 = new…
bei
  • 933
  • 3
  • 9
  • 17
23
votes
5 answers

undefined reference to `pthread_key_create' (linker error)

I have downloaded gtest 1.7.0 sources from here: https://code.google.com/p/googletest/downloads/list and build the gtest .a files (lib files) on ubuntu 13.10: Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64…
user3165964
  • 327
  • 1
  • 3
  • 8
22
votes
1 answer

How to use GoogleMock in Visual Studio?

This is going to be a self-answered, FAQ-style question. See answer below. With Visual Studio 2017/2019 it is really easy to set up a new Google Test project and start writing tests (as long as you don't mind using older versions of GoogleTest…
Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
22
votes
7 answers

How to set $(OutDir), $(TargetName), $(TargetExt), and %(Lib.OutputFile) with Visual Studio?

I'm trying to build gtest on Visual Studio 2010. After converting the solution file, I tried to build, and I got the following warning messages. Warning 1 warning MSB8012: TargetPath(C:\Users\sucho\Desktop\gtest-1.5.0\msvc\gtest/Debug\gtest.lib)…
prosseek
  • 182,215
  • 215
  • 566
  • 871
22
votes
3 answers

C++ project with Bazel and GTest

I want to create a Bazel C++ project with gtest for unit tests. What is the minimal setup? (I only have Bazel installed on my computer and I am running under Linux)
Picaud Vincent
  • 10,518
  • 5
  • 31
  • 70
21
votes
2 answers

Unit Test Output & Project Structure Advice --- CMake + Google Test Framework

I'm new to CMake and I'm using the Google Test Framework. I've looked for more complex examples of combining CMake and the Google testing framework, but I've not turned up much luck. I was hoping that someone could provide their opinion on the setup…
Landon
  • 561
  • 1
  • 3
  • 10
21
votes
4 answers

_stricmp with mingw and c++0x not existent?

I'm currently trying to use googletest with MinGW and -std=c++0x but it complains that _stricmp is not declared in this scope which it doesn't when I do not use -std=c++0x. I have no idea what _stricmp is, I just found out that it is defined in…
DaVinci
  • 1,391
  • 2
  • 12
  • 27
21
votes
2 answers

CMake Error: "add_subdirectory not given a binary directory"

I am trying to integrate Google Test into the subproject of bigger project and I cannot find the solution that would be satisfying for me. I have two constraints: the source code of Google Test is already somewhere in the project structure (thus…
Filip
  • 333
  • 1
  • 2
  • 9
21
votes
1 answer

How to clone and integrate external (from git) cmake project into local one

I faced with a problem when I was trying to use Google Test. There are lot of manuals on how to use ExternalProject_Add for the adding gtest into the project, however most of these describe a method based on downloading zip archive with gtest and…
amigo421
  • 2,429
  • 4
  • 26
  • 55
21
votes
2 answers

Google test - before class

I'm running google test. I need something like Before class. I have the SetUp() and TearDown() functions, but they run before and after each test. Now I need something global - like ctor, that should run only one time when the class loaded.
Rat
  • 357
  • 2
  • 5
  • 16