main_unittest.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright (C) 2012 Google, Inc.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions
  5. # are met:
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in the
  10. # documentation and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
  13. # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  15. # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
  16. # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  17. # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  18. # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  19. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  20. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  21. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  22. import logging
  23. import sys
  24. import unittest2 as unittest
  25. import StringIO
  26. from webkitpy.common.system.filesystem import FileSystem
  27. from webkitpy.common.system.executive import Executive
  28. from webkitpy.common.system.outputcapture import OutputCapture
  29. from webkitpy.test.main import Tester, _Loader
  30. STUBS_CLASS = __name__ + ".TestStubs"
  31. class TestStubs(unittest.TestCase):
  32. def test_empty(self):
  33. pass
  34. def integration_test_empty(self):
  35. pass
  36. def serial_test_empty(self):
  37. pass
  38. def serial_integration_test_empty(self):
  39. pass
  40. class TesterTest(unittest.TestCase):
  41. def test_no_tests_found(self):
  42. tester = Tester()
  43. errors = StringIO.StringIO()
  44. # Here we need to remove any existing log handlers so that they
  45. # don't log the messages webkitpy.test while we're testing it.
  46. root_logger = logging.getLogger()
  47. root_handlers = root_logger.handlers
  48. root_logger.handlers = []
  49. tester.printer.stream = errors
  50. tester.finder.find_names = lambda args, run_all: []
  51. oc = OutputCapture()
  52. try:
  53. oc.capture_output()
  54. self.assertFalse(tester.run())
  55. finally:
  56. _, _, logs = oc.restore_output()
  57. root_logger.handlers = root_handlers
  58. self.assertIn('No tests to run', errors.getvalue())
  59. self.assertIn('No tests to run', logs)
  60. def _find_test_names(self, args):
  61. tester = Tester()
  62. tester._options, args = tester._parse_args(args)
  63. return tester._test_names(_Loader(), args)
  64. def test_individual_names_are_not_run_twice(self):
  65. args = [STUBS_CLASS + '.test_empty']
  66. parallel_tests, serial_tests = self._find_test_names(args)
  67. self.assertEqual(parallel_tests, args)
  68. self.assertEqual(serial_tests, [])
  69. def test_integration_tests_are_not_found_by_default(self):
  70. parallel_tests, serial_tests = self._find_test_names([STUBS_CLASS])
  71. self.assertEqual(parallel_tests, [
  72. STUBS_CLASS + '.test_empty',
  73. ])
  74. self.assertEqual(serial_tests, [
  75. STUBS_CLASS + '.serial_test_empty',
  76. ])
  77. def test_integration_tests_are_found(self):
  78. parallel_tests, serial_tests = self._find_test_names(['--integration-tests', STUBS_CLASS])
  79. self.assertEqual(parallel_tests, [
  80. STUBS_CLASS + '.integration_test_empty',
  81. STUBS_CLASS + '.test_empty',
  82. ])
  83. self.assertEqual(serial_tests, [
  84. STUBS_CLASS + '.serial_integration_test_empty',
  85. STUBS_CLASS + '.serial_test_empty',
  86. ])
  87. def integration_test_coverage_works(self):
  88. filesystem = FileSystem()
  89. executive = Executive()
  90. module_path = filesystem.path_to_module(self.__module__)
  91. script_dir = module_path[0:module_path.find('webkitpy') - 1]
  92. proc = executive.popen([sys.executable, filesystem.join(script_dir, 'test-webkitpy'), '-c', STUBS_CLASS + '.test_empty'],
  93. stdout=executive.PIPE, stderr=executive.PIPE)
  94. out, _ = proc.communicate()
  95. retcode = proc.returncode
  96. self.assertEqual(retcode, 0)
  97. self.assertIn('Cover', out)