Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any point Unit testing serialization?

I have a class that serializes a set of objects (using XML serialization) that I want to unit test.

My problem is it feels like I will be testing the .NET implementation of XML serialization, instead of anything useful. I also have a slight chicken and egg scenario where in order to test the Reader, I will need a file produced by the Writer to do so.

I think the questions (there's 3 but they all relate) I'm ultimately looking for feedback on are:

  1. Is it possible to test the Writer, without using the Reader?
  2. What is the best strategy for testing the reader (XML file? Mocking with record/playback)? Is it the case that all you will really be doing is testing property values of the objects that have been deserialized?
  3. What is the best strategy for testing the writer!

Background info on Xml serialization

I'm not using a schema, so all XML elements and attributes match the objects' properties. As there is no schema, tags/attributes which do not match those found in properties of each object, are simply ignored by the XmlSerializer (so the property's value is null or default). Here is an example

<MyObject Height="300">
    <Name>Bob</Name>
    <Age>20</Age>
<MyObject>

would map to

public class MyObject
{
  public string Name { get;set; }
  public int Age { get;set; }

  [XmlAttribute]
  public int Height { get;set; }
}

and visa versa. If the object changed to the below the XML would still deserialize succesfully, but FirstName would be blank.

public class MyObject
{
  public string FirstName { get;set; }
  public int Age { get;set; }

  [XmlAttribute]
  public int Height { get;set; }
}

An invalid XML file would deserialize correctly, therefore the unit test would pass unless you ran assertions on the values of the MyObject.

like image 654
Chris S Avatar asked Jul 09 '09 08:07

Chris S


People also ask

Should you unit test serialization?

Whenever you write a class that is designed to be serialized using XML serialization (or any serialization for that matter), you should make sure you have some unit tests to ensure the data is persisted and retrieved as expected.

Is unit testing always necessary?

Unit tests are also especially useful when it comes to refactoring or re-writing a piece a code. If you have good unit tests coverage, you can refactor with confidence. Without unit tests, it is often hard to ensure the you didn't break anything.

How many types of serialization that are commonly used?

There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization.

Why unit testing is mandatory?

Unit testing ensures that all code meets quality standards before it's deployed. This ensures a reliable engineering environment where quality is paramount. Over the course of the product development life cycle, unit testing saves time and money, and helps developers write better code, more efficiently.


1 Answers

Do you need to be able to do backward compatibility? If so, it may be worth building up unit tests of files produced by old versions which should still be able to be deserialized by new versions.

Other than that, if you ever introduce anything "interesting" it may be worth a unit test to just check you can serialize and deserialize just to make sure you're not doing something funky with a readonly property etc.

like image 108
Jon Skeet Avatar answered Sep 25 '22 06:09

Jon Skeet