Program.cs源码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using MessageBoxOriginal = System.Windows.Forms.MessageBox;namespace WindowsFormsApplication2
{
  static class Program
  {    public static bool bDEBUG = true;
    public static List<string> sLastMsgInfo = new List<string>();
    public static List<DialogResult> oExpectedDialogResult = new List<DialogResult>();    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }    /// <summary>
    /// 待测试的函数
    /// 先弹出一个 OK 提示框
    /// 再弹出一个 YES or NO 选择框
    /// </summary>
    /// <returns>选择 YES 返回 1,选择 NO 返回 2</returns>
    public static int aa()
    {
      MessageBox.Show(
        "OK",
        Application.ProductName,
        MessageBoxButtons.OK,
        MessageBoxIcon.Information);
      if (
        MessageBox.Show(
          "==1?",
          Application.ProductName,
          MessageBoxButtons.YesNo,
          MessageBoxIcon.Question) == DialogResult.Yes)
      {
        return 1;
      }
      else
      {
        return 2;
      }
    }
  }  /// <summary>
  /// 代替系统的 MessageBox
  /// 只完成我最常用的一种参数形式
  /// </summary>
  class MessageBox
  {
    public static DialogResult Show(string sInfo, string sTitle, MessageBoxButtons oMBB, MessageBoxIcon oMBI)
    {
      if (Program.bDEBUG)
      {
        // 在调试状态下
        // 记录下有一个提示
        Program.sLastMsgInfo.Add(sInfo);        // 并返回计划的对话框选择结果
        return Program.oExpectedDialogResult[Program.sLastMsgInfo.Count - 1];
      }
      else
      {
        // 在用户实际运行状态下,运行系统的 MessageBox.Show
        return System.Windows.Forms.MessageBox.Show(sInfo, sTitle, oMBB, oMBI);
      }
    }
  }
}
=============
对 aa 的两个测试用例
    /// <summary>
    /// aa 的测试1
    /// </summary>
    [TestMethod()]
    public void aaTest1()
    {
      Program.sLastMsgInfo.Clear();
      Program.oExpectedDialogResult.Clear();
      Program.oExpectedDialogResult.AddRange(
        new System.Windows.Forms.DialogResult[2] 
        { 
          System.Windows.Forms.DialogResult.OK,
          System.Windows.Forms.DialogResult.Yes
        });
      string[] expectedMsgInfo = new string[2] { "OK", "==1" };
      int expected = 1;
      int actual;
      actual = Program.aa();
      Assert.AreEqual(expectedMsgInfo[0], Program.sLastMsgInfo[0]);
      Assert.IsTrue(Program.sLastMsgInfo[1].IndexOf(expectedMsgInfo[1]) == 0);
      Assert.AreEqual(expected, actual);
    }    /// <summary>
    /// aa 的测试2
    /// </summary>
    [TestMethod()]
    public void aaTest2()
    {
      Program.sLastMsgInfo.Clear();
      Program.oExpectedDialogResult.Clear();
      Program.oExpectedDialogResult.AddRange(
        new System.Windows.Forms.DialogResult[2] 
        { 
          System.Windows.Forms.DialogResult.OK,
          System.Windows.Forms.DialogResult.No
        });
      string[] expectedMsgInfo = new string[2] { "OK", "==1" };
      int expected = 2;
      int actual;
      actual = Program.aa();
      Assert.AreEqual(expectedMsgInfo[0], Program.sLastMsgInfo[0]);
      Assert.IsTrue(Program.sLastMsgInfo[1].IndexOf(expectedMsgInfo[1]) == 0);
      Assert.AreEqual(expected, actual);
    }
自动测试

解决方案 »

  1.   

    这样写没问题,就事论事。但是推而广之的话,一定要注意Mock对象和真实对象的区别,不然测试就没有意义了。
      

  2.   

    一直对测试没什么研究,听说过自动化测试,曾经玩过jmeter,很少编写单元测试用例。但是自动化测试是非常重要的,也是很有帮助的,一下。
      

  3.   

    记住,软件开发没有银弹,有人指望“设计模式”能改善程序质量,有人指望“单元测试”,事实上没有任何先进的方法可以代替一个优秀的程序员。测试说到底也是程序,那么测试本身不可避免,也会有bug,而且测试和被测试的程序高度耦合,同时又是两套代码,这本身也存在问题。
      

  4.   

    经过半个多月的折腾,发现测试是“理想很丰满,现实很骨感”,测试代码写得比代码还多,测试到哪一层面不好把握,环境构建复杂,各类MOCK都很难理解。