|
| 1 | +import os |
| 2 | + |
| 3 | +from e3.testsuite.result import TestStatus |
| 4 | + |
| 5 | +from drivers import ALSTestDriver |
| 6 | + |
| 7 | +import inspect |
| 8 | +import importlib.util |
| 9 | + |
| 10 | +from drivers.lsp_python_driver import run_simple_test |
| 11 | + |
| 12 | + |
| 13 | +class PythonTestDriver(ALSTestDriver): |
| 14 | + """Each test should have: |
| 15 | + - a test.yaml containing |
| 16 | + title: '<test name>' |
| 17 | +
|
| 18 | + - a number of test drivers, in .json files. |
| 19 | + """ |
| 20 | + |
| 21 | + def run(self, previous_values, slot): |
| 22 | + # Check whether the test should be skipped |
| 23 | + if self.should_skip(): |
| 24 | + return False |
| 25 | + |
| 26 | + # The working directory |
| 27 | + wd = self.test_env["working_dir"] |
| 28 | + |
| 29 | + status = TestStatus.PASS |
| 30 | + |
| 31 | + # If there is a "test.py", evaluate it |
| 32 | + if os.path.exists(os.path.join(wd, "test.py")): |
| 33 | + # Load test.py as a module |
| 34 | + python_file = os.path.join(wd, "test.py") |
| 35 | + spec = importlib.util.spec_from_file_location("module.name", python_file) |
| 36 | + module = importlib.util.module_from_spec(spec) |
| 37 | + spec.loader.exec_module(module) |
| 38 | + |
| 39 | + # Look for functions with the decorator @simple_test and run them |
| 40 | + errors = [ |
| 41 | + f"no function with @simple_test or @complex_test found in {python_file}" |
| 42 | + ] |
| 43 | + |
| 44 | + for _, obj in inspect.getmembers(module): |
| 45 | + if inspect.isfunction(obj) and ( |
| 46 | + hasattr(obj, "simple_test") or hasattr(obj, "complex_test") |
| 47 | + ): |
| 48 | + errors = obj(wd) |
| 49 | + |
| 50 | + if len(errors) > 0: |
| 51 | + self.result.log = "\n".join(errors) |
| 52 | + status = TestStatus.FAIL |
| 53 | + |
| 54 | + self.result.set_status(status) |
| 55 | + self.push_result() |
0 commit comments