我正在使用doctest测试我的代码,当我测试一个产生两行文本输出的简单函数时,我得到了意外的错误。为什么? Ubuntu 18.04上的Python 3.6.9。在Python2.7和Python3.9中可以观察到同样的错误。 测试程序(另存为doctest-bugs.py): #!/usr/bin/env python3
def testme():
"""
Emit test message.
>>> testme()
First line (no leading spaces)
Second lin
我有一个像这样的文档测试示例。
"""
This is the "iniFileGenerator" module.
>>> hintFile = "./tests/unit_test_files/hint.txt"
>>> f = iniFileGenerator(hintFile)
>>> print f.hintFilePath
./tests/unit_test_files/hint.txt
"""
class iniFileGenerator:
我试图在我的类中的Python2.7程序中在中使用一个实例。我没有从测试中得到任何输出,而是得到了一个回答:“一无所获”。
这个简化的测试用例演示了错误:
#!/usr/bin/env python2.7
# encoding: utf-8
class Dummy(object):
'''Dummy: demonstrates a doctest problem
>>> from StringIO import StringIO
... s = StringIO()
... print("s is creat
我的Django测试套件出了点小问题。
我正在开发一个可以在Django和Plone ()中运行的Python包。所有的测试都是以文档测试的形式编写的,或者是在Python代码中,或者是在单独的文档文件中(例如README.txt)。
我可以让这些测试运行得很好,但Django就是不计算它们:
[vincent ~/buildouts/tests/django_pyproxy]> bin/django test pyproxy
...
Creating test database for alias 'default'...
----------------------
我在运行django doctest和django-nose时遇到了问题。添加到/tests目录的单元测试运行良好,但文档测试运行不正常。
我正在尝试对我的"season“模块运行doctest:
python manage.py test season
并获得以下输出:
nosetests --verbosity 1 season --with-doctest
Creating test database for alias 'default'...
----------------------------------------------------------
考虑下面的演示脚本:
# -*- coding: utf-8 -*-
from __future__ import division
from __future__ import unicode_literals
def myDivi():
"""
This is a small demo that just returns the output of a divison.
>>> myDivi()
0.5
"""
return 1/2
def myUnic():
我有一个python模块some_module,它有一个导入方法的__init__.py文件,如下所示:
from .some_python_file import some_method
some_method有一个包含doctest的docstring:
def some_method():
"""
>>> assert False
"""
但是,当我在模块上运行doctest时,即使测试失败,它也会通过。
import some_module
# How do I get this to consi
我刚开始学习蟒蛇,我正在关注我所在大学的一些在线讲座。我刚刚被介绍用脚本编写文档测试。下面是我的代码:
""" Our first python source file. """
from operator import floordiv, mod
def divide_exact(n,d):
""" Return the quotient and remainder.
>>> q, r = divide_exact(2013, 10)
>>> q
我是一个Python初学者,为了学校,我有点挑剔。我的老师想让我写一个函数,返回一个带重音的句子,"print()“显示正确的字符,带重音,但doctest没有。
下面是我的代码:
def test() :
"""
>>> test()
à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û - ü - ÿ - ç
"""
print("à - â - ä - é - è - ê - ë - ï - î - ô - ö - ù - û -
我已经编写了一个用于测试的assert_raised上下文管理器,它会检查是否如预期的那样引发了异常,如果没有,就会引发AssertionError。我也写了一个doctest来测试这个,但是doctest总是失败,我不完全确定为什么。这里是doctest:
>>> for e in [TypeError, ValueError, KeyError]:
... with assert_raised(TypeError, ValueError):
... print('Raising {}...'.format(e.__name__))