2017-03-31 61 views
0

我想对序列化和反序列化行为的类执行NUnit或MS测试。如何执行单元测试序列化和C#中的类的反序列化?

我看着另一个stackoverflow文章here,但我仍然不明白如何做到这一点。请帮助我了解如何执行这些测试,

下面是我的部分代码:

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); 
     } 
    } 
} 
+0

你可以分享到目前为止你写的单元测试代码吗? –

+0

@ChetanRanpariya我是新的测试,我没有写这个测试用例。 –

+0

我无法帮助您为此做好现成的解决方案。您可以访问https://www.infragistics.com/community/blogs/dhananjay_kumar/archive/2015/07/27/getting-started-with-net-unit-testing-using-nunit.aspx和https:// www .nu​​nit.org/index.php?p = quickStart&r = 2.2来了解如何使用NUnit编写单元测试。 –

回答

0

有两种方法来测试序列化和反序列化:单独或两者一起。

如果序列化数据是由您无法控制的其他软件创建或使用的,则单独测试是最好的。在这种情况下,必须验证确切的格式。这也是困难的方式。

如果您的数据仅是序列化,并通过自己的类反序列化,那么你可以一次测试两个:

  1. 创建一个测试对象
  2. 在内存中创建一个作家或备份在磁盘上。
  3. 序列化到该作者。
  4. 创建第二个对象并从保存的数据反序列化它。
  5. 编写一堆断言,将原始对象的属性与新对象的属性进行比较。