Tests not passing in suite but pass individually

107 views
Skip to first unread message

Siddhi Divekar

unread,
Dec 2, 2015, 3:01:31 AM12/2/15
to Django users
Hi,

Am seeing that test case in suite are failing but passing when ran individually.
I have gone through most of the thread on the internet but did not find any solution.

Below is the snip of what am trying to do.

class A(TestCase):
  def test_a():
   create an obj in db
   retrive obj list from db and check the length of the list (should be 1)
   update same obj in db
   delte same obj in db

 def b():
  create an obj in db and check the length.

When ran in suite test_a fails as the length of the list is 2.
test_b() runs before test_a and leave the object in the database.

From various threads suggested using 'django.test import TestCase' 
instead of 'from django.unittest import TestCase' which am already doing.

Is there anything else i need to do here ?

Tim Graham

unread,
Dec 2, 2015, 9:28:06 AM12/2/15
to Django users
It will be easier to help if you can provide a sample project that reproduces the error.

Bill Freeman

unread,
Dec 2, 2015, 10:56:26 AM12/2/15
to django-users
Make test b clean up after itself, by deleting the test object.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f8ad70a1-4abf-406b-8668-a6d860a868bb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Siddhi Divekar

unread,
Dec 2, 2015, 11:20:27 AM12/2/15
to Django users
Hi Tim,

Below is what am trying to achieve.

class OrcaTestCase(TestCase):

    def test_customer_create_modify_delete(self):
        '''Test customer object create, modify and delete operations in  DB.'''
        # Create.
        CustomerDb.objects.create(c_name='Pnc', c_role='ADFS-Admin',
                                  c_date_created=timezone.now(),
                                  c_date_updated=timezone.now())
        customer_list = CustomerDb.objects.all()
        self.assertEqual(len(customer_list), 1)

        # Modify.
        customer = CustomerDb.objects.get(c_name='Pnc')
        self.assertNotEqual(customer, None)
        setattr(customer, 'c_name', 'Zoo')
        customer.save()
        customer_list = CustomerDb.objects.all()
        self.assertEqual(len(customer_list), 1)
        self.assertEqual(str(customer_list[0]), 'Gap')

        # Delete.
        customer = CustomerDb.objects.get(c_name='foo')
        self.assertNotEqual(customer, None)
        customer.delete()
        customer_list = CustomerDb.objects.all()
        self.assertEqual(len(customer_list), 0)

    def test_create_customer(self):
        '''Handle customer create.'''
        customer_list = CustomerDb.objects.all()
        self.assertEqual(len(customer_list), 1)

test_create_customer runs first, test_customer_create_modify_delete fails at the highlighted line.

Siddhi Divekar

unread,
Dec 2, 2015, 11:21:26 AM12/2/15
to Django users
Hi Ke1g,
That is the last option but
wanted to understand why database was not cleaned after first test.

Bill Freeman

unread,
Dec 2, 2015, 11:57:24 AM12/2/15
to django-users
Did you see some documentation that said that the test framework will clear the database?

I'm not sure that it's reasonable to ask a test framework to do that, given the number of possible databases and interface layers, though it is conceivable that django's variation on test could take care of this for the ORM.

Still, explicit is better than implicit.

A common way to clean up, by the way, is to start a transaction in setUp() and do a rollback in tearDown().

Tim Graham

unread,
Dec 2, 2015, 12:14:54 PM12/2/15
to Django users
How does the test fail? Please show the entire test file including imports.

Tom Evans

unread,
Dec 2, 2015, 12:28:11 PM12/2/15
to django...@googlegroups.com
You really really do not need to be testing that Django's CRUD
operations work - there is no value in doing so, since Django is well
tested.

Tests should concentrate on the custom business logic that your app
uses, not that when you create an object in the database, an object
exists in the database.

As to your question, what DB backend are you using for tests? Each
django.test.TestCase test is run inside a transaction that is rolled
back[1]; if your backend "supports" transactions by ignoring them
(cough, MySQL/MyISAM), then nothing happens when the transaction is
rolled back.

Cheers

Tom

[1] https://docs.djangoproject.com/en/1.9/topics/testing/overview/#writing-tests

learn django

