Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use assertRaises in Django unit testing correctly?

the question is pretty straightforward: I can't use self.assertRaises in a Django 1.5 unit test.

I'd really like to verify that when you try to delete a provider from a database who doesn't exist, a LookupError is raised.

As you can see, I have a ProviderDAO class, where I define how providers are to be deleted. Then I simply call this method, delete_provider(), in the unit test class ProviderManagerTests. I've outlined all the possibilities in the code, and at this point I've come here for answers (I've read the suggestions in related questions but nothing is doing the trick) I'm using python 2.7.5 Thanks!

class ProviderManager(models.Manager):
   def delete_provider(self, provider_id):
        matching_providers = Provider.objects.filter(providerId=provider_id)
        print 'delete provider method matching providers %s ' % matching_providers
        print 'delete provider method matching providers %s ' % matching_providers.count()
        if matching_providers.count() > 0:
            matching_providers.delete()
        elif matching_providers.count() == 0:
            raise LookupError('No deletion; no providers matching this id')

class ProviderManagerTests(TestCase):
    def test_delete_nonexistent_provider(self):
        provider_manager = ProviderManager()
        someRoles = Role(name='boss')
        someRoles.save()
        someSpecialties = Specialty(name='senior care')
        someSpecialties.save()
        provider_manager.delete_provider('398236503462063')
        #self.assertIsNone(provider,'provider should be none')
        #self.assertTrue(Provider.objects.filter(providerId='123456') == 0)
        ###self.assertRaises(LookupError,provider_manager.delete_provider, providerId='398236503462063')
        #self.assertRaises(LookupError,provider_manager.delete_provider, provider_id='398236503462063')
        #self.assertRaises(LookupError,lambda: provider_manager.delete_provider,provider_id='234234234')
        #self.assertRaises(LookupError,lambda: provider_manager.delete_provider(provider_id='234234234'))
        # with self.assertRaises(LookupError):
        #     provider_manager.delete_provider(provider_id='398236503462063')

        with self.assertRaises(LookupError):
            provider_manager.delete_provider('398236503462063')

and the stack track:

======================================================================
ERROR: test_delete_nonexistent_provider (edu.tests.ProviderTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/tests.py", line 68, in test_delete_nonexistent_provider
    provider_manager.delete_provider('398236503462063')
  File "/Users/models.py", line 92, in delete_provider
    raise LookupError('No deletion; no providers matching this id')
LookupError: No deletion; no providers matching this id

----------------------------------------------------------------------
Ran 14 tests in 0.306s

FAILED (errors=1)
like image 926
user798719 Avatar asked Jul 03 '13 05:07

user798719


1 Answers

By the way, you could write this way:

self.assertRaises(LookupError, provider_manager.delete_provider, '398236503462063')
like image 169
vinyll Avatar answered Oct 25 '22 11:10

vinyll