pytestconf.py
changeset 4766 162b2b127b15
child 4777 7e37cb866e97
equal deleted inserted replaced
4764:ec9c20c6b9f7 4766:162b2b127b15
       
     1 """pytest configuration file: we need this to properly remove ressources
       
     2 cached on test classes, at least until we've proper support for teardown_class
       
     3 """
       
     4 import sys
       
     5 from os.path import split, splitext
       
     6 from logilab.common.pytest import PyTester
       
     7 
       
     8 from cubicweb.etwist.server import _gc_debug
       
     9 
       
    10 class CustomPyTester(PyTester):
       
    11     def testfile(self, filename, batchmode=False):
       
    12         try:
       
    13             return super(CustomPyTester, self).testfile(filename, batchmode)
       
    14         finally:
       
    15             modname = splitext(split(filename)[1])[0]
       
    16             for cls in vars(sys.modules[modname]).values():
       
    17                 if getattr(cls, '__module__', None) != modname:
       
    18                     continue
       
    19                 clean_repo_test_cls(cls)
       
    20             #_gc_debug()
       
    21 
       
    22 def clean_repo_test_cls(cls):
       
    23     if 'repo' in cls.__dict__:
       
    24         if cls.repo.__dict__: # empty dict when already shutted down
       
    25             cls.repo.shutdown()
       
    26         del cls.repo
       
    27     for clsattr in ('cnx', '_orig_cnx', 'config', '_config', 'vreg', 'schema'):
       
    28         if clsattr in cls.__dict__:
       
    29             delattr(cls, clsattr)