-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path__init__.py
86 lines (74 loc) · 2.56 KB
/
__init__.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
85
86
#!/usr/bin/env python
##############################################################################
#
# diffpy.srreal by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2012 The Trustees of Columbia University
# in the City of New York. All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE_DANSE.txt for license information.
#
##############################################################################
"""Unit tests for diffpy.srreal.
"""
import unittest
import logging
# create logger instance for the tests subpackage
logging.basicConfig()
logger = logging.getLogger(__name__)
del logging
def testsuite(pattern=''):
'''Create a unit tests suite for diffpy.srreal package.
Parameters
----------
pattern : str, optional
Regular expression pattern for selecting test cases.
Select all tests when empty. Ignore the pattern when
any of unit test modules fails to import.
Returns
-------
suite : `unittest.TestSuite`
The TestSuite object containing the matching tests.
'''
import re
from os.path import dirname
from itertools import chain
from importlib.resources import files, as_file
loader = unittest.defaultTestLoader
ref = files(__package__)
with as_file(ref) as thisdir:
depth = __name__.count('.') + 1
topdir = thisdir
for i in range(depth):
topdir = dirname(topdir)
suite_all = loader.discover(thisdir, top_level_dir=topdir)
# always filter the suite by pattern to test-cover the selection code.
suite = unittest.TestSuite()
rx = re.compile(pattern)
tsuites = list(chain.from_iterable(suite_all))
tsok = all(isinstance(ts, unittest.TestSuite) for ts in tsuites)
if not tsok: # pragma: no cover
return suite_all
tcases = chain.from_iterable(tsuites)
for tc in tcases:
tcwords = tc.id().split('.')
shortname = '.'.join(tcwords[-3:])
if rx.search(shortname):
suite.addTest(tc)
# verify all tests are found for an empty pattern.
assert pattern or suite_all.countTestCases() == suite.countTestCases()
return suite
def test():
'''Execute all unit tests for the diffpy.srreal package.
Returns
-------
result : `unittest.TestResult`
'''
suite = testsuite()
runner = unittest.TextTestRunner()
result = runner.run(suite)
return result
# End of file