python doctest 可以忽略一些输出行吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1024411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can python doctest ignore some output lines?
提问by cjb
I'd like to write a doctest like this:
我想写一个这样的doctest:
"""
>>> print a.string()
foo : a
bar : b
date : <I don't care about the date output>
baz : c
"""
Is there any way to do this? I think it would make more sense to switch to unittest, but I'm curious whether it's possible to specify a range of output that shouldn't be matched for the test in doctest.
有没有办法做到这一点?我认为切换到 unittest 会更有意义,但我很好奇是否可以指定一个不应与 doctest 中的测试匹配的输出范围。
Thanks!
谢谢!
回答by Alex Martelli
With doctest.ELLIPSIS
, you can use ...
to mean "match any string here". You can set doctest
options with a doctest directive, to make it active for just one test case: one example in the online docsis:
使用doctest.ELLIPSIS
,您可以...
表示“匹配此处的任何字符串”。您可以doctest
使用 doctest 指令设置选项,使其仅对一个测试用例有效:在线文档中的一个示例是:
>>> print range(20) # doctest:+ELLIPSIS
[0, 1, ..., 18, 19]
If you want a doctest option to be active throughout, you can pass it as the optionflags=
argument to whatever doctest functions you use, e.g. doctest.testfile
. (You can pass multiple option flags there by using the |
operator to bit-or them).
如果您希望 doctest 选项始终处于活动状态,您可以将其作为optionflags=
参数传递给您使用的任何 doctest 函数,例如doctest.testfile
. (您可以通过使用|
运算符对它们进行位或它们传递多个选项标志)。
回答by Edward Loper
Responding to questions about "how can we ignore the whole line": yes, the fact that "..." also looks like a continuation like makes it hard to ignore the entire output. You can use "#doctest: +SKIP" if you just want to skip the example entirely, but that won't work if you are relying on its side-effects. If you reallyneed to do this, I suppose you could monkey-patch the doctest module itself, though I wouldn't particularly recommend it:
回答关于“我们如何忽略整行”的问题:是的,事实上“...”也看起来像一个延续,这使得很难忽略整个输出。如果您只想完全跳过该示例,可以使用 "#doctest: +SKIP",但如果您依赖其副作用,这将不起作用。如果您真的需要这样做,我想您可以对 doctest 模块本身进行猴子补丁,尽管我不会特别推荐它:
>>> import doctest
>>> doctest.ELLIPSIS_MARKER = '-etc-'
>>> print 12 # doctest: +ELLIPSIS
-etc-
(this test passes.)
(此测试通过。)
Or you could temporarily suppress stdout and/or stderr:
或者您可以暂时取消 stdout 和/或 stderr:
>>> # Suppress stdout
>>> import sys
>>> class DevNull:
... def noop(*args, **kwargs): pass
... close = write = flush = writelines = noop
>>> sys.stdout = DevNull()
>>> # Run a test and ignore output (but we need its side effects)
>>> print 12 # NOTE: stdout is suppressed!
>>> # Restore stdout
>>> sys.stdout = sys.__stdout__
(this test also passes.)
(这个测试也通过了。)
回答by Mark Horvath
Ignoring the whole line is bit tricky though. Here:
不过,忽略整条线有点棘手。这里:
"""
>>> do_your_thing() #doctest:+ELLIPSIS
...
"""
The triple dot will be interpreted as line continuation, and cause a syntax error.
三点将被解释为行继续,并导致语法错误。
If you want to ignore the whole line, you'll need something like:
如果你想忽略整行,你需要这样的东西:
"""
>>> sys.stdout.write('skip from here '); do_your_thing() #doctest:+ELLIPSIS
skip from here ...
"""
回答by WilliamMayor
I found it easier to simply assign the unneeded return values to a variable:
我发现将不需要的返回值简单地分配给变量更容易:
>>> _ = do_something()
>>> check_something()
True
回答by adalbertpl
You can write tuples before and after your function (hack inspired by answer of Mark Horvath):
您可以在函数之前和之后编写元组(受到 Mark Horvath 回答的启发):
def foo():
"""
>>> ();foo();() # doctest: +ELLIPSIS
(...)
"""
print "Hello world"
return "Hello world"
回答by samwyse
Can I have an ellipsis at the beginning of the line in a Python doctest?explains how to create a custom output checker that uses an additional string as an ellipsis. This would let you write the following, while still used '...' elsewhere.
我可以在 Python doctest 的行首使用省略号吗?解释了如何创建使用附加字符串作为省略号的自定义输出检查器。这将使您可以编写以下内容,同时在其他地方仍然使用“...”。
def foo():
"""
>>> foo() # doctest: +ELLIPSIS
[...] world
"""
print "hello world"