• <small id='oSS68'></small><noframes id='oSS68'>

    1. <i id='oSS68'><tr id='oSS68'><dt id='oSS68'><q id='oSS68'><span id='oSS68'><b id='oSS68'><form id='oSS68'><ins id='oSS68'></ins><ul id='oSS68'></ul><sub id='oSS68'></sub></form><legend id='oSS68'></legend><bdo id='oSS68'><pre id='oSS68'><center id='oSS68'></center></pre></bdo></b><th id='oSS68'></th></span></q></dt></tr></i><div id='oSS68'><tfoot id='oSS68'></tfoot><dl id='oSS68'><fieldset id='oSS68'></fieldset></dl></div>

        • <bdo id='oSS68'></bdo><ul id='oSS68'></ul>
      1. <tfoot id='oSS68'></tfoot>
        <legend id='oSS68'><style id='oSS68'><dir id='oSS68'><q id='oSS68'></q></dir></style></legend>

        模拟 subprocess.Popen 依赖于导入样式

        mocking subprocess.Popen dependant on import style(模拟 subprocess.Popen 依赖于导入样式)
        <tfoot id='U0wXF'></tfoot>

      2. <i id='U0wXF'><tr id='U0wXF'><dt id='U0wXF'><q id='U0wXF'><span id='U0wXF'><b id='U0wXF'><form id='U0wXF'><ins id='U0wXF'></ins><ul id='U0wXF'></ul><sub id='U0wXF'></sub></form><legend id='U0wXF'></legend><bdo id='U0wXF'><pre id='U0wXF'><center id='U0wXF'></center></pre></bdo></b><th id='U0wXF'></th></span></q></dt></tr></i><div id='U0wXF'><tfoot id='U0wXF'></tfoot><dl id='U0wXF'><fieldset id='U0wXF'></fieldset></dl></div>

        <small id='U0wXF'></small><noframes id='U0wXF'>

          <bdo id='U0wXF'></bdo><ul id='U0wXF'></ul>
            <tbody id='U0wXF'></tbody>

              • <legend id='U0wXF'><style id='U0wXF'><dir id='U0wXF'><q id='U0wXF'></q></dir></style></legend>

                1. 本文介绍了模拟 subprocess.Popen 依赖于导入样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在尝试模拟 Popen 时,只有在单元测试代码和主模块代码中子流程的导入匹配时,我才能使其成功.

                  When attempting to mock Popen I can only get it to succeed if the importing of subprocess matches in both unit test code and main module code.

                  给定以下模块 listdir.py:

                  Given following module listdir.py:

                  from subprocess import Popen, PIPE
                  
                  def listdir(dir):
                      cmd = ['ls', dir]
                      pc = Popen(cmd, stdout=PIPE, stderr=PIPE)
                      out, err = pc.communicate()
                      if pc.returncode != 0:
                          raise Exception
                      return out
                  

                  以及下面的单元测试代码test_listdir.py

                  and following unit test code test_listdir.py

                  import subprocess
                  import listdir
                  import mock
                  
                  @mock.patch.object(subprocess, 'Popen', autospec=True)
                  def test_listdir(mock_popen):
                      mock_popen.return_value.returncode = 0
                      mock_popen.return_value.communicate.return_value = ("output", "Error")
                      listdir.listdir("/fake_dir")
                  

                  由于某些原因,Popen 没有被模拟,因为两个 python 模块之间的导入样式不同,并且运行测试总是引发异常.

                  For some reason Popen is not being mocked, due to the import style being different between the two python modules, and running the test always raises an exception.

                  如果我更改 listdir.py 以导入所有子进程,例如

                  If I change listdir.py to import all of subproces e.g.

                  import subprocess
                  
                  def listdir(dir):
                      cmd = ['ls', dir]
                      pc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
                                            stderr=subprocess.PIPE)
                      out, err = pc.communicate()
                      if pc.returncode != 0:
                          raise ListingErrorException
                      return out
                  

                  然后在测试中返回输出".

                  Then "output" is returned in the test.

                  任何人都想解释一下为什么,我的偏好是from subprocess import Popen, Pipe in 这两个模块,但我就是无法模拟.

                  Anyone care to shed some light on why, my preference would be to have from subprocess import Popen, Pipe in both modules, but I just can't get that to mock.

                  推荐答案

                  你需要在 listdir 中修补 Popen 的副本,而不是你刚刚导入的那个.所以,不要使用 @mock.patch.object(subprocess, 'Popen', autospec=True),试试 @mock.patch.object(listdir, 'Popen', autospec=True)

                  You need to patch the copy of Popen in listdir, not the one you just imported. So, instead of @mock.patch.object(subprocess, 'Popen', autospec=True), try @mock.patch.object(listdir, 'Popen', autospec=True)

                  有关详细信息,请参阅此文档:http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

                  See this doc for more info: http://www.voidspace.org.uk/python/mock/patch.html#where-to-patch

                  这篇关于模拟 subprocess.Popen 依赖于导入样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <legend id='OSzxn'><style id='OSzxn'><dir id='OSzxn'><q id='OSzxn'></q></dir></style></legend>
                  <tfoot id='OSzxn'></tfoot>

                  • <bdo id='OSzxn'></bdo><ul id='OSzxn'></ul>

                    <i id='OSzxn'><tr id='OSzxn'><dt id='OSzxn'><q id='OSzxn'><span id='OSzxn'><b id='OSzxn'><form id='OSzxn'><ins id='OSzxn'></ins><ul id='OSzxn'></ul><sub id='OSzxn'></sub></form><legend id='OSzxn'></legend><bdo id='OSzxn'><pre id='OSzxn'><center id='OSzxn'></center></pre></bdo></b><th id='OSzxn'></th></span></q></dt></tr></i><div id='OSzxn'><tfoot id='OSzxn'></tfoot><dl id='OSzxn'><fieldset id='OSzxn'></fieldset></dl></div>

                    <small id='OSzxn'></small><noframes id='OSzxn'>

                            <tbody id='OSzxn'></tbody>