File: test_extra.py

package info (click to toggle)
python-reportlab 1.20debian-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 6,068 kB
  • ctags: 5,801
  • sloc: python: 53,293; xml: 1,494; makefile: 85
file content (101 lines) | stat: -rw-r--r-- 3,083 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""This executes tests defined outside the normal test suite.

See docstring for class ExternalTestCase for more information.
"""


import os, string, fnmatch, re, sys

import reportlab
from reportlab.test import unittest
from reportlab.test.utils import SecureTestCase


RL_HOME = os.path.dirname(reportlab.__file__)
EXTRA_FILE = 'extra.txt'


class ExternalTestCase(SecureTestCase):
    """Test case starting cases external to the normal RL suite.

    This test case runs additional test cases defined in external
    modules which must also be using the unittest framework.
    These additional modules must be defined in a file named
    'extra.txt' which needs to be located in the reportlab/test
    folder and contains one path per line.

    The paths of these modules can contain stuff from the Unix
    world like '~', '.', '..' and '$HOME' and can have absolute
    or relative paths. If they are relative they start from the
    reportlab/test folder.

    This is a sample 'extra.txt' file:

        foo.py
        ./foo.py
        bar/foo.py
        ../foo.py
        /bar/foo.py
        ~/foo.py
        ~/bar/foo.py
        ~/../foo.py
        $HOME/bar/foo.py
    """

    def test0(self):
        "Execute external test cases."

        cwd = os.getcwd()

        # look for a file named 'extra.txt' in test directory,
        # exit if not found
        extraFilename = os.path.join(RL_HOME, 'test', EXTRA_FILE)
        if not os.path.exists(extraFilename):
            return

        # read module paths from file
        extraModulenames = open(extraFilename).readlines()
        extraModulenames = map(string.strip, extraModulenames)

        # expand pathnames as much as possible
        for f in extraModulenames:
            if f == '':
                continue
            if f[0] == '#':
                continue
            f = os.path.expanduser(f)
            f = os.path.expandvars(f)
            f = os.path.abspath(f)

            if os.path.exists(f):
                # look for a makeSuite function and execute it if present
                folder = os.path.abspath(os.path.dirname(f))
                modname = os.path.splitext(os.path.basename(f))[0]
                os.chdir(folder)
                sys.path.insert(0, folder)

                module = __import__(modname) # seems to fail sometimes...
                if 'makeSuite' in dir(module):
                    print "running", f
                    testSuite = module.makeSuite()
                    unittest.TextTestRunner().run(testSuite)
                os.chdir(cwd)
                sys.path = sys.path[1:]


def makeSuite():
    suite = unittest.TestSuite()
    loader = unittest.TestLoader()
    if sys.platform[:4] != 'java':
        suite.addTest(loader.loadTestsFromTestCase(ExternalTestCase))

    return suite


#noruntests
if __name__ == "__main__":
    if len(sys.argv) > 1:
        EXTRA_FILE = sys.argv[1]
        assert os.path.isfile(EXTRA_FILE), 'file %s not found!' % EXTRA_FILE
    # otherwise, extra.txt will be used
    unittest.TextTestRunner().run(makeSuite())