9

i don't now why, but from one day to another y started to have problems when i tried to run the tests. I'm using django 1.1 (customer requirements) and when i running test whith:

python manage.py test --setting=settingsTest

it throw:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/usr/lib/pymodules/python2.6/django/core/management/__init__.py", line 362, in execute_manager
    ...
  File "/usr/lib/pymodules/python2.6/django/db/backends/sqlite3/base.py", line 193, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: programas_act_actividadesprograma

but i have in settingsTest.py

INSTALLED_APPS = (
    'django.contrib.auth',
    ...

    'site.programas_act',
    ...
)

and in /var/www/site/programas_act/models.py

class ActividadesPrograma(models.Model):
    ...

I removed the file named in settingsTest.DATABASE_NAME and run:

python manage.py syncdb --setting=settingsTest

but still failing.

If i run in django shell_plus:

import settings
print settings.INSTALLED_APPS
for app in settings.INSTALLED_APPS:
    print app

I can see the app, but when i run:

from django.db import connection
cursor = connection.cursor()
qry="""SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1"""
cursor.execute(qry)
results = cursor.fetchall()
for tablename in results:
    print tablename

I can't found that tablename.

Anyone can help me?

Thanks

gsoriano
  • 386
  • 1
  • 4
  • 6
  • This can also happen if a table in your models isn't created by your migrations. See: http://stackoverflow.com/a/39246325/1551116 – James Bradbury Aug 31 '16 at 09:53

4 Answers4

6

I had this problem, too. Turned out that I had to add a TEST_NAME property in the settings.py file to identify the test database properly. It solved the problem for me:

if 'test' in sys.argv:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(os.path.dirname(__file__), 'test.db'),
            'TEST_NAME': os.path.join(os.path.dirname(__file__), 'test.db'),
        }
    }
pkout
  • 6,430
  • 2
  • 45
  • 55
  • 5
    I get the same error even after using these settings for `NAME` and `TEST_NAME` – nnyby Oct 08 '14 at 14:10
  • I get this with two entries in settings.py DATABASES. Adding 'TEST_NAME' fixed it for me. One can be ':memory:' but not both. Attempts to use ':memory:?cache=shared' (per SQLite docs) didn't work. Python 2.7.8/Django 1.6. – JimB Oct 31 '14 at 13:51
  • Do not do this! Running tests from a non-memory database will take forever! – Cerin Mar 24 '16 at 04:43
  • 1
    What matters in the answer is that I addressed his or her issue. You just mentioned that this method of running tests is inefficient. That remark doesn't render my answer incorrect, so there shouldn't have been a downvote. – pkout Mar 24 '16 at 19:00
  • This should not be nessesary! Something is wrong if you have to define TEST_NAME – user1383029 Sep 22 '20 at 08:40
4

Are these Selenium tests, by any chance? This page says you need to define a TEST_NAME for your database, because for some reason the in-memory database is quite broken.

Joel Cross
  • 1,400
  • 15
  • 22
3

Personally, I'd neglected to run makemigrations before running my tests. The docs say migrate is automatically run for you on the test database, but you need to have run makemigrations to reflect the latest changes.

karuhanga
  • 3,010
  • 1
  • 27
  • 30
  • 1
    Thank you! Of course, I had to do this. By the way: I would be great to have a possibility to run it before testing "virtually". Otherwise you always have to create new migrations files when playing around with code and tests – user1383029 Sep 22 '20 at 08:40
0

I think 'manage.py test' starts with empty database, so there's no way how 'syncdb' can help you.

Also I'm not sure if it's typo just in your question, but settings parameter is called

--settings

and not:

--setting

btw. if you're importings settings in code, use import this way:

from django.conf import settings
yedpodtrzitko
  • 9,035
  • 2
  • 40
  • 42