Skip to content

Commit 116d087

Browse files
committed
test: clean up and improve conditions for optional torch/TF tests
1 parent 2aafdfb commit 116d087

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

imgcat/test_imgcat.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
from imgcat import imgcat
1919

2020

21+
def _importable(package_name: str) -> bool:
22+
import importlib.util
23+
return importlib.util.find_spec(package_name) is not None
24+
25+
2126
@pytest.fixture
2227
def mock_env(monkeypatch, env_profile):
2328
"""Mock environment variables (especially, TMUX)"""
@@ -138,9 +143,10 @@ def test_numpy(self):
138143
a[..., 0], a[..., 1], a[..., 2] = 0x37 / 255., 0xb2 / 255., 0x4d / 255.
139144
imgcat(a)
140145

146+
@pytest.mark.skipif(not _importable('torch'), reason="No torch installed")
141147
@parametrize_env
142148
def test_torch(self):
143-
import torch
149+
import torch # type: ignore
144150

145151
# uint8, grayscale
146152
with self.capture_and_validate():
@@ -156,13 +162,15 @@ def test_torch(self):
156162
a = torch.ones([3, 32, 32], dtype=torch.uint8) * 0
157163
imgcat(a)
158164

165+
@pytest.mark.skipif(not _importable('tensorflow'), reason="No tensorflow installed")
159166
@parametrize_env
160167
def test_tensorflow(self):
161168
try:
162169
import tensorflow.compat.v2 as tf # type: ignore # noqa
163170
except ImportError:
164171
pytest.skip("No tensorflow available")
165172

173+
import tensorflow.compat.v2 as tf # type: ignore
166174
tf.enable_v2_behavior()
167175
assert tf.executing_eagerly(), "Eager execution should be enabled."
168176

@@ -250,7 +258,6 @@ def test_args_another(self):
250258
preserve_aspect_ratio=False, fp=b)
251259

252260
v = b.getvalue()
253-
assert b'size=82;' in v
254261
assert b'height=12;' in v
255262
assert b'width=10;' in v
256263
assert b'name=Zm9vLnBuZw==;' in v # foo.png

0 commit comments

Comments
 (0)