Geeks With Blogs


Geekus Con Livus Malcolm Anderson's home for Geeks With Lives

This one is going to wander a bit, just a warning.  Ok,  You’ve been warned.




So one of the things about being in exile out here in the middle of the Atlantic is that I’m getting a chance to catch up on my DotNetRocks listening.  Right now I’m about 15 months behind (That would be October 2004ish) and just got done listening to show 86 with Juval Lowy.  (As a side note, its interesting to notice that so far, I don’t think there is a single show that should be marked “obsolete” as in “no longer relevant.”  The content from 3 and a half years ago old is still as useful today as it was when it first came out.)


Anyway, back to show 86 from Oct 22nd, 2004.  In it, Juval Lowy spent a good bit of time talking about Serialization.  Now it just so happens, I am currently in a spot where being able to save object state in the middle of a run is exactly what I need. 


I’ve got my unit tests broken into 2 groups: Unit Tests, and Integration Tests.  For those of you who don’t know, Unit Tests that are running a tenth of a second or longer, are officially, “way too slow”, one hundredth of a second is about right, but I’ll tolerate up to about 4 hundredths of a second (on a 2.8 gig machine)  Anything that is going to take more than a 50th of a second is being moved over to the Integration Test tree.  (Structural note, a suite of 100 unit tests that takes a second or 2 to run WILL be run every couple of minutes, a suite of 100 tests that takes 10 seconds to run will be run once an hour or so, losing one of the real values of TDD, finding side effects early.)


Now for the kicker.  One of my tests using production-like data runs for about 11 minutes.  That “oooooof” sound was me, the first time I ran the thing.     That’s 10.5 minutes in setup() routine, .5 - 1 seconds in each of my 13 integration tests.  I quickly learned about the [TestFixture] attribute.


How could I save the state of my objects AFTER the 10.5 minute routine, but before the 7 seconds worth of testing?


Enter Carl, Rory and Juval.  I got to hear how, “Serialization in Visual Studio 2003 missed the mark by a bit, but they got it right in 2005.” 


Being a self professed Mort, I wanted to find a quick walk though, so that I could get it done, and if I happened to learn any theory along the way, it was a bonus, I really just wanted to shorten my test cycle.  Let me tell you, what I learned was how difficult it was to find any quick walkthroughs on the subject.  I’m not even sure I know anymore what I used in google,  I just know that “System.Runtime.Serialization.Formatters.Binary” was part of the query.


<edit> ok, this worked in google

Juval Lowy System.Runtime.Serialization.Formatters.Binary visual studio 2005

<end edit>


Long story short, my 10 minute run is now a 20 second load (70 megs worth of data) which is switched by commenting or uncommenting a single “#define GENERATE_NEW” line.


There were a few gotcha’s, like if you have a class(A), that inherits from a base class(B) which contains a structure(B.C) then all three (including the internal structure in class B) must be marked with the [Serializable] attribute.


If enough people ask about it (that would probably be “one” at this moment) I can put up some mock code for what my setup looks like.


Oh, and as a side note, to Tuesday's post.  Tradition is a VERY powerful invisible structure that, “just works.”


Posted on Thursday, January 26, 2006 12:31 AM Agile Development , Stupid Code Tricks | Back to top

Comments on this post: Serialization and Unit Testing

# re: Serialization and Unit Testing
Requesting Gravatar...
I am interested in the mock code. I stumbles your blog by accident. I understand that you are giving a presentation in Portland about unit testing for functions with large and complex input arguments. Althouth I will not be able to attend the session I am intersted in the problem. I work in an organization that is becoming interested in adding unit testing to a lot of old code, C++, ATL, COM and now C#. This along with extending the functionality of the product. The mock code would be helpful. Thanks
Left by Andrew Wheeler on Jul 11, 2006 5:53 AM

Your comment:
 (will show your gravatar)

Copyright © Malcolm Anderson | Powered by: