Is there any point Unit testing serialization?

后端 未结 13 945
慢半拍i
慢半拍i 2021-02-01 23:38

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

13条回答
  •  被撕碎了的回忆
    2021-02-02 00:14

    Yes, as long as what needs to be tested is properly tested, through a bit of intervention.

    The fact that you're serializing and deserializing in the first place means that you're probably exchanging data with the "outside world" -- the world outside the .NET serialization domain. Therefore, your tests should have an aspect that's outside this domain. It is not OK to test the Writer using the Reader, and vice versa.

    It's not only about whether you would just end up testing the .NET serialization/deserialization; you have to test your interface with the outside world -- that you can output XML in the expected format and that you can properly consume XML in the anticipated format.

    You should have static XML data that can be used to compare against serialization output and to use as input data for deserialization.

    Assume you give the job of note taking and reading the notes back to the same guy:

    You - Bob, I want you to jot down the following: "small yellow duck."
    Bob - OK, got it.
    You - Now, read it back to me.
    Bob - "small yellow duck"
    

    Now, what have we tested here? Can Bob really write? Did Bob even write anything or did he memorize the words? Can Bob actually read? -- his own handwriting? What about another person's handwriting? We don't have answers to any of these questions.

    Now let's introduce Alice to the picture:

    You - Bob, I want you to jot down the following: "small yellow duck."
    Bob - OK, got it.
    You - Alice, can you please check what Bob wrote?
    Alice - OK, he's got it.
    You - Alice, can you please jot down a few words?
    Alice - Done.
    You - Bob, can you please read them?
    Bob - "red fox"
    Alice - Yup, that sounds right.
    

    We now know, with certainty, that Bob can write and read properly -- as long as we can completely trust Alice. Static XML data (ideally tested against a schema) should sufficiently be trustworthy.

提交回复
热议问题