Skip to content

Commit b7e3c09

Browse files
switch to pytest.raises to ensure that assertions are raised and not silently passing
1 parent f24cf31 commit b7e3c09

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

tests/test_base.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_base_delete_connection_error():
5454
def test_base_raise_exception_not_requests_response_object(mock_requests):
5555
mock_requests().delete.side_effect = [requests.RequestException()]
5656

57-
try:
57+
with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo:
5858
n = nomad.Nomad(
5959
host="162.16.10.102",
6060
port=common.NOMAD_PORT,
@@ -64,19 +64,24 @@ def test_base_raise_exception_not_requests_response_object(mock_requests):
6464

6565
_ = n.job.deregister_job("example")
6666

67-
except nomad.api.exceptions.BaseNomadException as err:
68-
assert hasattr(err, "text") is False
69-
assert isinstance(err.nomad_resp, requests.RequestException)
70-
assert "raised due" in str(err)
67+
# excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised.
68+
# The main attributes of interest are .type, .value and .traceback.
69+
# https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions
70+
assert hasattr(excinfo.value.nomad_resp, "text") is False
71+
assert isinstance(excinfo.value.nomad_resp, requests.RequestException)
72+
assert "raised due" in str(excinfo)
7173

7274

7375
def test_base_raise_exception_is_requests_response_object(nomad_setup):
74-
try:
75-
_ = nomad_setup.job.deregister_job("example")
76-
except nomad.api.exceptions.BaseNomadException as err:
77-
assert hasattr(err, "text") is True
78-
assert isinstance(err.nomad_resp, requests.Response)
79-
assert "raised with" in str(err)
76+
with pytest.raises(nomad.api.exceptions.BaseNomadException) as excinfo:
77+
_ = nomad_setup.job.get_job("examplezz")
78+
79+
# excinfo is a ExceptionInfo instance, which is a wrapper around the actual exception raised.
80+
# The main attributes of interest are .type, .value and .traceback.
81+
# https://docs.pytest.org/en/3.0.1/assert.html#assertions-about-expected-exceptions
82+
assert hasattr(excinfo.value.nomad_resp, "text") is True
83+
assert isinstance(excinfo.value.nomad_resp, requests.Response)
84+
assert "raised with" in str(excinfo)
8085

8186

8287
@pytest.mark.skipif(tuple(int(i) for i in os.environ.get("NOMAD_VERSION").split(".")) < (0, 7, 0), reason="Nomad dispatch not supported")

0 commit comments

Comments
 (0)