Skip to content

Commit 2c50723

Browse files
committed
Convert some tests to pytest style
To better follow conventional pytest style, this removes the outer wrapper class in favor of a function for some tests. These tests were picked as they are relatively simple and presented no barriers to a quick port. The assert* methods are replaced with assert statements. When necessary, a fixture is used to create a temporary directory. This commit does not convert the entire test suite to this style as some test classes use methods or other advanced features that are difficult to automatically convert. The goal is to address these issues in followup commits. Refs #4193
1 parent 3aa5d56 commit 2c50723

20 files changed

Lines changed: 507 additions & 499 deletions

Tests/test_000_sanity.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import PIL
22
import PIL.Image
33

4-
from .helper import PillowTestCase
54

5+
def test_sanity():
6+
# Make sure we have the binary extension
7+
PIL.Image.core.new("L", (100, 100))
68

7-
class TestSanity(PillowTestCase):
8-
def test_sanity(self):
9+
# Create an image and do stuff with it.
10+
im = PIL.Image.new("1", (100, 100))
11+
assert (im.mode, im.size) == ("1", (100, 100))
12+
assert len(im.tobytes()) == 1300
913

10-
# Make sure we have the binary extension
11-
PIL.Image.core.new("L", (100, 100))
12-
13-
# Create an image and do stuff with it.
14-
im = PIL.Image.new("1", (100, 100))
15-
self.assertEqual((im.mode, im.size), ("1", (100, 100)))
16-
self.assertEqual(len(im.tobytes()), 1300)
17-
18-
# Create images in all remaining major modes.
19-
PIL.Image.new("L", (100, 100))
20-
PIL.Image.new("P", (100, 100))
21-
PIL.Image.new("RGB", (100, 100))
22-
PIL.Image.new("I", (100, 100))
23-
PIL.Image.new("F", (100, 100))
14+
# Create images in all remaining major modes.
15+
PIL.Image.new("L", (100, 100))
16+
PIL.Image.new("P", (100, 100))
17+
PIL.Image.new("RGB", (100, 100))
18+
PIL.Image.new("I", (100, 100))
19+
PIL.Image.new("F", (100, 100))

Tests/test_binary.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
from PIL import _binary
22

3-
from .helper import PillowTestCase
43

4+
def test_standard():
5+
assert _binary.i8(b"*") == 42
6+
assert _binary.o8(42) == b"*"
57

6-
class TestBinary(PillowTestCase):
7-
def test_standard(self):
8-
self.assertEqual(_binary.i8(b"*"), 42)
9-
self.assertEqual(_binary.o8(42), b"*")
108

11-
def test_little_endian(self):
12-
self.assertEqual(_binary.i16le(b"\xff\xff\x00\x00"), 65535)
13-
self.assertEqual(_binary.i32le(b"\xff\xff\x00\x00"), 65535)
9+
def test_little_endian():
10+
assert _binary.i16le(b"\xff\xff\x00\x00") == 65535
11+
assert _binary.i32le(b"\xff\xff\x00\x00") == 65535
1412

15-
self.assertEqual(_binary.o16le(65535), b"\xff\xff")
16-
self.assertEqual(_binary.o32le(65535), b"\xff\xff\x00\x00")
13+
assert _binary.o16le(65535) == b"\xff\xff"
14+
assert _binary.o32le(65535) == b"\xff\xff\x00\x00"
1715

18-
def test_big_endian(self):
19-
self.assertEqual(_binary.i16be(b"\x00\x00\xff\xff"), 0)
20-
self.assertEqual(_binary.i32be(b"\x00\x00\xff\xff"), 65535)
2116

22-
self.assertEqual(_binary.o16be(65535), b"\xff\xff")
23-
self.assertEqual(_binary.o32be(65535), b"\x00\x00\xff\xff")
17+
def test_big_endian():
18+
assert _binary.i16be(b"\x00\x00\xff\xff") == 0
19+
assert _binary.i32be(b"\x00\x00\xff\xff") == 65535
20+
21+
assert _binary.o16be(65535) == b"\xff\xff"
22+
assert _binary.o32be(65535) == b"\x00\x00\xff\xff"

Tests/test_box_blur.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@
1414
# fmt: on
1515

1616

17-
class TestBoxBlurApi(PillowTestCase):
18-
def test_imageops_box_blur(self):
19-
i = sample.filter(ImageFilter.BoxBlur(1))
20-
self.assertEqual(i.mode, sample.mode)
21-
self.assertEqual(i.size, sample.size)
22-
self.assertIsInstance(i, Image.Image)
17+
def test_imageops_box_blur():
18+
i = sample.filter(ImageFilter.BoxBlur(1))
19+
assert i.mode == sample.mode
20+
assert i.size == sample.size
21+
assert isinstance(i, Image.Image)
2322

2423

2524
class TestBoxBlur(PillowTestCase):

Tests/test_core_resources.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,29 @@
66
from .helper import PillowTestCase, is_pypy
77

88

9-
class TestCoreStats(PillowTestCase):
10-
def test_get_stats(self):
11-
# Create at least one image
12-
Image.new("RGB", (10, 10))
13-
14-
stats = Image.core.get_stats()
15-
self.assertIn("new_count", stats)
16-
self.assertIn("reused_blocks", stats)
17-
self.assertIn("freed_blocks", stats)
18-
self.assertIn("allocated_blocks", stats)
19-
self.assertIn("reallocated_blocks", stats)
20-
self.assertIn("blocks_cached", stats)
21-
22-
def test_reset_stats(self):
23-
Image.core.reset_stats()
24-
25-
stats = Image.core.get_stats()
26-
self.assertEqual(stats["new_count"], 0)
27-
self.assertEqual(stats["reused_blocks"], 0)
28-
self.assertEqual(stats["freed_blocks"], 0)
29-
self.assertEqual(stats["allocated_blocks"], 0)
30-
self.assertEqual(stats["reallocated_blocks"], 0)
31-
self.assertEqual(stats["blocks_cached"], 0)
9+
def test_get_stats():
10+
# Create at least one image
11+
Image.new("RGB", (10, 10))
12+
13+
stats = Image.core.get_stats()
14+
assert "new_count" in stats
15+
assert "reused_blocks" in stats
16+
assert "freed_blocks" in stats
17+
assert "allocated_blocks" in stats
18+
assert "reallocated_blocks" in stats
19+
assert "blocks_cached" in stats
20+
21+
22+
def test_reset_stats():
23+
Image.core.reset_stats()
24+
25+
stats = Image.core.get_stats()
26+
assert stats["new_count"] == 0
27+
assert stats["reused_blocks"] == 0
28+
assert stats["freed_blocks"] == 0
29+
assert stats["allocated_blocks"] == 0
30+
assert stats["reallocated_blocks"] == 0
31+
assert stats["blocks_cached"] == 0
3232

3333

3434
class TestCoreMemory(PillowTestCase):

Tests/test_file_bufrstub.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 BufrStubImagePlugin, Image
23

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

56
TEST_FILE = "Tests/images/gfs.t06z.rassda.tm00.bufr_d"
67

78

8-
class TestFileBufrStub(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, "BUFR")
13+
# Assert
14+
assert im.format == "BUFR"
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, BufrStubImagePlugin.BufrStubImageFile, 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+
BufrStubImagePlugin.BufrStubImageFile(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.bufr")
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.bufr")
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)