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?
#1
Hello,

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

# myModule.py
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(re.search(r'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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  not able to call the variable inside the if/elif function mareeswaran 3 615 Feb-09-2025, 04:27 PM
Last Post: mareeswaran
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 1,950 May-02-2023, 08:40 AM
Last Post: Gribouillis
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 4,888 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  unittest.mock for an api key silver 3 2,331 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 7,522 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  Mock obj - How to call the side_effect every time during the run? pythonisbae 3 5,341 Mar-06-2022, 09:37 AM
Last Post: pythonisbae
  'int' object is not subscriptable after API call ed8484 1 2,607 Sep-18-2021, 02:06 PM
Last Post: ed8484
  how to call an object in another function in Maya bstout 0 2,686 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 4,984 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 3,098 Oct-29-2020, 02:45 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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