Skip to content

Commit

Permalink
Improve unit test temp file usage (#696)
Browse files Browse the repository at this point in the history
  • Loading branch information
woodsp-ibm authored Oct 5, 2023
1 parent 7f122be commit fe0a8f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
10 changes: 4 additions & 6 deletions test/algorithms/regressors/test_fidelity_quantum_kernel_qsvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ def test_save_load(self):
original_predicts = regressor.predict(test_features)

# save/load, change the quantum instance and check if predicted values are the same
file_name = os.path.join(tempfile.gettempdir(), "qsvr.model")
regressor.save(file_name)
try:
with tempfile.TemporaryDirectory() as dir_name:
file_name = os.path.join(dir_name, "qsvr.model")
regressor.save(file_name)

regressor_load = QSVR.load(file_name)
loaded_model_predicts = regressor_load.predict(test_features)

Expand All @@ -123,9 +124,6 @@ class FakeModel(SerializableModelMixin):
with self.assertRaises(TypeError):
FakeModel.load(file_name)

finally:
os.remove(file_name)


if __name__ == "__main__":
unittest.main()
10 changes: 4 additions & 6 deletions test/algorithms/regressors/test_qsvr.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ def test_save_load(self):
original_predicts = regressor.predict(test_features)

# save/load, change the quantum instance and check if predicted values are the same
file_name = os.path.join(tempfile.gettempdir(), "qsvr.model")
regressor.save(file_name)
try:
with tempfile.TemporaryDirectory() as dir_name:
file_name = os.path.join(dir_name, "qsvr.model")
regressor.save(file_name)

regressor_load = QSVR.load(file_name)
loaded_model_predicts = regressor_load.predict(test_features)

Expand All @@ -122,9 +123,6 @@ class FakeModel(SerializableModelMixin):
with self.assertRaises(TypeError):
FakeModel.load(file_name)

finally:
os.remove(file_name)


if __name__ == "__main__":
unittest.main()

0 comments on commit fe0a8f3

Please sign in to comment.