Skip to content

Commit e7b5b88

Browse files
committed
Migrate tests from unittest to pytest
1 parent f07f6a4 commit e7b5b88

File tree

9 files changed

+84
-111
lines changed

9 files changed

+84
-111
lines changed

dpnp/tests/third_party/cupy/core_tests/test_elementwise.py

Lines changed: 12 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest
1+
from __future__ import annotations
22

33
import numpy
44
import pytest
@@ -12,7 +12,7 @@
1212
from dpnp.tests.third_party.cupy import testing
1313

1414

15-
class TestElementwise(unittest.TestCase):
15+
class TestElementwise:
1616

1717
def check_copy(self, dtype, src_id, dst_id):
1818
with cuda.Device(src_id):
@@ -33,7 +33,7 @@ def test_copy(self, dtype):
3333
def test_copy_multigpu_nopeer(self, dtype):
3434
if cuda.runtime.deviceCanAccessPeer(0, 1) == 1:
3535
pytest.skip("peer access is available")
36-
with self.assertRaises(ValueError):
36+
with pytest.raises(ValueError):
3737
self.check_copy(dtype, 0, 1)
3838

3939
@pytest.mark.skip("elementwise_copy() argument isn't supported")
@@ -74,27 +74,26 @@ def test_copy_orders(self, order):
7474

7575

7676
@pytest.mark.skip("`ElementwiseKernel` isn't supported")
77-
class TestElementwiseInvalidShape(unittest.TestCase):
77+
class TestElementwiseInvalidShape:
7878

7979
def test_invalid_shape(self):
80-
with self.assertRaisesRegex(ValueError, "Out shape is mismatched"):
80+
with pytest.raises(ValueError, match="Out shape is mismatched"):
8181
f = cupy.ElementwiseKernel("T x", "T y", "y += x")
8282
x = cupy.arange(12).reshape(3, 4)
8383
y = cupy.arange(4)
8484
f(x, y)
8585

8686

8787
@pytest.mark.skip("`ElementwiseKernel` isn't supported")
88-
class TestElementwiseInvalidArgument(unittest.TestCase):
88+
class TestElementwiseInvalidArgument:
8989

9090
def test_invalid_kernel_name(self):
91-
with self.assertRaisesRegex(ValueError, "Invalid kernel name"):
91+
with pytest.raises(ValueError, match="Invalid kernel name"):
9292
cupy.ElementwiseKernel("T x", "", "", "1")
9393

9494

95-
class TestElementwiseType(unittest.TestCase):
95+
class TestElementwiseType:
9696

97-
@testing.with_requires("numpy>=2.0")
9897
@testing.for_int_dtypes(no_bool=True)
9998
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
10099
def test_large_int_upper_1(self, xp, dtype):
@@ -105,14 +104,6 @@ def test_large_int_upper_1(self, xp, dtype):
105104
@testing.for_int_dtypes(no_bool=True)
106105
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
107106
def test_large_int_upper_2(self, xp, dtype):
108-
if numpy_version() < "2.0.0":
109-
flag = dtype in [xp.int16, xp.int32, xp.int64, xp.longlong]
110-
if xp.issubdtype(dtype, xp.unsignedinteger) or flag:
111-
pytest.skip("numpy doesn't raise OverflowError")
112-
113-
if dtype in [xp.int8, xp.intc] and is_win_platform():
114-
pytest.skip("numpy promotes dtype differently")
115-
116107
a = xp.array([1], dtype=xp.int8)
117108
b = xp.iinfo(dtype).max - 1
118109
return a + b
@@ -121,62 +112,38 @@ def test_large_int_upper_2(self, xp, dtype):
121112
@testing.numpy_cupy_array_equal()
122113
def test_large_int_upper_3(self, xp, dtype):
123114
if (
124-
numpy.issubdtype(dtype, numpy.unsignedinteger)
125-
and numpy_version() < "2.0.0"
126-
):
127-
pytest.skip("numpy promotes dtype differently")
128-
elif (
129115
dtype in (numpy.uint64, numpy.ulonglong)
130116
and not has_support_aspect64()
131117
):
132118
pytest.skip("no fp64 support")
133119

134120
a = xp.array([xp.iinfo(dtype).max], dtype=dtype)
135-
b = numpy.int8(0)
121+
b = xp.int8(0)
136122
return a + b
137123

138124
@testing.for_int_dtypes(no_bool=True)
139125
@testing.numpy_cupy_array_equal()
140126
def test_large_int_upper_4(self, xp, dtype):
141127
if (
142-
numpy.issubdtype(dtype, numpy.unsignedinteger)
143-
and numpy_version() < "2.0.0"
144-
):
145-
pytest.skip("numpy promotes dtype differently")
146-
elif (
147128
dtype in (numpy.uint64, numpy.ulonglong)
148129
and not has_support_aspect64()
149130
):
150131
pytest.skip("no fp64 support")
151132

152133
a = xp.array([xp.iinfo(dtype).max - 1], dtype=dtype)
153-
b = numpy.int8(1)
134+
b = xp.int8(1)
154135
return a + b
155136

