-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_inomaly.py
84 lines (63 loc) · 2.33 KB
/
pytest_inomaly.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
import os
import pytest
from scipy.misc import imread
from scipy.linalg import norm
updated_files = []
def pytest_addoption(parser):
group = parser.getgroup('Inomaly')
group.addoption(
'--serve',
action='store',
dest='dest_foo',
default='2017',
help='Set the value for the fixture "bar".'
)
group.addoption(
'--update',
action='store_true',
dest='update_images',
default=False,
help='Test images have changed, so replace with current results, should be visually tested first'
)
def pytest_sessionfinish(session, exitstatus):
""" whole test run finishes. """
if not updated_files:
return
print('\n\nInomaly Complete, Updated these files\n')
for filepath in updated_files:
print('\t%s' % filepath)
def pytest_configure(config):
pytest.update_images = config.getoption('update_images')
pytest.idiff = idiff
pytest.idiff_variance = idiff_variance
def compare_images(actual_results, expected_results):
do_assets_exist(actual_results, expected_results)
# Make diffed results positive
diff = abs(imread(actual_results).astype(float) - imread(expected_results).astype(float))
# L1 Norm:(Taxicab norm or Manhattan norm)
l1_norm = sum(diff)
# L0-norm :Total number of non-zero elements in a vector. It is a cardinality function
l0_norm = norm(diff.ravel(), 0)
return l0_norm / diff.size
def do_assets_exist(actual_results, expected_results):
if not os.path.exists(actual_results):
raise IOError
if pytest.update_images is True:
with open(actual_results, 'rb') as actual_file:
with open(expected_results, 'wb') as expected_file:
expected_file.write(actual_file.read())
updated_files.append(expected_results)
if not os.path.exists(expected_results):
raise IOError
def idiff(actual_results, expected_results, tolerance=0.0):
norm = compare_images(
actual_results='%s' % actual_results,
expected_results='%s' % expected_results)
if norm <= tolerance:
return True
return False
def idiff_variance(actual_results, expected_results, tolerance=0.0):
return compare_images(
actual_results='%s' % actual_results,
expected_results='%s' % expected_results)