Walks through the examples folder and tests each example by dynamically importing them which will also run their code.
()
| 5 | |
| 6 | |
| 7 | def test_examples(): |
| 8 | """Walks through the examples folder and tests each example by |
| 9 | dynamically importing them which will also run their code. |
| 10 | """ |
| 11 | # The training and analysis examples are not included since they require |
| 12 | # installing quite large libraries and can take a significant amount of |
| 13 | # time. |
| 14 | exclude = { |
| 15 | "./forces_and_energies/training_pytorch.py", |
| 16 | "./forces_and_energies/training_tensorflow.py", |
| 17 | "./forces_and_energies/analysis.py", |
| 18 | "./clustering/training.py", |
| 19 | } |
| 20 | old_cwd = os.getcwd() |
| 21 | os.chdir(Path(__file__).parent.parent / "examples") |
| 22 | example_root = os.getcwd() |
| 23 | paths = set() |
| 24 | for root, dirs, files in list(os.walk("./")): |
| 25 | files.sort() |
| 26 | for f in files: |
| 27 | filename = os.path.join(root, f) |
| 28 | if filename.endswith(".py") and filename not in exclude: |
| 29 | parts = filename[2:-3] |
| 30 | parts = parts.rsplit("/", 1) |
| 31 | if len(parts) != 1: |
| 32 | folder = parts[0] |
| 33 | cwd = "{}/{}".format(example_root, folder) |
| 34 | else: |
| 35 | cwd = example_root |
| 36 | os.chdir(cwd) |
| 37 | modulename = parts[-1] |
| 38 | if cwd not in paths: |
| 39 | sys.path.insert(0, cwd) |
| 40 | paths.add(cwd) |
| 41 | import_module(modulename) |
| 42 | os.chdir(old_cwd) |