Skip to content

Checking an error is raised by a test #108

Closed
@p-j-smith

Description

@p-j-smith

In the Defensive Programming section, there is a test for patient_normalise that checks a ValueError is raised when negative values are passed to the function:

with pytest.raises(raises):
    npt.assert_almost_equal(patient_normalise(np.array(test)), np.array(expected), decimal=2)

This could be simplified to:

with pytest.raises(raises):
    patient_normalise(np.asarray(test))

Also, it's best to check the specific error message being thrown:

with pytest.raises(raises, match='Inflammation values should not be negative'):
    patient_normalise(np.asarray(test))

And there is a check for whether raises is ValueError or None:

if raises:

However, it's better to always explicitly check that a variable is not None:

if raises is not None:

It makes no difference here, but other times some statements (e.g. []) could evaluate to False and lead to the wrong code block executed.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions