Wednesday, December 2, 2015

Re: Tests not passing in suite but pass individually

Will try to send minimal project by end of the day.
Hopefully I can discover the reason during this exercise.

On Wednesday, December 2, 2015 at 3:33:30 PM UTC-8, Tim Graham wrote:
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.

On Wednesday, December 2, 2015 at 6:29:07 PM UTC-5, learn django wrote:
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)



On Wednesday, December 2, 2015 at 9:14:54 AM UTC-8, Tim Graham wrote:
How does the test fail? Please show the entire test file including imports.

On Wednesday, December 2, 2015 at 11:20:27 AM UTC-5, learn django wrote:
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.

On Wednesday, December 2, 2015 at 6:28:06 AM UTC-8, Tim Graham wrote:
It will be easier to help if you can provide a sample project that reproduces the error.

On Wednesday, December 2, 2015 at 3:01:31 AM UTC-5, Siddhi Divekar wrote:
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 ?

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/609a7678-f20b-46f1-8282-461200392d84%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment