问题标签 [doctest]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
639 浏览

web2py - Web2py 模块和文档测试

我有一个模块applications/webapp/modules/a.py,其中包含一个local_import来导入applications/webapp/modules/b.py。我想对 a.py 和 b.py 进行 doctest。带有“-T”选项的 web2py shell 部分工作,但在说“在 a.py 中查找测试:”后会抱怨下面粘贴的异常。

我知道这是因为“-T”选项仅用于测试控制器。但是由于 a.py 中的 local_import,没有简单的方法可以在 web2py 环境之外对 a.py 进行 doctest。那么我应该如何重构模块以便我可以对它们进行 doctest 呢?

回溯(最后一次调用):
文件“web2py.py”,第 19 行,在 gluon.widget.start(cron=True) 文件“/home/.../web2py/gluon/widget.py”,第 796 行,在开始测试(options.test,verbose=options.verbose)文件“/home/.../web2py/gluon/shell.py”,第 326 行,在测试 doctest_object(name,obj)文件“/home/.. ./web2py/gluon/shell.py”,第 322 行,在 doctest_object 中 doctest_object(attr_name, o) 文件“/home/.../web2py/gluon/shell.py”,第 321 行,在 doctest_object 中 o = eval(' %s.%s' % (name, attr_name), globs) File "", line 1, in NameError: name ' class ' is not defined

0 投票
2 回答
109 浏览

python - 在 python 中为同一事物测试多个函数的更好方法

假设一个包含一大堆函数的 python 文件,我想使用doctest. 例如,每个函数都接受一个字符串和一个连接对象 ( httplib.HTTPConnection(...))。因此,如果字符串为空或None. 测试看起来像这样。

如您所见,测试是相同的,只是函数名称发生了变化。是否可以对其进行重构以避免代码重复?

或者有没有更好的方法将这些测试集中在一起?

0 投票
1 回答
146 浏览

python - How to find out the location of a doctest file when running nose?

I am using python nose to run a bunch of doctests in subdirectories. The calling script is nose.cmd:

(The doctests live in *.rst files.)

Example directory structure:

How can the path of the rst file be retrieved in the python code inside the rst file itself? For example in mymod.rst the usual sys.path and __file__ give no information about the path \module1\ but I need that for the imports.

Of course I could introduce a constant into each rst file that contains its directory, but that's not what I prefer.

0 投票
2 回答
859 浏览

python - 如何测试通过 urllib2 检索数据的函数?

我开始在 python 中进行测试,我问自己如何测试这种方法。

使用 doctest 或 unittest,如何最好地实现?我想传递get_response()一个测试 url 和一些测试参数,它们存在于现实世界中,并检查是否response.read()返回预期的数据。但不知何故,我觉得,这不是应该做的方式。有什么建议么?我将不胜感激有关如何在发短信的情况下处理此类情况的建议。

0 投票
1 回答
1802 浏览

python - python doctest 异常测试处理

我在一个名为test2.txt.

我用 调用测试运行python -m test2.txt。下面的结果完全出乎我的意料。

终端输出截图

我的想法是测试应该成功,因为我已经在我的test2.txt文件中编写了预期的输出,并且它“几乎”与我从控制台输出中得到的结果相匹配。我尝试添加'File "G:\"'.... line?但测试仍然失败。

0 投票
1 回答
200 浏览

python - Django 和 docfiles 中的测试

我的 Django 测试套件有一个小问题。

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

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

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

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

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

谢谢你的帮助,文森特

0 投票
2 回答
942 浏览

python - python doctest 默认命名空间

在我的模块的文档测试中,我想用完整的命名空间引用我的模块,例如:

而且我想通过编写避免使文档测试混乱:

在每个文档测试中。

如果我运行 doctest.testmod,我知道我可以使用globs关键字来提供这个,而如果我运行nose,我可以使用该setup功能。

是否有另一种可以同时使用的标准方法?

0 投票
3 回答
1203 浏览

python - django doctests 没有运行

我在使用 django-nose 运行 django doctests 时遇到问题。添加到 /tests 目录的单元测试运行良好,但 doctests 不是。

我正在尝试在我的“季节”模块上运行文档测试:

并得到这个输出:

我只是在尝试一个基本的 doctest 来尝试让它工作,例如:

这是在我的models.py 中。我已经尝试过其他 doctests 实际测试代码,但仍然没有看到任何测试运行。当我使用 --verbosity 3 运行时,我看到一行可能是问题所在:

我找不到更多关于这意味着什么的信息。

来自 settings.py 的相关片段:

django_nose 在 INSTALLED_APPS 中位于南之后,如 django-nose 文档中所指定。我正在使用此处建议的 --with-doctest 参数:Nose not running Django doctests,并已按照此处的建议将我的 django-nose 更新到最新版本:Why is django-nose running the doctests in my models?

这些是我正在使用的版本:
django 1.3
python 2.7.1
django-nose 0.1.3
nose 1.1.2

我觉得我在这里缺少一些基本设置。让我知道是否需要任何其他信息。任何帮助表示赞赏,谢谢!

0 投票
4 回答
849 浏览

python - 如何确定代码是否在 doctest 中运行?

如何让我的(Python 2.7)代码知道它是否在 doctest 中运行?

场景如下:我有一个函数,它print()输出到作为参数传入的文件描述符,如下所示:

但是当我尝试printing_func()在 doctest 中使用时,测试失败了;由于我file在调用时指定了关键字参数print(),因此输出实际上会转到sys.stdout而不是 doctest 模块设置的任何默认输出重定向,并且 doctest 永远不会看到输出。

那么我怎样才能printing_func()知道它是否在 doctest 中运行,以便它知道file在调用时不传递关键字参数print()呢?

0 投票
1 回答
1206 浏览

python - 嵌套文档字符串的 Doctest

假设我有以下代码:

让我们将它保存为 foo.py。当我运行时:

已尝试缩进文档字符串(>>> a = '''...'''。已检查所有缩进 - 每个缩进 4 个空格)并将单引号更改为双引号(>>> a = """.. .."""),错误是不同的,文档测试不会成功。目前唯一可行的方法是将所有行加入一个极长的字符串并用'\r\n'分隔。

我错过了什么吗?