pytestconf.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 08 Apr 2010 11:04:17 +0200
changeset 5184 955ee1b24756
parent 5158 5e9055b8c10a
child 5423 e15abfdcce38
permissions -rw-r--r--
[c-c newcube] #1192: simpler cubicweb-ctl newcube, and more * new --layout option, to get 'simple' or 'full' cube * new --licence option to specify license for the cube * added README and sobjects.py template (the later only added in full layout) * fixed python file templates to get proper headers according to the config

"""pytest configuration file: we need this to properly remove ressources
cached on test classes, at least until we've proper support for teardown_class
"""
import sys
from os.path import split, splitext
from logilab.common.pytest import PyTester

class CustomPyTester(PyTester):
    def testfile(self, filename, batchmode=False):
        try:
            return super(CustomPyTester, self).testfile(filename, batchmode)
        finally:
            modname = splitext(split(filename)[1])[0]
            try:
                module = sys.modules[modname]
            except KeyError:
                # error during test module import
                return
            for cls in vars(module).values():
                if getattr(cls, '__module__', None) != modname:
                    continue
                clean_repo_test_cls(cls)

def clean_repo_test_cls(cls):
    if 'repo' in cls.__dict__:
        if not cls.repo._shutting_down:
            cls.repo.shutdown()
        del cls.repo
    for clsattr in ('cnx', '_orig_cnx', 'config', '_config', 'vreg', 'schema'):
        if clsattr in cls.__dict__:
            delattr(cls, clsattr)