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);
}
自动测试
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);
}
自动测试
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货