Re: [Pyunit-interest] Recursive assert_equals.
Brought to you by:
purcell
From: Desilets, A. <Ala...@nr...> - 2006-12-06 14:05:36
|
Actually, I went ahead and implemented a recursive assert_equal() = method. It works pretty well. It basically deals with all the primitive = types (ints, floats, strings, etc), all collections (lists, tuples, = dicts) and arbitrary objects. When a difference is found, it provides helpful messages that help you = drill down from the top to the bottom where the difference was found. Alain -----Original Message----- From: Mark Geyzer [mailto:mar...@gm...]=20 Sent: Tuesday, December 05, 2006 1:54 AM To: Desilets, Alain Cc: pyu...@li... Subject: Re: [Pyunit-interest] Recursive assert_equals. You may try to convert your dictionaries to strings with "str" or "repr" = function, and then call assert method on resulting strings. This will = save you the hassle of implementing recursive assert, but then - in case = of failure - you'll have top find the guilty field manually.=20 If you are not happy with that approach - you'll have to write a method = that parses your dictionaries and checks each value with an assert - = yucks! BTW, if you are not aware - failed assert INTERRUPTS the test, so that = if you have more than field with different values - it will throw = exception on the first one checked.=20 PyUnit is about FrameWork for your tests, that's all; alas, it is not = about checking complicated data structures. Hope, it helps On 11/29/06, Desilets, Alain <Ala...@nr...> wrote: Is there a way to do a recursive assert_equals on a complex data = structure? For example, say I want to assert that two dictionary are "equal". The = keys of the dictionary are strings, and the values are objects of type = SomeClass, which, among other things, have an attrigute called = some_attribute which is of type SomeOtherClass.=20 I want to write: Self.assert_equal_recusrsive(expected_dict, got_dict) And have the system do the following: - Make sure that expected_dict and got_dict are both of the same type (a = dictionary in this case)=20 - Make sure that they have the same string keys - For each string key, make sure that the associated objects are of the = same type (SomeClass), have the same attributes with same values. - In particular for the some_attribute attribute, it would make sure = that the values are also of the same type (SomeOtherClass), and that = they have the same attribute values pairs.=20 Has this been written before? If not would it be doble (it seems to me = it should be). I know that in jUnit (Java version), this kind of recursive comparison = is supported to at least some extend. For example, if I compare two = dictionnary objects, the system will make sure that the keys are the = same and the values too. But it doesn't go recursively into the = attributes of objects AFAIK.=20 ---- Alain D=E9silets, MASc Agent de recherches/Research Officer Institut de technologie de l'information du CNRC / NRC Institute for Information Technology ala...@nr... T=E9l/Tel (613) 990-2813 Facsimile/t=E9l=E9copieur: (613) 952-7151 Conseil national de recherches Canada, M50, 1200 chemin Montr=E9al, Ottawa (Ontario) K1A 0R6 National Research Council Canada, M50, 1200 Montreal Rd., Ottawa, ON=20 K1A 0R6 Gouvernement du Canada | Government of Canada -------------------------------------------------------------------------= Take Surveys. Earn Cash. Influence the Future of IT Join SourceForge.net 's Techsay panel and you'll get the chance to share = your opinions on IT & business topics through brief surveys - and earn cash http://www.techsay.com/default.php?page=3Djoin.php&p=3Dsourceforge&CID=3D= DEVDEV _______________________________________________ Pyunit-interest mailing list Pyu...@li... https://lists.sourceforge.net/lists/listinfo/pyunit-interest --=20 Mark Geyzer Software Engineer, tel: 972-52-6782603=20 |