googletest

How to catch segmentation fault with Google Test?

血红的双手。 提交于 2020-01-21 07:08:07
问题 How do I test that a function won't produce a segmentation fault? Here what I know right now, I can do: EXPECT_DEATH(foo(nullParameter)) In side the function, a segmentation fault is produce which is the behavior that I want to make fail. The snippet above will make the test pass because that is what is expected, the death of the process. Now, how can I make it fail? 回答1: Here's a function that will segfault if passed a null pointer argument and otherwise not: int deref(int * pint) { return

How to pass parameters to the gtest

喜夏-厌秋 提交于 2020-01-20 17:08:49
问题 How can I pass parameter to my test suites? gtest --number-of-input=5 I have the following main gtest code. And --number-of-input=5 should be passed to InitGoogleTest(). #include <iostream> #include <gtest/gtest.h> int main(int argc, char **argv) { std::cout << "Running main() from gtest_main.cc\n"; ::testing::GTEST_FLAG(output) = "xml:hello.xml"; testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } I don't know how to pass my parameter to the test suites/cases as follows? class

Google Mock unit testing static methods c++

孤街浪徒 提交于 2020-01-19 07:46:09
问题 I just started working on unit testing (using BOOST framework for testing, but for mocks I have to use Google Mock) and I have this situation : class A { static int Method1(int a, int b){return a+b;} }; class B { static int Method2(int a, int b){ return A::Method1(a,b);} }; So, I need to create mock class A, and to make my class B not to use real Method1 from A class, but to use mock. I'm not sure how to do this, and I could not find some similar example. 回答1: You could change class B into a

EXPECT_EQ Error

孤街醉人 提交于 2020-01-16 08:51:08
问题 I'm using the google test function EXPECT_EQ to run a test case for a function. The function, "find" returns a list and takes in a string of the name to find. Here's my test function: TEST_F(test_neighborhood, find) { list<Man> test; test.push_back(Man("username", "John", "Smith", 1, 1, "")); EXPECT_EQ(neighborhood.find("John"), test); } But when I try to "make", it gives me a long error /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/algorithm:665:71: error: invalid operands to

How to check if Google Test is running in my code

℡╲_俬逩灬. 提交于 2020-01-14 09:39:18
问题 I have a section of code that I would not like to run if it is being unit tested. I was hoping to find some #defined flag that is set by the gtest library that I can check. I couldn't find one that is used for that purpose, but after looking through the gtest header, I found one I thought I could use like this: SomeClass::SomeFunctionImUnitTesting() { // some code here #ifndef GTEST_NAME // some code I don't want to be tested here #endif // more code here } This doesn't seem to work as all

How to hide targets in Visual Studio from CMake

我是研究僧i 提交于 2020-01-13 08:48:09
问题 I am generating a .sln with CMake. I want to use Google Test and use that kind of code for adding a new tests: add_executable(my_test test/my_test.cpp) target_link_libraries(my_test gtest gmock_main) add_test(NAME my_test COMMAND my_test) It works fine, but when I open my .sln, I have all the targets appearing in the solution explorer: the libraries, the unit tests, etc. Is there a way to hide these target? 回答1: You can't do it explicitly only in cmake (ATM), but here is one way on how can

How to Test to cover all branchs of function casting char array to struct use Google test C language

落花浮王杯 提交于 2020-01-11 13:20:31
问题 I am practicing google test C. I have a bit of confusion when writing a test case for function_1 function. typedef struct { int a; //4 byte char b; //1 byte char c; //1 byte char d; //1 byte char arr[3]; //3 byte }ABCXYZ; static void function_1(char *abc) { if (abc != null) { ABCXYZ *name =(ABCXYZ *)(abc + 4); if (name->b == 1) printf("ONE"); else if (name->b == 2) printf("TWO"); else printf("OTHER"); } else { printf("INVALID"); } } I have written test case like this: TEST(Test_function_1,abc

How to call a private function via friend function?

℡╲_俬逩灬. 提交于 2020-01-11 09:56:06
问题 Hello I am trying to access a private member function is Gtest . The code looks somewhat similar to this. So, how can I access static void Pri_fun ? using namespace std; class test{ }; class abc{ public: friend class test; private: static void Pri_fun() { cout << "private fun called \n"; } }; int main() { abc ab; test *abd; abd->Pri_fun(); } 回答1: Since it's a static function, you should access it via the class name: abc::Pri_fun(); You should make a caller function though, or call it from the

how to use a .c file to write a test class in google test instead of .cpp file?

a 夏天 提交于 2020-01-11 07:53:13
问题 I have used googletest for my Android NDK project contain .c files. I have used a test class of the type .cpp to do the same. I want to use .c file instead. I get the following error when I try to use it : Running main() from gtest_main.cc [==========] Running 0 tests from 0 test cases. [==========] 0 tests from 0 test cases ran. (1 ms total) [ PASSED ] 0 tests. How can i solve this? 回答1: You cannot use a .c file to write a test class in googletest instead of a .cpp file. A .c file should

Googletest Eclipse C++ : How to have both test and production executable?

不羁的心 提交于 2020-01-10 19:53:11
问题 I have a basic question regarding Googletest in Eclipse. I am using the test-runner plug in to run the Googletests. But I need to specify a binary which runs my unit tests (of course that makes sense.) The problem is that in my project I now have two main functions, one to run the actual program and one int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } to run the google tests. Each time I want to run one I comment the other out, which of