pytestconf.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Mon, 26 Apr 2010 18:45:03 +0200
changeset 5412 27249e3fee3d
parent 5158 5e9055b8c10a
child 5423 e15abfdcce38
permissions -rw-r--r--
Added tag cubicweb-debian-version-3.8.1-1 for changeset eb972d125eef
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4766
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     1
"""pytest configuration file: we need this to properly remove ressources
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     2
cached on test classes, at least until we've proper support for teardown_class
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     3
"""
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     4
import sys
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     5
from os.path import split, splitext
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     6
from logilab.common.pytest import PyTester
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     7
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     8
class CustomPyTester(PyTester):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
     9
    def testfile(self, filename, batchmode=False):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    10
        try:
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    11
            return super(CustomPyTester, self).testfile(filename, batchmode)
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    12
        finally:
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    13
            modname = splitext(split(filename)[1])[0]
4804
daa71eaf11e8 no key error subsequent to test module import error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4796
diff changeset
    14
            try:
daa71eaf11e8 no key error subsequent to test module import error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4796
diff changeset
    15
                module = sys.modules[modname]
daa71eaf11e8 no key error subsequent to test module import error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4796
diff changeset
    16
            except KeyError:
daa71eaf11e8 no key error subsequent to test module import error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4796
diff changeset
    17
                # error during test module import
daa71eaf11e8 no key error subsequent to test module import error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4796
diff changeset
    18
                return
daa71eaf11e8 no key error subsequent to test module import error
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4796
diff changeset
    19
            for cls in vars(module).values():
4766
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    20
                if getattr(cls, '__module__', None) != modname:
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    21
                    continue
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    22
                clean_repo_test_cls(cls)
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    23
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    24
def clean_repo_test_cls(cls):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    25
    if 'repo' in cls.__dict__:
4796
a20edc0f8b30 oops, 'not' missing
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4777
diff changeset
    26
        if not cls.repo._shutting_down:
4766
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    27
            cls.repo.shutdown()
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    28
        del cls.repo
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    29
    for clsattr in ('cnx', '_orig_cnx', 'config', '_config', 'vreg', 'schema'):
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    30
        if clsattr in cls.__dict__:
162b2b127b15 [test] get a chance to get proper garbage collection when running pytest on whole cw
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
diff changeset
    31
            delattr(cls, clsattr)