简体   繁体   English

如何在C#中对类的序列化和反序列化执行单元测试?

[英]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. 我想对一个类执行NUnit或MS测试,以进行序列化和反序列化的行为。

I looked at another stackoverflow article here , but I still don't understand how to do this. 我在这里看了另一篇stackoverflow文章,但是我仍然不知道如何做到这一点。 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. 在内存中或磁盘上创建Writer。
  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. 编写一堆断言,将原始对象的属性与新对象的属性进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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