emb*_*emb 0 python doctest unit-testing
我喜欢doctest但是当你有一个复杂的参数需要在传递给函数之前设置它变得非常难以阅读..因此,你开始使用多行分配然后调用你想要测试的函数.这种方法然而,会报告你有多个测试而不是你拥有的实际测试数量.一个例子将说明我的意思..
def returnme(x):
"""
Returns what you pass
>>> y = (2, 3, 5, 7)
>>> returnme(y)
(2, 3, 5, 7)
"""
return x
Run Code Online (Sandbox Code Playgroud)
在上面的代码片段中,只有一个测试而另一个只是一个变量赋值,但是,这是报告的内容.
Trying:
y = (2, 3, 5, 7)
Expecting nothing
ok
Trying:
returnme(y)
Expecting:
(2, 3, 5, 7)
ok
2 tests in 2 items.
2 passed and 0 failed.
我看过记录的旗帜,肯定是我遗漏了什么......
前置三个句点表示您要继续当前行,如下所示:
def returnme(x):
"""
Returns what you pass
>>> y = (2, 3, 5, 7)
... returnme(y) # Note the difference here.
... # Another blank line ends this test.
(2, 3, 5, 7)
"""
return x
Run Code Online (Sandbox Code Playgroud)
这应该够了吧.您可以在此处详细了解doctest如何解释各个测试.
| 归档时间: |
|
| 查看次数: |
121 次 |
| 最近记录: |