1

So I'm pretty new to C++ unit testing, I have worked pretty much exclusively with JUnit. I've been playing around with Google Test on Xcode for a bit now but for some reason when I try to print out failure messages they never show up on the console. Here is the code:

#include "MyFuncs.h"
#include "gtest/gtest.h"    

TEST(MinTest, Positive) {
        EXPECT_TRUE(findMin(1,2) == 2) << "I'm not printing";
    }

I've tried to research why and I can't seem to find the answer which I suspect might be obvious. I've tried adding a new line character and the end and using endl to try to flush the output but that also did not work. Printing to stdout and stderr works just fine within the function, so my question is pretty much if anyone knows why this isn't working?

And, if you are wondering, I have included these in MyFuncs.h:

#include <iostream>
using namespace std;

Edit: Here is the code for findMin

int findMin(int a, int b) {
    return a<b ? a : b;
}

And here it the Google Test output:

[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from MinTest
[ RUN      ] MinTest.Positive
/Users/asocik/Desktop/Testing/Testing/mathTest.cpp:6: Failure

[  FAILED  ] MinTest.Positive (0 ms)
[----------] 1 test from MinTest (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] MinTest.Positive

 1 FAILED TEST
user2821731
  • 33
  • 1
  • 7
  • Show the output of gtest. Are you sure, your test is invoking? Show the definition of `findMin()`. – Alexey Shmalko Aug 10 '14 at 21:47
  • What is the implementation of `findMin` and why is it better to use that instead of `std::min`? – tillaert Aug 10 '14 at 21:50
  • Updated the question to include the implementation. There is no better use for findMin(), I'm just using it as a small test while exploring Google Test. – user2821731 Aug 10 '14 at 21:54
  • I would avoid using `using namespace std;` in any header. This can cause big problems. For example when somebody else uses `using namespace boost;`. These things are very unpredictable and very hard to debug. Move it from `MyFuncs.h` to your `.cpp` file. It might also mess up things in the `gtest/gtest.h` file. – tillaert Aug 10 '14 at 21:56
  • 1
    Just tried moving using namespace std. The message still does not print but thanks for the advice, might save me a headache in the future. – user2821731 Aug 10 '14 at 22:00
  • For me output looks like `..../my_test.cpp:70: Failure Value of: 0 == 2 Actual: false Expected: true My print message` – Alexey Shmalko Aug 10 '14 at 22:27
  • I don't understand why, maybe it's an Xcode thing but I followed all of the instructions in Google's guide: https://code.google.com/p/googletest/wiki/XcodeGuide – user2821731 Aug 10 '14 at 23:05
  • Try to run tests from command line – Alexey Shmalko Aug 10 '14 at 23:10
  • I still get the same result on the command line. – user2821731 Aug 10 '14 at 23:35
  • I know this is an old post, but i'm running in to the same problem. Gtest prints all the failed test messages to a "test-suite.log" file, instead of the console. It used to print to the console, but no more. Could this be some sort of ENV flag that gtest is looking for? It started happening when I started running the test on additional environments. – shfnet Aug 09 '20 at 12:59

0 Answers0