From 9eedc60aa0b11bd0ac7d0e136cf3afc865c62d3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Fri, 12 Mar 2010 17:47:49 +0100 Subject: [PATCH] Improve unittest to run test_suite inside setuptools --- setup.py | 2 + trytond/modules/__init__.py | 14 ++++++- trytond/server.py | 7 ++-- trytond/test_loader.py | 10 +++++ trytond/tests/__init__.py | 2 + trytond/tests/test_fields.py | 3 -- trytond/tests/test_importdata.py | 3 -- trytond/tests/test_modelsingleton.py | 3 -- trytond/tests/test_mptt.py | 3 -- trytond/tests/test_tryton.py | 56 +++------------------------- trytond/tools/misc.py | 7 +++- 11 files changed, 41 insertions(+), 69 deletions(-) create mode 100644 trytond/test_loader.py diff --git a/setup.py b/setup.py index 8190d13a3e..5c6279715c 100644 --- a/setup.py +++ b/setup.py @@ -62,4 +62,6 @@ setup(name=PACKAGE, 'timezone': ['pytz'], }, zip_safe=False, + test_suite='trytond.tests', + test_loader='trytond.test_loader:Loader', ) diff --git a/trytond/modules/__init__.py b/trytond/modules/__init__.py index 1132f4cc51..62dbc348f3 100644 --- a/trytond/modules/__init__.py +++ b/trytond/modules/__init__.py @@ -11,7 +11,7 @@ import traceback import logging OPJ = os.path.join -MODULES_PATH = os.path.dirname(__file__) +MODULES_PATH = os.path.abspath(os.path.dirname(__file__)) MODULES = [] @@ -134,7 +134,8 @@ def create_graph(module_list, force=None): tryton_file = OPJ(MODULES_PATH, module, '__tryton__.py') mod_path = OPJ(MODULES_PATH, module) if module in ('ir', 'workflow', 'res', 'webdav', 'test'): - root_path = os.path.dirname(os.path.dirname(__file__)) + root_path = os.path.abspath(os.path.dirname( + os.path.dirname(__file__))) tryton_file = OPJ(root_path, module, '__tryton__.py') mod_path = OPJ(root_path, module) elif module in EGG_MODULES: @@ -142,6 +143,11 @@ def create_graph(module_list, force=None): tryton_file = OPJ(ep.dist.location, 'trytond', 'modules', module, '__tryton__.py') mod_path = OPJ(ep.dist.location, 'trytond', 'modules', module) + if not os.path.isfile(tryton_file) or not os.path.isdir(mod_path): + # When testing modules from setuptools location is the module + # directory + tryton_file = OPJ(ep.dist.location, '__tryton__.py') + mod_path = os.path.dirname(ep.dist.location) if os.path.isfile(tryton_file) or zipfile.is_zipfile(mod_path+'.zip'): try: info = tools.safe_eval(tools.file_open(tryton_file, @@ -375,6 +381,10 @@ def register_classes(reload_p=False): ep = EGG_MODULES[module] mod_path = os.path.join(ep.dist.location, *ep.module_name.split('.')[:-1]) + if not os.path.isdir(mod_path): + # When testing modules from setuptools location is the module + # directory + mod_path = os.path.dirname(ep.dist.location) mod_file, pathname, description = imp.find_module(module, [mod_path]) try: diff --git a/trytond/server.py b/trytond/server.py index 3fd7a04644..8d417a5315 100644 --- a/trytond/server.py +++ b/trytond/server.py @@ -4,9 +4,6 @@ %prog [options] """ import logging -FORMAT = '[%(asctime)s] %(levelname)s:%(name)s:%(message)s' -DATEFMT = '%a %b %d %H:%M:%S %Y' -logging.basicConfig(level=logging.DEBUG, format=FORMAT, datefmt=DATEFMT) import logging.handlers import sys, os, signal import time @@ -25,6 +22,10 @@ import random class TrytonServer(object): def __init__(self): + logging.basicConfig(level=logging.DEBUG, + format='[%(asctime)s] %(levelname)s:%(name)s:%(message)s', + datefmt='%a %b %d %H:%M:%S %Y') + CONFIG.parse() if CONFIG['logfile']: diff --git a/trytond/test_loader.py b/trytond/test_loader.py new file mode 100644 index 0000000000..22ad30f72b --- /dev/null +++ b/trytond/test_loader.py @@ -0,0 +1,10 @@ +#This file is part of Tryton. The COPYRIGHT file at the top level of +#this repository contains the full copyright notices and license terms. + +from unittest import TestLoader + + +class Loader(TestLoader): + + def loadTestsFromModule(self, module): + return module.suite() diff --git a/trytond/tests/__init__.py b/trytond/tests/__init__.py index c86640b67f..11d6eb6791 100644 --- a/trytond/tests/__init__.py +++ b/trytond/tests/__init__.py @@ -1,2 +1,4 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. + +from test_tryton import all_suite as suite diff --git a/trytond/tests/test_fields.py b/trytond/tests/test_fields.py index 15f71e0f21..c88e09b9fa 100644 --- a/trytond/tests/test_fields.py +++ b/trytond/tests/test_fields.py @@ -3,9 +3,6 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -import logging -logging.basicConfig(level=logging.FATAL) - import unittest import datetime from decimal import Decimal diff --git a/trytond/tests/test_importdata.py b/trytond/tests/test_importdata.py index 786b59d799..7d91a6a993 100644 --- a/trytond/tests/test_importdata.py +++ b/trytond/tests/test_importdata.py @@ -3,9 +3,6 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -import logging -logging.basicConfig(level=logging.FATAL) - import unittest from trytond.tests.test_tryton import POOL, DB, USER, CONTEXT, install_module diff --git a/trytond/tests/test_modelsingleton.py b/trytond/tests/test_modelsingleton.py index 535262bbcc..7251250437 100644 --- a/trytond/tests/test_modelsingleton.py +++ b/trytond/tests/test_modelsingleton.py @@ -3,9 +3,6 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -import logging -logging.basicConfig(level=logging.FATAL) - import unittest from trytond.tests.test_tryton import POOL, DB, USER, CONTEXT, install_module diff --git a/trytond/tests/test_mptt.py b/trytond/tests/test_mptt.py index a8dcda5927..2d2cc3da4a 100644 --- a/trytond/tests/test_mptt.py +++ b/trytond/tests/test_mptt.py @@ -3,9 +3,6 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -import logging -logging.basicConfig(level=logging.FATAL) - import unittest from trytond.tests.test_tryton import POOL, DB, USER, CONTEXT, install_module diff --git a/trytond/tests/test_tryton.py b/trytond/tests/test_tryton.py index d9a5834536..6a9aeb7c9e 100644 --- a/trytond/tests/test_tryton.py +++ b/trytond/tests/test_tryton.py @@ -3,9 +3,6 @@ #This file is part of Tryton. The COPYRIGHT file at the top level of #this repository contains the full copyright notices and license terms. -import logging -logging.basicConfig(level=logging.FATAL) - import sys, os DIR = os.path.abspath(os.path.normpath(os.path.join(__file__, '..', '..', '..', 'trytond'))) @@ -49,19 +46,6 @@ Pool.test = True POOL = Pool(DB_NAME) -class DBTestCase(unittest.TestCase): - ''' - Test DB service. - ''' - - def test0010create(self): - ''' - Create database. - ''' - self.assert_(create(DB_NAME, USER_PASSWORD, 'en_US', - CONFIG['admin_passwd'])) - - class ModelViewTestCase(unittest.TestCase): ''' Test ModelView @@ -104,7 +88,7 @@ def install_module(name): database = Database().connect() cursor = database.cursor() if DB_NAME not in database.list(cursor): - create(DB_NAME, 'admin', 'en_US', CONFIG['admin_passwd']) + create(DB_NAME, USER_PASSWORD, 'en_US', CONFIG['admin_passwd']) cursor.close() cursor = DB.cursor() module_obj = POOL.get('ir.module.module') @@ -162,10 +146,7 @@ def suite(): ''' Return test suite for other modules ''' - # import module from root directory to be the same as in modules - import trytond.tests.test_tryton - return unittest.TestLoader().loadTestsFromTestCase( - trytond.tests.test_tryton.DBTestCase) + return unittest.TestSuite() def all_suite(): ''' @@ -206,39 +187,12 @@ def modules_suite(): test_mod = zimp.load_module(test_module) except zipimport.ZipImportError: continue - elif os.path.isdir(os.path.join(MODULES_PATH, module)): + elif os.path.isdir(os.path.join(MODULES_PATH, module)) or \ + module in EGG_MODULES: try: - mod_file, pathname, description = imp.find_module( - 'tests', [os.path.join(MODULES_PATH, module)]) + test_mod = __import__(test_module, fromlist=['']) except ImportError: continue - try: - try: - test_mod = imp.load_module(test_module, mod_file, pathname, - description) - except ImportError: - continue - finally: - if mod_file is not None: - mod_file.close() - elif module in EGG_MODULES: - egg_p = EGG_MODULES[module] - mod_path = os.path.join(egg_p.dist.location, - *egg_p.module_name.split('.')[:-1]) - try: - mod_file, pathname, description = imp.find_module( - 'tests', [os.path.join(mod_path, module)]) - except ImportError: - continue - try: - try: - test_mod = imp.load_module(test_module, mod_file, pathname, - description) - except ImportError: - continue - finally: - if mod_file is not None: - mod_file.close() else: continue for test in test_mod.suite(): diff --git a/trytond/tools/misc.py b/trytond/tools/misc.py index cf4133a9ee..8d2076dd88 100644 --- a/trytond/tools/misc.py +++ b/trytond/tools/misc.py @@ -78,7 +78,7 @@ def exec_command_pipe(name, *args): def file_open(name, mode="r", subdir='modules'): """Open a file from the root dir, using a subdir folder.""" from trytond.modules import EGG_MODULES - root_path = os.path.dirname(os.path.dirname(__file__)) + root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) name3 = False if subdir == 'modules': @@ -88,6 +88,11 @@ def file_open(name, mode="r", subdir='modules'): mod_path = os.path.join(epoint.dist.location, *epoint.module_name.split('.')[:-1]) name3 = os.path.join(mod_path, name) + if not os.path.isfile(name3): + # When testing modules from setuptools location is the module + # directory + name3 = os.path.join(os.path.dirname(epoint.dist.location), + name) if subdir: if subdir == 'modules'\