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
This commit is contained in:
Jon Dufresne 2019-11-03 14:18:55 -08:00
parent 3aa5d56bae
commit 2c50723f14
20 changed files with 485 additions and 477 deletions

View File

@ -1,19 +1,15 @@
import PIL
import PIL.Image
from .helper import PillowTestCase
class TestSanity(PillowTestCase):
def test_sanity(self):
def test_sanity():
# 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)
assert (im.mode, im.size) == ("1", (100, 100))
assert len(im.tobytes()) == 1300
# Create images in all remaining major modes.
PIL.Image.new("L", (100, 100))

View File

@ -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():
assert _binary.i16le(b"\xff\xff\x00\x00") == 65535
assert _binary.i32le(b"\xff\xff\x00\x00") == 65535
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)
assert _binary.o16le(65535) == b"\xff\xff"
assert _binary.o32le(65535) == b"\xff\xff\x00\x00"
self.assertEqual(_binary.o16le(65535), b"\xff\xff")
self.assertEqual(_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)
def test_big_endian():
assert _binary.i16be(b"\x00\x00\xff\xff") == 0
assert _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")
assert _binary.o16be(65535) == b"\xff\xff"
assert _binary.o32be(65535) == b"\x00\x00\xff\xff"

View File

@ -14,12 +14,11 @@ sample.putdata(sum([
# fmt: on
class TestBoxBlurApi(PillowTestCase):
def test_imageops_box_blur(self):
def test_imageops_box_blur():
i = sample.filter(ImageFilter.BoxBlur(1))
self.assertEqual(i.mode, sample.mode)
self.assertEqual(i.size, sample.size)
self.assertIsInstance(i, Image.Image)
assert i.mode == sample.mode
assert i.size == sample.size
assert isinstance(i, Image.Image)
class TestBoxBlur(PillowTestCase):

View File

@ -6,29 +6,29 @@ from PIL import Image
from .helper import PillowTestCase, is_pypy
class TestCoreStats(PillowTestCase):
def test_get_stats(self):
def test_get_stats():
# 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)
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(self):
def test_reset_stats():
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)
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):

View File

@ -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):
def test_open():
# Act
with Image.open(TEST_FILE) as im:
# Assert
self.assertEqual(im.format, "BUFR")
assert im.format == "BUFR"
# Dummy data from the stub
self.assertEqual(im.mode, "F")
self.assertEqual(im.size, (1, 1))
assert im.mode == "F"
assert im.size == (1, 1)
def test_invalid_file(self):
def test_invalid_file():
# Arrange
invalid_file = "Tests/images/flower.jpg"
# Act / Assert
self.assertRaises(
SyntaxError, BufrStubImagePlugin.BufrStubImageFile, invalid_file
)
with pytest.raises(SyntaxError):
BufrStubImagePlugin.BufrStubImageFile(invalid_file)
def test_load(self):
def test_load():
# Arrange
with Image.open(TEST_FILE) as im:
# Act / Assert: stub cannot load without an implemented handler
self.assertRaises(IOError, im.load)
with pytest.raises(IOError):
im.load()
def test_save(self):
def test_save(tmp_path):
# Arrange
im = hopper()
tmpfile = self.tempfile("temp.bufr")
tmpfile = str(tmp_path / "temp.bufr")
# Act / Assert: stub cannot save without an implemented handler
self.assertRaises(IOError, im.save, tmpfile)
with pytest.raises(IOError):
im.save(tmpfile)

View File

