简体   繁体   中英

How to Perform Unit testing for Serialization and Deserialization of a class in C#?

I want to perform NUnit or MS tests on a class for serialized and deserialized behavior.

I looked at another stackoverflow article here , but I still don't understand how to do this. Please help me to understand how to perform these tests,

Below is my part of code:

namespace PMT.Service.Common.DataContract
{
    public partial class MyBankInfo
    {
        public string MyId { get; set; }
        public string MyAccountNumber { get; set; }
        public string MyAccountType { get; set; }
        public string MyBankName { get; set; }
        public string MyBankBranchName { get; set; }
        public string MyBankCity { get; set; }
        public string MyBankCityPincode { get; set; }
        public string MyBankIFSCCode { get; set; }

        public void Serialize(BinaryStreamWriter binaryStreamWriter)
        {           
            binaryStreamWriter.Write(MyId);
            binaryStreamWriter.Write(MyAccountNumber);
            binaryStreamWriter.Write(MyAccountType);
            binaryStreamWriter.Write(MyBankName);
            binaryStreamWriter.Write(MyBankBranchName);
            binaryStreamWriter.Write(MyBankCity);
            binaryStreamWriter.Write(MyBankCityPincode);
            binaryStreamWriter.Write(MyBankIFSCCode);
        }

        public bool Deserialize(BinaryStreamReader binaryStreamReader,out   string errorString)
        {
            errorString = string.Empty;
            try
            {
                MyId = binaryStreamReader.ReadString();
                MyAccountNumber = binaryStreamReader.ReadString();
                MyAccountType = binaryStreamReader.ReadString();
                MyBankName = binaryStreamReader.ReadString();
                MyBankBranchName = binaryStreamReader.ReadString();
                MyBankCity = binaryStreamReader.ReadString();
                MyBankCityPincode = binaryStreamReader.ReadString();
                MyBankIFSCCode = binaryStreamReader.ReadString();

            }
            catch (Exception ex)
            {
                errorString = ex.Message;
            }
            return string.IsNullOrEmpty(errorString);
        }
    }
}

There are two ways to test serialization and deserialization: separately or both together.

Separate tests are best if the serialized data is created by or used by some other software that you don't have control over. In that case, exact formats have to be verified. This is also the hard way.

If your data is only serialized and deserialized by your own class, then you can test both at once:

  1. Create a test object
  2. Create a Writer in memory or backed on disk.
  3. Serialize to that writer.
  4. Create a second object and deserialize it from the saved data.
  5. Write a bunch of assertions that compare the original object's properties to the new object's.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM