Skip to content

Commit 7fd9663

Browse files
committed
Convert various tests to pytest style
1 parent 0bbee69 commit 7fd9663

25 files changed

Lines changed: 1162 additions & 1134 deletions

Tests/test_features.py

Lines changed: 83 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import io
2-
import unittest
32

3+
import pytest
44
from PIL import features
55

6-
from .helper import PillowTestCase
7-
86
try:
97
from PIL import _webp
108

@@ -13,78 +11,85 @@
1311
HAVE_WEBP = False
1412

1513

16-
class TestFeatures(PillowTestCase):
17-
def test_check(self):
18-
# Check the correctness of the convenience function
19-
for module in features.modules:
20-
self.assertEqual(features.check_module(module), features.check(module))
21-
for codec in features.codecs:
22-
self.assertEqual(features.check_codec(codec), features.check(codec))
23-
for feature in features.features:
24-
self.assertEqual(features.check_feature(feature), features.check(feature))
25-
26-
@unittest.skipUnless(HAVE_WEBP, "WebP not available")
27-
def test_webp_transparency(self):
28-
self.assertEqual(
29-
features.check("transp_webp"), not _webp.WebPDecoderBuggyAlpha()
30-
)
31-
self.assertEqual(features.check("transp_webp"), _webp.HAVE_TRANSPARENCY)
32-
33-
@unittest.skipUnless(HAVE_WEBP, "WebP not available")
34-
def test_webp_mux(self):
35-
self.assertEqual(features.check("webp_mux"), _webp.HAVE_WEBPMUX)
36-
37-
@unittest.skipUnless(HAVE_WEBP, "WebP not available")
38-
def test_webp_anim(self):
39-
self.assertEqual(features.check("webp_anim"), _webp.HAVE_WEBPANIM)
40-
41-
def test_check_modules(self):
42-
for feature in features.modules:
43-
self.assertIn(features.check_module(feature), [True, False])
44-
for feature in features.codecs:
45-
self.assertIn(features.check_codec(feature), [True, False])
46-
47-
def test_supported_modules(self):
48-
self.assertIsInstance(features.get_supported_modules(), list)
49-
self.assertIsInstance(features.get_supported_codecs(), list)
50-
self.assertIsInstance(features.get_supported_features(), list)
51-
self.assertIsInstance(features.get_supported(), list)
52-
53-
def test_unsupported_codec(self):
54-
# Arrange
55-
codec = "unsupported_codec"
56-
# Act / Assert
57-
self.assertRaises(ValueError, features.check_codec, codec)
58-
59-
def test_unsupported_module(self):
60-
# Arrange
61-
module = "unsupported_module"
62-
# Act / Assert
63-
self.assertRaises(ValueError, features.check_module, module)
64-
65-
def test_pilinfo(self):
66-
buf = io.StringIO()
67-
features.pilinfo(buf)
68-
out = buf.getvalue()
69-
lines = out.splitlines()
70-
self.assertEqual(lines[0], "-" * 68)
71-
self.assertTrue(lines[1].startswith("Pillow "))
72-
self.assertTrue(lines[2].startswith("Python "))
73-
lines = lines[3:]
74-
while lines[0].startswith(" "):
75-
lines = lines[1:]
76-
self.assertEqual(lines[0], "-" * 68)
77-
self.assertTrue(lines[1].startswith("Python modules loaded from "))
78-
self.assertTrue(lines[2].startswith("Binary modules loaded from "))
79-
self.assertEqual(lines[3], "-" * 68)
80-
jpeg = (
81-
"\n"
82-
+ "-" * 68
83-
+ "\n"
84-
+ "JPEG image/jpeg\n"
85-
+ "Extensions: .jfif, .jpe, .jpeg, .jpg\n"
86-
+ "Features: open, save\n"
87-
+ "-" * 68
88-
+ "\n"
89-
)
90-
self.assertIn(jpeg, out)
14+
def test_check():
15+
# Check the correctness of the convenience function
16+
for module in features.modules:
17+
assert features.check_module(module) == features.check(module)
18+
for codec in features.codecs:
19+
assert features.check_codec(codec) == features.check(codec)
20+
for feature in features.features:
21+
assert features.check_feature(feature) == features.check(feature)
22+
23+
24+
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
25+
def test_webp_transparency():
26+
assert features.check("transp_webp") != _webp.WebPDecoderBuggyAlpha()
27+
assert features.check("transp_webp") == _webp.HAVE_TRANSPARENCY
28+
29+
30+
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
31+
def test_webp_mux():
32+
assert features.check("webp_mux") == _webp.HAVE_WEBPMUX
33+
34+
35+
@pytest.mark.skipif(not HAVE_WEBP, reason="WebP not available")
36+
def test_webp_anim():
37+
assert features.check("webp_anim") == _webp.HAVE_WEBPANIM
38+
39+
40+
def test_check_modules():
41+
for feature in features.modules:
42+
assert features.check_module(feature) in [True, False]
43+
for feature in features.codecs:
44+
assert features.check_codec(feature) in [True, False]
45+
46+
47+
def test_supported_modules():
48+
assert isinstance(features.get_supported_modules(), list)
49+
assert isinstance(features.get_supported_codecs(), list)
50+
assert isinstance(features.get_supported_features(), list)
51+
assert isinstance(features.get_supported(), list)
52+
53+
54+
def test_unsupported_codec():
55+
# Arrange
56+
codec = "unsupported_codec"
57+
# Act / Assert
58+
with pytest.raises(ValueError):
59+
features.check_codec(codec)
60+
61+
62+
def test_unsupported_module():
63+
# Arrange
64+
module = "unsupported_module"
65+
# Act / Assert
66+
with pytest.raises(ValueError):
67+
features.check_module(module)
68+
69+
70+
def test_pilinfo():
71+
buf = io.StringIO()
72+
features.pilinfo(buf)
73+
out = buf.getvalue()
74+
lines = out.splitlines()
75+
assert lines[0] == "-" * 68
76+
assert lines[1].startswith("Pillow ")
77+
assert lines[2].startswith("Python ")
78+
lines = lines[3:]
79+
while lines[0].startswith(" "):
80+
lines = lines[1:]
81+
assert lines[0] == "-" * 68
82+
assert lines[1].startswith("Python modules loaded from ")
83+
assert lines[2].startswith("Binary modules loaded from ")
84+
assert lines[3] == "-" * 68
85+
jpeg = (
86+
"\n"
87+
+ "-" * 68
88+
+ "\n"
89+
+ "JPEG image/jpeg\n"
90+
+ "Extensions: .jfif, .jpe, .jpeg, .jpg\n"
91+
+ "Features: open, save\n"
92+
+ "-" * 68
93+
+ "\n"
94+
)
95+
assert jpeg in out