@ -1,22 +1,23 @@
from PIL import ContainerIO, Image
from .helper import PillowTestCase, hopper
from .helper import hopper
TEST_FILE = "Tests/images/dummy.container"
class TestFileContainer(PillowTestCase):
def test_sanity(self):
def test_sanity():
dir(Image)
dir(ContainerIO)
def test_isatty(self):
def test_isatty():
with hopper() as im:
container = ContainerIO.ContainerIO(im, 0, 0)
self.assertFalse(container.isatty())
assert container.isatty() is False
def test_seek_mode_0(self):
def test_seek_mode_0():
# Arrange
mode = 0
with open(TEST_FILE) as fh:
@ -27,9 +28,10 @@ class TestFileContainer(PillowTestCase):
container.seek(33, mode)
# Assert
self.assertEqual(container.tell(), 33)
assert container.tell() == 33
def test_seek_mode_1(self):
def test_seek_mode_1():
# Arrange
mode = 1
with open(TEST_FILE) as fh:
@ -40,9 +42,10 @@ class TestFileContainer(PillowTestCase):
container.seek(33, mode)
# Assert
self.assertEqual(container.tell(), 66)
assert container.tell() == 66
def test_seek_mode_2(self):
def test_seek_mode_2():
# Arrange
mode = 2
with open(TEST_FILE) as fh:
@ -53,9 +56,10 @@ class TestFileContainer(PillowTestCase):
container.seek(33, mode)
# Assert
self.assertEqual(container.tell(), 100)
assert container.tell() == 100
def test_read_n0(self):
def test_read_n0():
# Arrange
with open(TEST_FILE) as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
@ -65,9 +69,10 @@ class TestFileContainer(PillowTestCase):
data = container.read()
# Assert
self.assertEqual(data, "7\nThis is line 8\n")
assert data == "7\nThis is line 8\n"
def test_read_n(self):
def test_read_n():
# Arrange
with open(TEST_FILE) as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
@ -77,9 +82,10 @@ class TestFileContainer(PillowTestCase):
data = container.read(3)
# Assert
self.assertEqual(data, "7\nT")
assert data == "7\nT"
def test_read_eof(self):
def test_read_eof():
# Arrange
with open(TEST_FILE) as fh:
container = ContainerIO.ContainerIO(fh, 22, 100)
@ -89,9 +95,10 @@ class TestFileContainer(PillowTestCase):
data = container.read()
# Assert
self.assertEqual(data, "")
assert data == ""
def test_readline(self):
def test_readline():
# Arrange
with open(TEST_FILE) as fh:
container = ContainerIO.ContainerIO(fh, 0, 120)
@ -100,9 +107,10 @@ class TestFileContainer(PillowTestCase):
data = container.readline()
# Assert
self.assertEqual(data, "This is line 1\n")
assert data == "This is line 1\n"
def test_readlines(self):
def test_readlines():
# Arrange
expected = [
"This is line 1\n",
@ -122,4 +130,4 @@ class TestFileContainer(PillowTestCase):
# Assert
self.assertEqual(data, expected)
assert data == expected

View File

@ -1,20 +1,22 @@
import pytest
from PIL import GdImageFile, UnidentifiedImageError
from .helper import PillowTestCase
TEST_GD_FILE = "Tests/images/hopper.gd"
class TestFileGd(PillowTestCase):
def test_sanity(self):
def test_sanity():
with GdImageFile.open(TEST_GD_FILE) as im:
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "GD")
assert im.size == (128, 128)
assert im.format == "GD"
def test_bad_mode(self):
self.assertRaises(ValueError, GdImageFile.open, TEST_GD_FILE, "bad mode")
def test_invalid_file(self):
def test_bad_mode():
with pytest.raises(ValueError):
GdImageFile.open(TEST_GD_FILE, "bad mode")
def test_invalid_file():
invalid_file = "Tests/images/flower.jpg"
self.assertRaises(UnidentifiedImageError, GdImageFile.open, invalid_file)
with pytest.raises(UnidentifiedImageError):
GdImageFile.open(invalid_file)

View File

