Django 和文档文件中的测试

发布于 2024-12-07 07:23:31 字数 1593 浏览 1 评论 0原文

我的 Django 测试套件遇到了一个小问题。

我正在开发一个可以在 Django 和 Plone 中运行的 Python 包(http://pypi.python.org/pypi/jquery.pyproxy)。 所有测试都以文档测试的形式编写,无论是在 Python 代码中还是在单独的文档文件中(例如 README.txt)。

我可以让这些测试运行良好,但 Django 只是不计算它们:

[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

但是如果我有一些失败的测试,它将正确显示:

[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Failed example:
    1+1
Expected nothing
Got:
    2
**********************************************************************
1 items had failures:
   1 of  44 in README.rst
***Test Failed*** 1 failures.
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

这就是我的测试套件现在的声明方式:

import os
import doctest
from unittest import TestSuite

from jquery.pyproxy import base, utils

OPTIONFLAGS = (doctest.ELLIPSIS |
           doctest.NORMALIZE_WHITESPACE)

__test__ = {
    'base': doctest.testmod(
        m=base,
        optionflags=OPTIONFLAGS),

    'utils': doctest.testmod(
        m=utils,
        optionflags=OPTIONFLAGS),

    'readme': doctest.testfile(
        "../../../README.rst",
        optionflags=OPTIONFLAGS),

    'django': doctest.testfile(
        "django.txt",
        optionflags=OPTIONFLAGS),

    }

我想我在声明时做错了什么测试套件,但我不知道它到底是什么。

感谢您的帮助, 文森特

I'm having a small problem with my test suite with Django.

I'm working on a Python package that can run in both Django and Plone (http://pypi.python.org/pypi/jquery.pyproxy).
All the tests are written as doctests, either in the Python code or in separate docfiles (for example the README.txt).

I can have those tests running fine but Django just do not count them:

[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

But if I had some failing test, it will appear correctly:

[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Failed example:
    1+1
Expected nothing
Got:
    2
**********************************************************************
1 items had failures:
   1 of  44 in README.rst
***Test Failed*** 1 failures.
Creating test database for alias 'default'...

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

This is how my test suite is declared right now:

import os
import doctest
from unittest import TestSuite

from jquery.pyproxy import base, utils

OPTIONFLAGS = (doctest.ELLIPSIS |
           doctest.NORMALIZE_WHITESPACE)

__test__ = {
    'base': doctest.testmod(
        m=base,
        optionflags=OPTIONFLAGS),

    'utils': doctest.testmod(
        m=utils,
        optionflags=OPTIONFLAGS),

    'readme': doctest.testfile(
        "../../../README.rst",
        optionflags=OPTIONFLAGS),

    'django': doctest.testfile(
        "django.txt",
        optionflags=OPTIONFLAGS),

    }

I guess I'm doing something wrong when declaring the test suite but I don't have a clue what it is exactly.

Thanks for your help,
Vincent

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里南柯 2024-12-14 07:23:31

我终于用 suite() 方法解决了这个问题:

import os
import doctest
from django.utils import unittest

from jquery.pyproxy import base, utils

OPTIONFLAGS = (doctest.ELLIPSIS |
               doctest.NORMALIZE_WHITESPACE)

testmods = {'base': base,
            'utils': utils}
testfiles = {'readme': '../../../README.rst',
             'django': 'django.txt'}

def suite():
    return unittest.TestSuite(
        [doctest.DocTestSuite(mod, optionflags = OPTIONFLAGS)
         for mod in testmods.values()] + \
        [doctest.DocFileSuite(f, optionflags = OPTIONFLAGS)
         for f in testfiles.values()])

显然,调用 doctest.testfiledoctest.testmod 时的问题是测试是直接跑了。
使用 DocTestSuite/DocFileSuite 构建列表,然后测试运行程序运行它们。

I finally solved the problem with the suite() method:

import os
import doctest
from django.utils import unittest

from jquery.pyproxy import base, utils

OPTIONFLAGS = (doctest.ELLIPSIS |
               doctest.NORMALIZE_WHITESPACE)

testmods = {'base': base,
            'utils': utils}
testfiles = {'readme': '../../../README.rst',
             'django': 'django.txt'}

def suite():
    return unittest.TestSuite(
        [doctest.DocTestSuite(mod, optionflags = OPTIONFLAGS)
         for mod in testmods.values()] + \
        [doctest.DocFileSuite(f, optionflags = OPTIONFLAGS)
         for f in testfiles.values()])

Apparently the problem when calling doctest.testfile or doctest.testmod is that the tests are directly ran.
Using DocTestSuite/DocFileSuite builds the list and then the test runner runs them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文