Java Jester

Wednesday, November 26, 2008

Disadvantage of Integration Testing

The past few months, I've been a very big fan of integration testing because It removes the process where I have to create mock objects for each dao or services and the creation of object and all of its nested objects.

While at work, I realized that integration tests that uses or retrieves an entity from database first, couples the test to the developers database. After getting my test to work on my machine and then move it to another computer with a different database, I'll get a different record or even retrieve a non-existing record which will definitely cause serious problem.

I guess this is the part where unit test becomes useful but the work will be a bit longer since i need to create mock objects, objects and all nested objects.

 

Saturday, October 18, 2008

Unit Testing Django Component Example

At the moment I dont know how to test django components from eclipse so I'm running my test on the terminal window. To test your django components, create a tests.py file on your application folder. For example my myapp/tests.py looks like the following:


from myapp.models import *
from myapp.forms import *
from mypp import log
import unittest


class CountryTestCase(unittest.TestCase):
    """
    Test case for country model
    """
    
    def setUp(self):
        log.debug('Running CountryTestCase')
      
    def testCountCountries(self):
        allquery = Country.objects.all()
        self.assertTrue(allquery.count() > 0)
       
       
class RegistrationTestCase(unittest.TestCase):
    """
    Test case for user registration
    """
   
    def setUp(self):
        user = User(email='reyjexter@gmail.com', username='reyjexter@gmail.com', is_superuser=0,is_staff=0, is_active=1)
        user.save()
       
        user = User(email='core_reyj@yahoo.com', username='core_reyj@yahoo.com', is_superuser=0,is_staff=0, is_active=0)
        user.save()  
       
    def tearDown(self):
        userquery = User.objects.all()
        for user in userquery:
            user.delete()
       
    def testFailedRegistration(self):
        postdata = {
            'email': 'reyjexter@gmail.com',
            'password': 'rey123',
            'confirm_password': 'rey123',           
        }
        form = UserRegistrationForm(postdata)
        self.assertFalse(form.is_valid())
       
        postdata = {
            'email': '',
            'password': 'rey123',
            'confirm_password': 'rey123',           
        }
        form = UserRegistrationForm(postdata)
        self.assertFalse(form.is_valid())
       
        postdata = {
            'email': 'rey@ideyatech.com',
            'password': 'rey123',
            'confirm_password': 'rey1234',           
        }
        form = UserRegistrationForm(postdata)
        self.assertFalse(form.is_valid())
       
    def testSuccessRegistration(self):
        postdata = {
            'email': 'rey@ideyatech.com',
            'password': 'rey123',
            'confirm_password': 'rey123',           
        }
        form = UserRegistrationForm(postdata)
        self.assertTrue(form.is_valid())
       
        countBeforeInsert = User.objects.all().count()
        form.save()
        self.assertTrue(User.objects.all().count() == countBeforeInsert+1)
        

To execute the test simple run:

./manage.py test myapp

Or to run individual test, run this one:

./manage.py test myapp.RegistrationTestCase

 


recommended books
Jester Cap Logo by Vonn reyjexter.com | Valid XHTML & CSS | 2008 | Design & Devt. by: vonnhugo

powered by:

django framework python google app engine