Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert some tests to pytest style #4369

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions Tests/test_000_sanity.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import PIL
import PIL.Image

from .helper import PillowTestCase

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

class TestSanity(PillowTestCase):
def test_sanity(self):
# Create an image and do stuff with it.
im = PIL.Image.new("1", (100, 100))
assert (im.mode, im.size) == ("1", (100, 100))
assert len(im.tobytes()) == 1300

# Make sure we have the binary extension
PIL.Image.core.new("L", (100, 100))

# Create an image and do stuff with it.
im = PIL.Image.new("1", (100, 100))
self.assertEqual((im.mode, im.size), ("1", (100, 100)))
self.assertEqual(len(im.tobytes()), 1300)

# Create images in all remaining major modes.
PIL.Image.new("L", (100, 100))
PIL.Image.new("P", (100, 100))
PIL.Image.new("RGB", (100, 100))
PIL.Image.new("I", (100, 100))
PIL.Image.new("F", (100, 100))
# Create images in all remaining major modes.
PIL.Image.new("L", (100, 100))
PIL.Image.new("P", (100, 100))
PIL.Image.new("RGB", (100, 100))
PIL.Image.new("I", (100, 100))
PIL.Image.new("F", (100, 100))
29 changes: 14 additions & 15 deletions Tests/test_binary.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
from PIL import _binary

from .helper import PillowTestCase

def test_standard():
assert _binary.i8(b"*") == 42
assert _binary.o8(42) == b"*"

class TestBinary(PillowTestCase):
def test_standard(self):
self.assertEqual(_binary.i8(b"*"), 42)
self.assertEqual(_binary.o8(42), b"*")

def test_little_endian(self):
self.assertEqual(_binary.i16le(b"\xff\xff\x00\x00"), 65535)
self.assertEqual(_binary.i32le(b"\xff\xff\x00\x00"), 65535)
def test_little_endian():
assert _binary.i16le(b"\xff\xff\x00\x00") == 65535
assert _binary.i32le(b"\xff\xff\x00\x00") == 65535

self.assertEqual(_binary.o16le(65535), b"\xff\xff")
self.assertEqual(_binary.o32le(65535), b"\xff\xff\x00\x00")
assert _binary.o16le(65535) == b"\xff\xff"
assert _binary.o32le(65535) == b"\xff\xff\x00\x00"

def test_big_endian(self):
self.assertEqual(_binary.i16be(b"\x00\x00\xff\xff"), 0)
self.assertEqual(_binary.i32be(b"\x00\x00\xff\xff"), 65535)

self.assertEqual(_binary.o16be(65535), b"\xff\xff")
self.assertEqual(_binary.o32be(65535), b"\x00\x00\xff\xff")
def test_big_endian():
assert _binary.i16be(b"\x00\x00\xff\xff") == 0
assert _binary.i32be(b"\x00\x00\xff\xff") == 65535

assert _binary.o16be(65535) == b"\xff\xff"
assert _binary.o32be(65535) == b"\x00\x00\xff\xff"
11 changes: 5 additions & 6 deletions Tests/test_box_blur.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@
# fmt: on


class TestBoxBlurApi(PillowTestCase):
def test_imageops_box_blur(self):
i = sample.filter(ImageFilter.BoxBlur(1))
self.assertEqual(i.mode, sample.mode)
self.assertEqual(i.size, sample.size)
self.assertIsInstance(i, Image.Image)
def test_imageops_box_blur():
i = sample.filter(ImageFilter.BoxBlur(1))
assert i.mode == sample.mode
assert i.size == sample.size
assert isinstance(i, Image.Image)


class TestBoxBlur(PillowTestCase):
Expand Down
46 changes: 23 additions & 23 deletions Tests/test_core_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@
from .helper import PillowTestCase, is_pypy


class TestCoreStats(PillowTestCase):
def test_get_stats(self):
# Create at least one image
Image.new("RGB", (10, 10))

stats = Image.core.get_stats()
self.assertIn("new_count", stats)
self.assertIn("reused_blocks", stats)
self.assertIn("freed_blocks", stats)
self.assertIn("allocated_blocks", stats)
self.assertIn("reallocated_blocks", stats)
self.assertIn("blocks_cached", stats)

def test_reset_stats(self):
Image.core.reset_stats()

stats = Image.core.get_stats()
self.assertEqual(stats["new_count"], 0)
self.assertEqual(stats["reused_blocks"], 0)
self.assertEqual(stats["freed_blocks"], 0)
self.assertEqual(stats["allocated_blocks"], 0)
self.assertEqual(stats["reallocated_blocks"], 0)
self.assertEqual(stats["blocks_cached"], 0)
def test_get_stats():
# Create at least one image
Image.new("RGB", (10, 10))

stats = Image.core.get_stats()
assert "new_count" in stats
assert "reused_blocks" in stats
assert "freed_blocks" in stats
assert "allocated_blocks" in stats
assert "reallocated_blocks" in stats
assert "blocks_cached" in stats


def test_reset_stats():
Image.core.reset_stats()

stats = Image.core.get_stats()
assert stats["new_count"] == 0
assert stats["reused_blocks"] == 0
assert stats["freed_blocks"] == 0
assert stats["allocated_blocks"] == 0
assert stats["reallocated_blocks"] == 0
assert stats["blocks_cached"] == 0


class TestCoreMemory(PillowTestCase):
Expand Down
60 changes: 32 additions & 28 deletions Tests/test_file_bufrstub.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
import pytest
from PIL import BufrStubImagePlugin, Image

from .helper import PillowTestCase, hopper
from .helper import hopper

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


class TestFileBufrStub(PillowTestCase):
def test_open(self):
# Act
with Image.open(TEST_FILE) as im:
def test_open():
# Act
with Image.open(TEST_FILE) as im:

# Assert
self.assertEqual(im.format, "BUFR")
# Assert
assert im.format == "BUFR"

# Dummy data from the stub
self.assertEqual(im.mode, "F")
self.assertEqual(im.size, (1, 1))
# Dummy data from the stub
assert im.mode == "F"
assert im.size == (1, 1)

def test_invalid_file(self):
# Arrange
invalid_file = "Tests/images/flower.jpg"

# Act / Assert
self.assertRaises(
SyntaxError, BufrStubImagePlugin.BufrStubImageFile, invalid_file
)
def test_invalid_file():
# Arrange
invalid_file = "Tests/images/flower.jpg"

def test_load(self):
# Arrange
with Image.open(TEST_FILE) as im:
# Act / Assert
with pytest.raises(SyntaxError):
BufrStubImagePlugin.BufrStubImageFile(invalid_file)

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

def test_save(self):
# Arrange
im = hopper()
tmpfile = self.tempfile("temp.bufr")
def test_load():
# Arrange
with Image.open(TEST_FILE) as im:

# Act / Assert: stub cannot save without an implemented handler
self.assertRaises(IOError, im.save, tmpfile)
# Act / Assert: stub cannot load without an implemented handler
with pytest.raises(IOError):
im.load()


def test_save(tmp_path):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting something new to me, this looks handy:

The tmp_path fixture

https://docs.pytest.org/en/latest/tmpdir.html

# Arrange
im = hopper()
tmpfile = str(tmp_path / "temp.bufr")

# Act / Assert: stub cannot save without an implemented handler
with pytest.raises(IOError):
im.save(tmpfile)
Loading