Return a suite of all test cases given a string specifier. The name may resolve either to a module, a test case class, a test method within a test case class, or a callable object which returns a TestCase or TestSuite instance. The method optionally resolves the nam
(self, name, module=None)
| 118 | return tests |
| 119 | |
| 120 | def loadTestsFromName(self, name, module=None): |
| 121 | """Return a suite of all test cases given a string specifier. |
| 122 | |
| 123 | The name may resolve either to a module, a test case class, a |
| 124 | test method within a test case class, or a callable object which |
| 125 | returns a TestCase or TestSuite instance. |
| 126 | |
| 127 | The method optionally resolves the names relative to a given module. |
| 128 | """ |
| 129 | parts = name.split('.') |
| 130 | error_case, error_message = None, None |
| 131 | if module is None: |
| 132 | parts_copy = parts[:] |
| 133 | while parts_copy: |
| 134 | try: |
| 135 | module_name = '.'.join(parts_copy) |
| 136 | module = __import__(module_name) |
| 137 | break |
| 138 | except ImportError: |
| 139 | next_attribute = parts_copy.pop() |
| 140 | # Last error so we can give it to the user if needed. |
| 141 | error_case, error_message = _make_failed_import_test( |
| 142 | next_attribute, self.suiteClass) |
| 143 | if not parts_copy: |
| 144 | # Even the top level import failed: report that error. |
| 145 | self.errors.append(error_message) |
| 146 | return error_case |
| 147 | parts = parts[1:] |
| 148 | obj = module |
| 149 | for part in parts: |
| 150 | try: |
| 151 | parent, obj = obj, getattr(obj, part) |
| 152 | except AttributeError as e: |
| 153 | # We can't traverse some part of the name. |
| 154 | if (getattr(obj, '__path__', None) is not None |
| 155 | and error_case is not None): |
| 156 | # This is a package (no __path__ per importlib docs), and we |
| 157 | # encountered an error importing something. We cannot tell |
| 158 | # the difference between package.WrongNameTestClass and |
| 159 | # package.wrong_module_name so we just report the |
| 160 | # ImportError - it is more informative. |
| 161 | self.errors.append(error_message) |
| 162 | return error_case |
| 163 | else: |
| 164 | # Otherwise, we signal that an AttributeError has occurred. |
| 165 | error_case, error_message = _make_failed_test( |
| 166 | part, e, self.suiteClass, |
| 167 | 'Failed to access attribute:\n%s' % ( |
| 168 | traceback.format_exc(),)) |
| 169 | self.errors.append(error_message) |
| 170 | return error_case |
| 171 | |
| 172 | if isinstance(obj, types.ModuleType): |
| 173 | return self.loadTestsFromModule(obj) |
| 174 | elif ( |
| 175 | isinstance(obj, type) |
| 176 | and issubclass(obj, case.TestCase) |
| 177 | and obj not in (case.TestCase, case.FunctionTestCase) |