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