Replace unittest with pytest

This commit is contained in:
Hugo 2020-02-17 10:42:33 +02:00
parent 85e06057e2
commit 946a038b13

View File

@ -1,54 +1,59 @@
import io import io
import pytest
from PIL import BmpImagePlugin, Image from PIL import BmpImagePlugin, Image
from .helper import PillowTestCase, assert_image_equal, hopper from .helper import assert_image_equal, hopper
class TestFileBmp(PillowTestCase): def test_sanity(tmp_path):
def roundtrip(self, im): def roundtrip(im):
outfile = self.tempfile("temp.bmp") outfile = str(tmp_path / "temp.bmp")
im.save(outfile, "BMP") im.save(outfile, "BMP")
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
reloaded.load() reloaded.load()
self.assertEqual(im.mode, reloaded.mode) assert im.mode == reloaded.mode
self.assertEqual(im.size, reloaded.size) assert im.size == reloaded.size
self.assertEqual(reloaded.format, "BMP") assert reloaded.format == "BMP"
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp") assert reloaded.get_format_mimetype() == "image/bmp"
def test_sanity(self): roundtrip(hopper())
self.roundtrip(hopper())
self.roundtrip(hopper("1")) roundtrip(hopper("1"))
self.roundtrip(hopper("L")) roundtrip(hopper("L"))
self.roundtrip(hopper("P")) roundtrip(hopper("P"))
self.roundtrip(hopper("RGB")) roundtrip(hopper("RGB"))
def test_invalid_file(self):
def test_invalid_file():
with open("Tests/images/flower.jpg", "rb") as fp: with open("Tests/images/flower.jpg", "rb") as fp:
self.assertRaises(SyntaxError, BmpImagePlugin.BmpImageFile, fp) with pytest.raises(SyntaxError):
BmpImagePlugin.BmpImageFile(fp)
def test_save_to_bytes(self):
def test_save_to_bytes():
output = io.BytesIO() output = io.BytesIO()
im = hopper() im = hopper()
im.save(output, "BMP") im.save(output, "BMP")
output.seek(0) output.seek(0)
with Image.open(output) as reloaded: with Image.open(output) as reloaded:
self.assertEqual(im.mode, reloaded.mode) assert im.mode == reloaded.mode
self.assertEqual(im.size, reloaded.size) assert im.size == reloaded.size
self.assertEqual(reloaded.format, "BMP") assert reloaded.format == "BMP"
def test_save_too_large(self):
outfile = self.tempfile("temp.bmp") def test_save_too_large(tmp_path):
outfile = str(tmp_path / "temp.bmp")
with Image.new("RGB", (1, 1)) as im: with Image.new("RGB", (1, 1)) as im:
im._size = (37838, 37838) im._size = (37838, 37838)
with self.assertRaises(ValueError): with pytest.raises(ValueError):
im.save(outfile) im.save(outfile)
def test_dpi(self):
def test_dpi():
dpi = (72, 72) dpi = (72, 72)
output = io.BytesIO() output = io.BytesIO()
@ -57,12 +62,13 @@ class TestFileBmp(PillowTestCase):
output.seek(0) output.seek(0)
with Image.open(output) as reloaded: with Image.open(output) as reloaded:
self.assertEqual(reloaded.info["dpi"], dpi) assert reloaded.info["dpi"] == dpi
def test_save_bmp_with_dpi(self):
def test_save_bmp_with_dpi(tmp_path):
# Test for #1301 # Test for #1301
# Arrange # Arrange
outfile = self.tempfile("temp.jpg") outfile = str(tmp_path / "temp.jpg")
with Image.open("Tests/images/hopper.bmp") as im: with Image.open("Tests/images/hopper.bmp") as im:
# Act # Act
@ -71,51 +77,56 @@ class TestFileBmp(PillowTestCase):
# Assert # Assert
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
reloaded.load() reloaded.load()
self.assertEqual(im.info["dpi"], reloaded.info["dpi"]) assert im.info["dpi"] == reloaded.info["dpi"]
self.assertEqual(im.size, reloaded.size) assert im.size == reloaded.size
self.assertEqual(reloaded.format, "JPEG") assert reloaded.format == "JPEG"
def test_load_dpi_rounding(self):
def test_load_dpi_rounding():
# Round up # Round up
with Image.open("Tests/images/hopper.bmp") as im: with Image.open("Tests/images/hopper.bmp") as im:
self.assertEqual(im.info["dpi"], (96, 96)) assert im.info["dpi"] == (96, 96)
# Round down # Round down
with Image.open("Tests/images/hopper_roundDown.bmp") as im: with Image.open("Tests/images/hopper_roundDown.bmp") as im:
self.assertEqual(im.info["dpi"], (72, 72)) assert im.info["dpi"] == (72, 72)
def test_save_dpi_rounding(self):
outfile = self.tempfile("temp.bmp") def test_save_dpi_rounding(tmp_path):
outfile = str(tmp_path / "temp.bmp")
with Image.open("Tests/images/hopper.bmp") as im: with Image.open("Tests/images/hopper.bmp") as im:
im.save(outfile, dpi=(72.2, 72.2)) im.save(outfile, dpi=(72.2, 72.2))
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (72, 72)) assert reloaded.info["dpi"] == (72, 72)
im.save(outfile, dpi=(72.8, 72.8)) im.save(outfile, dpi=(72.8, 72.8))
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.info["dpi"], (73, 73)) assert reloaded.info["dpi"] == (73, 73)
def test_load_dib(self):
def test_load_dib():
# test for #1293, Imagegrab returning Unsupported Bitfields Format # test for #1293, Imagegrab returning Unsupported Bitfields Format
with Image.open("Tests/images/clipboard.dib") as im: with Image.open("Tests/images/clipboard.dib") as im:
self.assertEqual(im.format, "DIB") assert im.format == "DIB"
self.assertEqual(im.get_format_mimetype(), "image/bmp") assert im.get_format_mimetype() == "image/bmp"
with Image.open("Tests/images/clipboard_target.png") as target: with Image.open("Tests/images/clipboard_target.png") as target:
assert_image_equal(im, target) assert_image_equal(im, target)
def test_save_dib(self):
outfile = self.tempfile("temp.dib") def test_save_dib(tmp_path):
outfile = str(tmp_path / "temp.dib")
with Image.open("Tests/images/clipboard.dib") as im: with Image.open("Tests/images/clipboard.dib") as im:
im.save(outfile) im.save(outfile)
with Image.open(outfile) as reloaded: with Image.open(outfile) as reloaded:
self.assertEqual(reloaded.format, "DIB") assert reloaded.format == "DIB"
self.assertEqual(reloaded.get_format_mimetype(), "image/bmp") assert reloaded.get_format_mimetype() == "image/bmp"
assert_image_equal(im, reloaded) assert_image_equal(im, reloaded)
def test_rgba_bitfields(self):
def test_rgba_bitfields():
# This test image has been manually hexedited # This test image has been manually hexedited
# to change the bitfield compression in the header from XBGR to RGBA # to change the bitfield compression in the header from XBGR to RGBA
with Image.open("Tests/images/rgb32bf-rgba.bmp") as im: with Image.open("Tests/images/rgb32bf-rgba.bmp") as im: