Skip to content

Commit 2359c76

Browse files
authored
Merge pull request #9968 from chainer-ci/bp-9965-v14-testing-import-no-pytest
[backport] BUG: Avoid hard `pytest` dependency in `cupy.testing` (and test)
2 parents 7ed30be + 978638f commit 2359c76

3 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎cupy/testing/_condition.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import functools
44
import os
55

6-
import pytest
76
import unittest
87

98

@@ -29,6 +28,7 @@ def repeat_with_success_at_least(times, min_success):
2928
case is regarded as passed.
3029
3130
"""
31+
import pytest
3232

3333
assert times >= min_success
3434

‎cupy/testing/_random.py‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
import types
99
import unittest
1010

11-
import pytest
12-
1311
import cupy
1412

1513

@@ -108,6 +106,7 @@ def fix_random():
108106
It should not be applied within ``condition.retry`` or
109107
``condition.repeat``.
110108
"""
109+
import pytest
111110

112111
# TODO(niboshi): Prevent this decorator from being applied within
113112
# condition.repeat or condition.retry decorators. That would repeat

‎tests/cupy_tests/test_init.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,27 @@ def test_import_error(self):
5757
assert stdoutdata in (b'', b'RuntimeError\n')
5858

5959

60+
def test_testing_import_does_not_require_pytest():
61+
# cupy.testing import is lazy, but some environments tend to inspect
62+
# it anyway. Check that even an * import doesn't require pytest.
63+
returncode, stdoutdata, stderrdata = _run_script('''
64+
import sys
65+
66+
class BlockPytest:
67+
def find_spec(self, fullname, path=None, target=None):
68+
if fullname == 'pytest' or fullname.startswith('_pytest'):
69+
raise ModuleNotFoundError(fullname)
70+
return None
71+
import sys
72+
sys.meta_path.insert(0, BlockPytest())
73+
74+
# non-lazy import should succeed even if pytest is not available
75+
from cupy.testing import *
76+
''')
77+
assert returncode == 0, 'stderr: {!r}'.format(stderrdata)
78+
assert stdoutdata == b''
79+
80+
6081
if not cupy.cuda.runtime.is_hip:
6182
visible = 'CUDA_VISIBLE_DEVICES'
6283
else:

0 commit comments

Comments
 (0)