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


Messages In This Thread
How to mock an object that is created during function call? - by Schlangenversteher - Jan-31-2020, 01:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I call sys.argv list inside a function, from the CLI? billykid999 3 752 May-02-2023, 08:40 AM
Last Post: Gribouillis
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 2,207 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  unittest.mock for an api key silver 3 1,335 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  AttributeError: 'function' object has no attribute 'metadata 3lnyn0 5 4,521 Mar-28-2022, 04:42 PM
Last Post: Larz60+
  Mock obj - How to call the side_effect every time during the run? pythonisbae 3 2,554 Mar-06-2022, 09:37 AM
Last Post: pythonisbae
  'int' object is not subscriptable after API call ed8484 1 1,765 Sep-18-2021, 02:06 PM
Last Post: ed8484
  how to call an object in another function in Maya bstout 0 2,042 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 3,414 Dec-19-2020, 07:30 AM
Last Post: ndc85430
  Struggling for the past hour to define function and call it back godlyredwall 2 2,156 Oct-29-2020, 02:45 PM
Last Post: deanhystad
  list call problem in generator function using iteration and recursive calls postta 1 1,862 Oct-24-2020, 09:33 PM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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