root / ase / test / __init__.py @ 1
Historique | Voir | Annoter | Télécharger (2,63 ko)
1 |
import sys |
---|---|
2 |
import unittest |
3 |
from glob import glob |
4 |
|
5 |
class NotAvailable(SystemExit): |
6 |
def __init__(self, msg, code=0): |
7 |
SystemExit.__init__(self, (msg,code,)) |
8 |
self.msg = msg
|
9 |
self.code = code
|
10 |
|
11 |
# -------------------------------------------------------------------
|
12 |
|
13 |
# Custom test case/suite for embedding unittests in the test scripts
|
14 |
|
15 |
if sys.version_info < (2, 4, 0, 'final', 0): |
16 |
class CustomTestCase(unittest.TestCase): |
17 |
assertTrue = unittest.TestCase.failUnless |
18 |
assertFalse = unittest.TestCase.failIf |
19 |
else:
|
20 |
from unittest import TestCase as CustomTestCase |
21 |
|
22 |
from ase.parallel import paropen |
23 |
|
24 |
class CustomTextTestRunner(unittest.TextTestRunner): |
25 |
def __init__(self, logname, descriptions=1, verbosity=1): |
26 |
self.f = paropen(logname, 'w') |
27 |
unittest.TextTestRunner.__init__(self, self.f, descriptions, verbosity) |
28 |
|
29 |
def run(self, test): |
30 |
stderr_old = sys.stderr |
31 |
try:
|
32 |
sys.stderr = self.f
|
33 |
testresult = unittest.TextTestRunner.run(self, test)
|
34 |
finally:
|
35 |
sys.stderr = stderr_old |
36 |
return testresult
|
37 |
|
38 |
# -------------------------------------------------------------------
|
39 |
|
40 |
class ScriptTestCase(unittest.TestCase): |
41 |
def __init__(self, methodname='testfile', filename=None, display=True): |
42 |
unittest.TestCase.__init__(self, methodname)
|
43 |
self.filename = filename
|
44 |
self.display = display
|
45 |
|
46 |
def testfile(self): |
47 |
try:
|
48 |
execfile(self.filename, {'display': self.display}) |
49 |
except KeyboardInterrupt: |
50 |
raise RuntimeError('Keyboard interrupt') |
51 |
except NotAvailable, err:
|
52 |
# Only non-zero error codes are failures
|
53 |
if err.code:
|
54 |
raise
|
55 |
|
56 |
def id(self): |
57 |
return self.filename |
58 |
|
59 |
def __str__(self): |
60 |
return '%s (ScriptTestCase)' % self.filename.split('/')[-1] |
61 |
|
62 |
def __repr__(self): |
63 |
return "ScriptTestCase(filename='%s')" % self.filename |
64 |
|
65 |
|
66 |
def test(verbosity=1, dir=None, display=True, stream=sys.stdout): |
67 |
ts = unittest.TestSuite() |
68 |
if dir is None: |
69 |
dir = __path__[0]
|
70 |
tests = glob(dir + '/*.py') |
71 |
tests.sort() |
72 |
for test in tests: |
73 |
if test.endswith('__init__.py'): |
74 |
continue
|
75 |
if test.endswith('COCu111.py'): |
76 |
lasttest = test |
77 |
continue
|
78 |
ts.addTest(ScriptTestCase(filename=test, display=display)) |
79 |
ts.addTest(ScriptTestCase(filename=lasttest, display=display)) |
80 |
|
81 |
from ase.utils import devnull |
82 |
sys.stdout = devnull |
83 |
|
84 |
ttr = unittest.TextTestRunner(verbosity=verbosity, stream=stream) |
85 |
results = ttr.run(ts) |
86 |
|
87 |
sys.stdout = sys.__stdout__ |
88 |
|
89 |
return results
|