Can I force unit testing of properties using the Visual Studio unit testing framework?
I am working on a C# 3.5 project using visual studio 2008, where i have several objects that need to be serialized (they implement IXmlSerializable). In writing unit tests for my serialization code, i would like a way of making sure that when i add a new property to my serializable objects, i will be notified if the serializing and deserializing of the new property is not being tested in any of my unit tes开发者_StackOverflow社区ts. (Via some failed unit test)
Is this possible? I would think that if there is a special attribute that i can mark my properties with, that would let the Visual Studio unit testing framework know that it needs to be tested.
Or is my approach to this completely off base? I could switch to a different unit testing framework if required.
I think I would try to write a better set of serialization tests that would use reflection to populate all the properties of an object with unique values, serialize it, deserialize, and compare the initial object with the deserialized one. Of course it might be tricky if you have non-trivial validation checks in your classes. On the other hand you might use known good values for known properties and random stuff for unknown properties - this will fail for nontrivial new properties - which is what you want. This way you test that serialization works after adding new properties (what is really your objective).
If the option above is not possible than you might try generating a coverage report (usually these are in xml format) and then write a simple tool to make sure that your classes are fully tested. I think it will be really difficult to get this to work and I am not sure it is worth the effort. As far as I know there is no better way to check if a property was called.
精彩评论