Skip to content

Commit de26761

Browse files
committed
* python/tests/test_moocore.py (test_hv_approx_errors): New test.
Use pytest.raises correctly.
1 parent cd57166 commit de26761

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

python/tests/test_moocore.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,20 @@ def test_read_datasets_data(test_datapath, test, expected_name, expected_shape):
5252

5353
def test_read_datasets_badname():
5454
"""Check that the `moocore.read_datasets()` functions fails correctly after a bad file name is input."""
55-
with pytest.raises(Exception) as expt:
55+
with pytest.raises(
56+
FileNotFoundError, match=r"file 'nonexistent_file.txt' not found"
57+
):
5658
moocore.read_datasets("nonexistent_file.txt")
5759

58-
assert str(expt.value) == "file 'nonexistent_file.txt' not found"
59-
assert expt.type is FileNotFoundError
60-
6160

6261
def test_read_datasets_errorcode(test_datapath):
6362
"""Checks that an exception is raised when `read_datasets()` returns an error code, as well as checking specific error types from the `ReadDatasetsError` type."""
64-
with pytest.raises(Exception) as expt:
63+
with pytest.raises(moocore.ReadDatasetsError) as expt:
6564
moocore.read_datasets(test_datapath("empty"))
66-
assert expt.type is moocore.ReadDatasetsError
6765
assert expt.value.message == "READ_INPUT_FILE_EMPTY"
6866

69-
with pytest.raises(Exception) as expt:
67+
with pytest.raises(moocore.ReadDatasetsError) as expt:
7068
moocore.read_datasets(test_datapath("column_error.dat"))
71-
assert expt.type is moocore.ReadDatasetsError
7269
assert expt.value.message == "ERROR_COLUMNS"
7370

7471

@@ -118,9 +115,8 @@ def test_hv_output(self, test_datapath, immutable_call):
118115
def test_hv_wrong_ref(self, test_datapath):
119116
"""Check that the moocore.hypervolume() fails correctly after a ref with the wrong dimensions is input."""
120117
X = self.input1
121-
with pytest.raises(Exception) as expt:
118+
with pytest.raises(ValueError):
122119
moocore.hypervolume(X[X[:, 2] == 1, :2], ref=np.array([10, 10, 10]))
123-
assert expt.type is ValueError
124120

125121

126122
def test_igd():
@@ -391,9 +387,8 @@ def test_eaf(test_datapath, test_name, expected_eaf_name):
391387

392388

393389
def test_get_dataset_path():
394-
with pytest.raises(Exception) as expt:
390+
with pytest.raises(ValueError):
395391
moocore.get_dataset_path("notavailable")
396-
assert expt.type is ValueError
397392

398393

399394
# def test_eafdiff(test_datapath):
@@ -446,6 +441,15 @@ def test_hv_approx_default_seed():
446441
np.testing.assert_approx_equal(true_hv, appr_hv, significant=signif)
447442

448443

444+
def test_hv_approx_errors():
445+
with pytest.raises(
446+
ValueError, match=r".*nsamples must be an integer value.*"
447+
):
448+
moocore.hv_approx(
449+
[[0, 0]], [1, 1], method="DZ2019-MC", seed=None, nsamples="10"
450+
)
451+
452+
449453
def check_hvc(points, ref, err_msg):
450454
hvc = moocore.hv_contributions(points, ref=ref)
451455
is_nondom = moocore.is_nondominated(points, keep_weakly=True)

0 commit comments

Comments
 (0)