Skip to content

Commit f954246

Browse files
committed
Convert unit tests from unittest to pytest
1 parent 85975bf commit f954246

15 files changed

+1055
-1087
lines changed

Diff for: setup.cfg

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ test=pytest
33

44
[bdist_wheel]
55
universal=1
6+
7+
[tool:pytest]
8+
norecursedirs=tests/helpers

Diff for: tests/conftest.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import sys
2+
import os
3+
4+
sys.path.append(os.path.join(os.path.dirname(__file__), 'helpers'))

Diff for: tests/helpers.py renamed to tests/helpers/helpers.py

+65-63
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
# vim: set fileencoding=utf-8 :
22
# test helpers
3-
from __future__ import division
4-
53
import os
64
import tempfile
7-
import unittest
5+
import pytest
86

97
import pyvips
108

11-
IMAGES = os.path.join(os.path.dirname(__file__), 'images')
9+
IMAGES = os.path.join(os.path.dirname(__file__), os.pardir, 'images')
1210
JPEG_FILE = os.path.join(IMAGES, "йцук.jpg")
1311
SRGB_FILE = os.path.join(IMAGES, "sRGB.icm")
1412
MATLAB_FILE = os.path.join(IMAGES, "sample.mat")
@@ -158,62 +156,66 @@ def run_fn2(fn, x, y):
158156
return fn(x, y)
159157

160158

161-
class PyvipsTester(unittest.TestCase):
162-
# test a pair of things which can be lists for approx. equality
163-
def assertAlmostEqualObjects(self, a, b, places=4, msg=''):
164-
# print 'assertAlmostEqualObjects %s = %s' % (a, b)
165-
for x, y in zip_expand(a, b):
166-
self.assertAlmostEqual(x, y, places=places, msg=msg)
167-
168-
# test a pair of things which can be lists for equality
169-
def assertEqualObjects(self, a, b, places=4, msg=''):
170-
# print 'assertEqualObjects %s = %s' % (a, b)
171-
for x, y in zip_expand(a, b):
172-
self.assertEqual(x, y, msg=msg)
173-
174-
# test a pair of things which can be lists for difference less than a
175-
# threshold
176-
def assertLessThreshold(self, a, b, diff):
177-
for x, y in zip_expand(a, b):
178-
self.assertLess(abs(x - y), diff)
179-
180-
# run a function on an image and on a single pixel, the results
181-
# should match
182-
def run_cmp(self, message, im, x, y, fn):
183-
a = im(x, y)
184-
v1 = fn(a)
185-
im2 = fn(im)
186-
v2 = im2(x, y)
187-
self.assertAlmostEqualObjects(v1, v2, msg=message)
188-
189-
# run a function on an image,
190-
# 50,50 and 10,10 should have different values on the test image
191-
def run_image(self, message, im, fn):
192-
self.run_cmp(message, im, 50, 50, fn)
193-
self.run_cmp(message, im, 10, 10, fn)
194-
195-
# run a function on (image, constant), and on (constant, image).
196-
# 50,50 and 10,10 should have different values on the test image
197-
def run_const(self, message, fn, im, c):
198-
self.run_cmp(message, im, 50, 50, lambda x: run_fn2(fn, x, c))
199-
self.run_cmp(message, im, 50, 50, lambda x: run_fn2(fn, c, x))
200-
self.run_cmp(message, im, 10, 10, lambda x: run_fn2(fn, x, c))
201-
self.run_cmp(message, im, 10, 10, lambda x: run_fn2(fn, c, x))
202-
203-
# run a function on a pair of images and on a pair of pixels, the results
204-
# should match
205-
def run_cmp2(self, message, left, right, x, y, fn):
206-
a = left(x, y)
207-
b = right(x, y)
208-
v1 = fn(a, b)
209-
after = fn(left, right)
210-
v2 = after(x, y)
211-
self.assertAlmostEqualObjects(v1, v2, msg=message)
212-
213-
# run a function on a pair of images
214-
# 50,50 and 10,10 should have different values on the test image
215-
def run_image2(self, message, left, right, fn):
216-
self.run_cmp2(message, left, right, 50, 50,
217-
lambda x, y: run_fn2(fn, x, y))
218-
self.run_cmp2(message, left, right, 10, 10,
219-
lambda x, y: run_fn2(fn, x, y))
159+
# test a pair of things which can be lists for approx. equality
160+
def assert_almost_equal_objects(a, b, threshold=0.0001, msg=''):
161+
# print 'assertAlmostEqualObjects %s = %s' % (a, b)
162+
assert all([pytest.approx(a, abs=threshold) == b
163+
for a, b in zip_expand(a, b)]), msg
164+
165+
166+
# test a pair of things which can be lists for equality
167+
def assert_equal_objects(a, b, msg=''):
168+
# print 'assertEqualObjects %s = %s' % (a, b)
169+
assert all([a == b for a, b in zip_expand(a, b)]), msg
170+
171+
172+
# test a pair of things which can be lists for difference less than a
173+
# threshold
174+
def assert_less_threshold(a, b, diff):
175+
assert all([abs(a - b) < diff for a, b in zip_expand(a, b)])
176+
177+
178+
# run a function on an image and on a single pixel, the results
179+
# should match
180+
def run_cmp(message, im, x, y, fn):
181+
a = im(x, y)
182+
v1 = fn(a)
183+
im2 = fn(im)
184+
v2 = im2(x, y)
185+
assert_almost_equal_objects(v1, v2, msg=message)
186+
187+
188+
# run a function on an image,
189+
# 50,50 and 10,10 should have different values on the test image
190+
def run_image(message, im, fn):
191+
run_cmp(message, im, 50, 50, fn)
192+
run_cmp(message, im, 10, 10, fn)
193+
194+
195+
# run a function on (image, constant), and on (constant, image).
196+
# 50,50 and 10,10 should have different values on the test image
197+
def run_const(message, fn, im, c):
198+
run_cmp(message, im, 50, 50, lambda x: run_fn2(fn, x, c))
199+
run_cmp(message, im, 50, 50, lambda x: run_fn2(fn, c, x))
200+
run_cmp(message, im, 10, 10, lambda x: run_fn2(fn, x, c))
201+
run_cmp(message, im, 10, 10, lambda x: run_fn2(fn, c, x))
202+
203+
204+
# run a function on a pair of images and on a pair of pixels, the results
205+
# should match
206+
def run_cmp2(message, left, right, x, y, fn):
207+
a = left(x, y)
208+
b = right(x, y)
209+
v1 = fn(a, b)
210+
after = fn(left, right)
211+
v2 = after(x, y)
212+
assert_almost_equal_objects(v1, v2, msg=message)
213+
214+
215+
# run a function on a pair of images
216+
# 50,50 and 10,10 should have different values on the test image
217+
def run_image2(message, left, right, fn):
218+
run_cmp2(message, left, right, 50, 50,
219+
lambda x, y: run_fn2(fn, x, y))
220+
run_cmp2(message, left, right, 10, 10,
221+
lambda x, y: run_fn2(fn, x, y))

0 commit comments

Comments
 (0)