admin管理员组

文章数量:1313385

Iam writing pytest for method -

def upload(type="", local_file=None, remote_file=None,
           target_config=None, verify_existence=True,throw_on_error=False):
  if not local_file or not target_config:
    return None
  logger = default_logger
  target_path = remote_file if remote_file else local_file
  pdir = os.path.dirname(target_path)
  cip = getattr(
      target_config, "address", None) or target_config.cip
  if verify_existence:
    target_check = "test -f '%s'" % target_path
    _, _, ret = ssh(ip=cip,command=[target_check],throw_on_error=False)
    if ret == 0:
      message = ("Target file %s(%s) already exists. "
                 "Skipping upload" % (cip, target_path))
      logger.info(message)
      return local_file
    else:
      message = ("Target file %s(%s) not present. Uploading file"
                 % (cip, target_path))
      logger.info(message)
  for _ in range(10):
    _, _, ret = ssh(ip=cip, command=["mkdir", "-p", pdir],
        throw_on_error=False)
    _, _, ret = scp(
        ip=cip,target_path=target_path,files=[local_file],
        log_on_error=True, throw_on_error=False,
        timeout=None)
    if ret == 0:
      return local_file
  else:
    message = ("Failed to upload %s(%s) to %s:%s" % (type,
               local_file, cip, target_path))
    logger.error(message)
    if throw_on_error:
      raise Exception(message)

My test case - -

  def test_upload(self):
     default_logger = self.mock.CreateMockAnything()
     pdir ="tmp/sample_path"
     local_file = "tmp/local_path"
     cip = "10.1.1.1"
     verify_existence=True
     objectName.ssh(cip, command=["test", "-f", 'tmp/sample_path'], throw_on_error=False).AndReturn(("", "",0))
     self.mock.ReplayAll()
     result = objectName.upload( local_file= "tmp/local_path" , verify_existence=True)
     print(result)
     self.mock.VerifyAll()

But it fails with :test_upload_local_file - mox3.mox.ExpectedMethodCallsError: Verify: Expected methods never called: and also return None as result, I want local_file as my output. What is wrong in my test case

本文标签: pythonPytest not executing mock statementStack Overflow