Tests/test_file_cur.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
1+
import pytest
12
from PIL import CurImagePlugin, Image
23

3-
from .helper import PillowTestCase
4-
54
TEST_FILE = "Tests/images/deerstalker.cur"
65

76

8-
class TestFileCur(PillowTestCase):
9-
def test_sanity(self):
10-
with Image.open(TEST_FILE) as im:
11-
self.assertEqual(im.size, (32, 32))
12-
self.assertIsInstance(im, CurImagePlugin.CurImageFile)
13-
# Check some pixel colors to ensure image is loaded properly
14-
self.assertEqual(im.getpixel((10, 1)), (0, 0, 0, 0))
15-
self.assertEqual(im.getpixel((11, 1)), (253, 254, 254, 1))
16-
self.assertEqual(im.getpixel((16, 16)), (84, 87, 86, 255))
7+
def test_sanity():
8+
with Image.open(TEST_FILE) as im:
9+
assert im.size == (32, 32)
10+
assert isinstance(im, CurImagePlugin.CurImageFile)
11+
# Check some pixel colors to ensure image is loaded properly
12+
assert im.getpixel((10, 1)) == (0, 0, 0, 0)
13+
assert im.getpixel((11, 1)) == (253, 254, 254, 1)
14+
assert im.getpixel((16, 16)) == (84, 87, 86, 255)
15+
1716

18-
def test_invalid_file(self):
19-
invalid_file = "Tests/images/flower.jpg"
17+
def test_invalid_file():
18+
invalid_file = "Tests/images/flower.jpg"
2019

21-
self.assertRaises(SyntaxError, CurImagePlugin.CurImageFile, invalid_file)
20+
with pytest.raises(SyntaxError):
21+
CurImagePlugin.CurImageFile(invalid_file)
2222

23-
no_cursors_file = "Tests/images/no_cursors.cur"
23+
no_cursors_file = "Tests/images/no_cursors.cur"
2424

25-
cur = CurImagePlugin.CurImageFile(TEST_FILE)
26-
cur.fp.close()
27-
with open(no_cursors_file, "rb") as cur.fp:
28-
self.assertRaises(TypeError, cur._open)
25+
cur = CurImagePlugin.CurImageFile(TEST_FILE)
26+
cur.fp.close()
27+
with open(no_cursors_file, "rb") as cur.fp:
28+
with pytest.raises(TypeError):
29+
cur._open()

Tests/test_file_fitsstub.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,47 @@
1+
import pytest
12
from PIL import FitsStubImagePlugin, Image
23

3-
from .helper import PillowTestCase
4-
54
TEST_FILE = "Tests/images/hopper.fits"
65

76

8-
class TestFileFitsStub(PillowTestCase):
9-
def test_open(self):
10-
# Act
11-
with Image.open(TEST_FILE) as im:
7+
def test_open():
8+
# Act
9+
with Image.open(TEST_FILE) as im:
10+
11+
# Assert
12+
assert im.format == "FITS"
13+
14+
# Dummy data from the stub
15+
assert im.mode == "F"
16+
assert im.size == (1, 1)
17+
1218

13-
# Assert
14-
self.assertEqual(im.format, "FITS")
19+
def test_invalid_file():
20+
# Arrange
21+
invalid_file = "Tests/images/flower.jpg"
1522

16-
# Dummy data from the stub
17-
self.assertEqual(im.mode, "F")
18-
self.assertEqual(im.size, (1, 1))
23+
# Act / Assert
24+
with pytest.raises(SyntaxError):
25+
FitsStubImagePlugin.FITSStubImageFile(invalid_file)
1926

20-
def test_invalid_file(self):
21-
# Arrange
22-
invalid_file = "Tests/images/flower.jpg"
2327

24-
# Act / Assert
25-
self.assertRaises(
26-
SyntaxError, FitsStubImagePlugin.FITSStubImageFile, invalid_file
27-
)
28+
def test_load():
29+
# Arrange
30+
with Image.open(TEST_FILE) as im:
2831

29-
def test_load(self):
30-
# Arrange
31-
with Image.open(TEST_FILE) as im:
32+
# Act / Assert: stub cannot load without an implemented handler
33+
with pytest.raises(IOError):
34+
im.load()
3235

33-
# Act / Assert: stub cannot load without an implemented handler
34-
self.assertRaises(IOError, im.load)
3536

36-
def test_save(self):
37-
# Arrange
38-
with Image.open(TEST_FILE) as im:
39-
dummy_fp = None
40-
dummy_filename = "dummy.filename"
37+
def test_save():
38+
# Arrange
39+
with Image.open(TEST_FILE) as im:
40+
dummy_fp = None
41+
dummy_filename = "dummy.filename"
4142

42-
# Act / Assert: stub cannot save without an implemented handler
43-
self.assertRaises(IOError, im.save, dummy_filename)
44-
self.assertRaises(
45-
IOError, FitsStubImagePlugin._save, im, dummy_fp, dummy_filename
46-
)
43+
# Act / Assert: stub cannot save without an implemented handler
44+
with pytest.raises(IOError):
45+
im.save(dummy_filename)
46+
with pytest.raises(IOError):
47+
FitsStubImagePlugin._save(im, dummy_fp, dummy_filename)

Tests/test_file_gribstub.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
1+
import pytest
12
from PIL import GribStubImagePlugin, Image
23

3-
from .helper import PillowTestCase, hopper
4+
from .helper import hopper
45

56
TEST_FILE = "Tests/images/WAlaska.wind.7days.grb"
67

78

8-
class TestFileGribStub(PillowTestCase):
9-
def test_open(self):
10-
# Act
11-
with Image.open(TEST_FILE) as im:
9+
def test_open():
10+
# Act
11+
with Image.open(TEST_FILE) as im:
1212

13-
# Assert
14-
self.assertEqual(im.format, "GRIB")
13+
# Assert
14+
assert im.format == "GRIB"
1515

16-
# Dummy data from the stub
17-
self.assertEqual(im.mode, "F")
18-
self.assertEqual(im.size, (1, 1))
16+
# Dummy data from the stub
17+
assert im.mode == "F"
18+
assert im.size == (1, 1)
1919

20-
def test_invalid_file(self):
21-
# Arrange
22-
invalid_file = "Tests/images/flower.jpg"
2320

24-
# Act / Assert
25-
self.assertRaises(
26-
SyntaxError, GribStubImagePlugin.GribStubImageFile, invalid_file
27-
)
21+
def test_invalid_file():
22+
# Arrange
23+
invalid_file = "Tests/images/flower.jpg"
2824

29-
def test_load(self):
30-
# Arrange
31-
with Image.open(TEST_FILE) as im:
25+
# Act / Assert
26+
with pytest.raises(SyntaxError):
27+
GribStubImagePlugin.GribStubImageFile(invalid_file)
3228

33-
# Act / Assert: stub cannot load without an implemented handler
34-
self.assertRaises(IOError, im.load)
3529

36-
def test_save(self):
37-
# Arrange
38-
im = hopper()
39-
tmpfile = self.tempfile("temp.grib")
30+
def test_load():
31+
# Arrange
32+
with Image.open(TEST_FILE) as im:
4033

41-
# Act / Assert: stub cannot save without an implemented handler
42-
self.assertRaises(IOError, im.save, tmpfile)
34+
# Act / Assert: stub cannot load without an implemented handler
35+
with pytest.raises(IOError):
36+
im.load()
37+
38+
39+
def test_save(tmp_path):
40+
# Arrange
41+
im = hopper()
42+
tmpfile = str(tmp_path / "temp.grib")
43+
44+
# Act / Assert: stub cannot save without an implemented handler
45+
with pytest.raises(IOError):
46+
im.save(tmpfile)

0 commit comments

Comments
 (0)