pytest:错误OSError: reading from stdin while output is captured

@tly

pytest错误:

我在执行pytest的时候,程序爆出一下错误:
E           invoke.exceptions.ThreadException: 
E           Saw 1 exceptions within threads (OSError):
E           
E           
E           Thread args: {'kwargs': {'echo': None,
E                       'input_': <_pytest.capture.DontReadFromInput object at 0x7fee74656278>,
E                       'output': <_pytest.capture.EncodedFile object at 0x7fee74656470>},
E            'target': <bound method Runner.handle_stdin of <fabric.runners.Remote object at 0x7fee706d1978>>}
E           
E           Traceback (most recent call last):
E           
E             File "/root/venv/tamper_console/lib/python3.6/site-packages/invoke/util.py", line 233, in run
E               super(ExceptionHandlingThread, self).run()
E           
E             File "/usr/local/lib/python3.6/threading.py", line 864, in run
E               self._target(*self._args, **self._kwargs)
E           
E             File "/root/venv/tamper_console/lib/python3.6/site-packages/invoke/runners.py", line 641, in handle_stdin
E               data = self.read_our_stdin(input_)
E           
E             File "/root/venv/tamper_console/lib/python3.6/site-packages/invoke/runners.py", line 601, in read_our_stdin
E               bytes_ = input_.read(bytes_to_read(input_))
E           
E             File "/root/venv/tamper_console/lib/python3.6/site-packages/_pytest/capture.py", line 693, in read
E               raise IOError("reading from stdin while output is captured")
E           
E           OSError: reading from stdin while output is captured

临时解决办法,运行命令:
pytest test_xxx.py --capture=no
禁止捕获输出;

原因:
TODO

这个错误提示通常发生在你使用pytest运行测试时,测试框架试图捕获从标准输入(stdin)的输入,但是它同时也捕获了标准输出(stdout)。这在某些情况下可能会导致问题,比如在测试中需要从键盘读取输入,但是pytest却捕获了这些输入。 `-s` 选项是告诉pytest不要捕获标准输出,这样你就可以在测试中看到print()函数的输出,同时也可以正常地从键盘进行输入。 在VSCode中运行测试时,你可以通过修改运行配置来解决这个问题。具体步骤如下: 1. 打开你的项目文件夹。 2. 点击左上角的“Run”图标,打开“Run and Debug”视图。 3. 点击“create a launch.json file”链接,为你的项目创建一个调试配置文件。 4. 在`.vscode/launch.json`文件中,找到或创建针对Python测试的配置部分。 5. 在相应的配置中添加`"justMyCode": false`和`"console": "integratedTerminal"`,并确保没有设置`"captureOutput"`为`true`,或者将其设置为`false`。 例如,你的配置可能看起来像这样: ```json { "version": "0.2.0", "configurations": [ { "name": "Python: Current File", "type": "python", "request": "launch", "program": "${file}", "console": "integratedTerminal", "justMyCode": false, "cwd": "${workspaceFolder}", }, // ... 其他配置 ... ] } ``` 通过这些步骤,你应该能够在VSCode中正常运行pytest而不会遇到这个错误。如果你需要在测试中读取标准输入,确保测试设计为能够在不捕获输出的情况下运行。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值