doctest调用函数的顺序?

2024-10-02 10:22:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我很困惑为什么doctest.testmod()以奇怪的顺序调用测试函数

from doctest import testmod

def test_forwrite():
    '''
    >>> test_forwrite()
    OP: Done
    '''
    write()

def test_forread():
    '''
    >>> test_forread()
    OP: Done
    '''
    read()

if __name__ == "__main__":
    testmod(verbose = True)

为什么不考虑其定义的test_forread()总是首先测试的顺序


Tags: namefromtestimportreadifdef测试函数
2条回答

测试按名称排序test_forread按字母顺序在test_forwrite()之前排序

^{} source code^{} method

# Sort the tests by alpha order of names, for consistency in
# verbose-mode output.  This was a feature of doctest in Pythons
# <= 2.3 that got lost by accident in 2.4.  It was repaired in
# 2.4.4 and 2.5.
tests.sort()

doctest.testmod()uses ^{}查找模块中的测试

然而,您的测试不应依赖于给定的排序。编写独立的测试,这样就可以单独或并行运行测试

这是因为testmod函数通过按字母顺序调用给定模块/程序中的函数来测试它们

在您的情况下,test_forread()将首先被调用,因为当按字母顺序排序时,它位于test_forwrite()之前

相关问题 更多 >

    热门问题