@ -1,23 +1,25 @@
import pytest
from PIL.GimpPaletteFile import GimpPaletteFile
from .helper import PillowTestCase
class TestImage(PillowTestCase):
def test_sanity(self):
def test_sanity():
with open("Tests/images/test.gpl", "rb") as fp:
GimpPaletteFile(fp)
with open("Tests/images/hopper.jpg", "rb") as fp:
self.assertRaises(SyntaxError, GimpPaletteFile, fp)
with pytest.raises(SyntaxError):
GimpPaletteFile(fp)
with open("Tests/images/bad_palette_file.gpl", "rb") as fp:
self.assertRaises(SyntaxError, GimpPaletteFile, fp)
with pytest.raises(SyntaxError):
GimpPaletteFile(fp)
with open("Tests/images/bad_palette_entry.gpl", "rb") as fp:
self.assertRaises(ValueError, GimpPaletteFile, fp)
with pytest.raises(ValueError):
GimpPaletteFile(fp)
def test_get_palette(self):
def test_get_palette():
# Arrange
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
palette_file = GimpPaletteFile(fp)
@ -26,4 +28,4 @@ class TestImage(PillowTestCase):
palette, mode = palette_file.getpalette()
# Assert
self.assertEqual(mode, "RGB")
assert mode == "RGB"

View File

@ -1,10 +1,7 @@
from PIL import WalImageFile
from .helper import PillowTestCase
class TestFileWal(PillowTestCase):
def test_open(self):
def test_open():
# Arrange
TEST_FILE = "Tests/images/hopper.wal"
@ -12,7 +9,7 @@ class TestFileWal(PillowTestCase):
im = WalImageFile.open(TEST_FILE)
# Assert
self.assertEqual(im.format, "WAL")
self.assertEqual(im.format_description, "Quake2 Texture")
self.assertEqual(im.mode, "P")
self.assertEqual(im.size, (128, 128))
assert im.format == "WAL"
assert im.format_description == "Quake2 Texture"
assert im.mode == "P"
assert im.size == (128, 128)

View File

@ -1,16 +1,13 @@
from PIL import Image
from .helper import PillowTestCase
class TestFormatLab(PillowTestCase):
def test_white(self):
def test_white():
with Image.open("Tests/images/lab.tif") as i:
i.load()
self.assertEqual(i.mode, "LAB")
assert i.mode == "LAB"
self.assertEqual(i.getbands(), ("L", "A", "B"))
assert i.getbands() == ("L", "A", "B")
k = i.getpixel((0, 0))
@ -18,22 +15,24 @@ class TestFormatLab(PillowTestCase):
a = i.getdata(1)
b = i.getdata(2)
self.assertEqual(k, (255, 128, 128))
assert k == (255, 128, 128)
self.assertEqual(list(L), [255] * 100)
self.assertEqual(list(a), [128] * 100)
self.assertEqual(list(b), [128] * 100)
assert list(L) == [255] * 100
assert list(a) == [128] * 100
assert list(b) == [128] * 100
def test_green(self):
def test_green():
# l= 50 (/100), a = -100 (-128 .. 128) b=0 in PS
# == RGB: 0, 152, 117
with Image.open("Tests/images/lab-green.tif") as i:
k = i.getpixel((0, 0))
self.assertEqual(k, (128, 28, 128))
assert k == (128, 28, 128)
def test_red(self):
def test_red():
# l= 50 (/100), a = 100 (-128 .. 128) b=0 in PS
# == RGB: 255, 0, 124
with Image.open("Tests/images/lab-red.tif") as i:
k = i.getpixel((0, 0))
self.assertEqual(k, (128, 228, 128))
assert k == (128, 228, 128)

View File

