runner_unittest.py 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 re
  24. import StringIO
  25. import unittest2 as unittest
  26. from webkitpy.tool.mocktool import MockOptions
  27. from webkitpy.test.printer import Printer
  28. from webkitpy.test.runner import Runner
  29. class FakeModuleSuite(object):
  30. def __init__(self, name, result, msg):
  31. self.name = name
  32. self.result = result
  33. self.msg = msg
  34. def __str__(self):
  35. return self.name
  36. def run(self, result):
  37. result.testsRun += 1
  38. if self.result == 'F':
  39. result.failures.append((self.name, self.msg))
  40. elif self.result == 'E':
  41. result.errors.append((self.name, self.msg))
  42. class FakeTopSuite(object):
  43. def __init__(self, tests):
  44. self._tests = tests
  45. class FakeLoader(object):
  46. def __init__(self, *test_triples):
  47. self.triples = test_triples
  48. self._tests = []
  49. self._results = {}
  50. for test_name, result, msg in self.triples:
  51. self._tests.append(test_name)
  52. m = re.match("(\w+) \(([\w.]+)\)", test_name)
  53. self._results['%s.%s' % (m.group(2), m.group(1))] = tuple([test_name, result, msg])
  54. def top_suite(self):
  55. return FakeTopSuite(self._tests)
  56. def loadTestsFromName(self, name, _):
  57. return FakeModuleSuite(*self._results[name])
  58. class RunnerTest(unittest.TestCase):
  59. def setUp(self):
  60. # Here we have to jump through a hoop to make sure test-webkitpy doesn't log
  61. # any messages from these tests :(.
  62. self.root_logger = logging.getLogger()
  63. self.log_levels = []
  64. self.log_handlers = self.root_logger.handlers[:]
  65. for handler in self.log_handlers:
  66. self.log_levels.append(handler.level)
  67. handler.level = logging.CRITICAL
  68. def tearDown(self):
  69. for handler in self.log_handlers:
  70. handler.level = self.log_levels.pop(0)
  71. def test_run(self, verbose=0, timing=False, child_processes=1, quiet=False):
  72. options = MockOptions(verbose=verbose, timing=timing, child_processes=child_processes, quiet=quiet, pass_through=False)
  73. stream = StringIO.StringIO()
  74. loader = FakeLoader(('test1 (Foo)', '.', ''),
  75. ('test2 (Foo)', 'F', 'test2\nfailed'),
  76. ('test3 (Foo)', 'E', 'test3\nerred'))
  77. runner = Runner(Printer(stream, options), loader)
  78. runner.run(['Foo.test1', 'Foo.test2', 'Foo.test3'], 1)
  79. self.assertEqual(runner.tests_run, 3)
  80. self.assertEqual(len(runner.failures), 1)
  81. self.assertEqual(len(runner.errors), 1)