問題があり、Google でこの投稿を見つけました。私は代わりにコマンドを使用することを好みます。そのため、他の人に役立つ可能性のあるコードを次に示します。これは Django 1.4 にありますが、どのバージョンでも動作するはずです。私はWebTestを使用します:
基本テストクラス:
def get_test_image_input(self):
return (
'_test-image.jpg',
file(os.path.join(settings.TEST_FILES_PATH, "images", "test.jpg")).read()
)
どのテストでも:
form['image'] = self.get_test_image_input()
後でクリーンアップするには、コマンドで_test-imageで始まるすべてのファイルを削除します。
from django.core.management.base import BaseCommand
from fabric.operations import local
class Command(BaseCommand):
def handle(self, *args, **options):
# test an app call "tests"
local("python manage_test.py test tests -v 2")
# could use call_command(...) instead
# remove all test images
local("sudo rm -rf `find . -name _test-image*`")