S3BotoStorage and Unit Testing

2.3k views Asked by At

I am using the storages.backends.s3boto.S3BotoStorage for my DEFAULT_FILE_STORAGE in a Django project.

Ideally I would like to either use the default FileSystemStorage during unit tests or perhaps create a temporary bucket that is deleted on the conclusion of the tests.

What is the best way to accomplish this?

2

There are 2 answers

0
kevinharvey On

I typically keep a separate settings file for tests, test.py. You can configure the storage backend there, as well as any other settings you want to change during tests. Then, run the tests like this:

python manage.py tests --settings=project.test

The --settings flag is the dotted path the the settings module you want to use (The default would be project.settings).

Good optimizations for a test settings file: https://web.archive.org/web/20180204071529/http://www.daveoncode.com/2013/09/23/effective-tdd-tricks-to-speed-up-django-tests-up-to-10x-faster/

Two Scoops of Django is a good place to read about the multi-settings file setup.

0
hspandher On

First of all, use separate file for testing as pointed out above.

Secondly, there is a python library called moto for mocking and testing botos3.

git repository:- https://github.com/spulec/moto