unread,
Dec 2, 2015, 6:29:07 PM12/2/15
to Django users
Below is the test file & logs.
No matter in which order (using --reverse) I run the suite it fails.
The database backend is postgres. Is that an issue ?

File:-
===
import datetime
import pdb

from rest_framework import status
from rest_framework.test import APIClient

from django.http import HttpRequest
from django.contrib.auth.models import User
from django.utils import timezone
from django.test import TestCase

from orca.models import *

import orca.handlers.customer_handler as ch

# Create your tests here.
class OrcaTestCase(TestCase):

    def test_customer_create_modify_delete(self):
        '''Test customer object create, modify and delete operations in  DB.'''
        # Create.
        CustomerDb.objects.create(c_name='Zoo', c_role='Admin',
                                  c_date_created=timezone.now(),
                                  c_date_updated=timezone.now())
        customer_list = CustomerDb.objects.all()
        self.assertEqual(len(customer_list), 1)

    def test_launch(self):
        '''Test launch.'''
        CustomerDb.objects.create(c_name='Foo', c_role='Admin',
                                  c_date_created=timezone.now(),
                                  c_date_updated=timezone.now())
        customer_list = CustomerDb.objects.all()
        self.assertEqual(len(customer_list), 1)


Logs:-
====
======================================================================
FAIL: test_customer_create_modify_delete (orca.tests.tmp.OrcaTestCase)
Test customer object create, modify and delete operations in  DB.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/sidhesh/workspace/sztp/orca/tests/tmp.py", line 26, in test_customer_create_modify_delete
    self.assertEqual(len(customer_list), 1)
AssertionError: 2 != 1

----------------------------------------------------------------------
Ran 2 tests in 0.014s

FAILED (failures=1)

Tim Graham

unread,
Dec 2, 2015, 6:33:30 PM12/2/15
to Django users
It looks correct. I'd like a minimal project I could download to reproduce the issue. In putting that together, you might discover the reason for the failure.

learn django

unread,
Dec 2, 2015, 7:18:25 PM12/2/15
to Django users
Will try to send minimal project by end of the day.
Hopefully I can discover the reason during this exercise.

learn django

unread,
Dec 8, 2015, 8:30:05 PM12/8/15
to Django users
Hi Tim,

My test case was working with new app in new project which was
using default database.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

So I went back to my old app and debugged a bit.
If I make following changes in Database dictionary (commenting the bolded test) and
make my router for this app to point to default database the tests pass.
Is it because am using postgres that this issue is seen ?

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        #'ENGINE': 'django.db.backends.postgresql_psycopg2',
        #'USER': 'postgres',
        #'NAME': 'testdb',
        # 'PASSWORD':'v1ptela0212',
        # 'HOST':'localhost',
        # 'PORT':'',
        #'TEST': {
        #    'NAME': 'testdb',
        #},
    },
    'sztp': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'sztpdb',
        'USER':'postgres',
    },
    'orca': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'orcadb',
        'USER':'postgres',
    }
}

Tim Graham

unread,
Dec 9, 2015, 7:44:14 AM12/9/15
to Django users
It seems a bit suspicious that you're using the same database name for ['default']['NAME'] and ['default']['TEST']['NAME'] -- is that intentional?

learn django

unread,
Dec 9, 2015, 2:33:20 PM12/9/15
to Django users
After making following change & pointing router to 'default' database test case is passing.

    'default': {
        # 'ENGINE': 'django.db.backends.sqlite3',
        # 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'USER': 'postgres',
        'NAME': 'mydb',
        'PASSWORD':'0212',
        'HOST':'localhost',
        'PORT':'',
        'TEST': {
           'NAME': 'mytestdb',
        },
    },

However if I make router to point to app specific db which is defined as follows,
the test case still fails. I think its better not to keep on modifying the router.

Is there anything wrong with the app database definition below.

    'orca': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'orcadb',
        'USER':'postgres',
        'PASSWORD':'0212',
        'HOST':'localhost',
        'PORT':'',
        'TEST': {
           'NAME': 'orcatestdb',
        },
    }

Tim Graham

unread,
Dec 10, 2015, 4:16:56 PM12/10/15
to Django users
I didn't see any indication in your previous posts that you were using database routers. That's why I continue to ask for a sample project - it's difficult to debug problems without seeing the entire picture.
Reply all
Reply to author
Forward
0 new messages