Find and return all test modules from the specified start directory, recursing into subdirectories to find them and return all tests found within them. Only test files that match the pattern will be loaded. (Using shell style pattern matching.) All test modules must
(self, start_dir, pattern='test*.py', top_level_dir=None)
| 227 | return testFnNames |
| 228 | |
| 229 | def discover(self, start_dir, pattern='test*.py', top_level_dir=None): |
| 230 | """Find and return all test modules from the specified start |
| 231 | directory, recursing into subdirectories to find them and return all |
| 232 | tests found within them. Only test files that match the pattern will |
| 233 | be loaded. (Using shell style pattern matching.) |
| 234 | |
| 235 | All test modules must be importable from the top level of the project. |
| 236 | If the start directory is not the top level directory then the top |
| 237 | level directory must be specified separately. |
| 238 | |
| 239 | If a test package name (directory with '__init__.py') matches the |
| 240 | pattern then the package will be checked for a 'load_tests' function. If |
| 241 | this exists then it will be called with (loader, tests, pattern) unless |
| 242 | the package has already had load_tests called from the same discovery |
| 243 | invocation, in which case the package module object is not scanned for |
| 244 | tests - this ensures that when a package uses discover to further |
| 245 | discover child tests that infinite recursion does not happen. |
| 246 | |
| 247 | If load_tests exists then discovery does *not* recurse into the package, |
| 248 | load_tests is responsible for loading all tests in the package. |
| 249 | |
| 250 | The pattern is deliberately not stored as a loader attribute so that |
| 251 | packages can continue discovery themselves. top_level_dir is stored so |
| 252 | load_tests does not need to pass this argument in to loader.discover(). |
| 253 | |
| 254 | Paths are sorted before being imported to ensure reproducible execution |
| 255 | order even on filesystems with non-alphabetical ordering like ext3/4. |
| 256 | """ |
| 257 | original_top_level_dir = self._top_level_dir |
| 258 | set_implicit_top = False |
| 259 | if top_level_dir is None and self._top_level_dir is not None: |
| 260 | # make top_level_dir optional if called from load_tests in a package |
| 261 | top_level_dir = self._top_level_dir |
| 262 | elif top_level_dir is None: |
| 263 | set_implicit_top = True |
| 264 | top_level_dir = start_dir |
| 265 | |
| 266 | top_level_dir = os.path.abspath(top_level_dir) |
| 267 | |
| 268 | if not top_level_dir in sys.path: |
| 269 | # all test modules must be importable from the top level directory |
| 270 | # should we *unconditionally* put the start directory in first |
| 271 | # in sys.path to minimise likelihood of conflicts between installed |
| 272 | # modules and development versions? |
| 273 | sys.path.insert(0, top_level_dir) |
| 274 | self._top_level_dir = top_level_dir |
| 275 | |
| 276 | is_not_importable = False |
| 277 | is_namespace = False |
| 278 | tests = [] |
| 279 | if os.path.isdir(os.path.abspath(start_dir)): |
| 280 | start_dir = os.path.abspath(start_dir) |
| 281 | if start_dir != top_level_dir: |
| 282 | is_not_importable = not os.path.isfile(os.path.join(start_dir, '__init__.py')) |
| 283 | else: |
| 284 | # support for discovery from dotted module names |
| 285 | try: |
| 286 | __import__(start_dir) |