代码之家  ›  专栏  ›  技术社区  ›  Stefan

Pytest下的Python测试运行无法正确捕获被调用的进程错误异常

  •  0
  • Stefan  · 技术社区  · 6 年前

    在Pytest中运行Python doctests时,我目前面临一种奇怪的行为( py.test )。下面的代码片段突出显示了bevaviour。

    • 医生们 确实通过 通过调用脚本时 python2.7 ./weirdpytestbehaviour_test.py
    • 医生们 不通过 通过调用脚本时 py.test ./weirdpytestbehaviour_test.py --doctest-modules --tb=short
    • 如果我改变陈述 except CalledProcessError as e: 进入 except Exception as e: 医生也会通过 py。测验

    这看起来很无聊。测试对 CalledProcessError 异常类型。在中执行时,代码采用不同的求值路径是非常不直观的 py。测验 。 我如何控制或调整这种行为?

    #!/usr/bin/python
    # coding=utf-8
    # file: 'weirdpytestbehaviour_test.py'
    from subprocess import check_output, CalledProcessError
    
    def func(success):
        """The worker function.
    
        >>> func(success=True)
        >>> func(success=False)
        Ouch!
        Special treatment of this particular failure...
        """
        try:
            # Now invoke a subprocess
            if success:
                # Will always succeed and return with an exit code of 0
                check_output('echo "Hello!"; exit 0', shell=True)
            else:
                # Will always succeed and return with an exit code of 1
                check_output('echo "Ouch!"; exit 1', shell=True)
    
        # Does not catch the exception under pytest
        # 'except Exception as e:' would work as expected
        except CalledProcessError as e:
            print(e.output),
            if "Ouch!" in e.output:
                print("Special treatment of this particular failure...")
            else:
                raise
    
    if __name__ == '__main__':
        import doctest
        doctest.testmod()
    

    py的输出。失败情况下的测试(尝试捕获上面调用的进程错误)如下所示:

    $ py.test ./weirdpytestbehaviour_test.py --doctest-modules --tb=short 
    Test session starts (platform: linux2, Python 2.7.6, pytest 3.4.2, pytest-sugar 0.9.1)
    [...]
    
    007 The worker function.
    008 
    009     >>> func(success=True)
    010 
    011     >>> func(success=False)
    UNEXPECTED EXCEPTION: CalledProcessError()
    Traceback (most recent call last):
    
      File "/usr/lib/python2.7/doctest.py", line 1315, in __run
        compileflags, 1) in test.globs
    
      File "<doctest experiments.weirdpytestbehaviour_test.func[1]>", line 1, in <module>
    
      File "weirdpytestbehaviour_test.py", line 22, in func
        check_output('echo "Ouch!"; exit 1', shell=True)
    
      File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
        raise CalledProcessError(retcode, cmd, output=output)
    
    CalledProcessError: Command 'echo "Ouch!"; exit 1' returned non-zero exit status 1
    
    weirdpytestbehaviour_test.py:11: UnexpectedException
    
    weirdpytestbehaviour_test.py ⨯                                                                                                                                                                                        100% ██████████
    
    Results (0.19s):
           1 failed
             - weirdpytestbehaviour_test.py:11 [doctest] weirdpytestbehaviour_test.func
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Stefan    6 年前

    经过反复尝试,我不仅卸载并重新安装了pytest,而且还卸载并重新安装了我使用的插件,从而解决了这个问题。之后,它又开始工作了。

    正在清理缓存( __pycache__ )没有效果。显然,pytest及其插件的安装有问题。