Django Unit Testing taking a very long time to create test database

The final solution that fixes my problem is to force Django to disable migration during testing, which can be done from the settings like this TESTING = ‘test’ in sys.argv[1:] if TESTING: print(‘=========================’) print(‘In TEST Mode – Disableling Migrations’) print(‘=========================’) class DisableMigrations(object): def __contains__(self, item): return True def __getitem__(self, item): return None MIGRATION_MODULES = DisableMigrations() … Read more

Running django tutorial tests fail – No module named polls.tests

I had exactly the same issue with my Django project: $ python manage test polls.tests worked fine whereas the following failed with an import error: $ python manage test polls $ python manage test (…) ImportError: Failed to import test module: mydjango.polls.tests Traceback (most recent call last): (…) ImportError: No module named polls.tests Check carefully … Read more

How can I test binary file uploading with django-rest-framework’s test client?

When testing file uploads, you should pass the stream object into the request, not the data. This was pointed out in the comments by @arocks Pass { ‘image’: file} instead But that didn’t full explain why it was needed (and also didn’t match the question). For this specific question, you should be doing from PIL … Read more

What is the clean way to unittest FileField in django?

Django provides a great way to do this – use a SimpleUploadedFile or a TemporaryUploadedFile. SimpleUploadedFile is generally the simpler option if all you need to store is some sentinel data: from django.core.files.uploadedfile import SimpleUploadedFile my_model.file_field = SimpleUploadedFile( “best_file_eva.txt”, b”these are the file contents!” # note the b in front of the string [bytes] ) … Read more

How to see which tests were run during Django’s manage.py test command

You can pass -v 2 to the test command: python manage.py test -v 2 After running this command you’ll get something like this (I’m using django 2, feel free to ignore migrations/database stuff): Creating test database for alias ‘default’ (‘file:memorydb_default?mode=memory&cache=shared’)… Operations to perform: Synchronize unmigrated apps: messages, staticfiles Apply all migrations: admin, auth, contenttypes, sessions … Read more

How do you skip a unit test in Django?

Python’s unittest module has a few decorators: There is plain old @skip: from unittest import skip @skip(“Don’t want to test”) def test_something(): … If you can’t use @skip for some reason, @skipIf should work. Just trick it to always skip with the argument True: @skipIf(True, “I don’t want to run this test yet”) def test_something(): … Read more

tech