Skip to content

Commit

Permalink
Merge 2ce0685 into 75e8347
Browse files Browse the repository at this point in the history
Closes #1544
With refinements and changes

PiperOrigin-RevId: 215273083
  • Loading branch information
Arseny Aprelev authored and gennadiycivil committed Oct 2, 2018
1 parent 2e91bbc commit 00938b2
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 26 deletions.
1 change: 1 addition & 0 deletions googletest/CMakeLists.txt
Expand Up @@ -217,6 +217,7 @@ if (gtest_build_tests)
test/gtest-typed-test2_test.cc)
cxx_test(gtest_unittest gtest_main)
cxx_test(gtest-unittest-api_test gtest)
cxx_test(gtest_skip_test gtest_main)

############################################################
# C++ tests built with non-standard compiler flags.
Expand Down
12 changes: 8 additions & 4 deletions googletest/include/gtest/gtest-test-part.h
Expand Up @@ -53,7 +53,8 @@ class GTEST_API_ TestPartResult {
enum Type {
kSuccess, // Succeeded.
kNonFatalFailure, // Failed but the test can continue.
kFatalFailure // Failed and the test should be terminated.
kFatalFailure, // Failed and the test should be terminated.
kSkip // Skipped.
};

// C'tor. TestPartResult does NOT have a default constructor.
Expand Down Expand Up @@ -89,18 +90,21 @@ class GTEST_API_ TestPartResult {
// Gets the message associated with the test part.
const char* message() const { return message_.c_str(); }

// Returns true iff the test part was skipped.
bool skipped() const { return type_ == kSkip; }

// Returns true iff the test part passed.
bool passed() const { return type_ == kSuccess; }

// Returns true iff the test part failed.
bool failed() const { return type_ != kSuccess; }

// Returns true iff the test part non-fatally failed.
bool nonfatally_failed() const { return type_ == kNonFatalFailure; }

// Returns true iff the test part fatally failed.
bool fatally_failed() const { return type_ == kFatalFailure; }

// Returns true iff the test part failed.
bool failed() const { return fatally_failed() || nonfatally_failed(); }

private:
Type type_;

Expand Down
24 changes: 23 additions & 1 deletion googletest/include/gtest/gtest.h
Expand Up @@ -440,6 +440,9 @@ class GTEST_API_ Test {
// Returns true iff the current test has a non-fatal failure.
static bool HasNonfatalFailure();

// Returns true iff the current test was skipped.
static bool IsSkipped();

// Returns true iff the current test has a (either fatal or
// non-fatal) failure.
static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); }
Expand Down Expand Up @@ -574,7 +577,10 @@ class GTEST_API_ TestResult {
int test_property_count() const;

// Returns true iff the test passed (i.e. no test part failed).
bool Passed() const { return !Failed(); }
bool Passed() const { return !Skipped() && !Failed(); }

// Returns true iff the test was skipped.
bool Skipped() const;

// Returns true iff the test failed.
bool Failed() const;
Expand Down Expand Up @@ -854,6 +860,9 @@ class GTEST_API_ TestCase {
// Gets the number of successful tests in this test case.
int successful_test_count() const;

// Gets the number of skipped tests in this test case.
int skipped_test_count() const;

// Gets the number of failed tests in this test case.
int failed_test_count() const;

Expand Down Expand Up @@ -936,6 +945,11 @@ class GTEST_API_ TestCase {
return test_info->should_run() && test_info->result()->Passed();
}

// Returns true iff test skipped.
static bool TestSkipped(const TestInfo* test_info) {
return test_info->should_run() && test_info->result()->Skipped();
}

// Returns true iff test failed.
static bool TestFailed(const TestInfo* test_info) {
return test_info->should_run() && test_info->result()->Failed();
Expand Down Expand Up @@ -1258,6 +1272,9 @@ class GTEST_API_ UnitTest {
// Gets the number of successful tests.
int successful_test_count() const;

// Gets the number of skipped tests.
int skipped_test_count() const;

// Gets the number of failed tests.
int failed_test_count() const;

Expand Down Expand Up @@ -1835,6 +1852,11 @@ class TestWithParam : public Test, public WithParamInterface<T> {

// Macros for indicating success/failure in test code.

// Skips test in runtime.
// Skipping test aborts current function.
// Skipped tests are neither successful nor failed.
#define GTEST_SKIP() GTEST_SKIP_("Skipped")

// ADD_FAILURE unconditionally adds a failure to the current test.
// SUCCEED generates a success - it doesn't automatically make the
// current test successful, as a test is only successful when it has
Expand Down
5 changes: 4 additions & 1 deletion googletest/include/gtest/internal/gtest-internal.h
Expand Up @@ -1208,7 +1208,10 @@ class NativeArray {
#define GTEST_SUCCESS_(message) \
GTEST_MESSAGE_(message, ::testing::TestPartResult::kSuccess)

// Suppress MSVC warning 4702 (unreachable code) for the code following
#define GTEST_SKIP_(message) \
return GTEST_MESSAGE_(message, ::testing::TestPartResult::kSkip)

// Suppress MSVC warning 4072 (unreachable code) for the code following
// statement if it returns or throws (or doesn't return or throw in some
// situations).
#define GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement) \
Expand Down
3 changes: 3 additions & 0 deletions googletest/src/gtest-internal-inl.h
Expand Up @@ -544,6 +544,9 @@ class GTEST_API_ UnitTestImpl {
// Gets the number of successful tests.
int successful_test_count() const;

// Gets the number of skipped tests.
int skipped_test_count() const;

// Gets the number of failed tests.
int failed_test_count() const;

Expand Down
16 changes: 10 additions & 6 deletions googletest/src/gtest-test-part.cc
Expand Up @@ -47,12 +47,16 @@ std::string TestPartResult::ExtractSummary(const char* message) {

// Prints a TestPartResult object.
std::ostream& operator<<(std::ostream& os, const TestPartResult& result) {
return os
<< result.file_name() << ":" << result.line_number() << ": "
<< (result.type() == TestPartResult::kSuccess ? "Success" :
result.type() == TestPartResult::kFatalFailure ? "Fatal failure" :
"Non-fatal failure") << ":\n"
<< result.message() << std::endl;
return os << result.file_name() << ":" << result.line_number() << ": "
<< (result.type() == TestPartResult::kSuccess
? "Success"
: result.type() == TestPartResult::kSkip
? "Skipped"
: result.type() == TestPartResult::kFatalFailure
? "Fatal failure"
: "Non-fatal failure")
<< ":\n"
<< result.message() << std::endl;
}

// Appends a TestPartResult to the array.
Expand Down
90 changes: 81 additions & 9 deletions googletest/src/gtest.cc
Expand Up @@ -796,6 +796,11 @@ int UnitTestImpl::successful_test_count() const {
return SumOverTestCaseList(test_cases_, &TestCase::successful_test_count);
}

// Gets the number of skipped tests.
int UnitTestImpl::skipped_test_count() const {
return SumOverTestCaseList(test_cases_, &TestCase::skipped_test_count);
}

// Gets the number of failed tests.
int UnitTestImpl::failed_test_count() const {
return SumOverTestCaseList(test_cases_, &TestCase::failed_test_count);
Expand Down Expand Up @@ -2207,6 +2212,16 @@ void TestResult::Clear() {
elapsed_time_ = 0;
}

// Returns true off the test part was skipped.
static bool TestPartSkipped(const TestPartResult& result) {
return result.skipped();
}

// Returns true iff the test was skipped.
bool TestResult::Skipped() const {
return !Failed() && CountIf(test_part_results_, TestPartSkipped) > 0;
}

// Returns true iff the test failed.
bool TestResult::Failed() const {
for (int i = 0; i < total_part_count(); ++i) {
Expand Down Expand Up @@ -2537,6 +2552,11 @@ bool Test::HasNonfatalFailure() {
HasNonfatalFailure();
}

// Returns true iff the current test was skipped.
bool Test::IsSkipped() {
return internal::GetUnitTestImpl()->current_test_result()->Skipped();
}

// class TestInfo

// Constructs a TestInfo object. It assumes ownership of the test factory
Expand Down Expand Up @@ -2715,6 +2735,11 @@ int TestCase::successful_test_count() const {
return CountIf(test_info_list_, TestPassed);
}

// Gets the number of successful tests in this test case.
int TestCase::skipped_test_count() const {
return CountIf(test_info_list_, TestSkipped);
}

// Gets the number of failed tests in this test case.
int TestCase::failed_test_count() const {
return CountIf(test_info_list_, TestFailed);
Expand Down Expand Up @@ -2866,6 +2891,8 @@ static std::string FormatTestCaseCount(int test_case_count) {
// between the two when viewing the test result.
static const char * TestPartResultTypeToString(TestPartResult::Type type) {
switch (type) {
case TestPartResult::kSkip:
return "Skipped";
case TestPartResult::kSuccess:
return "Success";

Expand Down Expand Up @@ -3119,6 +3146,7 @@ class PrettyUnitTestResultPrinter : public TestEventListener {

private:
static void PrintFailedTests(const UnitTest& unit_test);
static void PrintSkippedTests(const UnitTest& unit_test);
};

// Fired before each iteration of tests starts.
Expand Down Expand Up @@ -3187,18 +3215,25 @@ void PrettyUnitTestResultPrinter::OnTestStart(const TestInfo& test_info) {
// Called after an assertion failure.
void PrettyUnitTestResultPrinter::OnTestPartResult(
const TestPartResult& result) {
// If the test part succeeded, we don't need to do anything.
if (result.type() == TestPartResult::kSuccess)
return;

// Print failure message from the assertion (e.g. expected this and got that).
PrintTestPartResult(result);
fflush(stdout);
switch (result.type()) {
// If the test part succeeded, or was skipped,
// we don't need to do anything.
case TestPartResult::kSkip:
case TestPartResult::kSuccess:
return;
default:
// Print failure message from the assertion
// (e.g. expected this and got that).
PrintTestPartResult(result);
fflush(stdout);
}
}

void PrettyUnitTestResultPrinter::OnTestEnd(const TestInfo& test_info) {
if (test_info.result()->Passed()) {
ColoredPrintf(COLOR_GREEN, "[ OK ] ");
} else if (test_info.result()->Skipped()) {
ColoredPrintf(COLOR_GREEN, "[ SKIPPED ] ");
} else {
ColoredPrintf(COLOR_RED, "[ FAILED ] ");
}
Expand Down Expand Up @@ -3248,7 +3283,7 @@ void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {
}
for (int j = 0; j < test_case.total_test_count(); ++j) {
const TestInfo& test_info = *test_case.GetTestInfo(j);
if (!test_info.should_run() || test_info.result()->Passed()) {
if (!test_info.should_run() || !test_info.result()->Failed()) {
continue;
}
ColoredPrintf(COLOR_RED, "[ FAILED ] ");
Expand All @@ -3259,6 +3294,30 @@ void PrettyUnitTestResultPrinter::PrintFailedTests(const UnitTest& unit_test) {
}
}

// Internal helper for printing the list of skipped tests.
void PrettyUnitTestResultPrinter::PrintSkippedTests(const UnitTest& unit_test) {
const int skipped_test_count = unit_test.skipped_test_count();
if (skipped_test_count == 0) {
return;
}

for (int i = 0; i < unit_test.total_test_case_count(); ++i) {
const TestCase& test_case = *unit_test.GetTestCase(i);
if (!test_case.should_run() || (test_case.skipped_test_count() == 0)) {
continue;
}
for (int j = 0; j < test_case.total_test_count(); ++j) {
const TestInfo& test_info = *test_case.GetTestInfo(j);
if (!test_info.should_run() || !test_info.result()->Skipped()) {
continue;
}
ColoredPrintf(COLOR_GREEN, "[ SKIPPED ] ");
printf("%s.%s", test_case.name(), test_info.name());
printf("\n");
}
}
}

void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
int /*iteration*/) {
ColoredPrintf(COLOR_GREEN, "[==========] ");
Expand All @@ -3273,6 +3332,13 @@ void PrettyUnitTestResultPrinter::OnTestIterationEnd(const UnitTest& unit_test,
ColoredPrintf(COLOR_GREEN, "[ PASSED ] ");
printf("%s.\n", FormatTestCount(unit_test.successful_test_count()).c_str());

const int skipped_test_count = unit_test.skipped_test_count();
if (skipped_test_count > 0) {
ColoredPrintf(COLOR_GREEN, "[ SKIPPED ] ");
printf("%s, listed below:\n", FormatTestCount(skipped_test_count).c_str());
PrintSkippedTests(unit_test);
}

int num_failures = unit_test.failed_test_count();
if (!unit_test.Passed()) {
const int failed_test_count = unit_test.failed_test_count();
Expand Down Expand Up @@ -4540,6 +4606,11 @@ int UnitTest::successful_test_count() const {
return impl()->successful_test_count();
}

// Gets the number of skipped tests.
int UnitTest::skipped_test_count() const {
return impl()->skipped_test_count();
}

// Gets the number of failed tests.
int UnitTest::failed_test_count() const { return impl()->failed_test_count(); }

Expand Down Expand Up @@ -4660,7 +4731,8 @@ void UnitTest::AddTestPartResult(
impl_->GetTestPartResultReporterForCurrentThread()->
ReportTestPartResult(result);

if (result_type != TestPartResult::kSuccess) {
if (result_type != TestPartResult::kSuccess &&
result_type != TestPartResult::kSkip) {
// gtest_break_on_failure takes precedence over
// gtest_throw_on_failure. This allows a user to set the latter
// in the code (perhaps in order to use Google Test assertions
Expand Down

0 comments on commit 00938b2

Please sign in to comment.