【问题标题】:What's wrong with this Python mock patch?这个 Python 模拟补丁有什么问题?
【发布时间】:2015-06-25 08:36:13
【问题描述】:

我无法在单元测试中模拟导入的模块。我正在尝试使用 mock 模块在我的模块tracker.models 中模拟 PIL Image 类。我知道你应该在使用它们的地方模拟东西,所以我写了@mock.patch('tracker.models.Image') 作为单元测试的装饰器。我正在尝试检查下载的图像是否作为 PIL 图像打开。模拟补丁似乎覆盖了整个 Image 模块。这是我运行测试时遇到的错误:

File "/home/ubuntu/workspace/tracker/models.py", line 40, in set_photo
    width, height = image.size
ValueError: need more than 0 values to unpack

这是我的单元测试:

test_models.py

@responses.activate
@mock.patch('tracker.models.Image')
def test_set_photo(self, mock_pil_image):
    # Initialize data
    hammer = Product.objects.get(name="Hammer")
    fake_url = 'http://www.example.com/prod.jpeg'
    fake_destination = 'Hammer.jpeg'

    # Mock successful image download using sample image. (This works fine)
    with open('tracker/tests/test_data/small_pic.jpeg', 'r') as pic:
        sample_pic_content = pic.read()
    responses.add(responses.GET, fake_url, body=sample_pic_content, status=200, content_type='image/jpeg')

    # Run the actual method
    hammer.set_photo(fake_url, fake_destination)

    # Check that it was opened as a PIL Image
    self.assertTrue(mock_pil_image.open.called,
                    "Failed to open the downloaded file as a PIL image.")

这是它正在测试的一段代码。

tracker/models.py

class Product(models.Model):
    def set_photo(self, url, filename):
        image_request_result = requests.get(url)
        image_request_result.content
        image = Image.open(StringIO(image_request_result.content))

        # Shrink photo if needed
        width, height = image.size  # Unit test fails here
        max_size = [MAX_IMAGE_SIZE, MAX_IMAGE_SIZE]
        if width > MAX_IMAGE_SIZE or height > MAX_IMAGE_SIZE:
            image.thumbnail(max_size)
        image_io = StringIO()
        image.save(image_io, format='JPEG')
        self.photo.save(filename, ContentFile(image_io.getvalue()))

【问题讨论】:

  • image.size 必须是一个元组,但您没有为该元组设置任何模拟值。所以image.size 会返回一个模拟对象。
  • @MartijnPieters 如何防止我的模拟覆盖默认功能?我想选择性地覆盖函数并检查.called,而不是为整个模块批发。
  • 你为什么需要那个?你不是在测试 PIL 模块是否工作,只有当你的代码调用正确的东西并传入正确的信息时。

标签: python django unit-testing mocking


【解决方案1】:

您需要配置Image.open的返回值以包含size属性:

opened_image = mock_pil_image.open.return_value
opened_image.size = (42, 83)

现在,当您的被测函数调用 Image.open 时,返回的 MagicMock 实例将具有一个 size 属性,它是一个元组。

您可以对需要返回某些内容的任何其他方法或属性执行相同的操作。

opened_image 引用也可用于测试被测函数的其他方面;你现在可以断言 image.thumbnailimage.save 被调用了:

opened_image = mock_pil_image.open.return_value
opened_image.size = (42, 83)

# Run the actual method
hammer.set_photo(fake_url, fake_destination)

# Check that it was opened as a PIL Image
self.assertTrue(mock_pil_image.open.called,
                "Failed to open the downloaded file as a PIL image.")

self.assertTrue(opened_image.thumbnail.called)
self.assertTrue(opened_image.save.called)

这让您可以非常准确地测试您的缩略图大小逻辑是否正常工作,例如,无需测试 PIL 是否在做它所做的事情;毕竟这里没有测试 PIL。

【讨论】:

    【解决方案2】:

    我正在编写一个类似的测试,但我的函数使用 Image.open 作为上下文管理器 (with Image.open(<filepath>) as img:)。感谢 Martijn Pieters 的回答和this one,我能够使用我的测试:

    mock_pil_image.open.return_value.__enter__.return_value.size = (42, 83)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多