librelist archives

« back to archive

How to write a test for file upload.

How to write a test for file upload.

From:
Low Kian Seong
Date:
2012-09-23 @ 09:24
Dear all,

I have an application like this:

http://pastebin.sabayon.org/pastie/10639

The application can do upload / download based on a download / upload var.
I have googled and seen several examples on how to create a unit test for
upload sending a file using StringIO. I don't know however how to send the
upload / download var together with the request.

Can someone help me out with this ?

Thank you.

Re: [flask] How to write a test for file upload.

From:
Steven Kryskalla
Date:
2012-09-23 @ 17:52
On Sun, Sep 23, 2012 at 2:24 AM, Low Kian Seong <kianseong@gmail.com> wrote:
> The application can do upload / download based on a download / upload var. I
> have googled and seen several examples on how to create a unit test for
> upload sending a file using StringIO. I don't know however how to send the
> upload / download var together with the request.

In the Flask docs on testing:

http://flask.pocoo.org/docs/testing/

You will see that you can instantiate a test_client, which comes from werkzeug:

http://werkzeug.pocoo.org/docs/test/

In those werkzeug docs it gives an example of what the POST data will
look like when uploading a file:

data={'file': (StringIO('my file contents'), 'test.txt')}

So for your app it would look like this:

https://gist.github.com/3772472

However I would override the UPLOAD_LOCATION and DOWNLOAD_LOCATION
(couldn't they be replaced with one config?) in unit tests with a
temporary directory that gets recreated and deleted on every test run.

-Steve

Re: [flask] How to write a test for file upload.

From:
Low Kian Seong
Date:
2012-09-23 @ 22:16
On Mon, Sep 24, 2012 at 1:52 AM, Steven Kryskalla <skryskalla@gmail.com>wrote:

> On Sun, Sep 23, 2012 at 2:24 AM, Low Kian Seong <kianseong@gmail.com>
> wrote:
> > The application can do upload / download based on a download / upload
> var. I
> > have googled and seen several examples on how to create a unit test for
> > upload sending a file using StringIO. I don't know however how to send
> the
> > upload / download var together with the request.
>
> In the Flask docs on testing:
>
> http://flask.pocoo.org/docs/testing/
>
> You will see that you can instantiate a test_client, which comes from
> werkzeug:
>
> http://werkzeug.pocoo.org/docs/test/
>
> In those werkzeug docs it gives an example of what the POST data will
> look like when uploading a file:
>
> data={'file': (StringIO('my file contents'), 'test.txt')}
>
> So for your app it would look like this:
>
> https://gist.github.com/3772472
>
>
Thank you very much. This works just as you said.

> However I would override the UPLOAD_LOCATION and DOWNLOAD_LOCATION
> (couldn't they be replaced with one config?) in unit tests with a
> temporary directory that gets recreated and deleted on every test run.
>
> -Steve
>