【发布时间】: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