googletest

What is the best way of testing private methods with GoogleTest? [closed]

瘦欲@ 提交于 2019-11-29 20:11:08
I would like to test some private methods using GoogleTest. class Foo { private: int bar(...) } GoogleTest allows a couple of ways of doing this. OPTION 1 With FRIEND_TEST : class Foo { private: FRIEND_TEST(Foo, barReturnsZero); int bar(...); } TEST(Foo, barReturnsZero) { Foo foo; EXPECT_EQ(foo.bar(...), 0); } This implies to include "gtest/gtest.h" in the production source file. OPTION 2 Declare a test fixture as a friend to the class and define accessors in the fixture: class Foo { friend class FooTest; private: int bar(...); } class FooTest : public ::testing::Test { protected: int bar(...)

Visual Studio C++: Unit test exe project with google test?

痴心易碎 提交于 2019-11-29 19:55:12
问题 Using Visual Studio 2010 C++. I'm experimenting with unit testing and decided to try Google Test (gtest). I have an existing project which compiles to an MFC executable (I'm also interested in how to test a project that compiles to a DLL). My understanding of the convention for unit testing is that you should create a new separate project for your tests. So I created a new project in the same solution for my unit tests. But how do I link the projects? Can I test arbitrary functions/methods of

googletest Undefined symbols for architecture x86_64 error

风流意气都作罢 提交于 2019-11-29 16:54:26
Let GTEST_DIR be the environment variable storing the path to the googletest directory. (I cloned googletest-master from googletest's github repo .) I cd 'ed into $GTEST_DIR , did a mkdir build && cd build , then executed the following command : cmake .. -DCMAKE_C_COMPILER=$GNU-6.0.0/bin/gcc-6.0.0 -DCMAKE_CXX_COMPILER=$GNU-6.0.0/bin/g++-6.0.0 where GNU-6.0.0 is the path to my gnu install. This generated a Makefile inside $GTEST_DIR/build that I tweaked as follows : I've added CC = $GNU-6.0.0/bin/gcc-6.0.0 CXX = $GNU-6.0.0/bin/g++-6.0.0 at its beginning, to be sure that the c and c++ compilers

Gtest: Undefined References

别等时光非礼了梦想. 提交于 2019-11-29 16:52:59
问题 I am trying to use GoogleTest to test a simple function, but as I run make in my build folder, the compiler throws Undefined Reference error messages at me. I've referenced the gtest header file, so I'm not sure what is wrong. Any ideas? I'm new to the entire subject of both unix and unit testing , so I could very well be missing something simple. Thanks in advance! Error Messages: CMakeFiles/Proj2.dir/main.cpp.o: In function `main': main.cpp:(.text+0x1e): undefined reference to `testing:

Uninteresting mock function call bla() && Expected: to be called at least once bla()?

僤鯓⒐⒋嵵緔 提交于 2019-11-29 14:31:00
I've written a small test with a mocked class. When I run it, first I get the warning that an uninteresting mock function was called and then the test fails because the expectation is not met, which is that the mocked function is called at least once. The funny thing is that that function is called as I see that warning message above. Do you have any ideas on this matter? Thank you! Edit: This is my code structure: class Bla { public: Bla(); virtual ~Bla(); virtual float myFunction(); } class MockBla : public Bla { MockBla(); ~MockBla(); MOCKMETHOD0(myFunction, float()); } class CallerClass {

Google Test: Is there a way to combine a test which is both type parameterized and value parameterized?

妖精的绣舞 提交于 2019-11-29 10:56:23
问题 I know how to develop a type-parameterized test and value-parameterized test separately. What I am trying to figure out is if it's possible to combine both. In other words, create a generic test which takes any type and range of values for that type. 回答1: There isn't any ready-to-wear combination of type-parameterized tests and value-parameterized tests. The googletest developers have been asked the question and they said No. However, there is a routine and simple way (as suggested by

Data-driven unit tests with google test

末鹿安然 提交于 2019-11-29 10:34:18
I am currently writing unit tests for an embedded application using googles unit test framework . Now my boss got upset that the data I test with (i.e. the values with which I call methods of the class under test) is hard wired in the tests. He requests to have this data read-in from a file. His argument is that it would thus be easier to add another test for a corner case that was previously forgotten. I am not that experienced with unit tests but so far that was not how I did it. So I tried to figure out what would be the best way to do it - even if it is a good idea to do it at all. I

pass method with template arguments to a macro

房东的猫 提交于 2019-11-29 09:09:08
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 make a lot of sense, of course this shoud not throw, but it is what stayed after simplifying what I had.) I get this output from MS VC++ 2008: warning C4002: too many actual parameters for macro 'ASSERT_THROW' error C2143: syntax error : missing ',' before ';' Whereas there are no problems with: ASSERT_THROW(Matrix<1>(), std::runtime_error); How can

Generate Google C++ Unit Test XML Report

纵饮孤独 提交于 2019-11-29 06:07:32
问题 I am new to using Google test framework for unit testing and am intending to generate an XML report of the tests or the command output as a report (I could just print it obviously). I have read up on Generate XML Report , but haven't been able to understand clearly on how to go about generating the report. Any help would be greatly appreciated. Cheers. 回答1: For Linux environments: It's simple you just have to set the GTEST_OUTPUT environment variable like this: export GTEST_OUTPUT="xml:/home

GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope

南笙酒味 提交于 2019-11-29 03:24:41
GoogleTest 1.6 with Cygwin 1.7: 'fileno' was not declared in this scope Error message when building a simple test on Factorial() function in Eclipse CDT: Invoking: Cygwin C++ Compiler g++ -std=c++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp" In file included from E:\source\gtest-1.6.0\include/gtest/internal/gtest-internal.h:40:0, from E:\source\gtest-1.6.0\include/gtest/gtest.h:57, from ../src/challenge.cpp:11: E:\source\gtest-1.6.0\include/gtest