Skip to content

Commit 7a6b69f

Browse files
Adapt to use assert raises (#7670)
### Description Some test cases use try/except command, instead of catch exceptions with assertRaise. This will cause extra execution time according to the experimental tests. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [ ] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [x] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [ ] Documentation updated, tested `make html` command in the `docs/` folder. Signed-off-by: Han Wang <freddie.wanah@gmail.com> Co-authored-by: YunLiu <55491388+KumoLiu@users.noreply.github.com>
1 parent 224c47a commit 7a6b69f

4 files changed

Lines changed: 7 additions & 16 deletions

File tree

tests/test_decathlondataset.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,14 @@ def _test_dataset(dataset):
8080
self.assertDictEqual(properties["labels"], {"0": "background", "1": "Anterior", "2": "Posterior"})
8181

8282
shutil.rmtree(os.path.join(testing_dir, "Task04_Hippocampus"))
83-
try:
83+
with self.assertRaisesRegex(RuntimeError, "^Cannot find dataset directory"):
8484
DecathlonDataset(
8585
root_dir=testing_dir,
8686
task="Task04_Hippocampus",
8787
transform=transform,
8888
section="validation",
8989
download=False,
9090
)
91-
except RuntimeError as e:
92-
print(str(e))
93-
self.assertTrue(str(e).startswith("Cannot find dataset directory"))
9491

9592

9693
if __name__ == "__main__":

tests/test_mednistdataset.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,8 @@ def _test_dataset(dataset):
6565
self.assertEqual(data[0]["class_name"], "AbdomenCT")
6666
self.assertEqual(data[0]["label"], 0)
6767
shutil.rmtree(os.path.join(testing_dir, "MedNIST"))
68-
try:
68+
with self.assertRaisesRegex(RuntimeError, "^Cannot find dataset directory"):
6969
MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False)
70-
except RuntimeError as e:
71-
print(str(e))
72-
self.assertTrue(str(e).startswith("Cannot find dataset directory"))
7370

7471

7572
if __name__ == "__main__":

tests/test_monai_utils_misc.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ def test_run_cmd(self):
9292
cmd2 = "-c"
9393
cmd3 = 'import sys; print("\\tThis is on stderr\\n", file=sys.stderr); sys.exit(1)'
9494
os.environ["MONAI_DEBUG"] = str(True)
95-
try:
95+
with self.assertRaises(RuntimeError) as cm:
9696
run_cmd([cmd1, cmd2, cmd3], check=True)
97-
except RuntimeError as err:
98-
self.assertIn("This is on stderr", str(err))
99-
self.assertNotIn("\\n", str(err))
100-
self.assertNotIn("\\t", str(err))
97+
self.assertIn("This is on stderr", str(cm.exception))
98+
self.assertNotIn("\\n", str(cm.exception))
99+
self.assertNotIn("\\t", str(cm.exception))
101100

102101

103102
if __name__ == "__main__":

tests/test_tciadataset.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def _test_dataset(dataset):
108108
)[0]
109109

110110
shutil.rmtree(os.path.join(testing_dir, collection))
111-
try:
111+
with self.assertRaisesRegex(RuntimeError, "^Cannot find dataset directory"):
112112
TciaDataset(
113113
root_dir=testing_dir,
114114
collection=collection,
@@ -117,8 +117,6 @@ def _test_dataset(dataset):
117117
download=False,
118118
val_frac=val_frac,
119119
)
120-
except RuntimeError as e:
121-
self.assertTrue(str(e).startswith("Cannot find dataset directory"))
122120

123121

124122
if __name__ == "__main__":

0 commit comments

Comments
 (0)