Is deserializing complex objects instead of creating them a good idea, in test setup?

Posted by Chris Bye on Programmers See other posts from Programmers or by Chris Bye
Published on 2012-09-07T20:34:15Z Indexed on 2012/09/07 21:50 UTC
Read the original article Hit count: 243

I'm writing tests for a component that takes very complex objects as input. These tests are mixes of tests against already existing components, and test-first tests for new features.

Instead of re-creating my input objects (this would be a large chunk of code) or reading one from our data store, I had the thought to serialize a live instance of one of these objects, and just deserialize it into test setup.

I can't decide if this is a reasonable idea that will save effort in long run, or whether it's the worst idea that I've ever had, causing those that will maintain this code will hunt me down as soon as they read it.

Is deserialization of inputs a valid means of test setup in some cases?

To give a sense of scale of what I'm dealing with, the size of serialization output for one of these input objects is 93KB. Obtained by, in C#:

new BinaryFormatter().Serialize((Stream)fileStream, myObject);

© Programmers or respective owner

Related posts about programming-practices

Related posts about testing