View All Posts. MiCHiLU.com powered by Django ;-)

[Django]: tests.pyを分割する

Djangoは <app>/tests.py(models.py) しか実行してくれないので、任意のモジュールのTestCaseをglobalにimportして実行させる。

import imp, unittest

def get_tests(module):
    result = dict()
    f, fn, desc = imp.find_module("tests", module.__path__)
    _module = imp.load_module("tests", f, fn, desc)
    for key, value in _module.__dict__.items():
        if type(value) is type and issubclass(value, unittest.TestCase):
            result[key] = value
    return result

from contrib import webdesign
globals().update(get_tests(webdesign))

とかすると、 somewhere/contrib/webdesign/tests.py のTestCaseをimportした<app>で実行できる。 utils系のモジュールを階層化して管理したい時などに便利。

Sun, 9 Sep 2007 22:53:17 +0900 source edit
Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 2.1 Japan License.
View All Posts. MiCHiLU.com powered by Django ;-)