printing_unittest.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. # Copyright (C) 2010, 2012 Google Inc. All rights reserved.
  2. #
  3. # Redistribution and use in source and binary forms, with or without
  4. # modification, are permitted provided that the following conditions are
  5. # met:
  6. #
  7. # * Redistributions of source code must retain the above copyright
  8. # notice, this list of conditions and the following disclaimer.
  9. # * Redistributions in binary form must reproduce the above
  10. # copyright notice, this list of conditions and the following disclaimer
  11. # in the documentation and/or other materials provided with the
  12. # distribution.
  13. # * Neither the name of Google Inc. nor the names of its
  14. # contributors may be used to endorse or promote products derived from
  15. # this software without specific prior written permission.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. """Unit tests for printing.py."""
  29. import optparse
  30. import StringIO
  31. import sys
  32. import time
  33. import unittest2 as unittest
  34. from webkitpy.common.host_mock import MockHost
  35. from webkitpy.common.system import logtesting
  36. from webkitpy import port
  37. from webkitpy.layout_tests.controllers import manager
  38. from webkitpy.layout_tests.models import test_expectations
  39. from webkitpy.layout_tests.models import test_failures
  40. from webkitpy.layout_tests.models import test_results
  41. from webkitpy.layout_tests.views import printing
  42. def get_options(args):
  43. print_options = printing.print_options()
  44. option_parser = optparse.OptionParser(option_list=print_options)
  45. return option_parser.parse_args(args)
  46. class TestUtilityFunctions(unittest.TestCase):
  47. def test_print_options(self):
  48. options, args = get_options([])
  49. self.assertIsNotNone(options)
  50. class Testprinter(unittest.TestCase):
  51. def assertEmpty(self, stream):
  52. self.assertFalse(stream.getvalue())
  53. def assertNotEmpty(self, stream):
  54. self.assertTrue(stream.getvalue())
  55. def assertWritten(self, stream, contents):
  56. self.assertEqual(stream.buflist, contents)
  57. def reset(self, stream):
  58. stream.buflist = []
  59. stream.buf = ''
  60. def get_printer(self, args=None):
  61. args = args or []
  62. printing_options = printing.print_options()
  63. option_parser = optparse.OptionParser(option_list=printing_options)
  64. options, args = option_parser.parse_args(args)
  65. host = MockHost()
  66. self._port = host.port_factory.get('test', options)
  67. nproc = 2
  68. regular_output = StringIO.StringIO()
  69. printer = printing.Printer(self._port, options, regular_output)
  70. return printer, regular_output
  71. def get_result(self, test_name, result_type=test_expectations.PASS, run_time=0):
  72. failures = []
  73. if result_type == test_expectations.TIMEOUT:
  74. failures = [test_failures.FailureTimeout()]
  75. elif result_type == test_expectations.CRASH:
  76. failures = [test_failures.FailureCrash()]
  77. return test_results.TestResult(test_name, failures=failures, test_run_time=run_time)
  78. def test_configure_and_cleanup(self):
  79. # This test verifies that calling cleanup repeatedly and deleting
  80. # the object is safe.
  81. printer, err = self.get_printer()
  82. printer.cleanup()
  83. printer.cleanup()
  84. printer = None
  85. def test_print_config(self):
  86. printer, err = self.get_printer()
  87. # FIXME: it's lame that i have to set these options directly.
  88. printer._options.pixel_tests = True
  89. printer._options.new_baseline = True
  90. printer._options.time_out_ms = 6000
  91. printer._options.slow_time_out_ms = 12000
  92. printer.print_config('/tmp')
  93. self.assertIn("Using port 'test-mac-leopard'", err.getvalue())
  94. self.assertIn('Test configuration: <leopard, x86, release>', err.getvalue())
  95. self.assertIn('Placing test results in /tmp', err.getvalue())
  96. self.assertIn('Baseline search path: test-mac-leopard -> test-mac-snowleopard -> generic', err.getvalue())
  97. self.assertIn('Using Release build', err.getvalue())
  98. self.assertIn('Pixel tests enabled', err.getvalue())
  99. self.assertIn('Command line:', err.getvalue())
  100. self.assertIn('Regular timeout: ', err.getvalue())
  101. self.reset(err)
  102. printer._options.quiet = True
  103. printer.print_config('/tmp')
  104. self.assertNotIn('Baseline search path: test-mac-leopard -> test-mac-snowleopard -> generic', err.getvalue())
  105. def test_print_one_line_summary(self):
  106. printer, err = self.get_printer()
  107. printer._print_one_line_summary(1, 1, 0)
  108. self.assertWritten(err, ["The test ran as expected.\n", "\n"])
  109. printer, err = self.get_printer()
  110. printer._print_one_line_summary(1, 1, 0)
  111. self.assertWritten(err, ["The test ran as expected.\n", "\n"])
  112. printer, err = self.get_printer()
  113. printer._print_one_line_summary(2, 1, 1)
  114. self.assertWritten(err, ["\n", "1 test ran as expected, 1 didn't:\n", "\n"])
  115. printer, err = self.get_printer()
  116. printer._print_one_line_summary(3, 2, 1)
  117. self.assertWritten(err, ["\n", "2 tests ran as expected, 1 didn't:\n", "\n"])
  118. printer, err = self.get_printer()
  119. printer._print_one_line_summary(3, 2, 0)
  120. self.assertWritten(err, ['\n', "2 tests ran as expected (1 didn't run).\n", '\n'])
  121. def test_test_status_line(self):
  122. printer, _ = self.get_printer()
  123. printer._meter.number_of_columns = lambda: 80
  124. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  125. self.assertEqual(80, len(actual))
  126. self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associa...after-index-assertion-fail1.html passed')
  127. printer._meter.number_of_columns = lambda: 89
  128. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  129. self.assertEqual(89, len(actual))
  130. self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-...ents-after-index-assertion-fail1.html passed')
  131. printer._meter.number_of_columns = lambda: sys.maxint
  132. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  133. self.assertEqual(90, len(actual))
  134. self.assertEqual(actual, '[0/0] fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html passed')
  135. printer._meter.number_of_columns = lambda: 18
  136. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  137. self.assertEqual(18, len(actual))
  138. self.assertEqual(actual, '[0/0] f...l passed')
  139. printer._meter.number_of_columns = lambda: 10
  140. actual = printer._test_status_line('fast/dom/HTMLFormElement/associated-elements-after-index-assertion-fail1.html', ' passed')
  141. self.assertEqual(actual, '[0/0] associated-elements-after-index-assertion-fail1.html passed')
  142. def test_details(self):
  143. printer, err = self.get_printer(['--details'])
  144. result = self.get_result('passes/image.html')
  145. printer.print_started_test('passes/image.html')
  146. printer.print_finished_test(result, expected=False, exp_str='', got_str='')
  147. self.assertNotEmpty(err)
  148. def test_print_found(self):
  149. printer, err = self.get_printer()
  150. printer.print_found(100, 10, 1, 1)
  151. self.assertWritten(err, ["Found 100 tests; running 10, skipping 90.\n"])
  152. self.reset(err)
  153. printer.print_found(100, 10, 2, 3)
  154. self.assertWritten(err, ["Found 100 tests; running 10 (6 times each: --repeat-each=2 --iterations=3), skipping 90.\n"])