我正在使用pytest-django客户端。我正在测试的功能需要请求的FILE属性中的图像。我可以使用“ RequestFactory”帮助程序直接调用该函数,但是我想使用pytest-django客户端。有什么方法可以使测试中的客户端可以使用该更改请求对象?即,我要添加图像。请参见代码中的注释。
def test_using_pytest_django_client(client):
request_factory = RequestFactory()
request = request_factory.post('/some-fake-url')
with open('somepicture.png', 'rb') as test_image:
uploaded_file = UploadedFile(test_image, content_type='image/png')
request.FILES['1'] = uploaded_file
endpoint = reverse('the-real-endpoint')
# Need to somehow make the image available on the request
# client.request = request does not work
client.post(endpoint)