mardi 31 mars 2015

Python properties and unittest TestCase

Today I wrote test and typoed in one of test methods. My tests failed but I don't understand why. Is it special behaviour of Python properties or something else?



from unittest import TestCase


class FailObject(object):
def __init__(self):
super(FailObject, self).__init__()
self.__action = None

@property
def action(self):
return self.__action

@action.setter
def action(self, value):
self.__action = value


def do_some_work(fcells, fvalues, action, value):
currentFailObject = FailObject()
rects = [currentFailObject]
return rects


class TestModiAction(TestCase):
def testSetFailObjectAction(self):
rect = FailObject # IMPORTANT PART
rect.action = "SOME_ACTION" # No fail!
self.assertEquals("SOME_ACTION", rect.action)

def testSimple(self):
fcells = []
fvalues = []
rects = do_some_work(fcells, fvalues, 'act', 0.56)

rect = rects[0]
self.assertEquals('act', rect.action)


When I run this testcase with nose tests:



.F
======================================================================
FAIL: testSimple (test.ufsim.office.core.ui.cubeeditor.TestProperty.TestModiAction)
----------------------------------------------------------------------
Traceback (most recent call last):
File "TestProperty.py", line 36, in testSimple
self.assertEquals('act', rect.action)
AssertionError: 'act' != 'SOME_ACTION'

----------------------------------------------------------------------
Ran 2 tests in 0.022s

FAILED (failures=1)


If I fix typo with instance creation in testSetFailObjectAction all tests are work as expected. But this example turn me back to question: Is it safe to use properties? What if I will typo again some day?


Aucun commentaire:

Enregistrer un commentaire