def main(pattern='test_*.py'): try: folder = os.path.dirname(__file__) assert folder except: folder = os.path.dirname(sys.argv[0]) or os.getcwd() #allow for Benn's "screwball cygwin distro": if folder == '': folder = '.' from reportlab.lib.utils import isSourceDistro haveSRC = isSourceDistro() def cleanup(folder, patterns=('*.pdf', '*.log', '*.svg', 'runAll.txt', 'test_*.txt', '_i_am_actually_a_*.*')): if not folder: return for pat in patterns: for filename in GlobDirectoryWalker(folder, pattern=pat): try: os.remove(filename) except: pass # special case for reportlab/test directory - clean up # all PDF & log files before starting run. You don't # want this if reusing runAll anywhere else. if string.find(folder, 'reportlab' + os.sep + 'test') > -1: cleanup(folder) cleanup(outputfile('')) NI = [] cleanOnly = '--clean' in sys.argv if not cleanOnly: testSuite = makeSuite(folder, nonImportable=NI, pattern=pattern + (not haveSRC and 'c' or '')) unittest.TextTestRunner().run(testSuite) if haveSRC: cleanup(folder, patterns=('*.pyc', '*.pyo')) if not cleanOnly: if NI: sys.stderr.write( '\n###################### the following tests could not be imported\n' ) for f, tb in NI: print 'file: "%s"\n%s\n' % (f, string.join(tb, '')) printLocation()
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:]
from reportlab.lib.utils import FmtSelfDict class MixedIn(FmtSelfDict): def __init__(self): self.a = 'AA' self._b = '_BB' self.d = '(overridden)' obj = MixedIn() self.assertEqual('blah', obj._fmt('blah')) self.assertEqual('blah %', obj._fmt('blah %%')) self.assertRaises(ValueError, obj._fmt, 'blah %') self.assertEqual( 'moon AA june_BB spoon %(a)sCC ni', obj._fmt('moon %(a)s june%(_b)s spoon %%(a)s%(c)s %(d)s', c='CC', C='boon', d='ni')) self.assertRaises(AttributeError, obj._fmt, '%(c)s') # XXX bit weird, can this be changed? def makeSuite(): return makeSuiteForClasses(FmtTestCase) #noruntests if __name__ == "__main__": unittest.TextTestRunner().run(makeSuite()) printLocation()