2017-03-24 76 views
0

pytest capsys fixture确实很有用,但它只要将它包含在夹具中就能立即捕获所有东西。我只想在测试中捕获特定行的输出。明确启用的capsys pytest夹具

它实际上提供了“禁用”背景:

def test_disabling_capturing(capsys): 
    print('this output is captured') 
    with capsys.disabled(): 
     print('output not captured, going directly to sys.stdout') 
    print('this output is also captured') 

但我想是这样的,该相反:

def test_disabling_capturing(capsys): 
    print('this output is not captured') 
    with capsys.enabled(): 
     print('output is captured') 
    print('this output is also not captured') 

是否有可能与capsys工装夹具不知何故?

回答

0

因为当前版本的Python提供了contextlib中的功能,所以我决定我并不需要这个功能。简单地导入上下文管理器,而不是使用灯具是没有缺点的。

的接口是在redirect_stdoutredirect_stderr稍微不同是分别提供的,但它是微不足道的那些包裹成相同种类的固定装置接口capsys提供如果必要的。