A_script.py
from uuid import uuid4
def get_unique_identifier(env, customer_id):
return env '-' customer_id '-' str(uuid4())[0:8]
test_A_script.py
import unittest
from unittest.mock import patch
import src.A_script as a_script
class MyTestCase(unittest.TestCase):
@patch('uuid.uuid4')
def test_get_unique_identifier(self, mock_uuid4):
mock_uuid4.return_value = 'abcd1234'
expected = 'test_env-test_cust-abcd1234'
unique_identifier = a_script.get_unique_identifier('test_env', 'test_cust')
self.assertEqual(expected, unique_identifier)
How can I make uuid4 return 'abcd1234'?
CodePudding user response:
You need to mock the method in the module where it's used. In your case you use it in module A_script
so you have to call patch
with 'A_script.uuid4
.
CodePudding user response:
You have to patch uuid that are imported in your srcipt, so change
@patch('uuid.uuid4')
to
@patch('src.A_script.uuid4')
# or @patch('src.A_script.uuid4', return_value="abcd1234")
# or @patch('src.A_script.uuid4', new=lambda:"abcd1234")
In last case you do not pass mock as function parameter at all