googletest

Google Unit Test Confusion

半腔热情 提交于 2019-12-02 10:03:59
I'm a bit confused about where the unit tests should be. All the documentation leads me to believe that I must create a Test Project. REALLY? With JUnit we just created a Test package (folder) inside our application project and the tests would run with every build. I'm new to C++ and trying to figure out how Google Test works. I've found a lot of really good questions and answers here on SO and Yes I have read the Google Test documentation but I'm still confused about this one thing: Can I create a subfolder in my project with all my unit tests, such that they will execute with each build? If

How to test input and output overloaded operator in C++ Gtest

纵饮孤独 提交于 2019-12-02 08:23:33
I am using following example from here Consider I have following class #include <iostream> class Distance { private: int feet; int inches; public: Distance() : feet(), inches() {} Distance(int f, int i) : feet(f), inches(i) {} friend std::ostream &operator<<( std::ostream &output, const Distance &D ) { output << "F : " << D.feet << " I : " << D.inches; return output; } friend std::istream &operator>>( std::istream &input, Distance &D ) { input >> D.feet >> D.inches; return input; } }; I am using Gtest to test this class. But I could not find better way to test it. I can use the macro provided

Confusion about unit tests (googletest) and projects/folder/files

风流意气都作罢 提交于 2019-12-02 05:36:26
It's the first time I want to use unit tests in my c++ project. Hence I've many existing classes for which I will write tests (at least for some of them). Further, the application has of course a main() function. I'm using qt creator with qmake but could also switch to cmake. The project, qt creator and qmake are working nicely. My confusion is now how do I add unit test? I intent to use googletest. I've already run a test in a new project, testing some dummy add(int, int) function, with everything in one file (function, tests and main). How does that work with an existing project (which has

Reducing output of gtest, to look similar to output of cxxtest

不羁岁月 提交于 2019-12-02 03:30:27
I have a set of unit tests using gtest as framework. Build, and execute, and the output looks like this : I'd like to have the output similar to the output when using cxxunit ( . when test pass, detail error when it fails). If possibly, with colors. How to do this? If I wasn't clear, I am looking for code and exact way how to do this. The output should be something like : Executing dummy_tests ..... ../code/app/unit_tests/unittest_dummy.cpp:25: Failure Value of: 2 Expected: 1 .. FAILED I am trying to use this example and create custom listener. But the example doesn't have colorful output, and

CMake: How to specify directory where ctest should look for executables?

谁说胖子不能爱 提交于 2019-12-01 18:13:09
问题 I wanted to integrate ctest to a c++/c project. I use google tests to write unit tests. Relevant part of my CMakeLists.txt looks like this: ... ####### CREATING EXE ####### add_executable(test_exe main.cpp test.cpp) target_link_libraries(test_exe GTest::GTest GTest::Main) set_target_properties (test_exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${UNIT_TEST_BIN_OUTPUT_DIR}) add_test(test_exe test_exe) As you can see i specified the output directory of my executable (UNIT_TEST_BIN_OUTPUT_DIR). The

How to initialize constant string for multiple tests in google test?

你说的曾经没有我的故事 提交于 2019-12-01 17:25:55
问题 I'm using google test and I have a cpp-file containing several tests. I would like to initialize a string with the current date and time when starting the first test. I would like to use this string in all other tests, too. How can I do this. I've tried the following ( m_string being a protected member of CnFirstTest ), but it didn't work (since the constructor and SetUp will be called before each test): CnFirstTest::CnFirstTest(void) { m_string = currentDateTime(); } void CnFirstTest::SetUp(

Convenient method in GoogleTest for a double comparison of not equal?

折月煮酒 提交于 2019-12-01 16:04:59
I'm looking for something similar to the ASSERT_EQ / ASSERT_NE for ASSERT_DOUBLE_EQ. Maybe I'm missing an easy way of doing this without having a ASSERT_DOUBLE_NE? You can use the companion mocking framework Google Mock. It has a powerful library of matchers (a la Hamcrest), which you can use with the EXPECT_THAT/ASSERT_THAT macros: EXPECT_THAT(value, FloatEq(1)); EXPECT_THAT(another_value, Not(DoubleEq(3.14))); It looks like you're out of luck. However, you could add one yourself. I built the following code using ASSERT_DOUBLE_EQ and ASSERT_NE as a pattern. #define ASSERT_DOUBLE_NE(expected,

GoogleTest PrintTo not getting called for a class

不想你离开。 提交于 2019-12-01 16:00:10
I'm having a rather strange problem telling googletest to print a certain class the way I want using PrintTo. The class is a very simple 2D point, it is in a namespace and the PrintTo function is in the same namespace. In fact, I have a derived class (a 3D point) which prints perfectly. Here's some code for the tests and PrintTo functions (namespace name edited, but everything else is copied and pasted from the actual code): // PrintTo Functions namespace MyNamespace { void PrintTo(const MyNamespace::CPunto2D& pto, ::std::ostream* os) { *os << "("; *os << pto.X(); *os << ","; *os << pto.Y();

How to get backtrace information from exception in googletest?

梦想与她 提交于 2019-12-01 12:58:04
I'm trying to do some semi test driven design, and occasionally when I implement a new feature, it will have an exception somewhere. All gtest tells me is what the exception is, and does not give me any backtrace information. If I run gdb --args --gtest_catch_exceptions=0, it will stop at the test with the exception but not have any backtrace information. It simply states: [ RUN ] TESTNAME.test_case EXCEPTION: exception description[Inferior 1 (process 30528) exited with code 0377] (gdb) bt No stack. Use catch throw gdb command to set special breakpoint before your exception is thrown. When it

Google Test separate project - How to get tests running against the C++ project

浪子不回头ぞ 提交于 2019-12-01 10:33:28
I am trying to figure out how to run Google Test against my C++ project using CMake. So far I have created a project called Simple and a Google Test project called SimpleTest. For the Simple Project Here's my CMakeLists.txt file: cmake_minimum_required(VERSION 2.8.4) project(Simple) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(SOURCE_FILES main.cpp NewCppClass.cpp NewCppClass.h) add_executable(Simple ${SOURCE_FILES}) Here's my main.cpp file: #include <iostream> #include "NewCppClass.h" using namespace std; int main() { NewCppClass newCppClass; int i = newCppClass.getNumberToTest();