py.test终止而未完成终端%

时间:2016-01-17 05:33:24

标签: python pytest

我第一次使用pytest,大部分时间都很棒,但有时它会在没有完成的情况下终止,没有错误信息。

=========================================================================================== test session starts ============================================================================================
platform darwin -- Python 3.5.1, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
rootdir: /Users/dacre/mike_tools/python, inifile:
collected 7 items

test_pipeline.py ......F%

1 个答案:

答案 0 :(得分:1)

当我正在发布这个问题的过程中,我想出来了,但是我在这里发布它是为了有同样问题的其他人。

问题是,如果您的代码偶然在close()sys.stderr上调用sys.stdout。这发生在我身上,因为我有一个日志功能,试图通过.name属性区分sys.stderr和不同的文件句柄。 py.test控制sys.stderr并将其重新映射到_pytest.capture.EncodedFile个对象。因此我的函数误解了它,并在该对象上调用了close()。这导致提前终止而没有错误。

我只是重写了我的代码以避免这个问题,但另一个选择是从sys导入模块,然后在模块中测试'pytest':

from sys import modules
if 'pytest' in modules:
    do something...