Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/pytest-handle-warning.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* catch division by zero warning messages in tests

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
25 changes: 19 additions & 6 deletions tests/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,19 +117,32 @@ def test_tth_to_q_bad(wavelength, tth, expected_error_type, expected_error_msg):


@pytest.mark.parametrize(
"q, expected_d",
"q, expected_d, warning_expected",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking having warning_expected explicitly provides a quick overview whether each test case provides a warning or not. @sbillinge how do you like it?

[
# UC1: User specified empty q values
(np.array([]), np.array([])),
# UC2: User specified valid q values
# Test conversion of q to d with valid values
# Case 1: empty q values, expect empty d values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put a high level contextual statement always to make it easier to review. To see what I mean you could look at my edits to the PR on scale_to and eq.

Here it would be something like

# test conversion of q to d with good values
# Case 1: empty q values, expect empty d values

...

# Case 2: valid q values, expect d values without warning

...

Case 3.  valid q values containing 0, expect d values with divide by zero warning 

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

(np.array([]), np.array([]), False),
# Case 2:
# 1. valid q values, expect d values without warning
(
np.array([0.1, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi]),
np.array([62.83185307, 2, 1, 0.66667, 0.5, 0.4]),
False,
),
# 2. valid q values containing 0, expect d values with divide by zero warning
(
np.array([0, 1 * np.pi, 2 * np.pi, 3 * np.pi, 4 * np.pi, 5 * np.pi]),
np.array([np.inf, 2, 1, 0.66667, 0.5, 0.4]),
True,
),
],
)
def test_q_to_d(q, expected_d):
actual_d = q_to_d(q)
def test_q_to_d(q, expected_d, warning_expected):
if warning_expected:
with pytest.warns(RuntimeWarning, match="divide by zero encountered in divide"):
actual_d = q_to_d(q)
else:
actual_d = q_to_d(q)
assert np.allclose(actual_d, expected_d)


Expand Down
Loading