为什么py.test会向后运行模块doctests?

时间:2018-02-19 16:41:49

标签: python python-3.x module pytest doctest

如果我有这两个文件:

first_module.py

def add(a, b):
    """
    >>> add(1, 2) # should this not fail first?
    3
    """
    return a - b # (because here's the mistake)

second_module.py

from first_module import *

def anti_add(a, b):
    """
    >>> anti_add(1, 2) # why does this fail first?
    -3
    """
    return -add(a, b)

我跑:

py.test --doctest-modules -x second_module.py

我明白了:

========================================== FAILURES ===========================================
__________________________________ [doctest] second_module.anti_add __________________________________
005 
006     >>> anti_add(1, 2) # why does this fail first?
Expected:
    -3
Got:
    1

.../second_module.py:6: DocTestFailure

但实际上我希望第一个测试首先失败,因为add需要anti_add才能正常工作。
我有时会感到困惑,因为anti_addadd中的错误而导致测试失败,但我在add中没有看到失败,所以我认为它可以正常工作。

如何让py.test以相反的方式在模块中运行测试?

1 个答案:

答案 0 :(得分:1)

可悲的是,pytest没有这样的功能。它只能运行所选文件或文件夹的测试。

此命令:

pytest --doctest-modules -x second_module.py

表示pytest将扫描文件second_module.py中的文档字符串并为它们运行测试(-x表示首次失败时停止)。它不会在所需的模块中查找文档字符串。

如果您想测试所有模块,您只需使用:

pytest --doctest-modules 

或试用选项--doctest-glob。

相关问题