This function takes the fully qualified path to a test file, along with any needed keyword arguments, then dynamically loads the file as a module and finds the test class defined inside of it via inspection. It then uses the keyword arguments to instantiate the test class and retur
(pathToScript, **kwargs)
| 602 | # documentation to help people avoid that. |
| 603 | # ============================================================================= |
| 604 | def instantiate_test_subclass(pathToScript, **kwargs): |
| 605 | """ |
| 606 | This function takes the fully qualified path to a test file, along with |
| 607 | any needed keyword arguments, then dynamically loads the file as a module |
| 608 | and finds the test class defined inside of it via inspection. It then |
| 609 | uses the keyword arguments to instantiate the test class and return the |
| 610 | instance. |
| 611 | |
| 612 | pathToScript: Fully qualified path to python file containing defined |
| 613 | subclass of one of the test base classes. |
| 614 | kwargs: Keyword arguments to be passed to the constructor of the |
| 615 | testing subclass. |
| 616 | """ |
| 617 | |
| 618 | # Load the file as a module |
| 619 | moduleName = imp.load_source("dynamicTestModule", pathToScript) |
| 620 | instance = None |
| 621 | |
| 622 | # Inspect dynamically loaded module members |
| 623 | for name, obj in inspect.getmembers(moduleName): |
| 624 | # Looking for classes only |
| 625 | if inspect.isclass(obj): |
| 626 | instance = obj.__new__(obj) |
| 627 | # And only classes defined in the dynamically loaded module |
| 628 | if instance.__module__ == "dynamicTestModule": |
| 629 | try: |
| 630 | instance.__init__(**kwargs) |
| 631 | break |
| 632 | except Exception as inst: |
| 633 | print("Caught exception: " + str(type(inst))) |
| 634 | print(inst) |
| 635 | raise |
| 636 | |
| 637 | return instance |
| 638 | |
| 639 | |
| 640 | # ============================================================================= |