Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(17)

Side by Side Diff: expect_tests/unittest_helper.py

Issue 412773002: Copy expect_tests from build (Closed) Base URL: https://chromium.googlesource.com/infra/testing/expect_tests@master
Patch Set: Fancy ast walker Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « expect_tests/type_definitions.py ('k') | expect_tests/util.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 # found in the LICENSE file.
4
5 import unittest
6
7 from expect_tests.type_definitions import (
8 Test, Result, MultiTest, FuncCall, Bind)
9
10
11 def _SetUpClass(test_class):
12 inst = test_class('__init__')
13 inst.setUpClass()
14 return inst
15
16
17 def _TearDownClass(test_class_inst):
18 test_class_inst.tearDownClass()
19
20
21 def _RunTestCaseSingle(test_case, test_name, test_instance=None):
22 # The hack is so that unittest.TestCase has something to pretend is the
23 # test method without the BS of wrapping each method in a new TestCase
24 # class...
25 test_instance = test_instance or test_case('__init__')
26 test_instance.setUp()
27 try:
28 return Result(getattr(test_instance, test_name)())
29 finally:
30 test_instance.tearDown()
31
32
33 def UnittestTestCase(test_case, name_prefix='', ext='json'):
34 """Yield a MultiTest or multiple Test instances for the unittest.TestCase
35 derived |test_case|.
36
37 If the TestCase has a field `__expect_tests_serial__` defined to be True, then
38 all test methods in the TestCase will be guaranteed to run in a single process
39 with the same instance. This is automatically set to True if your test class
40 relies on setUpClass/tearDownClass.
41
42 If the TestCase has a field `__expect_tests_atomic__` defined to be True, then
43 in the event of a test filter which matches any test method in |test_case|,
44 the ENTIRE |test_case| will be executed (i.e. the TestCase has interdependant
45 test methods). This should only need to be set for very poorly designed tests.
46
47 `__expect_tests_atomic__` implies `__expect_tests_serial__`.
48
49 @type test_case: unittest.TestCase
50 """
51 name_prefix = name_prefix + test_case.__name__
52 def _tests_from_class(cls, *args, **kwargs):
53 for test_name in unittest.defaultTestLoader.getTestCaseNames(cls):
54 yield Test(
55 name_prefix + '.' + test_name,
56 FuncCall(_RunTestCaseSingle, cls, test_name, *args, **kwargs),
57 ext=ext, break_funcs=[getattr(cls, test_name)],
58 )
59
60 if hasattr(test_case, '__expect_tests_serial__'):
61 serial = getattr(test_case, '__expect_tests_serial__', False)
62 else:
63 default_setup = unittest.TestCase.setUpClass.im_func
64 default_teardown = unittest.TestCase.tearDownClass.im_func
65 serial = (
66 test_case.setUpClass.im_func is not default_setup or
67 test_case.tearDownClass.im_func is not default_teardown)
68
69 atomic = getattr(test_case, '__expect_tests_atomic__', False)
70 if atomic or serial:
71 yield MultiTest(
72 name_prefix,
73 FuncCall(_SetUpClass, test_case),
74 FuncCall(_TearDownClass, Bind(name='context')),
75 list(_tests_from_class(test_case, test_instance=Bind(name='context'))),
76 atomic
77 )
78 else:
79 for test in _tests_from_class(test_case):
80 yield test
81
82
83 def UnitTestModule(test_module, name_prefix='', ext='json'):
84 """Yield MultiTest's and/or Test's for the python module |test_module| which
85 contains zero or more unittest.TestCase implementations.
86
87 @type test_module: types.ModuleType
88 """
89 name_prefix = name_prefix + test_module.__name__ + '.'
90 for name in dir(test_module):
91 obj = getattr(test_module, name)
92 if isinstance(obj, type) and issubclass(obj, unittest.TestCase):
93 for test in UnittestTestCase(obj, name_prefix, ext):
94 yield test
95 # TODO(iannucci): Make this compatible with the awful load_tests hack?
OLDNEW
« no previous file with comments | « expect_tests/type_definitions.py ('k') | expect_tests/util.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698