I am trying to unit test a script that reads a requirements.txt file and goes through each line installing it with pip. However it my unit test it doesn't seem to enter the loop because the self.requirements is empty. I tried setting it to self.setupvirtualenv.requirements to ['foobar'] but this doesn't work.
My question is how can I mock the self.requirements list so that it enters the for loop and the subprocess.call gets called?
def install_requirements(self):
self.requirements = open(self.requirements_path, 'r').readlines()
for req in self.requirements:
req = req.strip()
pip_command = r'pip install {}'.format(req)
subprocess.call(pip_command, shell=True)
My unit test:
import setupvirualenv
import unittest
from mock import patch, Mock
def test_install_requirements(self, mock_open, mock_subprocess_call):
self.setupvirtualenv.requirements = ['foobar']
self.setupvirtualenv.install_requirements()
mock_open.assert_called_once_with('foobar', 'r')
mock_subprocess_call.assert_called_once()
The failure message
AssertionError: Expected 'call' to have been called once. Called 0 times.