Re: [Pyunit-interest] Recursive assert_equals.
Brought to you by:
purcell
From: Mark G. <mar...@gm...> - 2006-12-05 06:54:18
|
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. If you are not happy with that approach - you'll have to write a method tha= t 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. 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. > > 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) > - 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. > > 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 dictionn= ary > objects, the system will make sure that the keys are the same and the val= ues > too. But it doesn't go recursively into the attributes of objects AFAIK. > > > ---- > 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 > 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 |