Home | History | Annotate | Download | only in test
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2006, Google Inc.
      4 # All rights reserved.
      5 #
      6 # Redistribution and use in source and binary forms, with or without
      7 # modification, are permitted provided that the following conditions are
      8 # met:
      9 #
     10 #     * Redistributions of source code must retain the above copyright
     11 # notice, this list of conditions and the following disclaimer.
     12 #     * Redistributions in binary form must reproduce the above
     13 # copyright notice, this list of conditions and the following disclaimer
     14 # in the documentation and/or other materials provided with the
     15 # distribution.
     16 #     * Neither the name of Google Inc. nor the names of its
     17 # contributors may be used to endorse or promote products derived from
     18 # this software without specific prior written permission.
     19 #
     20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     23 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     24 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     30 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31 
     32 """Unit test for Google Test's --gtest_list_tests flag.
     33 
     34 A user can ask Google Test to list all tests by specifying the
     35 --gtest_list_tests flag.  This script tests such functionality
     36 by invoking gtest_list_tests_unittest_ (a program written with
     37 Google Test) the command line flags.
     38 """
     39 
     40 __author__ = 'phanna (at] google.com (Patrick Hanna)'
     41 
     42 import gtest_test_utils
     43 import re
     44 
     45 
     46 # Constants.
     47 
     48 # The command line flag for enabling/disabling listing all tests.
     49 LIST_TESTS_FLAG = 'gtest_list_tests'
     50 
     51 # Path to the gtest_list_tests_unittest_ program.
     52 EXE_PATH = gtest_test_utils.GetTestExecutablePath('gtest_list_tests_unittest_')
     53 
     54 # The expected output when running gtest_list_tests_unittest_ with
     55 # --gtest_list_tests
     56 EXPECTED_OUTPUT_NO_FILTER_RE = re.compile(r"""FooDeathTest\.
     57   Test1
     58 Foo\.
     59   Bar1
     60   Bar2
     61   DISABLED_Bar3
     62 Abc\.
     63   Xyz
     64   Def
     65 FooBar\.
     66   Baz
     67 FooTest\.
     68   Test1
     69   DISABLED_Test2
     70   Test3
     71 TypedTest/0\.  # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
     72   TestA
     73   TestB
     74 TypedTest/1\.  # TypeParam = int\s*\*
     75   TestA
     76   TestB
     77 TypedTest/2\.  # TypeParam = .*MyArray<bool,\s*42>
     78   TestA
     79   TestB
     80 My/TypeParamTest/0\.  # TypeParam = (VeryLo{245}|class VeryLo{239})\.\.\.
     81   TestA
     82   TestB
     83 My/TypeParamTest/1\.  # TypeParam = int\s*\*
     84   TestA
     85   TestB
     86 My/TypeParamTest/2\.  # TypeParam = .*MyArray<bool,\s*42>
     87   TestA
     88   TestB
     89 MyInstantiation/ValueParamTest\.
     90   TestA/0  # GetParam\(\) = one line
     91   TestA/1  # GetParam\(\) = two\\nlines
     92   TestA/2  # GetParam\(\) = a very\\nlo{241}\.\.\.
     93   TestB/0  # GetParam\(\) = one line
     94   TestB/1  # GetParam\(\) = two\\nlines
     95   TestB/2  # GetParam\(\) = a very\\nlo{241}\.\.\.
     96 """)
     97 
     98 # The expected output when running gtest_list_tests_unittest_ with
     99 # --gtest_list_tests and --gtest_filter=Foo*.
    100 EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(r"""FooDeathTest\.
    101   Test1
    102 Foo\.
    103   Bar1
    104   Bar2
    105   DISABLED_Bar3
    106 FooBar\.
    107   Baz
    108 FooTest\.
    109   Test1
    110   DISABLED_Test2
    111   Test3
    112 """)
    113 
    114 # Utilities.
    115 
    116 
    117 def Run(args):
    118   """Runs gtest_list_tests_unittest_ and returns the list of tests printed."""
    119 
    120   return gtest_test_utils.Subprocess([EXE_PATH] + args,
    121                                      capture_stderr=False).output
    122 
    123 
    124 # The unit test.
    125 
    126 class GTestListTestsUnitTest(gtest_test_utils.TestCase):
    127   """Tests using the --gtest_list_tests flag to list all tests."""
    128 
    129   def RunAndVerify(self, flag_value, expected_output_re, other_flag):
    130     """Runs gtest_list_tests_unittest_ and verifies that it prints
    131     the correct tests.
    132 
    133     Args:
    134       flag_value:         value of the --gtest_list_tests flag;
    135                           None if the flag should not be present.
    136       expected_output_re: regular expression that matches the expected
    137                           output after running command;
    138       other_flag:         a different flag to be passed to command
    139                           along with gtest_list_tests;
    140                           None if the flag should not be present.
    141     """
    142 
    143     if flag_value is None:
    144       flag = ''
    145       flag_expression = 'not set'
    146     elif flag_value == '0':
    147       flag = '--%s=0' % LIST_TESTS_FLAG
    148       flag_expression = '0'
    149     else:
    150       flag = '--%s' % LIST_TESTS_FLAG
    151       flag_expression = '1'
    152 
    153     args = [flag]
    154 
    155     if other_flag is not None:
    156       args += [other_flag]
    157 
    158     output = Run(args)
    159 
    160     if expected_output_re:
    161       self.assert_(
    162           expected_output_re.match(output),
    163           ('when %s is %s, the output of "%s" is "%s",\n'
    164            'which does not match regex "%s"' %
    165            (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output,
    166             expected_output_re.pattern)))
    167     else:
    168       self.assert_(
    169           not EXPECTED_OUTPUT_NO_FILTER_RE.match(output),
    170           ('when %s is %s, the output of "%s" is "%s"'%
    171            (LIST_TESTS_FLAG, flag_expression, ' '.join(args), output)))
    172 
    173   def testDefaultBehavior(self):
    174     """Tests the behavior of the default mode."""
    175 
    176     self.RunAndVerify(flag_value=None,
    177                       expected_output_re=None,
    178                       other_flag=None)
    179 
    180   def testFlag(self):
    181     """Tests using the --gtest_list_tests flag."""
    182 
    183     self.RunAndVerify(flag_value='0',
    184                       expected_output_re=None,
    185                       other_flag=None)
    186     self.RunAndVerify(flag_value='1',
    187                       expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
    188                       other_flag=None)
    189 
    190   def testOverrideNonFilterFlags(self):
    191     """Tests that --gtest_list_tests overrides the non-filter flags."""
    192 
    193     self.RunAndVerify(flag_value='1',
    194                       expected_output_re=EXPECTED_OUTPUT_NO_FILTER_RE,
    195                       other_flag='--gtest_break_on_failure')
    196 
    197   def testWithFilterFlags(self):
    198     """Tests that --gtest_list_tests takes into account the
    199     --gtest_filter flag."""
    200 
    201     self.RunAndVerify(flag_value='1',
    202                       expected_output_re=EXPECTED_OUTPUT_FILTER_FOO_RE,
    203                       other_flag='--gtest_filter=Foo*')
    204 
    205 
    206 if __name__ == '__main__':
    207   gtest_test_utils.Main()
    208