File: spytest.py

package info (click to toggle)
python-spectral 0.22.4-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,064 kB
  • sloc: python: 13,161; makefile: 7
file content (79 lines) | stat: -rw-r--r-- 2,419 bytes parent folder | download
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
'''
Base class for all tests.
'''

from __future__ import absolute_import, division, print_function, unicode_literals

import collections
import sys

class SpyTest(object):
    '''Base class for test cases.

    Test classes are created by sub-classing SpyTest and defining methods
    whose names start with "test_".
    '''
    def setup(self):
        '''Method to be run before derived class test methods are called.'''
        pass

    def finish(self):
        '''Method run after all test methods have run.'''
        pass

    def run(self):
        '''Runs all "test_*" methods in a derived class.

        Before running subclass test_ methods, the `startup` method will be
        called. After all test_ methods have been run, the `finish` method
        is called.
        '''
        import spectral.tests as tests
        from spectral.tests import abort_on_fail

        self.setup()
        class NullStdOut(object):
            def write(*args, **kwargs):
                pass
            def flush(self):
                pass
        null = NullStdOut()
        methods = [getattr(self, s) for s in sorted(dir(self)) if s.startswith('test_')]
        methods = [m for m in methods if isinstance(m, collections.Callable)]
        stdout = sys.stdout
        for method in methods:
            print(format('Testing ' + method.__name__.split('_', 1)[-1],
                         '.<60'), end=' ')
            tests._num_tests_run += 1
            try:
                sys.stdout = null
                method()
                stdout.write('OK\n')
            except AssertionError:
                stdout.write('FAILED\n')
                tests._num_tests_failed += 1
                if tests.abort_on_fail:
                    raise
            finally:
                sys.stdout = stdout
        self.finish()

# The following test method is now deprecated and should no longer be used.

def test_method(method):
    '''Decorator function for unit tests.'''
    import spectral.tests as tests

    def meth(self):
        print(format('Testing ' + method.__name__.split('_', 1)[-1],
                     '.<40'), end=' ')
        try:
            method(self)
            print('OK')
            tests._num_tests_run += 1
        except AssertionError:
            print('FAILED')
            tests._num_tests_failed += 1
            if tests.abort_on_fail:
                raise
    return meth