10

次のコマンドを実行した場合:

>python manage.py test

Djangoは私のアプリケーションでtests.pyを調べ、そのファイルですべてのdoctestまたは単体テストを実行します。また、実行する追加のテストについて__test__ディクショナリを調べます。したがって、次のように他のモジュールからのdoctestをリンクできます。

#tests.py
from myapp.module1 import _function1, _function2

__test__ = {
    "_function1": _function1,
    "_function2": _function2
}

より多くのdoctestを含めたい場合、この辞書にすべてを列挙するよりも簡単な方法はありますか?理想的には、Djangoにmyappアプリケーションのすべてのモジュールのすべてのdoctestを見つけてもらいたいだけです。

私がなりたい場所に私を連れて行くような反射ハックはありますか?

4

5 に答える 5

2

私は少し前にこれを自分で解決しました:

アプリ=settings.INSTALLED_APPS

アプリ内のアプリの場合:
    試す:
        a = app +'.test'
        __import __(a)
        m = sys.modules [a]
    ImportErrorを除く:#このモジュールのテストジョブはありません。次のモジュールに進みます
        継続する
    #インポートされたモジュールmを使用してテストを実行します

これにより、モジュールごとのテストを独自のtest.pyファイルに入れることができたため、残りのアプリケーションコードと混同されることはありませんでした。これを変更して、各モジュールでドキュメントテストを探し、見つかった場合は実行するのは簡単です。

于 2009-10-29T03:04:19.990 に答える
2

noseはすべてのテストを再帰的に自動的に検出するため、django-noseを使用してください。

于 2012-04-09T19:54:15.433 に答える
1

ソリューションの重要な要素は次のとおりです。

tests.py:

def find_modules(package):
    """Return list of imported modules from given package"""
    files = [re.sub('\.py$', '', f) for f in os.listdir(os.path.dirname(package.__file__))
             if f.endswith(".py") and os.path.basename(f) not in ('__init__.py', 'test.py')]
    return [imp.load_module(file, *imp.find_module(file, package.__path__)) for file in files]

def suite(package=None):
    """Assemble test suite for Django default test loader"""
    if not package: package = myapp.tests # Default argument required for Django test runner
    return unittest.TestSuite([doctest.DocTestSuite(m) for m in find_modules(package)])

再帰を追加するos.walk()には、モジュールツリーをトラバースしてPythonパッケージを検索します。

于 2009-10-29T02:36:03.123 に答える
1

アレックスとポールに感謝します。これは私が思いついたものです:

# tests.py
import sys, settings, re, os, doctest, unittest, imp

# import your base Django project
import myapp

# Django already runs these, don't include them again
ALREADY_RUN = ['tests.py', 'models.py']

def find_untested_modules(package):
    """ Gets all modules not already included in Django's test suite """
    files = [re.sub('\.py$', '', f) 
             for f in os.listdir(os.path.dirname(package.__file__))
             if f.endswith(".py") 
             and os.path.basename(f) not in ALREADY_RUN]
    return [imp.load_module(file, *imp.find_module(file, package.__path__))
             for file in files]

def modules_callables(module):
    return [m for m in dir(module) if callable(getattr(module, m))]

def has_doctest(docstring):
    return ">>>" in docstring

__test__ = {}
for module in find_untested_modules(myapp.module1):
    for method in modules_callables(module):
        docstring = str(getattr(module, method).__doc__)
        if has_doctest(docstring):

            print "Found doctest(s) " + module.__name__ + "." + method

            # import the method itself, so doctest can find it
            _temp = __import__(module.__name__, globals(), locals(), [method])
            locals()[method] = getattr(_temp, method)

            # Django looks in __test__ for doctests to run
            __test__[method] = getattr(module, method)
于 2009-10-31T05:01:52.933 に答える
1

私はDjanoのテストに精通していませんが、私が理解しているように、Djanoは、Noseと同じように、自動ユニットテスト検出を使用してますpython -m unittest discover

もしそうなら、次のファイルをディスカバリーが見つける場所に置くだけです(通常は名前を付けるtest_doctest.pyか、同様の問題です)。

your_packageテストするパッケージに変更します。すべてのモジュール(サブパッケージを含む)がドキュメントテストされます。

import doctest
import pkgutil

import your_package as root_package


def load_tests(loader, tests, ignore):
    modules = pkgutil.walk_packages(root_package.__path__, root_package.__name__ + '.')
    for _, module_name, _ in modules:
        try:
            suite = doctest.DocTestSuite(module_name)
        except ValueError:
            # Presumably a "no docstrings" error. That's OK.
            pass
        else:
            tests.addTests(suite)
    return tests
于 2014-10-10T18:37:09.817 に答える