pytestconf.py
author Sylvain Thénault <sylvain.thenault@logilab.fr>
Tue, 20 Apr 2010 09:58:28 +0200
changeset 5334 7da1a6ca8f65
parent 5158 5e9055b8c10a
child 5423 e15abfdcce38
permissions -rw-r--r--
[skel] packaging fix: should build package without setuptools, and fix default for dependency_links
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)