Python 中 doctest 用法简例

1. 在代码中直接写doctest测试用例。

测试用例使用“>>>”,应输出结果就直接换行,不使用“>>>”。如果测试结果和应输出结果相同,则通过测试。用例的位置一是模块的开头,或者是函数声明语句的下一行(本文给的例子就这种情况)。其中注意下应输出的结果一定要严格匹配,比如“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}

测试时候直接用以下命令:

python3 -m doctest -v doctest_practice.py

测试结果由于是verbose所以会比较详细:

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测试用例单独放在一个文件里面

我这里就把其中一个用例放在了doctest_practice_testfile.txt里面:

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

注意一下这个里面的from ... import... 也需要用“>>>”。测试的时候就直接输入以下命令就好了:

python3 -m doctest -v doctest_practice_testfile.txt

测试执行结果:

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.

你可能感兴趣的:(Python 中 doctest 用法简例)