Python 中 doctest 用法簡(jiǎn)例

1. 在代碼中直接寫doctest測(cè)試用例。

測(cè)試用例使用“>>>”,應(yīng)輸出結(jié)果就直接換行,不使用“>>>”。如果測(cè)試結(jié)果和應(yīng)輸出結(jié)果相同,則通過測(cè)試。用例的位置一是模塊的開頭,或者是函數(shù)聲明語句的下一行(本文給的例子就這種情況)。其中注意下應(yīng)輸出的結(jié)果一定要嚴(yán)格匹配,比如“hello”和'hello'就是不同的輸出。

# doctest_practice.py
def add_two_positive_number(a,b):
    """
    >>> add_two_positive_number(2,3)
    5
    >>> add_two_positive_number(100,200)
    300
    """
    assert a > 0 and b> 0, "both needs to be positive"
    return a+b

add_two_positive_number(1,3)
#add_two_positive_number(-1,4) # AssertionError
# using python -o assert-and-testing.py will ignore all the assertion code!!

def double(values):
    """ double the values in a list

    >>> double([1,2,3,4])
    [2, 4, 6, 8]

    >>> double([])
    []

    >>> double(['a', 'b', 'c'])
    ['aa', 'bb', 'cc']

    >>> double([True, None])
    Traceback (most recent call last):
        ...
    TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
    """
    return [2 * element for element in values]

# doctest expect all str to have single str not double str
def say_hi():
    """
    >>> say_hi()
    'hi'
    """
    return "hi"


# Watch out for whitespace!
# (There's a trailing space on line 42)
def true_that():
    """
    >>> true_that()
    True
    """
    return True

# Order of keys in dicts matters in doctests
def make_dict(keys):
    """
    >>> make_dict(['a','b'])
    {'a': True, 'b': True}
    """
    return {key: True for key in keys}

測(cè)試時(shí)候直接用以下命令:

python3 -m doctest -v doctest_practice.py

測(cè)試結(jié)果由于是verbose所以會(huì)比較詳細(xì):

Trying:
    add_two_positive_number(2,3)
Expecting:
    5
ok
Trying:
    add_two_positive_number(100,200)
Expecting:
    300
ok
Trying:
    double([1,2,3,4])
Expecting:
    [2, 4, 6, 8]
ok
Trying:
    double([])
Expecting:
    []
ok
Trying:
    double(['a', 'b', 'c'])
Expecting:
    ['aa', 'bb', 'cc']
ok
Trying:
    double([True, None])
Expecting:
    Traceback (most recent call last):
            ...
    TypeError: unsupported operand type(s) for *: 'int' and 'NoneType'
ok
Trying:
    make_dict(['a','b'])
Expecting:
    {'a': True, 'b': True}
ok
Trying:
    say_hi()
Expecting:
    'hi'
ok
Trying:
    true_that()
Expecting:
    True
ok
1 items had no tests:
    doctest_practice
5 items passed all tests:
   2 tests in doctest_practice.add_two_positive_number
   4 tests in doctest_practice.double
   1 tests in doctest_practice.make_dict
   1 tests in doctest_practice.say_hi
   1 tests in doctest_practice.true_that
9 tests in 6 items.
9 passed and 0 failed.
Test passed.

2. doctest測(cè)試用例單獨(dú)放在一個(gè)文件里面

我這里就把其中一個(gè)用例放在了doctest_practice_testfile.txt里面:

>>> from doctest_practice import double
>>> double([1,2,3,4])
[2, 4, 6, 8]

注意一下這個(gè)里面的from ... import... 也需要用“>>>”。測(cè)試的時(shí)候就直接輸入以下命令就好了:

python3 -m doctest -v doctest_practice_testfile.txt

測(cè)試執(zhí)行結(jié)果:

Trying:
    from doctest_practice import double
Expecting nothing
ok
Trying:
    double([1,2,3,4])
Expecting:
    [2, 4, 6, 8]
ok
1 items passed all tests:
   2 tests in doctest_practice_testfile.txt
2 tests in 1 items.
2 passed and 0 failed.
Test passed.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 6,340評(píng)論 0 10
  • 什么是運(yùn)維 術(shù)語名詞 IDC--(Internet Data Center)互聯(lián)網(wǎng)數(shù)據(jù)中心,主要服務(wù)包括整機(jī)租用、...
    lyh165閱讀 2,881評(píng)論 0 19
  • 1.問:你在測(cè)試中發(fā)現(xiàn)了一個(gè) bug ,但是開發(fā)經(jīng)理認(rèn)為這不是一個(gè) bug ,你應(yīng)該怎樣解決。 首先,將問題提...
    qianyewhy閱讀 9,396評(píng)論 4 123
  • 年少往往都被打上無知或者輕狂的標(biāo)簽。而我,屬于前者。有一段塵封的往事,提起來令人難過,都是因?yàn)槲业臒o知與失禮...
    筆崖閱讀 849評(píng)論 0 10
  • 周末回娘家,和閨蜜約了一起吃晚飯,和今年的每個(gè)周五晚上差不多,隨便吃點(diǎn)什么都好,主要是見個(gè)面。這是我們不成文的約定...
    尚未放棄治療女青年閱讀 651評(píng)論 1 1

友情鏈接更多精彩內(nèi)容