首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C#如何对下面的代码进行单元测试

2012-05-29 
C#怎么对下面的代码进行单元测试方法如下为测试的代码:private void Sort(int[] list){int tempfor (int

C#怎么对下面的代码进行单元测试
方法如下为测试的代码:
private void Sort(int[] list)
  {
  int temp;
  for (int i = 0; i < 10; i++)
  {
  for (int j = 0; j < 9 - i; j++)
  {
  if (list[j] > list[j + 1])
  {
  temp = list[j];
  list[j] = list[j + 1];
  list[j + 1] = temp;
  }
  }
  }
  }
下面为实现:
private void Form1_Load(object sender, EventArgs e)
  {  
  int[] s = new int[] { 2, 1, 3, 5, 4, 77, 7, 8, 69, 9 };
  Sort(s);
  for (int m = 0; m < 10; m++)
  {
  label1.Text = label1.Text + " " + s[m].ToString();
  }
  }

[解决办法]
你可以直接在Form1_Load中写断言,例如

C# code
private void Form1_Load(object sender, EventArgs e)  {     int[] s = new int[] { 2, 1, 3, 5, 4, 77, 7, 8, 69, 9 };  Sort(s);  Debug.Assert(s.Length==10);  Debug.Assert(s[2]==3);  Debug.Assert(s[9]==77);  for (int m = 0; m < 10; m++)  {  label1.Text = label1.Text + " " + s[m].ToString();  }  } 

热点排行