|
17 | 17 | import openml |
18 | 18 | from openml.tasks import TaskTypeEnum |
19 | 19 |
|
| 20 | +import pytest |
| 21 | + |
20 | 22 |
|
21 | 23 | class TestBase(unittest.TestCase): |
22 | 24 | """Base class for tests |
@@ -44,7 +46,6 @@ def setUp(self, n_levels: int = 1): |
44 | 46 | Number of nested directories the test is in. Necessary to resolve the path to the |
45 | 47 | ``files`` directory, which is located directly under the ``tests`` directory. |
46 | 48 | """ |
47 | | - |
48 | 49 | # This cache directory is checked in to git to simulate a populated |
49 | 50 | # cache |
50 | 51 | self.maxDiff = None |
@@ -103,6 +104,48 @@ def tearDown(self): |
103 | 104 | openml.config.server = self.production_server |
104 | 105 | openml.config.connection_n_retries = self.connection_n_retries |
105 | 106 |
|
| 107 | + @pytest.fixture(scope="session", autouse=True) |
| 108 | + def _cleanup_fixture(self): |
| 109 | + """Cleans up files generated by Unit tests |
| 110 | +
|
| 111 | + This function is called at the beginning of the invocation of |
| 112 | + TestBase (defined below), by each of class that inherits TestBase. |
| 113 | + The 'yield' creates a checkpoint and breaks away to continue running |
| 114 | + the unit tests of the sub class. When all the tests end, execution |
| 115 | + resumes from the checkpoint. |
| 116 | + """ |
| 117 | + |
| 118 | + abspath_this_file = os.path.abspath(inspect.getfile(self.__class__)) |
| 119 | + static_cache_dir = os.path.dirname(abspath_this_file) |
| 120 | + # Could be a risky while condition, however, going up a directory |
| 121 | + # n-times will eventually end at main directory |
| 122 | + while True: |
| 123 | + if 'openml' in os.listdir(static_cache_dir): |
| 124 | + break |
| 125 | + else: |
| 126 | + static_cache_dir = os.path.join(static_cache_dir, '../') |
| 127 | + directory = os.path.join(static_cache_dir, 'tests/files/') |
| 128 | + # directory = "{}/tests/files/".format(static_cache_dir) |
| 129 | + files = os.walk(directory) |
| 130 | + old_file_list = [] |
| 131 | + for root, _, filenames in files: |
| 132 | + for filename in filenames: |
| 133 | + old_file_list.append(os.path.join(root, filename)) |
| 134 | + # context switches to other remaining tests |
| 135 | + # pauses the code execution here till all tests in the 'session' is over |
| 136 | + yield |
| 137 | + # resumes from here after all collected tests are completed |
| 138 | + files = os.walk(directory) |
| 139 | + new_file_list = [] |
| 140 | + for root, _, filenames in files: |
| 141 | + for filename in filenames: |
| 142 | + new_file_list.append(os.path.join(root, filename)) |
| 143 | + # filtering the files generated during this run |
| 144 | + new_file_list = list(set(new_file_list) - set(old_file_list)) |
| 145 | + print("Files to delete in local: {}".format(new_file_list)) |
| 146 | + for file in new_file_list: |
| 147 | + os.remove(file) |
| 148 | + |
106 | 149 | def _get_sentinel(self, sentinel=None): |
107 | 150 | if sentinel is None: |
108 | 151 | # Create a unique prefix for the flow. Necessary because the flow |
|
0 commit comments