| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 import unittest | 5 import unittest |
| 6 | 6 |
| 7 from .type_definitions import Test, Result, MultiTest, FuncCall, Bind | 7 from .type_definitions import Test, Result, MultiTest, FuncCall, Bind |
| 8 | 8 |
| 9 | 9 |
| 10 def _SetUpClass(test_class): | 10 def _SetUpClass(test_class): |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 # test method without the BS of wrapping each method in a new TestCase | 22 # test method without the BS of wrapping each method in a new TestCase |
| 23 # class... | 23 # class... |
| 24 test_instance = test_instance or test_case('__init__') | 24 test_instance = test_instance or test_case('__init__') |
| 25 test_instance.setUp() | 25 test_instance.setUp() |
| 26 try: | 26 try: |
| 27 return Result(getattr(test_instance, test_name)()) | 27 return Result(getattr(test_instance, test_name)()) |
| 28 finally: | 28 finally: |
| 29 test_instance.tearDown() | 29 test_instance.tearDown() |
| 30 | 30 |
| 31 | 31 |
| 32 def UnittestTestCase(test_case, expect_dir, name_prefix='', ext='json'): | 32 def UnittestTestCase(test_case, name_prefix='', ext='json'): |
| 33 """Yield a MultiTest or multiple Test instances for the unittest.TestCase | 33 """Yield a MultiTest or multiple Test instances for the unittest.TestCase |
| 34 derived |test_case|. | 34 derived |test_case|. |
| 35 | 35 |
| 36 If the TestCase has a field `__expect_tests_serial__` defined to be True, then | 36 If the TestCase has a field `__expect_tests_serial__` defined to be True, then |
| 37 all test methods in the TestCase will be guaranteed to run in a single process | 37 all test methods in the TestCase will be guaranteed to run in a single process |
| 38 with the same instance. This is automatically set to True if your test class | 38 with the same instance. This is automatically set to True if your test class |
| 39 relies on setUpClass/tearDownClass. | 39 relies on setUpClass/tearDownClass. |
| 40 | 40 |
| 41 If the TestCase has a field `__expect_tests_atomic__` defined to be True, then | 41 If the TestCase has a field `__expect_tests_atomic__` defined to be True, then |
| 42 in the event of a test filter which matches any test method in |test_case|, | 42 in the event of a test filter which matches any test method in |test_case|, |
| 43 the ENTIRE |test_case| will be executed (i.e. the TestCase has interdependant | 43 the ENTIRE |test_case| will be executed (i.e. the TestCase has interdependant |
| 44 test methods). This should only need to be set for very poorly designed tests. | 44 test methods). This should only need to be set for very poorly designed tests. |
| 45 | 45 |
| 46 `__expect_tests_atomic__` implies `__expect_tests_serial__`. | 46 `__expect_tests_atomic__` implies `__expect_tests_serial__`. |
| 47 | 47 |
| 48 @type test_case: unittest.TestCase | 48 @type test_case: unittest.TestCase |
| 49 """ | 49 """ |
| 50 name_prefix = name_prefix + test_case.__name__ | 50 name_prefix = name_prefix + test_case.__name__ |
| 51 def _tests_from_class(cls, *args, **kwargs): | 51 def _tests_from_class(cls, *args, **kwargs): |
| 52 for test_name in unittest.defaultTestLoader.getTestCaseNames(cls): | 52 for test_name in unittest.defaultTestLoader.getTestCaseNames(cls): |
| 53 yield Test( | 53 yield Test( |
| 54 name_prefix + '.' + test_name, | 54 name_prefix + '.' + test_name, |
| 55 FuncCall(_RunTestCaseSingle, cls, test_name, *args, **kwargs), | 55 FuncCall(_RunTestCaseSingle, cls, test_name, *args, **kwargs), |
| 56 ext=ext, break_funcs=[getattr(cls, test_name)], | 56 ext=ext, break_funcs=[getattr(cls, test_name)], |
| 57 expect_dir=expect_dir, | |
| 58 ) | 57 ) |
| 59 | 58 |
| 60 if hasattr(test_case, '__expect_tests_serial__'): | 59 if hasattr(test_case, '__expect_tests_serial__'): |
| 61 serial = getattr(test_case, '__expect_tests_serial__', False) | 60 serial = getattr(test_case, '__expect_tests_serial__', False) |
| 62 else: | 61 else: |
| 63 default_setup = unittest.TestCase.setUpClass.im_func | 62 default_setup = unittest.TestCase.setUpClass.im_func |
| 64 default_teardown = unittest.TestCase.tearDownClass.im_func | 63 default_teardown = unittest.TestCase.tearDownClass.im_func |
| 65 serial = ( | 64 serial = ( |
| 66 test_case.setUpClass.im_func is not default_setup or | 65 test_case.setUpClass.im_func is not default_setup or |
| 67 test_case.tearDownClass.im_func is not default_teardown) | 66 test_case.tearDownClass.im_func is not default_teardown) |
| 68 | 67 |
| 69 atomic = getattr(test_case, '__expect_tests_atomic__', False) | 68 atomic = getattr(test_case, '__expect_tests_atomic__', False) |
| 70 if atomic or serial: | 69 if atomic or serial: |
| 71 yield MultiTest( | 70 yield MultiTest( |
| 72 name_prefix, | 71 name_prefix, |
| 73 FuncCall(_SetUpClass, test_case), | 72 FuncCall(_SetUpClass, test_case), |
| 74 FuncCall(_TearDownClass, Bind(name='context')), | 73 FuncCall(_TearDownClass, Bind(name='context')), |
| 75 list(_tests_from_class(test_case, test_instance=Bind(name='context'))), | 74 list(_tests_from_class(test_case, test_instance=Bind(name='context'))), |
| 76 atomic | 75 atomic |
| 77 ) | 76 ) |
| 78 else: | 77 else: |
| 79 for test in _tests_from_class(test_case): | 78 for test in _tests_from_class(test_case): |
| 80 yield test | 79 yield test |
| 81 | 80 |
| 82 | 81 |
| 83 def UnitTestModule(test_module, expect_dir=None, name_prefix='', ext='json'): | 82 def UnitTestModule(test_module, name_prefix='', ext='json'): |
| 84 """Yield MultiTest's and/or Test's for the python module |test_module| which | 83 """Yield MultiTest's and/or Test's for the python module |test_module| which |
| 85 contains zero or more unittest.TestCase implementations. | 84 contains zero or more unittest.TestCase implementations. |
| 86 | 85 |
| 87 @type test_module: types.ModuleType | 86 @type test_module: types.ModuleType |
| 88 @type expect_dir: str | |
| 89 """ | 87 """ |
| 90 name_prefix = name_prefix + test_module.__name__ + '.' | 88 name_prefix = name_prefix + test_module.__name__ + '.' |
| 91 for name in dir(test_module): | 89 for name in dir(test_module): |
| 92 obj = getattr(test_module, name) | 90 obj = getattr(test_module, name) |
| 93 if isinstance(obj, type) and issubclass(obj, unittest.TestCase): | 91 if isinstance(obj, type) and issubclass(obj, unittest.TestCase): |
| 94 for test in UnittestTestCase(obj, expect_dir, name_prefix, ext): | 92 for test in UnittestTestCase(obj, name_prefix, ext): |
| 95 yield test | 93 yield test |
| 96 # TODO(iannucci): Make this compatible with the awful load_tests hack? | 94 # TODO(iannucci): Make this compatible with the awful load_tests hack? |
| OLD | NEW |