美文网首页
Python 中 doctest 用法简例

Python 中 doctest 用法简例

作者: realnickman | 来源:发表于2019-04-07 01:42 被阅读0次

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 用法简例

    1. 在代码中直接写doctest测试用例。 测试用例使用“>>>”,应输出结果就直接换行,不使用“>>>”。如果...

  • 文档测试

    Python内置的“文档测试”(doctest)模块可以直接提取注释中的代码并执行测试。doctest严格按照Py...

  • Python测试框架pytest

    Python 有多个测试框架如unittest doctest pytest nose选择pytest是因为...

  • 书籍:Python Testing Cookbook, 2nd

    简介 借助此基于解决方案的指南,修复Python中的日常测试问题 主要特点 使用doctest和unittest等...

  • 2022-08-18 node 与 python 交互

    node与python交互,可以使用 python-shell 。 用法: node中: python: 更多用法...

  • 使用doctest进行测试

    python中的doctest可以运行文档中嵌入的例子,并验证它们能否生成所期望的结果,从而对源代码进行测试。 1...

  • python 判断 循环 包 模块 函数

    标签 : python 判断 python中是没有switch这个用法的,实现这个用法最简单的就是上面的if......

  • 20160105| 1.2作业

    BeautifulSoup用法简说python split 拆分字符串python 判断变量类型为什么少用type...

  • Python笔记setdefault用法

    Python字典中setdefault的用法: Python 字典 setdefault() 方法和get()方法...

  • tushare用法记录

    以中通客车为例,tushare 用法记录下

网友评论

      本文标题:Python 中 doctest 用法简例

      本文链接:https://www.haomeiwen.com/subject/qshjiqtx.html