diff options
author | Alexandre Fayolle <alexandre.fayolle@logilab.fr> | 2008-01-08 12:45:15 +0100 |
---|---|---|
committer | Alexandre Fayolle <alexandre.fayolle@logilab.fr> | 2008-01-08 12:45:15 +0100 |
commit | beb566fd0fdb228c9216394ad343838a37c36460 (patch) | |
tree | 88fe72bda4e21dd9011e0d38965e9ca35688e641 | |
parent | aa207c56090e3fd59d666b1c43912840822e853c (diff) | |
parent | 63c07976443a753f2f77c6bd225a5a3c7b1887e4 (diff) | |
download | logilab-common-beb566fd0fdb228c9216394ad343838a37c36460.tar.gz |
merge
-rw-r--r-- | test/unittest_testlib.py | 35 | ||||
-rw-r--r-- | testlib.py | 28 |
2 files changed, 62 insertions, 1 deletions
diff --git a/test/unittest_testlib.py b/test/unittest_testlib.py index 1ac737e..1306483 100644 --- a/test/unittest_testlib.py +++ b/test/unittest_testlib.py @@ -5,7 +5,7 @@ __revision__ = '$Id: unittest_testlib.py,v 1.5 2006-02-09 22:37:46 nico Exp $' import unittest import os import sys -from os.path import join, dirname, isdir, isfile +from os.path import join, dirname, isdir, isfile, abspath from cStringIO import StringIO import tempfile import shutil @@ -170,6 +170,39 @@ class TestlibTC(TestCase): self.assertEquals(exc.args, ('foo',)) + def test_default_datadir(self): + expected_datadir = join(dirname(abspath(__file__)), 'data') + self.assertEquals(self.datadir, expected_datadir) + self.assertEquals(self.datapath('foo'), join(expected_datadir, 'foo')) + + def test_custom_datadir(self): + class MyTC(TestCase): + datadir = 'foo' + def test_1(self): pass + + # class' custom datadir + tc = MyTC('test_1') + self.assertEquals(tc.datapath('bar'), join('foo', 'bar')) + # instance's custom datadir + tc.datadir = 'spam' + self.assertEquals(tc.datapath('bar'), join('spam', 'bar')) + + + def test_cached_datadir(self): + """test datadir is cached on the class""" + class MyTC(TestCase): + def test_1(self): pass + + expected_datadir = join(dirname(abspath(__file__)), 'data') + tc = MyTC('test_1') + self.assertEquals(tc.datadir, expected_datadir) + # changing module should not change the datadir + MyTC.__module__ = 'os' + self.assertEquals(tc.datadir, expected_datadir) + # even on new instances + tc2 = MyTC('test_1') + self.assertEquals(tc2.datadir, expected_datadir) + class GenerativeTestsTC(TestCase): @@ -54,6 +54,7 @@ from logilab.common.deprecation import class_renamed, deprecated_function, \ from logilab.common.compat import set, enumerate from logilab.common.modutils import load_module_from_name from logilab.common.debugger import Debugger +from logilab.common.decorators import cached __all__ = ['main', 'unittest_main', 'find_tests', 'run_test', 'spawn'] @@ -796,6 +797,19 @@ def parse_generative_args(params): return args, kwargs + +class ClassGetProperty(object): + """this is a simple property-like class but for + class attributes. + """ + + def __init__(self, getter): + self.getter = getter + + def __get__(self, obj, objtype): + return self.getter(objtype) + + class TestCase(unittest.TestCase): """unittest.TestCase with some additional methods""" @@ -817,6 +831,20 @@ class TestCase(unittest.TestCase): self._err = [] self._current_test_descr = None + def datadir(cls): + """helper attribute holding the standard test's data directory + + NOTE: this is a logilab's standard + """ + mod = __import__(cls.__module__) + return osp.join(osp.dirname(osp.abspath(mod.__file__)), 'data') + # cache it (use a class method to cache on class since TestCase is + # instantiated for each test run) + datadir = ClassGetProperty(cached(datadir)) + + def datapath(self, fname): + """joins the object's datadir and `fname`""" + return osp.join(self.datadir, fname) def set_description(self, descr): """sets the current test's description. |