Unittest Django:模拟外部API,正确的方式是什么?

21

我不理解模拟(mock)的工作原理,也不知道如何使用mock对象编写单元测试。我想每当我的模型(model)调用save()方法时,都能够模拟一个外部API调用。

我的代码:models.py

from . import utils

class Book(Titleable, Isactiveable, Timestampable, IsVoidable, models.Model):
   title
   orig_author
   orig_title
   isbn 

    def save(self, *args, **kwargs):
        if self.isbn:
            google_data = utils.get_original_title_and_name(self.isbn)
            if google_data:
                self.original_author = google_data['author']
                self.original_title = google_data['title']
        super().save(*args, **kwargs)

utils.py

def get_original_title_and_name(isbn, **kawargs):
    isbn_search_string = 'isbn:{}'.format(isbn)
    payload = {
        'key': GOOGLE_API_KEY,
        'q': isbn_search_string,
        'printType': 'books',
    }
    r = requests.get(GOOGLE_API_URL, params=payload)
    response = r.json()
    if 'items' in response.keys():
        title = response['items'][THE_FIRST_INDEX]['volumeInfo']['title']
        author = response['items'][THE_FIRST_INDEX]['volumeInfo']['authors'][THE_FIRST_INDEX]

        return {
            'title': title,
            'author': author
        }
    else:
        return None

我开始阅读文档并编写测试:

test.py

from unittest import mock
from django.test import TestCase
from rest_framework import status
from .constants import THE_FIRST_INDEX, GOOGLE_API_URL, GOOGLE_API_KEY

class BookModelTestCase(TestCase):
    @mock.patch('requests.get')
    def test_get_original_title_and_name_from_google_api(self, mock_get):
        # Define new Mock object
        mock_response = mock.Mock()
        # Define response data from Google API
        expected_dict = {
            'kind': 'books#volumes',
            'totalItems': 1,
            'items': [
                {
                    'kind': 'books#volume',
                    'id': 'IHxXBAAAQBAJ',
                    'etag': 'B3N9X8vAMWg',
                    'selfLink': 'https://www.googleapis.com/books/v1/volumes/IHxXBAAAQBAJ',
                    'volumeInfo': {
                        'title': "Alice's Adventures in Wonderland",
                        'authors': [
                            'Lewis Carroll'
                        ]
                    }
                }
                    ]
            }

        # Define response data for my Mock object
        mock_response.json.return_value = expected_dict
        mock_response.status_code = 200

        # Define response for the fake API
        mock_get.return_value = mock_response

首先,我无法正确编写 @mock.patch 中的 target。如果我将 target 定义为 utuls.get_original_title_and_name.requests.get,则会得到 ModuleNotFoundError。同时,我不明白如何对外部API进行伪造调用并验证已接收的数据(即使我已经定义了mock_response.json.return_value = expected_dict),以及如何验证我的 save() 方法是否正常工作?

我应该如何为这些情况编写测试呢?有人可以解释一下吗?


3
完全没有关系,但我肯定不会在 model.save() 中进行 API 调用。我宁愿使用异步队列来处理这种事情。 - bruno desthuilliers
1
你走在正确的道路上,但是不要修补utils.get_original_title_and_name.requests.get,而是修补utils.requests.get - hoefling
1
可能是Python模拟导入模块中的函数的重复问题。 - hoefling
您写到了ModuleNotFoundError。在utuls.get_original_title_and_name.requests.get中是否有打字错误(utuls->utils)? - Andrew_Lvov
1个回答

22

你应该模拟测试代码的直接协作者。对于Book来说,那会是utils。对于utils来说,那会是requests

因此对于BookModelTestCase

class BookModelTestCase(TestCase):

    @mock.patch('app.models.utils')
    def test_save_book_calls_google_api(self, mock_utils):
        mock_utils.get_original_title_and_name.return_value = {
            'title': 'Google title',
            'author': 'Google author'
        }

        book = Book(
            title='Some title',
            isbn='12345'
        )
        book.save()

        self.assertEqual(book.title, 'Google title')
        self.assertEqual(book.author, 'Google author')
        mock_utils.get_original_title_and_name.assert_called_once_with('12345')

然后您可以创建一个单独的测试用例来测试get_original_title_and_name

class GetOriginalTitleAndNameTestCase(TestCase):

    @mock.patch('app.utils.requests.get')
    def test_get_original_title_and_name_from_google_api(self, mock_get):
        mock_response = mock.Mock()
        # Define response data from Google API
        expected_dict = {
            'kind': 'books#volumes',
            'totalItems': 1,
            'items': [
                {
                    'kind': 'books#volume',
                    'id': 'IHxXBAAAQBAJ',
                    'etag': 'B3N9X8vAMWg',
                    'selfLink': 'https://www.googleapis.com/books/v1/volumes/IHxXBAAAQBAJ',
                    'volumeInfo': {
                        'title': "Alice's Adventures in Wonderland",
                        'authors': [
                            'Lewis Carroll'
                        ]
                    }
                }
                    ]
            }

        # Define response data for my Mock object
        mock_response.json.return_value = expected_dict
        mock_response.status_code = 200

        # Define response for the fake API
        mock_get.return_value = mock_response

        # Call the function
        result = get_original_title_and_name(12345)

        self.assertEqual(result, {
            'title': "Alice's Adventures in Wonderland", 
            'author': 'Lewis Carroll'
        })
        mock_get.assert_called_once_with(GOOGLE_API_URL, params={
            'key': GOOGLE_API_KEY,
            'q': 'isbn:12345',
            'printType': 'books',
        })

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接