mirror of
https://github.com/triqs/dft_tools
synced 2024-08-31 16:23:41 +02:00
![Oleg E. Peil](/assets/img/avatar_default.png)
The names of the test suites have been prefixed with an underscore to avoid name conflicts with corresponding modules. Also an attempt to make a scan of all tests has been made by creating a 'test_all.py' script that is supposed to discover all test cases and run them. Unfortunately, this does not work as expected because many tests use input files assumed to be found in the current directory, which is not true if the tests are run from a different (parent) directory. This can be fixed by either forcing the change of directory (but it seems that 'unittest' does not have this functionality) or prepending input file names with the current module directory.
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
r"""
|
|
Module defining a custom TestCase with extra functionality.
|
|
"""
|
|
|
|
import unittest
|
|
import numpy as np
|
|
import difflib
|
|
|
|
class MyTestCase(unittest.TestCase):
|
|
"""
|
|
Custom TestCase class supporting additional equality checks:
|
|
- numpy array equality
|
|
- file equality
|
|
"""
|
|
def __init__(self, *args, **kwargs):
|
|
"""
|
|
Initializes a custom equality function for comparing numpy arrays.
|
|
"""
|
|
super(MyTestCase, self).__init__(*args, **kwargs)
|
|
self.addTypeEqualityFunc(np.ndarray, self.is_arrays_equal)
|
|
|
|
def is_arrays_equal(self, arr1, arr2, msg=None):
|
|
"""
|
|
Raises self.failureException is arrays arr1 and arr2
|
|
are not equal.
|
|
"""
|
|
if not np.allclose(arr1, arr2):
|
|
raise self.failureException(msg)
|
|
|
|
def assertFileEqual(self, file1, file2):
|
|
"""
|
|
Compares two files using difflib.
|
|
Empty lines are ignored.
|
|
Files are assumed to be relatively small, otherwise
|
|
the data is truncated.
|
|
"""
|
|
MAX_SIZE = 100000
|
|
with open(file1, 'r') as f1:
|
|
str1 = f1.read(MAX_SIZE)
|
|
with open(file2, 'r') as f2:
|
|
str2 = f2.read(MAX_SIZE)
|
|
#
|
|
# Make a diff
|
|
#
|
|
# Remove empty lines
|
|
lstr1 = filter(lambda s: s.strip() != '', str1.splitlines(True))
|
|
lstr2 = filter(lambda s: s.strip() != '', str2.splitlines(True))
|
|
# diff
|
|
delta = difflib.unified_diff(lstr1, lstr2)
|
|
# combine delta's to a string
|
|
diff = ''.join(delta)
|
|
# if 'diff' is non-empty, files are different
|
|
if diff:
|
|
return self.fail("Files '%s' and '%s' differ"%(file1, file2))
|
|
|
|
|