Makes --gtest_list_tests honor the test filter (by Jay Campan).

This commit is contained in:
zhanyong.wan 2009-04-24 20:27:29 +00:00
parent f2d0d0e3d5
commit fa2b06c52f
5 changed files with 86 additions and 41 deletions

View File

@ -376,7 +376,12 @@ class TestInfo {
// Returns the test comment.
const char* comment() const;
// Returns true if this test should run.
// Returns true if this test matches the user-specified filter.
bool matches_filter() const;
// Returns true if this test should run, that is if the test is not disabled
// (or it is disabled but the also_run_disabled_tests flag has been specified)
// and its full name matches the user-specified filter.
//
// Google Test allows the user to filter the tests by their full names.
// The full name of a test Bar in test case Foo is defined as

View File

@ -621,6 +621,12 @@ class TestInfoImpl {
// Sets the is_disabled member.
void set_is_disabled(bool is) { is_disabled_ = is; }
// Returns true if this test matches the filter specified by the user.
bool matches_filter() const { return matches_filter_; }
// Sets the matches_filter member.
void set_matches_filter(bool matches) { matches_filter_ = matches; }
// Returns the test case name.
const char* test_case_name() const { return test_case_name_.c_str(); }
@ -667,6 +673,8 @@ class TestInfoImpl {
const TypeId fixture_class_id_; // ID of the test fixture class
bool should_run_; // True iff this test should run
bool is_disabled_; // True iff this test is disabled
bool matches_filter_; // True if this test matches the
// user-specified filter.
internal::TestFactoryBase* const factory_; // The factory that creates
// the test object
@ -1164,8 +1172,8 @@ class UnitTestImpl {
// Returns the number of tests that should run.
int FilterTests(ReactionToSharding shard_tests);
// Lists all the tests by name.
void ListAllTests();
// Prints the names of the tests matching the user-specified filter flag.
void ListTestsMatchingFilter();
const TestCase* current_test_case() const { return current_test_case_; }
TestInfo* current_test_info() { return current_test_info_; }

View File

@ -2172,6 +2172,9 @@ const char* TestInfo::comment() const {
// Returns true if this test should run.
bool TestInfo::should_run() const { return impl_->should_run(); }
// Returns true if this test matches the user-specified filter.
bool TestInfo::matches_filter() const { return impl_->matches_filter(); }
// Returns the result of the test.
const internal::TestResult* TestInfo::result() const { return impl_->result(); }
@ -3297,8 +3300,8 @@ void UnitTest::AddTestPartResult(TestPartResultType result_type,
ReportTestPartResult(result);
if (result_type != TPRT_SUCCESS) {
// gunit_break_on_failure takes precedence over
// gunit_throw_on_failure. This allows a user to set the latter
// 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
// with another testing framework) and specify the former on the
// command line for debugging.
@ -3591,13 +3594,6 @@ int UnitTestImpl::RunAllTests() {
// protocol.
internal::WriteToShardStatusFileIfNeeded();
// Lists all the tests and exits if the --gtest_list_tests
// flag was specified.
if (GTEST_FLAG(list_tests)) {
ListAllTests();
return 0;
}
// True iff we are in a subprocess for running a thread-safe-style
// death test.
bool in_subprocess_for_death_test = false;
@ -3618,6 +3614,13 @@ int UnitTestImpl::RunAllTests() {
? HONOR_SHARDING_PROTOCOL
: IGNORE_SHARDING_PROTOCOL) > 0;
// List the tests and exit if the --gtest_list_tests flag was specified.
if (GTEST_FLAG(list_tests)) {
// This must be called *after* FilterTests() has been called.
ListTestsMatchingFilter();
return 0;
}
// True iff at least one test has failed.
bool failed = false;
@ -3808,10 +3811,14 @@ int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {
kDisableTestFilter);
test_info->impl()->set_is_disabled(is_disabled);
const bool is_runnable =
(GTEST_FLAG(also_run_disabled_tests) || !is_disabled) &&
const bool matches_filter =
internal::UnitTestOptions::FilterMatchesTest(test_case_name,
test_name);
test_info->impl()->set_matches_filter(matches_filter);
const bool is_runnable =
(GTEST_FLAG(also_run_disabled_tests) || !is_disabled) &&
matches_filter;
const bool is_selected = is_runnable &&
(shard_tests == IGNORE_SHARDING_PROTOCOL ||
@ -3828,23 +3835,26 @@ int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) {
return num_selected_tests;
}
// Lists all tests by name.
void UnitTestImpl::ListAllTests() {
// Prints the names of the tests matching the user-specified filter flag.
void UnitTestImpl::ListTestsMatchingFilter() {
for (const internal::ListNode<TestCase*>* test_case_node = test_cases_.Head();
test_case_node != NULL;
test_case_node = test_case_node->next()) {
const TestCase* const test_case = test_case_node->element();
// Prints the test case name following by an indented list of test nodes.
printf("%s.\n", test_case->name());
bool printed_test_case_name = false;
for (const internal::ListNode<TestInfo*>* test_info_node =
test_case->test_info_list().Head();
test_info_node != NULL;
test_info_node = test_info_node->next()) {
const TestInfo* const test_info = test_info_node->element();
printf(" %s\n", test_info->name());
if (test_info->matches_filter()) {
if (!printed_test_case_name) {
printed_test_case_name = true;
printf("%s.\n", test_case->name());
}
printf(" %s\n", test_info->name());
}
}
}
fflush(stdout);
@ -3941,6 +3951,7 @@ TestInfoImpl::TestInfoImpl(TestInfo* parent,
fixture_class_id_(fixture_class_id),
should_run_(false),
is_disabled_(false),
matches_filter_(false),
factory_(factory) {
}

View File

@ -56,12 +56,12 @@ EXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_list_tests_unittest_')
# The expected output when running gtest_list_tests_unittest_ with
# --gtest_list_tests
EXPECTED_OUTPUT = """FooDeathTest.
EXPECTED_OUTPUT_NO_FILTER = """FooDeathTest.
Test1
Foo.
Bar1
Bar2
Bar3
DISABLED_Bar3
Abc.
Xyz
Def
@ -69,17 +69,33 @@ FooBar.
Baz
FooTest.
Test1
Test2
DISABLED_Test2
Test3
"""
# The expected output when running gtest_list_tests_unittest_ with
# --gtest_list_tests and --gtest_filter=Foo*.
EXPECTED_OUTPUT_FILTER_FOO = """FooDeathTest.
Test1
Foo.
Bar1
Bar2
DISABLED_Bar3
FooBar.
Baz
FooTest.
Test1
DISABLED_Test2
Test3
"""
# Utilities.
def Run(command):
"""Runs a command and returns the list of tests printed.
"""
stdout_file = os.popen(command, "r")
def Run(command):
"""Runs a command and returns the list of tests printed."""
stdout_file = os.popen(command, 'r')
output = stdout_file.read()
@ -90,8 +106,7 @@ def Run(command):
# The unit test.
class GTestListTestsUnitTest(unittest.TestCase):
"""Tests using the --gtest_list_tests flag to list all tests.
"""
"""Tests using the --gtest_list_tests flag to list all tests."""
def RunAndVerify(self, flag_value, expected_output, other_flag):
"""Runs gtest_list_tests_unittest_ and verifies that it prints
@ -126,12 +141,12 @@ class GTestListTestsUnitTest(unittest.TestCase):
output = Run(command)
msg = ('when %s is %s, the output of "%s" is "%s".' %
(LIST_TESTS_FLAG, flag_expression, command, output))
(LIST_TESTS_FLAG, flag_expression, command, output))
if expected_output is not None:
self.assert_(output == expected_output, msg)
else:
self.assert_(output != EXPECTED_OUTPUT, msg)
self.assert_(output != EXPECTED_OUTPUT_NO_FILTER, msg)
def testDefaultBehavior(self):
"""Tests the behavior of the default mode."""
@ -147,18 +162,24 @@ class GTestListTestsUnitTest(unittest.TestCase):
expected_output=None,
other_flag=None)
self.RunAndVerify(flag_value='1',
expected_output=EXPECTED_OUTPUT,
expected_output=EXPECTED_OUTPUT_NO_FILTER,
other_flag=None)
def testOverrideOtherFlags(self):
"""Tests that --gtest_list_tests overrides all other flags."""
def testOverrideNonFilterFlags(self):
"""Tests that --gtest_list_tests overrides the non-filter flags."""
self.RunAndVerify(flag_value="1",
expected_output=EXPECTED_OUTPUT,
other_flag="--gtest_filter=*")
self.RunAndVerify(flag_value="1",
expected_output=EXPECTED_OUTPUT,
expected_output=EXPECTED_OUTPUT_NO_FILTER,
other_flag="--gtest_break_on_failure")
def testWithFilterFlags(self):
"""Tests that --gtest_list_tests takes into account the
--gtest_filter flag."""
self.RunAndVerify(flag_value="1",
expected_output=EXPECTED_OUTPUT_FILTER_FOO,
other_flag="--gtest_filter=Foo*")
if __name__ == '__main__':
gtest_test_utils.Main()

View File

@ -50,7 +50,7 @@ TEST(Foo, Bar1) {
TEST(Foo, Bar2) {
}
TEST(Foo, Bar3) {
TEST(Foo, DISABLED_Bar3) {
}
TEST(Abc, Xyz) {
@ -68,7 +68,7 @@ class FooTest : public testing::Test {
TEST_F(FooTest, Test1) {
}
TEST_F(FooTest, Test2) {
TEST_F(FooTest, DISABLED_Test2) {
}
TEST_F(FooTest, Test3) {