0

I have a problem with unittest. The patched method does not return a list of tuples, but only one tuple:

My code

from date import Date
from ditreport import DIT_report
from unittest import TestCase
from unittest.mock import patch, Mock

def mock_get_uuid_messages_with_tmpls():
    result = [('43e89d3a-af91-465f-a2db-3147988d1168',), ('93963bf6-9f75-4ffe-80e6-745307ef0a10',),
            ('88e68d67-8969-4059-9f6c-ff161879eb38',), ('39191cbd-79bb-483a-8df7-04aaf72253f1',),
            ('44a685c4-fb12-4431-ae18-3fb220e4d3e7',), ('3eef8189-7509-4dc1-9d71-c04f1cfc0d88',),
            ('4736beae-aa55-4bb3-b41d-3f94b1b178d1',), ('260db4a6-aab8-4d34-b293-cbf5fe6c7400',),
            ('7b73dfe0-5b8a-4a63-8607-43827eeac4c0',), ('fb51668f-0d2f-4958-911d-07d57a73fe56',)]
    return result

class TestDIT_report(TestCase):
    def setUp(self):
        self.date_start='2020-01-12'
        self.date_end = '2020-02-01'
        self.configfile='config/config.def.xml'
        self.param='covid'
        self.report = DIT_report(self.date_start, self.date_end, self.configfile)

    @patch('ditreport.DIT_report.get_uuid_messages_with_tmpls', side_effect=mock_get_uuid_messages_with_tmpls())
    def test_get_uuid_messages_with_tmpls(self, get_uuid_messages_with_tmpls):
        messages_uuid = get_uuid_messages_with_tmpls()
        self.assertEqual(10,len(messages_uuid))

messages_uuid should get a list of ten tuples but get the first tuple

Launching unittests with arguments python -m unittest TestDIT_report.TestDIT_report.test_get_uuid_messages_with_tmpls in /home/skif/PycharmProjects/reports

init config

Ran 1 test in 0.026s

FAILED (failures=1)

1 != 10

Expected :10 Actual :1

Traceback (most recent call last): File "/home/skif/pycharm-2020/plugins/python/helpers/pycharm/teamcity/diff_tools.py", line 32, in _patched_equals old(self, first, second, msg) File "/usr/lib/python3.8/unittest/case.py", line 912, in assertEqual assertion_func(first, second, msg=msg) File "/usr/lib/python3.8/unittest/case.py", line 905, in _baseAssertEqual raise self.failureException(msg) AssertionError: 10 != 1

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/usr/lib/python3.8/unittest/case.py", line 60, in testPartExecutor yield File "/usr/lib/python3.8/unittest/case.py", line 676, in run self._callTestMethod(testMethod) File "/usr/lib/python3.8/unittest/case.py", line 633, in _callTestMethod method() File "/usr/lib/python3.8/unittest/mock.py", line 1325, in patched return func(*newargs, **newkeywargs) File "/home/skif/PycharmProjects/reports/TestDIT_report.py", line 86, in test_get_uuid_messages_with_tmpls self.assertEqual(10,len(messages_uuid))

Why do I get this error? Maybe I missed what parameter? But I have a similar code and it returns a normal list of tuples.

Vlad Kn
  • 141
  • 2
  • 13

1 Answers1

0

Its a simple mistake.

@patch('ditreport.DIT_report.get_uuid_messages_with_tmpls', 
side_effect=mock_get_uuid_messages_with_tmpls())

should be

@patch('ditreport.DIT_report.get_uuid_messages_with_tmpls', 
side_effect=mock_get_uuid_messages_with_tmpls)
Vlad Kn
  • 141
  • 2
  • 13