@ -1,16 +1,13 @@
from PIL import Image
from .helper import PillowTestCase
class TestImageGetBands(PillowTestCase):
def test_getbands(self):
self.assertEqual(Image.new("1", (1, 1)).getbands(), ("1",))
self.assertEqual(Image.new("L", (1, 1)).getbands(), ("L",))
self.assertEqual(Image.new("I", (1, 1)).getbands(), ("I",))
self.assertEqual(Image.new("F", (1, 1)).getbands(), ("F",))
self.assertEqual(Image.new("P", (1, 1)).getbands(), ("P",))
self.assertEqual(Image.new("RGB", (1, 1)).getbands(), ("R", "G", "B"))
self.assertEqual(Image.new("RGBA", (1, 1)).getbands(), ("R", "G", "B", "A"))
self.assertEqual(Image.new("CMYK", (1, 1)).getbands(), ("C", "M", "Y", "K"))
self.assertEqual(Image.new("YCbCr", (1, 1)).getbands(), ("Y", "Cb", "Cr"))
def test_getbands():
assert Image.new("1", (1, 1)).getbands() == ("1",)
assert Image.new("L", (1, 1)).getbands() == ("L",)
assert Image.new("I", (1, 1)).getbands() == ("I",)
assert Image.new("F", (1, 1)).getbands() == ("F",)
assert Image.new("P", (1, 1)).getbands() == ("P",)
assert Image.new("RGB", (1, 1)).getbands() == ("R", "G", "B")
assert Image.new("RGBA", (1, 1)).getbands() == ("R", "G", "B", "A")
assert Image.new("CMYK", (1, 1)).getbands() == ("C", "M", "Y", "K")
assert Image.new("YCbCr", (1, 1)).getbands() == ("Y", "Cb", "Cr")

View File

@ -1,38 +1,38 @@
from PIL import Image
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageGetBbox(PillowTestCase):
def test_sanity(self):
def test_sanity():
bbox = hopper().getbbox()
self.assertIsInstance(bbox, tuple)
assert isinstance(bbox, tuple)
def test_bbox(self):
def test_bbox():
# 8-bit mode
im = Image.new("L", (100, 100), 0)
self.assertIsNone(im.getbbox())
assert im.getbbox() is None
im.paste(255, (10, 25, 90, 75))
self.assertEqual(im.getbbox(), (10, 25, 90, 75))
assert im.getbbox() == (10, 25, 90, 75)
im.paste(255, (25, 10, 75, 90))
self.assertEqual(im.getbbox(), (10, 10, 90, 90))
assert im.getbbox() == (10, 10, 90, 90)
im.paste(255, (-10, -10, 110, 110))
self.assertEqual(im.getbbox(), (0, 0, 100, 100))
assert im.getbbox() == (0, 0, 100, 100)
# 32-bit mode
im = Image.new("RGB", (100, 100), 0)
self.assertIsNone(im.getbbox())
assert im.getbbox() is None
im.paste(255, (10, 25, 90, 75))
self.assertEqual(im.getbbox(), (10, 25, 90, 75))
assert im.getbbox() == (10, 25, 90, 75)
im.paste(255, (25, 10, 75, 90))
self.assertEqual(im.getbbox(), (10, 10, 90, 90))
assert im.getbbox() == (10, 10, 90, 90)
im.paste(255, (-10, -10, 110, 110))
self.assertEqual(im.getbbox(), (0, 0, 100, 100))
assert im.getbbox() == (0, 0, 100, 100)

View File

@ -1,10 +1,9 @@
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageGetIm(PillowTestCase):
def test_sanity(self):
def test_sanity():
im = hopper()
type_repr = repr(type(im.getim()))
self.assertIn("PyCapsule", type_repr)
self.assertIsInstance(im.im.id, int)
assert "PyCapsule" in type_repr
assert isinstance(im.im.id, int)

View File

