Python Forum
How to mock an object that is created during function call?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to mock an object that is created during function call?

i tried to create a python test for a subprocess call. Below i created a minimalistic example.

import subprocess

def isScriptVersioned():
    isVersioned = False
    command = ["svn", "info"]
    process = subprocess.Popen(command, bufsize=1, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
        for line in iter(process.stdout.readline, ''):
            # Extract path by parsing the svn output vis regex
            if('URL: (\S*)', line)):
                isVersioned = True
    return isVersioned   
I would like to mock the process.stdout.readline string but i have no idea how to access this variable. Here are some things i tried:

# my test module
def mockreturn():
    return "//my//path//Application"

# 1. try
def test_isScriptVersioned(monkeypatch):
    monkeypatch.setattr(subprocess.Popen.stdout.readline, "//my//path//Application")
    assert utilities.isScriptVersioned() == "True"

# 2. try
def test_isScriptVersioned(monkeypatch):
    monkeypatch.setattr(subprocess.Popen.stdout, "readline", mockreturn)
    assert utilities.isScriptVersioned() == "True"

# 3. try
def test_isScriptVersioned(monkeypatch):
    monkeypatch.setattr(process.stdout.readline, "//my//path//Application")
    assert utilities.isScriptVersioned() == "True"
I think at this points its clear that i am just doing try and error and looking for some explanation on whats going on here and how to solve it.

Possibly Related Threads…
Thread Author Replies Views Last Post
  'int' object is not subscriptable after API call ed8484 1 519 Sep-18-2021, 02:06 PM
Last Post: ed8484
  how to call an object in another function in Maya bstout 0 752 Apr-05-2021, 07:12 PM
Last Post: bstout
  In this function y initially has no value, but a call to foo() gives no error. Why? Pedroski55 8 1,394 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 988 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 798 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  function call at defined system time? Holon 5 1,457 Oct-06-2020, 03:58 PM
Last Post: snippsat
  subprogram issues: cannot unpack non-iterable function object error djwilson0495 13 2,850 Aug-20-2020, 05:53 PM
Last Post: deanhystad
  Object already contains values when created Stef 4 1,203 Aug-20-2020, 09:42 AM
Last Post: Stef
  How to call/read function for all elements in my list in python johnny_sav1992 1 1,084 Jul-27-2020, 04:19 PM
Last Post: buran
  StopIteration exception when mock PostgreSQL connection in several tests igor87z 1 1,253 Jun-10-2020, 06:16 PM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020