Skip to content

Commit 5ef82b9

Browse files
committed
parametrize over different reduction operations
1 parent 003ffaf commit 5ef82b9

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

xarray_array_testing/reduction.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from contextlib import nullcontext
22

3+
import pytest
4+
35
import hypothesis.strategies as st
46
import xarray.testing.strategies as xrst
57
from hypothesis import given
@@ -12,22 +14,15 @@ class ReductionTests(DuckArrayTestMixin):
1214
def expected_errors(op, **parameters):
1315
return nullcontext()
1416

17+
@pytest.mark.parametrize("op", ["mean", "sum", "prod", "std", "var"])
1518
@given(st.data())
16-
def test_variable_mean(self, data):
17-
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))
18-
19-
with self.expected_errors("mean", variable=variable):
20-
actual = variable.mean().data
21-
expected = self.xp.mean(variable.data)
22-
23-
self.assert_equal(actual, expected)
24-
25-
@given(st.data())
26-
def test_variable_prod(self, data):
19+
def test_variable_mean(self, op, data):
2720
variable = data.draw(xrst.variables(array_strategy_fn=self.array_strategy_fn))
2821

29-
with self.expected_errors("prod", variable=variable):
30-
actual = variable.prod().data
31-
expected = self.xp.prod(variable.data)
22+
with self.expected_errors(op, variable=variable):
23+
# compute using xr.Variable.<OP>()
24+
actual = getattr(variable, op)().data
25+
# compute using xp.<OP>(array)
26+
expected = getattr(self.xp, op)(variable.data)
3227

3328
self.assert_equal(actual, expected)

0 commit comments

Comments
 (0)