Django testing model with ImageField

后端 未结 8 1650
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-07 16:52

I need to test the Photo model of my Django application. How can I mock the ImageField with a test image file?

tests.py

class PhotoT         


        
8条回答
  •  甜味超标
    2020-12-07 17:16

    You can use a temporary file, using tempfile. So you don't need a real file to do your tests.

    import tempfile
    
    image = tempfile.NamedTemporaryFile(suffix=".jpg").name
    

    If you prefer to do manual clean-up, use tempfile.mkstemp() instead.

提交回复
热议问题