156137
@testing.for_int_dtypes(no_bool=True)
157138
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
158139
def test_large_int_lower_1(self, xp, dtype):
159-
if numpy_version() < "2.0.0":
160-
if dtype in [xp.int16, xp.int32, xp.int64, xp.longlong]:
161-
pytest.skip("numpy doesn't raise OverflowError")
162-
163-
if dtype in [xp.int8, xp.intc] and is_win_platform():
164-
pytest.skip("numpy promotes dtype differently")
165-
166140
a = xp.array([0], dtype=xp.int8)
167141
b = xp.iinfo(dtype).min
168142
return a + b
169143

170144
@testing.for_int_dtypes(no_bool=True)
171145
@testing.numpy_cupy_array_equal(accept_error=OverflowError)
172146
def test_large_int_lower_2(self, xp, dtype):
173-
if numpy_version() < "2.0.0":
174-
if dtype in [xp.int16, xp.int32, xp.int64, xp.longlong]:
175-
pytest.skip("numpy doesn't raise OverflowError")
176-
177-
if dtype in [xp.int8, xp.intc] and is_win_platform():
178-
pytest.skip("numpy promotes dtype differently")
179-
180147
a = xp.array([-1], dtype=xp.int8)
181148
b = xp.iinfo(dtype).min + 1
182149
return a + b
@@ -185,18 +152,13 @@ def test_large_int_lower_2(self, xp, dtype):
185152
@testing.numpy_cupy_array_equal()
186153
def test_large_int_lower_3(self, xp, dtype):
187154
if (
188-
numpy.issubdtype(dtype, numpy.unsignedinteger)
189-
and numpy_version() < "2.0.0"
190-
):
191-
pytest.skip("numpy promotes dtype differently")
192-
elif (
193155
dtype in (numpy.uint64, numpy.ulonglong)
194156
and not has_support_aspect64()
195157
):
196158
pytest.skip("no fp64 support")
197159

198160
a = xp.array([xp.iinfo(dtype).min], dtype=dtype)
199-
b = numpy.int8(0)
161+
b = xp.int8(0)
200162
return a + b
201163

202164
@testing.for_int_dtypes(no_bool=True)
@@ -209,5 +171,5 @@ def test_large_int_lower_4(self, xp, dtype):
209171
pytest.skip("no fp64 support")
210172

211173
a = xp.array([xp.iinfo(dtype).min + 1], dtype=dtype)
212-
b = numpy.int8(-1)
174+
b = xp.int8(-1)
213175
return a + b

dpnp/tests/third_party/cupy/core_tests/test_function.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

3-
import unittest
4-
3+
import numpy
54
import pytest
65

76
import dpnp as cupy
@@ -23,7 +22,7 @@ def _compile_func(kernel_name, code):
2322
return mod.get_function(kernel_name)
2423

2524

26-
class TestFunction(unittest.TestCase):
25+
class TestFunction:
2726

