“function”对象在unittest中没有属性“assert_called”

2024-06-07 14:23:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用fixture在Python中创建一个测试类,并验证我的函数是否在测试中被调用

测试类:

    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()

被测试的班级:

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

我得到的错误是

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

课外测试,测试通过


Tags: topathfromtestimportpytestdefextract

热门问题