@ -1,20 +1,19 @@
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageGetPalette(PillowTestCase):
def test_palette(self):
def test_palette():
def palette(mode):
p = hopper(mode).getpalette()
if p:
return p[:10]
return None
self.assertIsNone(palette("1"))
self.assertIsNone(palette("L"))
self.assertIsNone(palette("I"))
self.assertIsNone(palette("F"))
self.assertEqual(palette("P"), [0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
self.assertIsNone(palette("RGB"))
self.assertIsNone(palette("RGBA"))
self.assertIsNone(palette("CMYK"))
self.assertIsNone(palette("YCbCr"))
assert palette("1") is None
assert palette("L") is None
assert palette("I") is None
assert palette("F") is None
assert palette("P") == [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
assert palette("RGB") is None
assert palette("RGBA") is None
assert palette("CMYK") is None
assert palette("YCbCr") is None

View File

@ -1,10 +1,10 @@
import pytest
from PIL import ImagePalette
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImagePutPalette(PillowTestCase):
def test_putpalette(self):
def test_putpalette():
def palette(mode):
im = hopper(mode).copy()
im.putpalette(list(range(256)) * 3)
@ -13,19 +13,26 @@ class TestImagePutPalette(PillowTestCase):
return im.mode, p[:10]
return im.mode
self.assertRaises(ValueError, palette, "1")
with pytest.raises(ValueError):
palette("1")
for mode in ["L", "LA", "P", "PA"]:
self.assertEqual(
palette(mode),
("PA" if "A" in mode else "P", [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]),
assert palette(mode) == (
"PA" if "A" in mode else "P",
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
)
self.assertRaises(ValueError, palette, "I")
self.assertRaises(ValueError, palette, "F")
self.assertRaises(ValueError, palette, "RGB")
self.assertRaises(ValueError, palette, "RGBA")
self.assertRaises(ValueError, palette, "YCbCr")
with pytest.raises(ValueError):
palette("I")
with pytest.raises(ValueError):
palette("F")
with pytest.raises(ValueError):
palette("RGB")
with pytest.raises(ValueError):
palette("RGBA")
with pytest.raises(ValueError):
palette("YCbCr")
def test_imagepalette(self):
def test_imagepalette():
im = hopper("P")
im.putpalette(ImagePalette.negative())
im.putpalette(ImagePalette.random())

View File

@ -1,7 +1,6 @@
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageToBytes(PillowTestCase):
def test_sanity(self):
def test_sanity():
data = hopper().tobytes()
self.assertIsInstance(data, bytes)
assert isinstance(data, bytes)

View File

@ -1,10 +1,10 @@
import pytest
from PIL import Image, ImageStat
from .helper import PillowTestCase, hopper
from .helper import hopper
class TestImageStat(PillowTestCase):
def test_sanity(self):
def test_sanity():
im = hopper()
@ -22,34 +22,38 @@ class TestImageStat(PillowTestCase):
st.var
st.stddev
self.assertRaises(AttributeError, lambda: st.spam)
with pytest.raises(AttributeError):
st.spam()
self.assertRaises(TypeError, ImageStat.Stat, 1)
with pytest.raises(TypeError):
ImageStat.Stat(1)
def test_hopper(self):
def test_hopper():
im = hopper()
st = ImageStat.Stat(im)
# verify a few values
self.assertEqual(st.extrema[0], (0, 255))
self.assertEqual(st.median[0], 72)
self.assertEqual(st.sum[0], 1470218)
self.assertEqual(st.sum[1], 1311896)
self.assertEqual(st.sum[2], 1563008)
assert st.extrema[0] == (0, 255)
assert st.median[0] == 72
assert st.sum[0] == 1470218
assert st.sum[1] == 1311896
assert st.sum[2] == 1563008
def test_constant(self):
def test_constant():
im = Image.new("L", (128, 128), 128)
st = ImageStat.Stat(im)
self.assertEqual(st.extrema[0], (128, 128))
self.assertEqual(st.sum[0], 128 ** 3)
self.assertEqual(st.sum2[0], 128 ** 4)
self.assertEqual(st.mean[0], 128)
self.assertEqual(st.median[0], 128)
self.assertEqual(st.rms[0], 128)
self.assertEqual(st.var[0], 0)
self.assertEqual(st.stddev[0], 0)
assert st.extrema[0] == (128, 128)
assert st.sum[0] == 128 ** 3
assert st.sum2[0] == 128 ** 4
assert st.mean[0] == 128
assert st.median[0] == 128
assert st.rms[0] == 128
assert st.var[0] == 0
assert st.stddev[0] == 0

View File

@ -1,10 +1,8 @@
import locale
import unittest
import pytest
from PIL import Image
from .helper import PillowTestCase
# ref https://github.com/python-pillow/Pillow/issues/272
# on windows, in polish locale:
@ -23,14 +21,13 @@ from .helper import PillowTestCase
path = "Tests/images/hopper.jpg"
class TestLocale(PillowTestCase):
def test_sanity(self):
def test_sanity():
with Image.open(path):
pass
try:
locale.setlocale(locale.LC_ALL, "polish")
except locale.Error:
unittest.skip("Polish locale not available")
pytest.skip("Polish locale not available")
try:
with Image.open(path):

View File

@ -1,23 +1,21 @@
import os
import subprocess
import sys
from unittest import TestCase
class TestMain(TestCase):
def test_main(self):
def test_main():
out = subprocess.check_output([sys.executable, "-m", "PIL"]).decode("utf-8")
lines = out.splitlines()
self.assertEqual(lines[0], "-" * 68)
self.assertTrue(lines[1].startswith("Pillow "))
self.assertTrue(lines[2].startswith("Python "))
assert lines[0] == "-" * 68
assert lines[1].startswith("Pillow ")
assert lines[2].startswith("Python ")
lines = lines[3:]
while lines[0].startswith(" "):
lines = lines[1:]
self.assertEqual(lines[0], "-" * 68)
self.assertTrue(lines[1].startswith("Python modules loaded from "))
self.assertTrue(lines[2].startswith("Binary modules loaded from "))
self.assertEqual(lines[3], "-" * 68)
assert lines[0] == "-" * 68
assert lines[1].startswith("Python modules loaded from ")
assert lines[2].startswith("Binary modules loaded from ")
assert lines[3] == "-" * 68
jpeg = (
os.linesep
+ "-" * 68
@ -31,4 +29,4 @@ class TestMain(TestCase):
+ "-" * 68
+ os.linesep
)
self.assertIn(jpeg, out)
assert jpeg in out

View File

@ -1,12 +1,8 @@
import unittest
import pytest
from PIL import _util
from .helper import PillowTestCase
class TestUtil(PillowTestCase):
def test_is_path(self):
def test_is_path():
# Arrange
fp = "filename.ext"
@ -14,10 +10,11 @@ class TestUtil(PillowTestCase):
it_is = _util.isPath(fp)
# Assert
self.assertTrue(it_is)
assert it_is
@unittest.skipUnless(_util.py36, "os.path support for Paths added in 3.6")
def test_path_obj_is_path(self):
@pytest.mark.skipif(not _util.py36, reason="os.path support for Paths added in 3.6")
def test_path_obj_is_path():
# Arrange
from pathlib import Path
@ -27,20 +24,22 @@ class TestUtil(PillowTestCase):
it_is = _util.isPath(test_path)
# Assert
self.assertTrue(it_is)
assert it_is
def test_is_not_path(self):
def test_is_not_path(tmp_path):
# Arrange
filename = self.tempfile("temp.ext")
fp = open(filename, "w").close()
with (tmp_path / "temp.ext").open("w") as fp:
pass
# Act
it_is_not = _util.isPath(fp)
# Assert
self.assertFalse(it_is_not)
assert not it_is_not
def test_is_directory(self):
def test_is_directory():
# Arrange
directory = "Tests"
@ -48,9 +47,10 @@ class TestUtil(PillowTestCase):
it_is = _util.isDirectory(directory)
# Assert
self.assertTrue(it_is)
assert it_is
def test_is_not_directory(self):
def test_is_not_directory():
# Arrange
text = "abc"
@ -58,13 +58,15 @@ class TestUtil(PillowTestCase):
it_is_not = _util.isDirectory(text)
# Assert
self.assertFalse(it_is_not)
assert not it_is_not
def test_deferred_error(self):
def test_deferred_error():
# Arrange
# Act
thing = _util.deferred_error(ValueError("Some error text"))
# Assert
self.assertRaises(ValueError, lambda: thing.some_attr)
with pytest.raises(ValueError):
thing.some_attr