# Copyright (c) 2002-2004 LOGILAB S.A. (Paris, FRANCE). # http://www.logilab.fr/ -- mailto:contact@logilab.fr # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 2 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. """ functions usefull to generate unit tests """ __revision__ = "$Id: testutils.py,v 1.6 2005/04/15 11:00:46 syt Exp $" from pyreverse.utils import time_tag as _time_tag def open_unittest(pgm, name, header='', cvs_vars=[('__revision__', 'Id', '')], doc=''): """open a new unit test file""" cvs_string = '' doc = doc or '' for var_title, cvs_var, select in cvs_vars: cvs_string = '%s%s = "$%s:$"%s\n' % ( cvs_string, var_title, cvs_var, select) return '''%s"""unit tests for module %s squeleton generated by %s on %s %s """ %s import unittest import sys from %s import *'''% (header, name, pgm, _time_tag(), doc, cvs_string, name) def close_unittest(): """close the unittest by adding test execution functions """ return ''' if __name__ == "__main__": unittest.main()''' def open_testcase(name, doc=None, setup=None, teardown=None): """return a test case statement""" klass = '\n\nclass %s(unittest.TestCase):' % name if doc is not None: klass += '''\n """%s"""\n''' % doc if setup is not None: klass += ''' def setUp(self): """called before each test from this class""" %s ''' % setup if teardown is not None: klass += ''' def tearDown(self): """called after each test from this class""" %s ''' % teardown return klass def open_func(name, doc=None): """return a function definition statement""" func = '''\n\n def %s(self):''' % (name) if doc is not None: func += '''\n """%s"""''' % doc.lstrip() return func ## def assert_(expression, msg='Assertion failed'): ## """return an assert_ statement""" ## return ''' ## self.assert_(%s, %s)''' % (expression, msg) def assert_equal(first, second, msg=None): """return an assertEqual statement""" if msg is not None: return ''' self.assertEquals(%s, %s, %s)''' % (first, second, msg) else: return ''' self.assertEquals(%s, %s)''' % (first, second) def assert_raise(exception, arguments): """return an assertRaises statement""" return ''' self.assertRaises(%s, %s)''' % (exception, arguments) def wrap_file(filename): """wrap a file with into comment """ stream = open(filename) from cStringIO import StringIO output = StringIO for line in stream.readlines(): output.write('# %s'%line) stream.close() return output.getvalue()