在一个实现ImageDraw的方法上在python中进行单元测试

时间:2015-03-25 16:18:29

标签: python image unit-testing bitmap pytest

我目前正在尝试使用pytest模块为我正在进行的项目创建单元测试。我试图测试“add_point”#39;基于一组像素绘制椭圆的方法。我想做的是检查'画'确保已成功创建椭圆。不幸的是,我不知道该怎么做,所以任何帮助将不胜感激。到目前为止,这是我的代码:

  

(A)TheSlicePreviewMaker.py

import os, Image, ImageDraw, ImageFont
from json_importer import json_importer

class SlicePreviewer(object):
    def __init__(self):
        self.screen_size = (470, 470)
        self.background_colour = (86,0,255)
        self.platform_fill_colour = (100, 100, 100)
        self.platform_outline_colour = (0, 0, 0)
        self.platform_window = (0,0,469,469)
        self.point_colour = (0,0,255)
        self.config_object = json_importer("ConfigFile.txt")
        self.image = None

    def initialise_image(self):
        self.image = Image.new('RGB',self.screen_size,self.background_colour)
        draw = ImageDraw.Draw(self.image)
        draw.rectangle(self.platform_window,outline=self.platform_outline_colour,fill=self.platform_fill_colour)
        del draw

    def add_point(self, px, py):
        x1 = px - 1
        y1 = py - 1
        x2 = px + 1
        y2 = py + 1
        draw = ImageDraw.Draw(self.image)
        draw.ellipse((x1,y1,x2,y2),outline=self.point_colour,fill=self.point_colour)
        return draw #del draw

    def save_image(self, file_name):
        self.image.save(file_name, "BMP")
  

(B)test_TheSlicePreviewMaker.py

from TheSlicePreviewMaker import SlicePreviewer

slice_preview = SlicePreviewer()
class TestSlicePreviewer:
    def test_init(self):
        '''check that the config file object has been created on init'''
        assert slice_preview.config_object != None

    def test_initialise_image(self):
        '''verify if the image has been successfully initialised'''
        assert slice_preview.image.mode == 'RGB'

    def test_add_point(self):
        '''has the point been drawn successfully?'''
        draw = slice_preview.add_point(196,273)
        assert something

import pytest
if __name__ == '__main__':
    pytest.main("--capture=sys -v")

SN :我分别运行TheSlicePreviewMaker.py来检查它产生的位图文件,所以我知道代码可以运行。我想要实现的是单元测试,这样每次我都不必去检查位图。

1 个答案:

答案 0 :(得分:4)

一种方法是手动检查生成的图像,如果看起来没问题,请将其保存在测试旁边并使用图像差异算法(例如ImageChops.difference)来获取可用于的阈值确保未来的测试运行仍然绘制相同的图像。

例如:

# contents of conftest.py
from PIL import ImageChops, ImageDraw, Image
import pytest
import os
import py.path
import math
import operator

def rms_diff(im1, im2):
    """Calculate the root-mean-square difference between two images
    Taken from: http://snipplr.com/view/757/compare-two-pil-images-in-python/
    """
    h1 = im1.histogram()
    h2 = im2.histogram()

    def mean_sqr(a,b):
        if not a:
            a = 0.0
        if not b:
            b = 0.0
        return (a-b)**2

    return math.sqrt(reduce(operator.add, map(mean_sqr, h1, h2))/(im1.size[0]*im1.size[1]))


class ImageDiff:
    """Fixture used to make sure code that generates images continues to do so
    by checking the difference of the genereated image against known good versions.
    """

    def __init__(self, request):
        self.directory = py.path.local(request.node.fspath.dirname) / request.node.fspath.purebasename
        self.expected_name = (request.node.name + '.png') 
        self.expected_filename = self.directory / self.expected_name

    def check(self, im, max_threshold=0.0):
        __tracebackhide__ = True
        local = py.path.local(os.getcwd()) / self.expected_name
        if not self.expected_filename.check(file=1):
            msg = '\nExpecting image at %s, but it does not exist.\n'
            msg += '-> Generating here: %s'
            im.save(str(local))
            pytest.fail(msg % (self.expected_filename, local))
        else:
            expected = Image.open(str(self.expected_filename))
            rms_value = rms_diff(im, expected)
            if rms_value > max_threshold:
                im.save(str(local))
                msg = '\nrms_value %s > max_threshold of %s.\n'
                msg += 'Obtained image saved at %s'
                pytest.fail(msg % (rms_value, max_threshold, str(local))) 

@pytest.fixture
def image_diff(request):        
    return ImageDiff(request)

现在您可以在测试中使用image_diff夹具。例如:

def create_image():
    """ dummy code that generates an image, simulating some actual code """
    im = Image.new('RGB', (100, 100), (0, 0, 0))
    draw = ImageDraw.Draw(im)
    draw.ellipse((10, 10, 90, 90), outline=(0, 0, 255), 
                 fill=(255, 255, 255))
    return im      


def test_generated_image(image_diff):        
    im = create_image()
    image_diff.check(im) 

第一次运行此测试时,此输出将失败:

================================== FAILURES ===================================
____________________________ test_generated_image _____________________________

image_diff = <test_foo.ImageDiff instance at 0x029ED530>

    def test_generated_image(image_diff):
        im = create_image()
>       image_diff.check(im)
E       Failed:
E       Expecting image at X:\temp\sandbox\img-diff\test_foo\test_generated_image.png, but it does not exist.
E       -> Generating here: X:\temp\sandbox\img-diff\test_generated_image.png

然后,您可以手动检查图像,如果一切正常,请将其移至与测试文件同名的目录,并将测试名称作为文件名加“.png”扩展名。从现在开始,每当测试运行时,它将检查图像是否在可接受的量内是相似的。

假设您更改了代码并生成略有不同的图像,测试现在将失败:

================================== FAILURES ===================================
____________________________ test_generated_image _____________________________

image_diff = <test_foo.ImageDiff instance at 0x02A4B788>

    def test_generated_image(image_diff):
        im = create_image()
>       image_diff.check(im)
E       Failed:
E       rms_value 2.52 > max_threshold of 0.0.
E       Obtained image saved at X:\temp\sandbox\img-diff\test_generated_image.png

test_foo.py:63: Failed
========================== 1 failed in 0.03 seconds ===========================

代码需要一些抛光,但应该是一个良好的开端。您可以找到此代码的here版本。

干杯,