From: Mark G. <mar...@gm...> - 2006-12-14 21:37:33
|
Actually, what you implemented is a comparison method, which you choose to call assert_equal(). In the xUnit world assert (in every modification) is a method that immediately throws exception when asserted condition fails. xUnits are Unit Test tools (not that you are prevented from forcing it into something different:) ). Pure terminology, you'll say? You are right! But important one. Assert stop= s execution on the failure and unrolls return stack to the error processing point. I think this should be kept in mind - in order not to distort this vision created by our peers.No offense. Sorry for late response - was busy grokking new job. On 12/6/06, Desilets, Alain <Ala...@nr...> wrote: > > Actually, I went ahead and implemented a recursive assert_equal() method. > It works pretty well. It basically deals with all the primitive types (in= ts, > floats, strings, etc), all collections (lists, tuples, dicts) and arbitra= ry > 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...] > 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 sav= e > 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 > that parses your dictionaries and checks each value with an assert - yuck= s! > > 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 > > > > > -- > Mark Geyzer > Software Engineer, > tel: 972-52-6782603 > --=20 Mark Geyzer Software Engineer, tel: 972-52-6782603 |