最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Pytest method mocking - Stack Overflow

programmeradmin10浏览0评论
def create_interface(iface, address, netmask):
  validate_interface(iface, address, netmask)
  if ping_test(address):
    raise Exception("Address %s is already in use. interface %s with ip %s." % (address, iface, address))
  proc = Popen(["sudo ifconfig %s %s netmask %s up" %
               (iface, address, netmask)], shell=True, stdout=PIPE, stderr=PIPE)
  out, err = procmunicate()
  ret = proc.returncode
  if ret:
    raise Exception("Failed to create virtual interface '%s': %s" %
                        (iface, out + err))
  else:
    logging.info("Created %s with ip %s and netmask %s successfully" %
                 (iface, address, netmask))

This is the method for which I need to write a test case.

My test case is as follows:

def test_create_interface(self):
    iface = "eth0:1"
    address = "1.2.3.4"
    netmask = "255.255.255.0"
    import subprocess
    process = self.mox.CreateMock(subprocess.Popen)
    process.returncode = 1
    self.mox.StubOutWithMock(vi, "validate_interface")
    vi.validate_interface(iface, address, netmask).AndReturn(None)
    self.mox.StubOutWithMock(vi, "ping_test")
    vi.ping_test(address).AndReturn(False)
    self.mox.StubOutWithMock(subprocess, "Popen")
    subprocess.Popen(  IgnoreArg(), stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).AndReturn(process)
    self.mox.StubOutWithMock(process, "communicate")
    processmunicate().AndReturn(("", 0))
    self.mox.ReplayAll()
    vi.create_interface(iface, address, netmask)
    self.mox.VerifyAll()

but it fails with:

Unexpected method call function.__call__('eth0:1', '1.2.3.4', '255.255.255.0') -> None.

What am I missing?

def create_interface(iface, address, netmask):
  validate_interface(iface, address, netmask)
  if ping_test(address):
    raise Exception("Address %s is already in use. interface %s with ip %s." % (address, iface, address))
  proc = Popen(["sudo ifconfig %s %s netmask %s up" %
               (iface, address, netmask)], shell=True, stdout=PIPE, stderr=PIPE)
  out, err = proc.communicate()
  ret = proc.returncode
  if ret:
    raise Exception("Failed to create virtual interface '%s': %s" %
                        (iface, out + err))
  else:
    logging.info("Created %s with ip %s and netmask %s successfully" %
                 (iface, address, netmask))

This is the method for which I need to write a test case.

My test case is as follows:

def test_create_interface(self):
    iface = "eth0:1"
    address = "1.2.3.4"
    netmask = "255.255.255.0"
    import subprocess
    process = self.mox.CreateMock(subprocess.Popen)
    process.returncode = 1
    self.mox.StubOutWithMock(vi, "validate_interface")
    vi.validate_interface(iface, address, netmask).AndReturn(None)
    self.mox.StubOutWithMock(vi, "ping_test")
    vi.ping_test(address).AndReturn(False)
    self.mox.StubOutWithMock(subprocess, "Popen")
    subprocess.Popen(  IgnoreArg(), stdout=subprocess.PIPE,
    stderr=subprocess.PIPE).AndReturn(process)
    self.mox.StubOutWithMock(process, "communicate")
    process.communicate().AndReturn(("", 0))
    self.mox.ReplayAll()
    vi.create_interface(iface, address, netmask)
    self.mox.VerifyAll()

but it fails with:

Unexpected method call function.__call__('eth0:1', '1.2.3.4', '255.255.255.0') -> None.

What am I missing?

Share Improve this question edited Feb 7 at 8:08 globglogabgalab 4593 silver badges14 bronze badges asked Feb 5 at 7:59 aΨVaNaΨVaN 1,1462 gold badges9 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I would say it is caused by the fact that you did not run

self.mox.StubOutWithMock(vi, "create_interface")

before trying to call the mock method

vi.create_interface(...)

Therefore it raises mox.UnexpectedMethodCallError, without the trace of the error I would say it happens at this point in the mox library : pymox/mox.py#L1475

发布评论

评论列表(0)

  1. 暂无评论