[googletest] Where and how can I see the tests' output?

13,134 views
Skip to first unread message

xuxu

unread,
May 1, 2010, 2:58:10 AM5/1/10
to Google C++ Testing Framework
Hi,
I'm new here... I'm trying to write a test to a silly program but I
can't see the output
I've looked for some help and find this:

// Copyright 2009 Google Inc. All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
are
// met:
//
// * Redistributions of source code must retain the above
copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
//
// Author: vl...@google.com (Vlad Losev)

// This sample shows how to use Google Test listener API to implement
// an alternative console output and how to use the UnitTest
reflection API
// to enumerate test cases and tests and to inspect their results.

#include <stdio.h>

#include <gtest/gtest.h>

using ::testing::InitGoogleTest;
using ::testing::Test;
using ::testing::TestInfo;
using ::testing::TestPartResult;
using ::testing::UnitTest;
using ::testing::internal::EmptyTestEventListener;
using ::testing::internal::EventListeners;
using ::testing::internal::TestCase;

namespace testing {
namespace internal {

// TODO(vl...@google.com): Get rid of the accessor class once the API
is
// published.
class UnitTestAccessor {
public:
static bool Passed(const UnitTest& unit_test) { return
unit_test.Passed(); }
static EventListeners& listeners(UnitTest* unit_test) {
return unit_test->listeners();
}
static int GetTotalTestCaseCount(const UnitTest& unit_test) {
return unit_test.total_test_case_count();
}
static const TestCase* GetTestCase(const UnitTest& unit_test, int
index) {
return unit_test.GetTestCase(index);
}
};

} // namespace internal
} // namespace testing

using ::testing::internal::UnitTestAccessor;

namespace {

// Provides alternative output mode which produces minimal amount of
// information about tests.
class TersePrinter : public EmptyTestEventListener {
private:
// Called before any test activity starts.
virtual void OnTestProgramStart(const UnitTest& unit_test) {}

// Called after all test activities have ended.
virtual void OnTestProgramEnd(const UnitTest& unit_test) {
fprintf(stdout,
"TEST %s\n",
UnitTestAccessor::Passed(unit_test) ? "PASSED" :
"FAILED");
fflush(stdout);
}

// Called before a test starts.
virtual void OnTestStart(const TestInfo& test_info) {
fprintf(stdout,
"*** Test %s.%s starting.\n",
test_info.test_case_name(),
test_info.name());
fflush(stdout);
}

// Called after a test ends.
virtual void OnTestEnd(const TestInfo& test_info) {
fprintf(stdout,
"*** Test %s.%s ending.\n",
test_info.test_case_name(),
test_info.name());
fflush(stdout);
}

// Called after a failed assertion or a SUCCESS().
virtual void OnNewTestPartResult(const TestPartResult&
test_part_result) {
fprintf(stdout,
"%s in %s:%d\n%s\n",
test_part_result.failed() ? "*** Failure" : "Success",
test_part_result.file_name(),
test_part_result.line_number(),
test_part_result.summary());
fflush(stdout);
}
}; // class TersePrinter

TEST(CustomOutputTest, PrintsMessage) {
printf("Printing something from the test body...\n");
}

TEST(CustomOutputTest, Succeeds) {
SUCCEED() << "SUCCEED() has been invoked from here";
}

TEST(CustomOutputTest, Fails) {
EXPECT_EQ(1, 2)
<< "This test fails in order to demonstrate alternative failure
messages";
}

} // namespace

int main(int argc, char **argv) {
InitGoogleTest(&argc, argv);

bool terse_output = false;
if (argc > 1 && strcmp(argv[1], "--terse_output") == 0 )
terse_output = true;
else
printf("%s\n", "Run this program with --terse_output to change the
way "
"it prints its output.");

// If we are given the --terse_output command line flag, suppresses
the
// standard output and attaches own result printer.
if (terse_output) {
EventListeners& listeners = UnitTestAccessor::listeners(
UnitTest::GetInstance());

// Removes the default console output listener from the list so it
will
// not receive events from Google Test and won't print any output.
Since
// this operation transfers ownership of the listener to the
caller we
// have to delete it as well.
delete listeners.Release(listeners.default_result_printer());

// Adds the custom output listener to the list. It will now
receive
// events from Google Test and print the alternative output. We
don't
// have to worry about deleting it since Google Test assumes
ownership
// over it after adding it to the list.
listeners.Append(new TersePrinter);
}
int ret_val = RUN_ALL_TESTS();

// This is an example of using the UnitTest reflection API to
inspect test
// results. Here we discount failures from the tests we expected to
fail.
int unexpectedly_failed_tests = 0;
for (int i = 0;
i <
UnitTestAccessor::GetTotalTestCaseCount(*UnitTest::GetInstance());
++i) {
const TestCase* test_case = UnitTestAccessor::GetTestCase(
*UnitTest::GetInstance(), i);
for (int j = 0; j < test_case->total_test_count(); ++j) {
const TestInfo* test_info = test_case->GetTestInfo(j);
// Counts failed tests that were not meant to fail (those
without
// 'Fails' in the name).
if (test_info->result()->Failed() &&
strcmp(test_info->name(), "Fails") != 0) {
unexpectedly_failed_tests++;
}
}
}

// Test that were meant to fail should not affect the test program
outcome.
if (unexpectedly_failed_tests == 0)
ret_val = 0;

return ret_val;
}


