三言两语聊Python模块–文档测试模块doctest

python

doctest是属于测试模块里的一种,对注释文档里的示例进行检测。

给出一个例子:

def split(line, types=None, delimiter=None):

"""Splits a line of test and optionally performs type conversion.

For example:

>>> split('GOOD 100 490.50')

['GOOD', '100', '490.50']

>>> split('GOOD 100 490.50', [str, int, float])

['GOOD', 100, 490.50]

>>>

By default, splitting is perfomed on whitespace, but a different delimiter

can be selected with the delimiter keyword argument:

>>> split('GOOD, 100, 490.50', delimiter=',')

['GOOOD', '100', '490.50']

>>>

"""

fields = line.split(delimiter)

if types:

fields = [ty(val) for ty, val in zip(types, fields)]

return fields


可以有2种方式来使用doctest:

  • 第一种,单独建一个脚本来测试。

# testsplitter.py

import splitter

import doctest

nfail, ntests = doctest.testmod(splitter, verbose=True)


运行结果:

>>>

**********************************************************************

File "C:/Users/Administrator/Desktop/Python Scripts\splitter.py", line 8, in splitter.split

Failed example:

split('GOOD 100 490.50', [str, int, float])

Expected:

['GOOD', 100, 490.50]

Got:

['GOOD', 100, 490.5]

**********************************************************************

File "C:/Users/Administrator/Desktop/Python Scripts\splitter.py", line 14, in splitter.split

Failed example:

split('GOOD, 100, 490.50', delimiter=',')

Expected:

['GOOOD', '100', '490.50']

Got:

['GOOD', ' 100', ' 490.50']

**********************************************************************

1 items had failures:

2 of 3 in splitter.split

***Test Failed*** 2 failures.

>>>


这样注意一点,这个比对是严格比对,所以490.50和490.5是不同的。

doctest.testmod还可以带一个参数使用,显示更为详细的结果

nfail, ntests = doctest.testmod(splitter, verbose=True)

>>>

Trying:

split('GOOD 100 490.50')

Expecting:

['GOOD', '100', '490.50']

ok

Trying:

split('GOOD 100 490.50', [str, int, float])

Expecting:

['GOOD', 100, 490.50]

**********************************************************************

File "C:/Users/Administrator/Desktop/Python Scripts\splitter.py", line 8, in splitter.split

Failed example:

split('GOOD 100 490.50', [str, int, float])

Expected:

['GOOD', 100, 490.50]

Got:

['GOOD', 100, 490.5]

Trying:

split('GOOD, 100, 490.50', delimiter=',')

Expecting:

['GOOOD', '100', '490.50']

**********************************************************************

File "C:/Users/Administrator/Desktop/Python Scripts\splitter.py", line 14, in splitter.split

Failed example:

split('GOOD, 100, 490.50', delimiter=',')

Expected:

['GOOOD', '100', '490.50']

Got:

['GOOD', ' 100', ' 490.50']

1 items had no tests:

splitter

**********************************************************************

1 items had failures:

2 of 3 in splitter.split

3 tests in 2 items.

1 passed and 2 failed.

***Test Failed*** 2 failures.

>>>

  • 第二种,使用__name__ == ‘__main__’: (大部分好像都使用这种的)

# splitter.py

def split(line, types=None, delimiter=None):

"""Splits a line of test and optionally performs type conversion.

For example:

>>> split('GOOD 100 490.50')

['GOOD', '100', '490.50']

>>> split('GOOD 100 490.50', [str, int, float])

['GOOD', 100, 490.50]

>>>

By default, splitting is perfomed on whitespace, but a different delimiter

can be selected with the delimiter keyword argument:

>>> split('GOOD, 100, 490.50', delimiter=',')

['GOOOD', '100', '490.50']

>>>

"""

fields = line.split(delimiter)

if types:

fields = [ty(val) for ty, val in zip(types, fields)]

return fields

if __name__ == '__main__':

# test myself

import doctest

doctest.testmod()


结果是一样的:

**********************************************************************

File "C:/Users/Administrator/Desktop/Python Scripts/splitter.py", line 8, in __main__.split

Failed example:

split('GOOD 100 490.50', [str, int, float])

Expected:

['GOOD', 100, 490.50]

Got:

['GOOD', 100, 490.5]

**********************************************************************

File "C:/Users/Administrator/Desktop/Python Scripts/splitter.py", line 14, in __main__.split

Failed example:

split('GOOD, 100, 490.50', delimiter=',')

Expected:

['GOOOD', '100', '490.50']

Got:

['GOOD', ' 100', ' 490.50']

**********************************************************************

1 items had failures:

2 of 3 in __main__.split

***Test Failed*** 2 failures.

以上是 三言两语聊Python模块–文档测试模块doctest 的全部内容, 来源链接: utcz.com/z/386684.html

回到顶部