Skip to content

Commit dc559f5

Browse files
committed
differences for PR #451
1 parent 8cc64a2 commit dc559f5

File tree

2 files changed

+67
-5
lines changed

2 files changed

+67
-5
lines changed

24-diagnosing-issues-improving-robustness.md

+66-4
Original file line numberDiff line numberDiff line change
@@ -610,13 +610,14 @@ def test_patient_normalise(test, expected, expect_raises):
610610

611611
if expect_raises is not None:
612612
with pytest.raises(expect_raises):
613-
result = patient_normalise(np.array(test))
614-
npt.assert_allclose(result, np.array(expected), rtol=1e-2, atol=1e-2)
613+
patient_normalise(np.array(test))
615614
else:
616615
result = patient_normalise(np.array(test))
617616
npt.assert_allclose(result, np.array(expected), rtol=1e-2, atol=1e-2)
618617
```
619618

619+
Notice that under the `pytest.raises` context manager, it isn't necessary to perform the `npt.assert_allclose` because just the call to `patient_normalise()` will raise the `ValueError`.
620+
620621
Be sure to commit your changes so far and push them to GitHub.
621622

622623
::::::::::::::::::::::::::::::::::::::: challenge
@@ -629,9 +630,17 @@ You will find the Python function
629630
[`isinstance`](https://docs.python.org/3/library/functions.html#isinstance)
630631
useful here, as well as the Python exception
631632
[`TypeError`](https://docs.python.org/3/library/exceptions.html#TypeError).
633+
634+
You can take this even further if your solution code involves multiple `ValueError`s or `TypeError`'s being raised at different locations.
635+
In this case, you want to make sure the function is raising the precise `Exception` that occurs at a specific point in your function.
636+
You might have noticed that `Exception`s can take a string argument corresponding to a message / description of the exception that has occurred.
637+
The [`pytest.raises()`](https://docs.pytest.org/en/stable/reference/reference.html#pytest.raises) context manager can query this message with its `match=` argument.
638+
See if you can use that to more precisely test the exceptions raised by your function.
639+
632640
Once you are done, commit your new files,
633641
and push the new commits to your remote repository on GitHub.
634642

643+
635644
::::::::::::::: solution
636645

637646
## Solution
@@ -683,6 +692,11 @@ from inflammation.models import patient_normalise
683692
None,
684693
TypeError,
685694
),
695+
(
696+
[4, 5, 6],
697+
None,
698+
ValueError,
699+
),
686700
(
687701
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
688702
[[0.33, 0.67, 1], [0.67, 0.83, 1], [0.78, 0.89, 1]],
@@ -695,8 +709,56 @@ def test_patient_normalise(test, expected, expect_raises):
695709
test = np.array(test)
696710
if expect_raises is not None:
697711
with pytest.raises(expect_raises):
698-
result = patient_normalise(test)
699-
npt.assert_allclose(result, np.array(expected), rtol=1e-2, atol=1e-2)
712+
patient_normalise(test)
713+
714+
else:
715+
result = patient_normalise(test)
716+
npt.assert_allclose(result, np.array(expected), rtol=1e-2, atol=1e-2)
717+
...
718+
```
719+
720+
Or, if you decided to match the specific exceptions in `test/test_models.py`:
721+
722+
```python
723+
from inflammation.models import patient_normalise
724+
...
725+
@pytest.mark.parametrize(
726+
"test, expected, expect_raises",
727+
[
728+
...
729+
(
730+
[[-1, 2, 3], [4, 5, 6], [7, 8, 9]],
731+
None,
732+
ValueError('inflammation values should be non-negative'),
733+
),
734+
(
735+
[4, 5, 6],
736+
None,
737+
ValueError('inflammation array should be 2-dimensional'),
738+
),
739+
(
740+
'hello',
741+
None,
742+
TypeError('data input should be ndarray'),
743+
),
744+
(
745+
3,
746+
None,
747+
TypeError('data input should be ndarray'),
748+
),
749+
(
750+
[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
751+
[[0.33, 0.67, 1], [0.67, 0.83, 1], [0.78, 0.89, 1]],
752+
None,
753+
)
754+
])
755+
def test_patient_normalise(test, expected, expect_raises):
756+
"""Test normalisation works for arrays of one and positive integers."""
757+
if isinstance(test, list):
758+
test = np.array(test)
759+
if expect_raises is not None:
760+
with pytest.raises(expect_raises, match=str(expect_raises)):
761+
patient_normalise(test)
700762

701763
else:
702764
result = patient_normalise(test)

md5sum.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"episodes/21-automatically-testing-software.md" "ea242bf1a65757da67bb2237fb522445" "site/built/21-automatically-testing-software.md" "2025-04-01"
2020
"episodes/22-scaling-up-unit-testing.md" "049c86638afb5e99590dbf04a3f92f61" "site/built/22-scaling-up-unit-testing.md" "2025-03-07"
2121
"episodes/23-continuous-integration-automated-testing.md" "3345e68ba82b2ff28da1f99d3e05e7a9" "site/built/23-continuous-integration-automated-testing.md" "2025-04-01"
22-
"episodes/24-diagnosing-issues-improving-robustness.md" "b9d217dd779141be70604c0b6b13195a" "site/built/24-diagnosing-issues-improving-robustness.md" "2024-12-06"
22+
"episodes/24-diagnosing-issues-improving-robustness.md" "32ac0fb6b1d419f5bc63c98d5acb2ab3" "site/built/24-diagnosing-issues-improving-robustness.md" "2025-04-17"
2323
"episodes/25-section2-optional-exercises.md" "439682a4955568fa290b79ab2b486797" "site/built/25-section2-optional-exercises.md" "2024-12-06"
2424
"episodes/30-section3-intro.md" "24e70667c1848061ecb3d42ecf17dbf8" "site/built/30-section3-intro.md" "2024-12-06"
2525
"episodes/31-software-requirements.md" "adf1d73eb1d6449a95378b3f931327e2" "site/built/31-software-requirements.md" "2025-02-25"

0 commit comments

Comments
 (0)