but when I try to compile it I get an error:
Error 1 error C2731: 'main' : function cannot be overloaded
I can't find the reason ...

My main goal is to create a test using gtest and to see the result...

thanks for any help

Vlad Losev

unread,
May 3, 2010, 12:57:57 PM5/3/10
to Google C++ Testing Framework
Hi,
This file looks like gtest-listener_test.cc. Have you modified it in any way before trying to compile? If not, the problem is likely in the compiler options you used to build it. You can try to generate a project to build it with CMake and look up the options that project uses. To generate VC++ project files, create a directory for them, run your Visual Studio Command Prompt, cd into that directory and run

cmake <path_to_gtest_root> -Dbuild_all_gtest_tests

from there.

HTH,
Vlad

xuxu

unread,
May 7, 2010, 3:30:57 AM5/7/10
to Google C++ Testing Framework
Vlad,
thanks for your help but I found the problem it seem that that code is
already exists and I'm was trying to run it twice...
i understand the problem and work it out.
But, I have another question - how can I the output in XML file?
> cmake <*path_to_gtest_root>* -Dbuild_all_gtest_tests
>
> from there.
>
> HTH,
> Vlad

Vlad Losev

unread,
May 7, 2010, 11:31:19 AM5/7/10
to Google C++ Testing Framework
That should be described on this wiki page: http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide.

xuxu

unread,
May 8, 2010, 12:06:05 PM5/8/10
to Google C++ Testing Framework
thanks Vlad,
but I've been trying to do as the wiki says:
"To generate the XML report, set the GTEST_OUTPUT environment variable
or the --gtest_output flag to the string "xml:_path_to_output_file_",
which will create the file at the given location. You can also just
use the string "xml", in which case the output can be found in the
test_detail.xml file in the current directory. "

but when I change the gtest.cc file at the function
"UnitTestOptions::GetOutputFormat" the value of variable
"gtest_output_flag" from: GTEST_FLAG(output).c_str() to: "xml"
my computer crashed down...
I'm not sure that I'm doing things right and I don't know where I can
check what I'm doing


On May 7, 6:31 pm, Vlad Losev <v...@losev.com> wrote:
> That should be described on this wiki page:http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide.
>

Vlad Losev

unread,
May 8, 2010, 7:28:47 PM5/8/10
to Google C++ Testing Framework
On Sat, May 8, 2010 at 9:06 AM, xuxu <shlomi...@gmail.com> wrote:
thanks Vlad,
but I've been trying to do as the wiki says:
"To generate the XML report, set the GTEST_OUTPUT environment variable
or the --gtest_output flag to the string "xml:_path_to_output_file_",
which will create the file at the given location. You can also just
use the string "xml", in which case the output can be found in the
test_detail.xml file in the current directory. "

but when I change the gtest.cc file at the function
"UnitTestOptions::GetOutputFormat" the value of variable
"gtest_output_flag"  from: GTEST_FLAG(output).c_str() to: "xml"
my computer crashed down...
I'm not sure that I'm doing things right and I don't know where I can
check what I'm doing

You don't have to modify the source code. Just provide the --gtest_output command line flag to the test executable as explained there.

xuxu

unread,
May 14, 2010, 9:03:41 AM5/14/10
to Google C++ Testing Framework
So, where I need to change it?
it says :
set the GTEST_OUTPUT environment variable or the --gtest_output flag
to the string "xml:_path_to_output_file_"
Canyou help me finding the flag or the environment variable?




On May 9, 2:28 am, Vlad Losev <v...@losev.com> wrote:
> ...
>
> read more »

xuxu

unread,
May 21, 2010, 4:05:22 PM5/21/10
to Google C++ Testing Framework
OK I find the place....
now I have another question
where do I need to change the code so that the output won't be shown
on screen but will be printed in a file
thanks
> ...
>
> read more »

Vlad Losev

unread,
May 24, 2010, 12:48:27 AM5/24/10
to Google C++ Testing Framework
The easiest way to suppress console output is to redirect it to the null device:

test_binary >/dev/null (on Linux and the like)
or
test_binary.exe >nul (on Windows)

If you need to suppress the output from inside the program, see the wiki section on event listeners.
- Vlad

xuxu

unread,
May 24, 2010, 8:37:32 AM5/24/10
to Google C++ Testing Framework
I need that the same output that I got today on the srceen will be out
to a txt file
I'm using visual studio 2008, so where I need to write this:
> test_binary.exe >nul (on Windows)


On 24 מאי, 07:48, Vlad Losev <v...@losev.com> wrote:
> The easiest way to suppress console output is to redirect it to the null
> device:
>
> test_binary >/dev/null (on Linux and the like)
> or
> test_binary.exe >nul (on Windows)
>
> If you need to suppress the output from inside the program, see the wiki
> section on event
> listeners<http://code.google.com/p/googletest/wiki/GoogleTestAdvancedGuide#Exte...>
> .
> ...
>
> קרא עוד »

Vlad Losev

unread,
May 24, 2010, 11:56:34 AM5/24/10
to Google C++ Testing Framework
On Mon, May 24, 2010 at 5:37 AM, xuxu <shlomi...@gmail.com> wrote:
I need that the same output that I got today on the srceen will be out
to a txt file

Just google "output redirection".
 
I'm using visual studio 2008, so where I need to write this:
> test_binary.exe >nul (on Windows)

In your command line. 
Reply all
Reply to author
Forward
0 new messages