2827
def test_python_scalar(self):
2928
code = """

dpnp/tests/third_party/cupy/core_tests/test_include.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from __future__ import annotations
2+
13
import os
2-
from unittest import mock
34

45
import pytest
56

@@ -71,22 +72,23 @@ def test_nvcc(self):
7172
_code_nvcc, options=options, arch=arch
7273
)
7374

74-
def test_nvrtc(self):
75+
def test_nvrtc(self, monkeypatch):
7576
cuda_ver = cupy.cuda.runtime.runtimeGetVersion()
7677
options = self._get_options()
7778
for arch in self._get_cuda_archs():
78-
with mock.patch(
79-
"cupy.cuda.compiler._get_arch_for_options_for_nvrtc",
79+
monkeypatch.setattr(
80+
cupy.cuda.compiler,
81+
"_get_arch_for_options_for_nvrtc",
8082
lambda _: (f"-arch=compute_{arch}", "ptx"),
81-
):
83+
)
84+
cupy.cuda.compiler.compile_using_nvrtc(_code_nvrtc, options=options)
85+
86+
if cuda_ver >= 11010:
87+
monkeypatch.setattr(
88+
cupy.cuda.compiler,
89+
"_get_arch_for_options_for_nvrtc",
90+
lambda _: (f"-arch=sm_{arch}", "cubin"),
91+
)
8292
cupy.cuda.compiler.compile_using_nvrtc(
8393
_code_nvrtc, options=options
8494
)
85-
if cuda_ver >= 11010:
86-
with mock.patch(
87-
"cupy.cuda.compiler._get_arch_for_options_for_nvrtc",
88-
lambda _: (f"-arch=sm_{arch}", "cubin"),
89-
):
90-
cupy.cuda.compiler.compile_using_nvrtc(
91-
_code_nvrtc, options=options
92-
)

dpnp/tests/third_party/cupy/core_tests/test_ndarray_complex_ops.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest
1+
from __future__ import annotations
22

33
import numpy
44
import pytest
@@ -7,7 +7,7 @@
77
from dpnp.tests.third_party.cupy import testing
88

99

10-
class TestConj(unittest.TestCase):
10+
class TestConj:
1111

1212
@testing.for_all_dtypes()
1313
@testing.numpy_cupy_array_almost_equal()
@@ -38,7 +38,7 @@ def test_conjugate_pass(self, xp, dtype):
3838
return y
3939

4040

41-
class TestAngle(unittest.TestCase):
41+
class TestAngle:
4242

4343
# For dtype=int8, uint8, NumPy returns float16, but dpnp returns float32
4444
# so type_check=False
@@ -49,7 +49,7 @@ def test_angle(self, xp, dtype):
4949
return xp.angle(x)
5050

5151

52-
class TestRealImag(unittest.TestCase):
52+
class TestRealImag:
5353

5454
@testing.for_all_dtypes()
5555
@testing.numpy_cupy_array_almost_equal(accept_error=False)
@@ -157,7 +157,7 @@ def test_imag_inplace(self, dtype):
157157
assert cupy.all(x == expected)
158158

159159

160-
class TestScalarConversion(unittest.TestCase):
160+
class TestScalarConversion:
161161

162162
@testing.for_all_dtypes()
163163
def test_scalar_conversion(self, dtype):

dpnp/tests/third_party/cupy/core_tests/test_ndarray_contiguity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import unittest
1+
from __future__ import annotations
22

33
from dpnp.tests.third_party.cupy import testing
44

55

6-
class TestArrayContiguity(unittest.TestCase):
6+
class TestArrayContiguity:
77

88
def test_is_contiguous(self):
99
a = testing.shaped_arange((2, 3, 4))
Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,69 @@
11
from __future__ import annotations
22

3-
import unittest
4-
53
import numpy
64
import pytest
75

86
import dpnp as cupy
7+
8+
# from cupy.cuda import runtime
99
from dpnp.tests.third_party.cupy import testing
1010

1111

12-
@testing.parameterize(
13-
{"shape": ()},
14-
{"shape": (1,)},
15-
{"shape": (1, 1, 1)},
12+
@pytest.mark.parametrize(
13+
"shape",
14+
[
15+
(),
16+
(1,),
17+
(1, 1, 1),
18+
],
1619
)
17-
class TestNdarrayItem(unittest.TestCase):
20+
class TestNdarrayItem:
1821

1922
@testing.for_all_dtypes()
2023
@testing.numpy_cupy_equal()
21-
def test_item(self, xp, dtype):
22-
a = xp.full(self.shape, 3, dtype=dtype)
24+
def test_item(self, xp, dtype, shape):
25+
a = xp.full(shape, 3, dtype=dtype)
2326
return a.item()
2427

2528

26-
@testing.parameterize(
27-
{"shape": (0,)},
28-
{"shape": (2, 3)},
29-
{"shape": (1, 0, 1)},
29+
@pytest.mark.parametrize(
30+
"shape",
31+
[
32+
(0,),
33+
(2, 3),
34+
(1, 0, 1),
35+
],
3036
)
31-
class TestNdarrayItemRaise(unittest.TestCase):
37+
class TestNdarrayItemRaise:
3238

33-
def test_item(self):
39+
def test_item(self, shape):
3440
for xp in (numpy, cupy):
35-
a = testing.shaped_arange(self.shape, xp, xp.float32)
41+
a = testing.shaped_arange(shape, xp, xp.float32)
3642
with pytest.raises(ValueError):
3743
a.item()
3844

3945

40-
@testing.parameterize(
41-
{"shape": ()},
42-
{"shape": (1,)},
43-
{"shape": (2, 3)},
44-
{"shape": (2, 3), "order": "C"},
45-
{"shape": (2, 3), "order": "F"},
46+
@pytest.mark.parametrize(
47+
"shape, order",
48+
[
49+
((), None),
50+
((1,), None),
51+
((2, 3), None),
52+
((2, 3), "C"),
53+
((2, 3), "F"),
54+
],
4655
)
47-
class TestNdarrayToBytes(unittest.TestCase):
56+
class TestNdarrayToBytes:
4857

4958
@testing.for_all_dtypes()
5059
@testing.numpy_cupy_equal()
51-
def test_item(self, xp, dtype):
52-
a = testing.shaped_arange(self.shape, xp, dtype)
53-
if hasattr(self, "order"):
54-
return a.tobytes(self.order)
60+
def test_item(self, xp, dtype, shape, order):
61+
# if runtime.is_hip and (
62+
# shape == (1,) or (shape == (2, 3) and order is None)
63+
# ):
64+
# pytest.xfail("ROCm/HIP may have a bug")
65+
a = testing.shaped_arange(shape, xp, dtype)
66+
if order is not None:
67+
return a.tobytes(order)
5568
else:
5669
return a.tobytes()

dpnp/tests/third_party/cupy/core_tests/test_ndarray_owndata.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import unittest
1+
from __future__ import annotations
22

33
import pytest
44

@@ -7,9 +7,9 @@
77
pytest.skip("owndata attribute is not supported", allow_module_level=True)
88

99

10-
class TestArrayOwndata(unittest.TestCase):
10+
class TestArrayOwndata:
1111

12-
def setUp(self):
12+
def setup_method(self):
1313
self.a = _core.ndarray(())
1414

1515
def test_original_array(self):

0 commit comments

Comments
 (0)