【问题标题】:Python/Flask Unittest, suppress Broken Pipe Error 32Python/Flask Unittest,抑制断管错误 32
【发布时间】:2020-11-18 12:09:38
【问题描述】:

在运行单元测试时,我经常得到error: [Errno 32] Broken pipe。这似乎是测试期间发生的无害错误,但我无法阻止或以其他方式抑制它。

我尝试过的一些事情包括将SIGPIPE 处理更改为SIG_DFL 并使用threaded=True 运行应用程序。如果我要去try/except,我不知道要包装哪个代码,因为这是在单元测试的上下文中。

我不关心捕获/防止错误,只关心在所有其他测试完成运行时抑制其输出。我还应该尝试什么?

编辑:

以下是一些经常但并不总是导致错误的示例代码:

  def test_register(self):                                                                              
    self.driver.get(self.get_server_url() + url_for(u'register'))                                       
    body = self.driver.find_element_by_id(u'body')

    username_input = body.find_element_by_id(u'username')                                               
    username_input.send_keys(self.USER1_DISPLAY_USERNAME)               
    privacy_policy = body.find_element_by_id(u'privacy_policy')                                         
    privacy_policy.click()
    #NOTE: not shown - more lines filling out form elements exactly like the above lines

    self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")                       
    register_button = body.find_element_by_id(u'onclick-register')                                      
    register_button.click()

这里是错误信息的一个例子:

----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 44668)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 599, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 657, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 716, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 283, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 307, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe
----------------------------------------

这似乎只发生在集成测试中。我正在使用 Selenium 和 LiveServerTestCase,并将 LIVESERVER_PORT 端口设置为 8943。错误消息中提到端口44668 看起来很可疑。

我的问题听起来与Suppress print output in unittests 完全一样,但这些解决方案都不起作用。

【问题讨论】:

  • 您能想出的最简单的测试(和代码)是什么?发布。

标签: python python-2.7 flask python-unittest broken-pipe


【解决方案1】:

我通过重定向stderrstdout 找到了解决方案。

create_app() 中添加以下内容:

_stderr, _stdout = sys.stderr, sys.stdout                                                           
null = open(os.devnull,'wb')                                                     
sys.stdout = sys.stderr = null

【讨论】:

    猜你喜欢
    • 2015-02-02
    • 2020-07-20
    • 2018-12-05
    • 2016-06-20
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多