Skip to content

Commit d1a21b5

Browse files
committed
Make tests more idiomatic for pytest
We don't need to put tests in a class anymore. I also sprinkled some type hints here and there. Left a few because the commit would be too big otherwise.
1 parent 3c0f496 commit d1a21b5

18 files changed

+1723
-1719
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fate-suite:
2828
rsync -vrltLW rsync://fate-suite.ffmpeg.org/fate-suite/ tests/assets/fate-suite/
2929

3030
lint:
31-
$(PIP) install -U black isort flake8 flake8-pyproject pillow numpy mypy==1.11.2
31+
$(PIP) install -U black isort flake8 flake8-pyproject pillow numpy mypy==1.11.2 pytest
3232
black --check av examples tests setup.py
3333
flake8 av
3434
isort --check-only --diff av examples tests

tests/common.py

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import types
66
from unittest import TestCase as _Base
77

8+
import numpy as np
9+
810
from av.datasets import fate as fate_suite
911

1012
try:
@@ -55,7 +57,7 @@ def asset(*args: str) -> str:
5557
os.environ["PYAV_TESTDATA_DIR"] = asset()
5658

5759

58-
def fate_png():
60+
def fate_png() -> str:
5961
return fate_suite("png1/55c99e750a5fd6_50314226.png")
6062

6163

@@ -85,33 +87,32 @@ def _inner(self, *args, **kwargs):
8587
return _inner
8688

8789

88-
class MethodLogger:
89-
def __init__(self, obj):
90-
self._obj = obj
91-
self._log = []
90+
def assertNdarraysEqual(a: np.ndarray, b: np.ndarray) -> None:
91+
assert a.shape == b.shape
92+
93+
comparison = a == b
94+
if not comparison.all():
95+
it = np.nditer(comparison, flags=["multi_index"])
96+
msg = ""
97+
for equal in it:
98+
if not equal:
99+
msg += "- arrays differ at index {}; {} {}\n".format(
100+
it.multi_index,
101+
a[it.multi_index],
102+
b[it.multi_index],
103+
)
104+
assert False, f"ndarrays contents differ\n{msg}"
92105

93-
def __getattr__(self, name):
94-
value = getattr(self._obj, name)
95-
if isinstance(
96-
value,
97-
(
98-
types.MethodType,
99-
types.FunctionType,
100-
types.BuiltinFunctionType,
101-
types.BuiltinMethodType,
102-
),
103-
):
104-
return functools.partial(self._method, name, value)
105-
else:
106-
self._log.append(("__getattr__", (name,), {}))
107-
return value
108106

109-
def _method(self, name, meth, *args, **kwargs):
110-
self._log.append((name, args, kwargs))
111-
return meth(*args, **kwargs)
107+
def assertImagesAlmostEqual(a, b, epsilon=0.1):
108+
import PIL.ImageFilter as ImageFilter
112109

113-
def _filter(self, type_):
114-
return [log for log in self._log if log[0] == type_]
110+
assert a.size == b.size
111+
a = a.filter(ImageFilter.BLUR).getdata()
112+
b = b.filter(ImageFilter.BLUR).getdata()
113+
for i, ax, bx in zip(range(len(a)), a, b):
114+
diff = sum(abs(ac / 256 - bc / 256) for ac, bc in zip(ax, bx)) / 3
115+
assert diff < epsilon, f"images differed by {diff} at index {i}; {ax} {bx}"
115116

116117

117118
class TestCase(_Base):
@@ -129,34 +130,3 @@ def sandboxed(self, *args, **kwargs) -> str:
129130
kwargs.setdefault("sandbox", self.sandbox)
130131
kwargs.setdefault("timed", True)
131132
return sandboxed(*args, **kwargs)
132-
133-
def assertNdarraysEqual(self, a, b):
134-
import numpy
135-
136-
self.assertEqual(a.shape, b.shape)
137-
138-
comparison = a == b
139-
if not comparison.all():
140-
it = numpy.nditer(comparison, flags=["multi_index"])
141-
msg = ""
142-
for equal in it:
143-
if not equal:
144-
msg += "- arrays differ at index %s; %s %s\n" % (
145-
it.multi_index,
146-
a[it.multi_index],
147-
b[it.multi_index],
148-
)
149-
self.fail("ndarrays contents differ\n%s" % msg)
150-
151-
def assertImagesAlmostEqual(self, a, b, epsilon=0.1, *args):
152-
import PIL.ImageFilter as ImageFilter
153-
154-
self.assertEqual(a.size, b.size, "sizes dont match")
155-
a = a.filter(ImageFilter.BLUR).getdata()
156-
b = b.filter(ImageFilter.BLUR).getdata()
157-
for i, ax, bx in zip(range(len(a)), a, b):
158-
diff = sum(abs(ac / 256 - bc / 256) for ac, bc in zip(ax, bx)) / 3
159-
if diff > epsilon:
160-
self.fail(
161-
"images differed by %s at index %d; %s %s" % (diff, i, ax, bx)
162-
)

tests/test_audioformat.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
import sys
22

3+
import pytest
4+
35
from av import AudioFormat
46

5-
from .common import TestCase
6-
7-
postfix = "le" if sys.byteorder == "little" else "be"
8-
9-
10-
class TestAudioFormats(TestCase):
11-
def test_s16_inspection(self) -> None:
12-
fmt = AudioFormat("s16")
13-
assert fmt.name == "s16"
14-
assert not fmt.is_planar
15-
assert fmt.bits == 16
16-
assert fmt.bytes == 2
17-
assert fmt.container_name == "s16" + postfix
18-
assert fmt.planar.name == "s16p"
19-
assert fmt.packed is fmt
20-
21-
def test_s32p_inspection(self) -> None:
22-
fmt = AudioFormat("s32p")
23-
assert fmt.name == "s32p"
24-
assert fmt.is_planar
25-
assert fmt.bits == 32
26-
assert fmt.bytes == 4
27-
self.assertRaises(ValueError, lambda: fmt.container_name)
7+
8+
def test_s16_inspection() -> None:
9+
fmt = AudioFormat("s16")
10+
postfix = "le" if sys.byteorder == "little" else "be"
11+
12+
assert fmt.name == "s16"
13+
assert not fmt.is_planar
14+
assert fmt.bits == 16
15+
assert fmt.bytes == 2
16+
assert fmt.container_name == "s16" + postfix
17+
assert fmt.planar.name == "s16p"
18+
assert fmt.packed is fmt
19+
20+
21+
def test_s32p_inspection() -> None:
22+
fmt = AudioFormat("s32p")
23+
assert fmt.name == "s32p"
24+
assert fmt.is_planar
25+
assert fmt.bits == 32
26+
assert fmt.bytes == 4
27+
28+
pytest.raises(ValueError, lambda: fmt.container_name)

0 commit comments

Comments
 (0)