pytestconf.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """pytest configuration file: we need this to properly remove ressources
       
    19 cached on test classes, at least until we've proper support for teardown_class
       
    20 """
       
    21 import sys
       
    22 from os.path import split, splitext
       
    23 from logilab.common.pytest import PyTester
       
    24 
       
    25 class CustomPyTester(PyTester):
       
    26     def testfile(self, filename, batchmode=False):
       
    27         try:
       
    28             return super(CustomPyTester, self).testfile(filename, batchmode)
       
    29         finally:
       
    30             modname = splitext(split(filename)[1])[0]
       
    31             try:
       
    32                 module = sys.modules[modname]
       
    33             except KeyError:
       
    34                 # error during test module import
       
    35                 return
       
    36             for cls in vars(module).values():
       
    37                 if getattr(cls, '__module__', None) != modname:
       
    38                     continue
       
    39                 clean_repo_test_cls(cls)
       
    40 
       
    41 def clean_repo_test_cls(cls):
       
    42     if 'repo' in cls.__dict__:
       
    43         if not cls.repo.shutting_down:
       
    44             cls.repo.shutdown()
       
    45         del cls.repo
       
    46     for clsattr in ('cnx', 'config', '_config', 'vreg', 'schema'):
       
    47         if clsattr in cls.__dict__:
       
    48             delattr(cls, clsattr)