0

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);
        }
    }
}
Community
  • 1
  • 1
Ankit Raman
  • 79
  • 1
  • 14
  • can you share the unit test code you have written so far? – Chetan Apr 01 '17 at 05:39
  • @ChetanRanpariya I am new in Testing and I have not written test cases for this.kindly suggest me. – Ankit Raman Apr 01 '17 at 10:10
  • I can not help you with the readymade solution for this. You can visit https://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/07/27/getting-started-with-net-unit-testing-using-nunit.aspx and https://www.nunit.org/index.php?p=quickStart&r=2.2 to get idea of how to write unit tests using NUnit. – Chetan Apr 01 '17 at 23:22

1 Answers1

1

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.
Charlie
  • 12,928
  • 1
  • 27
  • 31