13
Worked it out.
It turns out I had done django-admin.py startproject pyDietTracker
but not python manage.py startapp myApp
. After going back and doing this, it did work as documented. It would appear I have a lot to learn about reading and the difference between a site and an app in Django.
Thank you for your help S.Lott and Emil StenstrΓΆm. I wish I could accept both your answers because they are both helped alot.
Most important lesson Tests only work at the app level not the site level
234
I had the same issue but my root cause was different.
I was getting Ran 0 tests
, as OP.
But it turns out the test methods inside your test class must start with keyword test
to run.
Example:
from django.test import TestCase
class FooTest(TestCase):
def setUp(self):
pass
def tearDown(self):
pass
def this_wont_run(self):
print 'Fail'
def test_this_will(self):
print 'Win'
Also the files with your TestCases in them have to start with test
.
- [Django]-What's the idiomatic Python equivalent to Django's 'regroup' template tag?
- [Django]-Pagination in Django-Rest-Framework using API-View
- [Django]-Django 1.5b1: executing django-admin.py causes "No module named settings" error
154
If youβre using a yourapp/tests
package/style for unittests, make sure thereβs a __init__.py
in your tests
folder (since thatβs what makes it a Python module!).
- [Django]-Choose test database?
- [Django]-How to debug in Django, the good way?
- [Django]-How to do math in a Django template?
48
I can run test for specific apps e.g.
python project/manage.py test app_name
but when I run
python project/manage.py test
0 tests was found
Figure out I need to run this in the same directory as manage.py
so the solution would be, cd to project directory and run
python manage.py test
- [Django]-Django urlsafe base64 decoding with decryption
- [Django]-Django: Safely Remove Old Migrations?
- [Django]-Django related_name for field clashes
28
In my case, the app folder itself was missing an __init__.py
. This results in the behaviour that the test will be run with python manage.py test project.app_name
but not with python manage.py test
.
project/
app_name/
__init__.py # this was missing
- [Django]-Why doesn't django's model.save() call full_clean()?
- [Django]-Django: ImproperlyConfigured: The SECRET_KEY setting must not be empty
- [Django]-Django β How to use decorator in class-based view methods?
19
This may also happen when you are using a tests
module instead of a tests.py
. In this case you need to import all the test classes into the __init__.py
of your tests module, e.g.
tests/
__init__.py
somemodule.py
In your __init__.py
you now need to import the somemodule
like this:
from .somemodule import *
- [Django]-How do I use django rest framework to send a file in response?
- [Django]-Default value for user ForeignKey with Django admin
- [Django]-Django: Grab a set of objects from ID list (and sort by timestamp)
18
In my case, I typed def
instead of class
. Instead of
class TestDisplayWeight(TestCase): # correct!
I had
def TestDisplayWeight(TestCase): # wrong!
- [Django]-How do I reference a Django settings variable in my models.py?
- [Django]-How do I filter ForeignKey choices in a Django ModelForm?
- [Django]-Django Rest Framework File Upload
- [Django]-Iterate over model instance field names and values in template
- [Django]-Gunicorn autoreload on source change
- [Django]-What is actually assertEquals in Python?
8
I know I am late at this point but I also had trouble with
Found 0 test(s).
System check identified no issues (1 silenced).
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
I have followed all the steps still I was facing the same issue. My fix was I missed __init__.py
file in the test directory. Adding the file and re-running the command solved my issue.
HIGHLIGHTING IT A BIT:
Make sure you have __init__.py
file
- [Django]-How can I get the full/absolute URL (with domain) in Django?
- [Django]-Django β Annotate multiple fields from a Subquery
- [Django]-How to combine django "prefetch_related" and "values" methods?
7
in my case, I miss starting my functions name with test_
and when run my test with :
python manage.py test myapp
result was :
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
it seems Django cannot recognize my tests!
then i change myproject/myapp/test.py file like this :
from django.test import TestCase
# Create your tests here.
class apitest(TestCase):
def test_email(self):
pass
def test_secend(self):
pass
after that result is:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
..
----------------------------------------------------------------------
Ran 2 tests in 2.048s
OK
Destroying test database for alias 'default'...
- [Django]-Count() vs len() on a Django QuerySet
- [Django]-Django-tables2: How to use accessor to bring in foreign columns?
- [Django]-Assign variables to child template in {% include %} tag Django
6
Hereβs another one that Iβve just had: Check your test files are not executable. My virtualbox auto-mounted them as executable so the test discover missed them completely. I had to add them into the relevant __init__.py
files before someone told me what the issue was as a work around, but now they are removed, and non-executable and everything _just_works.
- [Django]-Django models.py Circular Foreign Key
- [Django]-Django: Reference to an outer query may only be used in a subquery
- [Django]-OneToOneField() vs ForeignKey() in Django
3
I had this happen when I had a test.py file, and a test/ subdirectory, in the same Django app directory. I guess Iβm confusing python or the test runner whether Iβm looking for a test module (in test.py) or a test package (in test/ subdir).
- [Django]-How to change field name in Django REST Framework
- [Django]-Get the list of checkbox post in django views
- [Django]-How to access request body when using Django Rest Framework and avoid getting RawPostDataException
1
See https://docs.djangoproject.com/en/1.11/topics/testing/overview/
The most common reason for tests not running is that your settings arenβt right, and your module is not in INSTALLED_APPS.
We use django.test.TestCase
instead of unittest.TestCase
. It has the Client
bundled in.
https://docs.djangoproject.com/en/1.11/topics/testing/tools/#django.test.TestCase
- [Django]-Django filter on the basis of text length
- [Django]-How do you perform Django database migrations when using Docker-Compose?
- [Django]-POST jQuery array to Django
1
If you are trying to run a test in your main app, such as my_app/my_app/ make sure you have the following checked:
- App name is listed in INSTALLED_APPS inside
settings.py
- Make sure your
DATABASES['default']
insidesettings.py
is set properly - The App has a
models.py
(even if you are not using one, at least an empty one is required to be there)
- [Django]-What is a "slug" in Django?
- [Django]-Django: How to manage development and production settings?
- [Django]-Why does DEBUG=False setting make my django Static Files Access fail?
1
Using this syntax
python manage.py test
instead of ./manage.py test
solved this problem for me.
- [Django]-How can I keep test data after Django tests complete?
- [Django]-Pylint "unresolved import" error in Visual Studio Code
- [Django]-What is the purpose of adding to INSTALLED_APPS in Django?
1
If you encounter this error after upgrading to Django 3, it might be because the -k
parameter changed meaning from:
-k, --keepdb Preserves the test DB between runs.
to
-k TEST_NAME_PATTERNS Only run test methods and classes that match the pattern or substring. Can be used multiple times. Same as unittest -k option.
So just replace -k
with --keepdb
to make it work again.
- [Django]-How do I do an OR filter in a Django query?
- [Django]-Python Django Gmail SMTP setup
- [Django]-Migrating Django fixtures?
1
I had the same problem, turns out I saved the __init__
as a python file but it did not put .py
at the end of its name. I added .py
at the end of fileβs name. it was ok afterwards
(in other words, I had created __init__
instead of __init__.py
)
- [Django]-Django connection to postgres by docker-compose
- [Django]-What is a NoReverseMatch error, and how do I fix it?
- [Django]-How does one make logging color in Django/Google App Engine?
- [Django]-Iterating over related objects in Django: loop over query set or use one-liner select_related (or prefetch_related)
- [Django]-Django β Static file not found
- [Django]-Django admin TabularInline β is there a good way of adding a custom html column?
0
In the same file, I had two test classes with the SAME NAME, and of course this prevented all tests from running.
- [Django]-Change a field in a Django REST Framework ModelSerializer based on the request type?
- [Django]-Django unit tests without a db
- [Django]-How to check if a user is logged in (how to properly use user.is_authenticated)?
0
I created a method called run
in my test class which turned out to be a very bad idea. Python could see that I wanted to run tests, but was unable to. This problem is slightly different, but the result is the same β it made it seem as if the tests couldnβt be found.
Note that the following message was displayed:
You want to run the existing test: <unittest.runner.TextTestResult run=0 errors=0 failures=0>
- [Django]-Split views.py in several files
- [Django]-Django Passing Custom Form Parameters to Formset
- [Django]-How to change the name of a Django app?
0
Run --help
and look for verbose. Crank it to max.
I ran manage.py test --verbose
and found this debug output right at the top:
>nosetests --with-spec --spec-color --verbose --verbosity=2
.
Oh look! I had installed and forgotten about nosetests
. And it says --verbosity=2
. I figured out that 3 is the max and running it with 3 I found lots of these:
nose.selector: INFO: /media/sf_C_DRIVE/Users/me/git/django/app/tests/test_processors.py is executable; skipped
That gave me the right hint. It indeed has problems with files having the x-bit set. However, I was thrown off the track as it had run SOME of the tests β even though it explicitly said it would skip them. Changing bits is not possible, as I run the tests in a VM, sharing my Windows NTFS-disk. So adding --exe
fixed it.
- [Django]-What is the difference between null=True and blank=True in Django?
- [Django]-Reload django object from database
- [Django]-Using JSON in django template
0
Had the same issue and it was because my filename had a -
char in its name.
My filename was route-tests.py
and changed it to route_tests.py
- [Django]-No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model
- [Django]-Filter Queryset on empty ImageField
- [Django]-Django β iterate number in for loop of a template
0
Django engine searches files and folders with test_
prefix (inside of a tests
folder). In my case it was simple solution.
So, be sure to checkout file/folder name starts with it.
- [Django]-How do I migrate a model out of one django app and into a new one?
- [Django]-RuntimeWarning: DateTimeField received a naive datetime
- [Django]-Disabled field is not passed through β workaround needed
0
I had the same problem, it was caused by init.py at the project root β deleted that, all tests ran fine again.
- [Django]-Python/Django: log to console under runserver, log to file under Apache
- [Django]-Disable migrations when running unit tests in Django 1.7
- [Django]-How to make two django projects share the same database
0
This is late. but you can simply add your app name in front of importing models. like
from myapp.models import something
This works for Me.
- [Django]-Setting the selected value on a Django forms.ChoiceField
- [Django]-Override existing Django Template Tags
- [Django]-Pagination in Django-Rest-Framework using API-View
0
In Django, methods in test classes must start with "test" keyword. for example test_is_true(). methods name like is_true() will not execute.
- [Django]-Disabled field is not passed through β workaround needed
- [Django]-Django's Double Underscore
- [Django]-How to execute a Python script from the Django shell?
0
In my i resolve this, using python manage.py test apps and works, because iβm creating my apps inside folder apps, not at the same level of project.
.
βββ HFarm
βββ .vscode
βββ apps/
β βββ accounts/
β βββ migrations
β βββ templates
β βββ tests/
β β βββ __init__.py
β β βββ testAccount.py
β βββ utils/
β β βββ __init__.py
β β βββ mail.py
β βββ __init__.py
β βββ admin.py
β βββ apps.py
β βββ forms.py
β βββ models.py
β βββ urls.py
β βββ views.py
βββ backend/
β βββ __init__.py
β βββ asgi.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ manage.py
#tests/init.py
from .testsAccount import *
- [Django]-OneToOneField() vs ForeignKey() in Django
- [Django]-Django error when installing Graphite β settings.DATABASES is improperly configured. Please supply the ENGINE value
- [Django]-What are the limitations of Django's ORM?