Python Forum
Mock call to subprocess.Popen failing with StopIteration
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Mock call to subprocess.Popen failing with StopIteration
#1
A unit test that I have inherited is now failing on

p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
logging.warning('Not hitting here.')
with a traceback of

Error:
File "/usr1/tsmith/miniconda3/lib/python3.7/unittest/mock.py", line 960, in __call__ return _mock_self._mock_call(*args, **kwargs) File "/usr1/tsmith/miniconda3/lib/python3.7/unittest/mock.py", line 1022, in _mock_call result = next(effect)
StopIteration`

Any idea how to investigate this?
Reply
#2
StopIteration is the normal exception sent by an iterator at the end of its iteration. For example
>>> L = [1, 2, 3]
>>> effect = iter(L)
>>> next(effect)
1
>>> next(effect)
2
>>> next(effect)
3
>>> next(effect)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
It means that the iterable 'effect' was exhausted.
Reply
#3
(Nov-07-2019, 08:57 PM)Gribouillis Wrote: StopIteration is the normal exception sent by an iterator at the end of its iteration.
It means that the iterable 'effect' was exhausted.

If it was not intentional that it be called too many times, does it imply an error?
Reply
#4
Yes it implies an error because nobody writes applications that crash. The error comes from the code that calls the unittest.mock module. I suppose the error traceback is longer than what you showed. Look for the last line from the application code in that traceback.
Reply
#5
Yes, the line before it is about application code.
Error:
File "/usr1/tsmith/git/gg_database.py", line 89, in get_database_size_via_subprocess p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE)
And line 89 of the application code is:
side_effect = [
            MockPopenResponse(stdout=stdout, status_code=self.returncode)
        ]
followed by
 logging.warning('td90 se count = ' + str(len(side_effect)))
which returns
Output:
WARNING:root:td90 se count = 1
But I cannot seem to figure out where it is calling the mocked function when side_effect is empty.
Reply
#6
So the title of the thread is incorrect, it should be 'mocked call to subprocess.Popen failing...'. I'm afraid this is a little too hard for me, I don't have much experience with the mock module, but perhaps someone else in the forum can answer this. It really looks like a issue with the testing rather than with the applicatino code.
Reply
#7
(Nov-07-2019, 10:23 PM)Gribouillis Wrote: So the title of the thread is incorrect, it should be 'mocked call to subprocess.Popen failing...'.
It doesn't look like I'm able to change the title, but as a moderator, I would bet that you're able to. You're welcome to as far as I'm concerned.
Reply
#8
tharpa Wrote:You're welcome to as far as I'm concerned.
Done.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information subprocess.Popen() suddenly giving me grief? davecotter 3 604 Dec-13-2023, 10:49 PM
Last Post: davecotter
  Failing regex, space before and after the "match" tester_V 6 1,176 Mar-06-2023, 03:03 PM
Last Post: deanhystad
  Failing to print sorted files tester_V 4 1,245 Nov-12-2022, 06:49 PM
Last Post: tester_V
  unittest.mock for an api key silver 3 1,379 Aug-29-2022, 03:52 PM
Last Post: ndc85430
  Failing reading a file and cannot exit it... tester_V 8 1,798 Aug-19-2022, 10:27 PM
Last Post: tester_V
  Failing regex tester_V 3 1,165 Aug-16-2022, 03:53 PM
Last Post: deanhystad
  Use subprocess.Popen and time.sleep chucky831 2 1,934 Aug-11-2022, 07:53 PM
Last Post: carecavoador
  Mock obj - How to call the side_effect every time during the run? pythonisbae 3 2,613 Mar-06-2022, 09:37 AM
Last Post: pythonisbae
  continue if 'subprocess.call' failes tester_V 11 5,140 Aug-26-2021, 12:16 AM
Last Post: tester_V
  Failing to connect to a host with WMI tester_V 6 4,350 Aug-10-2021, 06:25 PM
Last Post: tester_V

Forum Jump:

User Panel Messages

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