322👍
In my experience, weird ImportErrors when running tests are caused by an ImportError in the tests module itself.
Ensure that your tests module can be imported:
$ python manage.py shell
...
>>> import foo.exports.tests
Edit:
If that causes an error, make sure you do not have both a directory foo/exports/tests
and a file foo/exports/tests.py
116👍
As Daniel Hepper said in a comment above, try checking whether you have both a app/tests
folder and a app/tests.py
file in your app.
Django startapp
creates a tests.py
file automatically so there might be a file that you haven’t noticed.
If you simply delete the automatically generated tests.py
file, it should work. (Obviously you should check the contents of the file before deleting anything!)
- [Django]-How to do SELECT MAX in Django?
- [Django]-Trying to migrate in Django 1.9 — strange SQL error "django.db.utils.OperationalError: near ")": syntax error"
- [Django]-Django, creating a custom 500/404 error page
31👍
In case you have created a directory named tests
and have written test files inside it, for example test_views.py
, test_models.py
, etc., make sure you remove the file test.py
created automatically by the command python manage.py startapp
.
- [Django]-Change a form value before validation in Django form
- [Django]-Django template can't see CSS files
- [Django]-Django-Forms with json fields
5👍
Try checking whether you have both a app/tests
folder and a app/tests.py
file in your app.
By default a file is automatically called tests.py
delete this file it the error will be resolved
- [Django]-How do I perform HTML decoding/encoding using Python/Django?
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
- [Django]-How to tell if a task has already been queued in django-celery?
5👍
In one word: delete test.py or tests folder
I had the same issue when I copied some tests I wrote before in one of my projects into my new projects where I had more than 5 same APIs there.
Usually I do create a new folder called tests and write all my tests in a folder for each app so everything looks better
The mistake I made which led me through this problem was not deleting the test.py file from app folder when I created tests folder in the same app
Because you cant have both tests folder and test.py in the same app.
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
- [Django]-Django admin TabularInline – is there a good way of adding a custom html column?
- [Django]-Django-rest-framework returning 403 response on POST, PUT, DELETE despite AllowAny permissions
2👍
Just to add to the list of possible cases.
This can also happen inside a virtual env when the package you’re on was locally installed.
In that case you just need to uninstall the version that was installed an “re-link” it (I don’t known the correct term) by using the develop command
~/dev/stufflib% pip uninstall stufflib
~/dev/stufflib% python setup.py develop
~/dev/stufflib% python setup.py test
- [Django]-Django REST framework post array of objects
- [Django]-What's the best way to store a phone number in Django models?
- [Django]-Django rest framework, use different serializers in the same ModelViewSet
1👍
In my case problem was because I tried to start django test task from symlink to folder with project, not from “real” path. When I run django test task from project folder not using symlink I don’t get this error.
- [Django]-How to test auto_now_add in django
- [Django]-A field with precision 10, scale 2 must round to an absolute value less than 10^8
- [Django]-Django: manage.py does not print stack trace for errors
1👍
make sure you don’t have 2 files named test.py in your tree files that way Python should pick the one you wanted to.
- [Django]-Django dump data for a single model?
- [Django]-Django models.py Circular Foreign Key
- [Django]-How do I use an UpdateView to update a Django Model?