Django 使用测试装置测试 FileField
我正在尝试为某些具有 FileField 的模型构建测试。模型如下:
class SolutionFile(models.Model):
'''
A file from a solution.
'''
solution = models.ForeignKey(Solution)
file = models.FileField(upload_to=make_solution_file_path)
我遇到了两个问题:
使用
./manage.py dumpdata将数据保存到fixture
时,文件内容没有保存,只保存了文件名固定装置。虽然我发现这是预期的行为,因为文件内容未保存到数据库中,但我想以某种方式在测试夹具中包含此信息。我有一个用于上传文件的测试用例,如下所示:
def test_post_solution_file(自身): 导入临时文件 导入操作系统 文件名 = tempfile.mkstemp()[1] f = 打开(文件名,'w') f.write('这些是文件内容') f.close() f = 打开(文件名,'r') post_data = {'文件': f} 响应 = self.client.post(self.solution.get_absolute_url()+'add_solution_file/', post_data, 遵循=真) f.close() os.remove(文件名) self.assertTemplateUsed(响应,'tests/solution_detail.html') self.assertContains(响应, os.path.basename(文件名))
虽然此测试工作得很好,但完成后它会将上传的文件保留在媒体目录中。当然,删除可以在tearDown()中处理,但我想知道Django是否有另一种方法来处理这个问题。
我想到的一种解决方案是使用不同的媒体文件夹进行测试,该文件夹必须与测试装置保持同步。运行测试时,有什么方法可以在 settings.py
中指定另一个媒体目录?我可以包含某种转储数据的挂钩,以便它同步媒体文件夹中的文件吗?
那么,是否有一种更 Pythonic 或 Django 特定的方法来处理涉及文件的单元测试?
I'm trying to build tests for some models that have a FileField. The model looks like this:
class SolutionFile(models.Model):
'''
A file from a solution.
'''
solution = models.ForeignKey(Solution)
file = models.FileField(upload_to=make_solution_file_path)
I have encountered two problems:
When saving data to a fixture using
./manage.py dumpdata
, the file contents are not saved, only the file name is saved into the fixture. While I find this to be the expected behavior as the file contents are not saved into the database, I'd like to somehow include this information in the fixture for tests.I have a test case for uploading a file that looks like this:
def test_post_solution_file(self): import tempfile import os filename = tempfile.mkstemp()[1] f = open(filename, 'w') f.write('These are the file contents') f.close() f = open(filename, 'r') post_data = {'file': f} response = self.client.post(self.solution.get_absolute_url()+'add_solution_file/', post_data, follow=True) f.close() os.remove(filename) self.assertTemplateUsed(response, 'tests/solution_detail.html') self.assertContains(response, os.path.basename(filename))
While this test works just fine, it leaves the uploaded file in the media directory after finishing. Of course, the deletion could be taken care of in tearDown()
, but I was wondering if Django had another way of dealing with this.
One solution I was thinking of was using a different media folder for tests which must be kept synced with the test fixtures. Is there any way to specify another media directory in settings.py
when tests are being run? And can I include some sort of hook to dumpdata so that it syncs the files in the media folders?
So, is there a more Pythonic or Django-specific way of dealing with unit tests involving files?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Django 提供了一种在 FileFields 上编写测试的好方法,而无需在实际文件系统中进行修改 - 使用 SimpleUploadedFile。
这是 django 的神奇功能之一,但在文档中却没有出现:)。然而,它被引用这里。
Django provides a great way to write tests on FileFields without mucking about in the real filesystem - use a SimpleUploadedFile.
It's one of django's magical features-that-don't-show-up-in-the-docs :). However it is referred to here.
您可以使用
@override_settings()
装饰器覆盖测试的MEDIA_ROOT
设置如文档所述:You can override the
MEDIA_ROOT
setting for your tests using the@override_settings()
decorator as documented:我之前为整个图库应用程序编写过单元测试,对我来说效果好的方法是使用 python tempfile 和 Shutil 模块在临时目录中创建测试文件的副本,然后将它们全部删除。
以下示例不工作/不完整,但应该让您走上正确的道路:
我在单元测试的 setUp() 和 TeaDown() 方法中运行这些方法,效果非常好!您已经获得了文件的干净副本来测试可重用且可预测的文件字段。
I've written unit tests for an entire gallery app before, and what worked well for me was using the python tempfile and shutil modules to create copies of the test files in temporary directories and then delete them all afterwards.
The following example is not working/complete, but should get you on the right path:
I run those methods in the setUp() and tearDown() methods of my unit tests and it works great! You've got a clean copy of your files to test your filefield that are reusable and predictable.
使用 pytest 和 pytest-django,我在
conftest.py
文件中使用它:可能会有所帮助:
with pytest and pytest-django, I use this in
conftest.py
file:might be helpful:
这就是我为测试所做的。上传文件后,它应该最终出现在我的组织模型对象的照片属性中:
This is what I did for my test. After uploading the file it should end up in the photo property of my organization model object: