3

I have the following test code snippet

with self.assertRaises(models.NotEnoughInventorySpace):
    self.inv2.add_item(self.item2, 1)

The test fails:

Creating test database for alias 'default'...
E.
======================================================================
ERROR: test_limited_inventory (wotw_project.game.tests.TestInventory)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\...\wotw\wotw_project\..\wotw_project\game\tests.py", line 34, in test_limited_inventory
    self.inv2.add_item(self.item2, 1)
  File "D:\...\wotw\wotw_project\..\wotw_project\game\models.py", line 460, in add_item
    raise NotEnoughInventorySpace(self)
NotEnoughInventorySpace: There is not enough space in the inventory 'Inventory: 2'

----------------------------------------------------------------------
Ran 2 tests in 0.015s

FAILED (errors=1)
Destroying test database for alias 'default'...
Finished "D:\...\wotw\wotw_project\manage.py test game" execution.

I do not see why the exception raised is not identical to the one I pass into assertRaises

I run the code using manage.py test game with uses the tests.py file next to my models.py file in wotw_project.game.

My research into this problem shows that there may be a problem with imports (ie. the main file is different to the same file imported). However I am using the standard method to run the unittest so I do not see why this is a problem for only me, surely other people have used assertRaises with Django!

QasimK
  • 440
  • 3
  • 11
  • 1
    possibly related: http://stackoverflow.com/questions/549677/django-unittest-assertraise-for-a-custome-error – Ofri Raviv Sep 11 '11 at 23:05
  • @Ofri Yes that solved the import problem, using `from project.app import models`. It is unfortunate that I can't mark comments as being the answer. I also seemed to have missed this already-asked-question when I was trying to search for the solutions! (Maybe you should put this as an answer so I can tick it.) – QasimK Sep 12 '11 at 11:54
  • Maybe this post will help. It explains in a bit more nuance, the assertRaises funtion: http://stackoverflow.com/questions/1274047/why-isnt-assertraises-catching-my-attribute-error-using-python-unittest – Adam Hammouda Dec 22 '11 at 22:57

1 Answers1

-1

The problem is not with assertRaises, the problem will be that your exception class has been imported differently in your product code than in your test code.

Double check your imports. Make sure the name is imported from the same module in both cases. Frequently in Django projects, the same module is importable through different names, for example because the directory is reachable from two entries in the Python path.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
  • What is the difference between using from `project.app import models` versus from `app import models`? Using the former works just fine. Also note, in my product code the exception class is not imported as it is in `project.app.models`, it is only imported in the test code. – QasimK Sep 12 '11 at 18:56