Django使用测试夹具测试FileField [英] Django test FileField using test fixtures

查看:130
本文介绍了Django使用测试夹具测试FileField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为具有FileField的某些模型构建测试。该模型看起来像这样:

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)

我遇到两个问题:


  1. 使用 ./ manage.py dumpdata ,文件内容不保存,只有文件名被保存到灯具中。虽然我发现这是预期的行为,因为文件内容没有保存到数据库中,我想以某种方式将这些信息包含在夹具中进行测试。

  1. 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))


虽然这个测试工作很好,完成后将上传的文件放在媒体目录中。当然,删除可以在 tearDown()中予以处理,但我想知道Django是否有另一种处理方式。

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.

我正在考虑的一个解决方案是使用不同的媒体文件夹进行测试,这些测试必须与测试夹具保持同步。在运行测试时,有没有办法在 settings.py 中指定另一个媒体目录?我可以在dumpdata中添加一些钩子,以便同步媒体文件夹中的文件?

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?

那么,是否有更多的Pythonic或Django特定的交易方式涉及文件的单元测试?

So, is there a more Pythonic or Django-specific way of dealing with unit tests involving files?

推荐答案

Django提供了一种在FileField上编写测试的好方法,而不会在真实的文件系统中使用 - 使用一个SimpleUploadedFile。

Django provides a great way to write tests on FileFields without mucking about in the real filesystem - use a SimpleUploadedFile.

from django.core.files.uploadedfile import SimpleUploadedFile

my_model.file_field = SimpleUploadedFile('best_file_eva.txt', 'these are the file contents!')

这是django的魔法之一功能 - 不会显示在文档:)。但是,请参阅此处

It's one of django's magical features-that-don't-show-up-in-the-docs :). However it is referred to here.

这篇关于Django使用测试夹具测试FileField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