代码之家  ›  专栏  ›  技术社区  ›  iago-lito

为什么py。测试是否向后运行模块doctests?

  •  1
  • iago-lito  · 技术社区  · 7 年前

    如果我有这两个文件:

    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\u添加 由于中的错误,测试失败 添加 ,但我在 添加 所以我假设它工作正常。

    我该怎么做 py.test 以相反的方式在模块中运行测试?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Piotr Banaś    7 年前

    遗憾的是,pytest没有这样的功能。它只能对选定的文件或文件夹运行测试。

    此命令:

    pytest --doctest-modules -x second_module.py
    

    意味着pytest将扫描文件second\u模块中的docstring。py并为其运行测试(-x表示第一次失败时停止)。它不在所需的模块中查找docstring。

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

    pytest --doctest-modules 
    

    或者尝试使用选项——doctest glob。