Skip to content

Commit

Permalink
Adapt to use assert raises (#7670)
Browse files Browse the repository at this point in the history
### 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 <[email protected]>
Co-authored-by: YunLiu <[email protected]>
  • Loading branch information
freddiewanah and KumoLiu authored Apr 19, 2024
1 parent 224c47a commit 7a6b69f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 16 deletions.
5 changes: 1 addition & 4 deletions tests/test_decathlondataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,14 @@ def _test_dataset(dataset):
self.assertDictEqual(properties["labels"], {"0": "background", "1": "Anterior", "2": "Posterior"})

shutil.rmtree(os.path.join(testing_dir, "Task04_Hippocampus"))
try:
with self.assertRaisesRegex(RuntimeError, "^Cannot find dataset directory"):
DecathlonDataset(
root_dir=testing_dir,
task="Task04_Hippocampus",
transform=transform,
section="validation",
download=False,
)
except RuntimeError as e:
print(str(e))
self.assertTrue(str(e).startswith("Cannot find dataset directory"))


if __name__ == "__main__":
Expand Down
5 changes: 1 addition & 4 deletions tests/test_mednistdataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ def _test_dataset(dataset):
self.assertEqual(data[0]["class_name"], "AbdomenCT")
self.assertEqual(data[0]["label"], 0)
shutil.rmtree(os.path.join(testing_dir, "MedNIST"))
try:
with self.assertRaisesRegex(RuntimeError, "^Cannot find dataset directory"):
MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False)
except RuntimeError as e:
print(str(e))
self.assertTrue(str(e).startswith("Cannot find dataset directory"))


if __name__ == "__main__":
Expand Down
9 changes: 4 additions & 5 deletions tests/test_monai_utils_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,11 @@ def test_run_cmd(self):
cmd2 = "-c"
cmd3 = 'import sys; print("\\tThis is on stderr\\n", file=sys.stderr); sys.exit(1)'
os.environ["MONAI_DEBUG"] = str(True)
try:
with self.assertRaises(RuntimeError) as cm:
run_cmd([cmd1, cmd2, cmd3], check=True)
except RuntimeError as err:
self.assertIn("This is on stderr", str(err))
self.assertNotIn("\\n", str(err))
self.assertNotIn("\\t", str(err))
self.assertIn("This is on stderr", str(cm.exception))
self.assertNotIn("\\n", str(cm.exception))
self.assertNotIn("\\t", str(cm.exception))


if __name__ == "__main__":
Expand Down
4 changes: 1 addition & 3 deletions tests/test_tciadataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def _test_dataset(dataset):
)[0]

shutil.rmtree(os.path.join(testing_dir, collection))
try:
with self.assertRaisesRegex(RuntimeError, "^Cannot find dataset directory"):
TciaDataset(
root_dir=testing_dir,
collection=collection,
Expand All @@ -117,8 +117,6 @@ def _test_dataset(dataset):
download=False,
val_frac=val_frac,
)
except RuntimeError as e:
self.assertTrue(str(e).startswith("Cannot find dataset directory"))


if __name__ == "__main__":
Expand Down

0 comments on commit 7a6b69f

Please sign in to comment.