Search code examples
pythonunit-testingpropertiesvariable-assignmentassertraises

Testing failure of an assignment with unittest


One of my attributes is a property where the setter calls a validation function that raises an exception if the new value is invalid:

pos.offset = 0
# @offset.setter calls validate(offset=0)
# PositionError: Offset may not be 0.

I'm trying to add a test to ensure that this fails. However, I can't figure out how to get assertRaises to work with an assignment.

The normal syntax of assertRaises requires a method, not an attribute/property:

self.assertRaises(PositionError, pos.offset, 0)
# TypeError: 'int' object is not callable

The other forms I've tried are invalid Python:

self.assertRaises(PositionError, pos.offset = 0)
# SyntaxError: Keyword can't be an expression
self.assertRaises(PositionError, lambda: pos.offset = 0)
# SyntaxError: lambda cannot contain assignment

How do I test failure of assignment to a property?

Note: Python 2.6, I know unittest has some new features in 2.7


Solution

  • When you want to use unittest to test that an exception occurs in a block of code rather than just a function call, you can use assertRaises as a context manager:

    with self.assertRaises(PositionError):
        pos.offset = 0
    

    This use can be found in the unittest docs.

    While this is not available in Python 2.6, and won't work for you as such (I just saw your note), I think it's worth including among the answers, as it's probably the clearer way to do this for python 2.7+.