vendredi 26 mars 2021

'function' object has no attribute 'assert_called' in unittest

I'm trying to create a test Class in Python using a fixture and verify if my function is called inside the test

The test class:

    from unittest.mock import ANY
    from path_to_class.extract_class_test import TestExtract
    import unittest 
    from mock import patch, call
    from unittest.mock import ANY
    import pytest
    
    @pytest.fixture(autouse=True, scope='class')
    def test_hook_class_mock():
        with patch('path_to_hook_class.classHookName.get_test_data') as mock:
            yield mock
    
    @pytest.mark.usefixtures("test_hook_class_mock")        
    class TestExtract(unittest.TestCase):
    
        def test_if_the_request_method_is_called(self):
            example_test_endpoint = 'test'
            
            test_extract_class = TestExtract
            test_extract_class.execute(endpoint=example_test_endpoint)
            
            test_hook_class_mock.assert_called()

The class tested:

import logging
from path_to_hook_class import classHookName

class TestExtract():
          
    def execute(endpoint):
        test_hook= classHookName(endpoint=endpoint, header='test')
        response = test_hook.get_test_data()
        
        return response

The error I'm getting is

AttributeError: 'function' object has no attribute 'assert_called'

Aucun commentaire:

Enregistrer un commentaire