在.NET2005里用Test做自动化测试(WinForm)
来源:互联网 发布:json date 格式化输出 编辑:程序博客网 时间:2024/05/01 20:28
如果您还不会在.NET2005创建Test工程的话,请您查看我的另一篇文章《在.NET2005里创建测试工程》。
下面我们就切入正题
第一:我们要创建一个WinForm的工程。
第二:写一段简单的小功能。
(描述一下功能:选择不同的按钮,就会显示出选择按钮上面的文字)
下面是具体的代码:
(Display.Designer.cs用来对窗体UI的一些设计,可以说是一些表现层的内容,系统自己生成的,不重要)
namespace ConsoleEnglist
...{
partial class Display
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
} Windows 窗体设计器生成的代码 /// #region Windows 窗体设计器生成的代码 /// <summary>
/**//// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btnA = new System.Windows.Forms.Button();
this.btnB = new System.Windows.Forms.Button();
this.btnC = new System.Windows.Forms.Button();
this.btnD = new System.Windows.Forms.Button();
this.btnE = new System.Windows.Forms.Button();
this.btnF = new System.Windows.Forms.Button();
this.labDisplay = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnA
//
this.btnA.Location = new System.Drawing.Point(12, 61);
this.btnA.Name = "btnA";
this.btnA.Size = new System.Drawing.Size(75, 23);
this.btnA.TabIndex = 0;
this.btnA.Text = "A";
this.btnA.UseVisualStyleBackColor = true;
this.btnA.Click += new System.EventHandler(this.btnA_Click);
//
// btnB
//
this.btnB.Location = new System.Drawing.Point(93, 61);
this.btnB.Name = "btnB";
this.btnB.Size = new System.Drawing.Size(75, 23);
this.btnB.TabIndex = 1;
this.btnB.Text = "B";
this.btnB.UseVisualStyleBackColor = true;
this.btnB.Click += new System.EventHandler(this.btnB_Click);
//
// btnC
//
this.btnC.Location = new System.Drawing.Point(12, 110);
this.btnC.Name = "btnC";
this.btnC.Size = new System.Drawing.Size(75, 23);
this.btnC.TabIndex = 2;
this.btnC.Text = "C";
this.btnC.UseVisualStyleBackColor = true;
this.btnC.Click += new System.EventHandler(this.btnC_Click);
//
// btnD
//
this.btnD.Location = new System.Drawing.Point(93, 110);
this.btnD.Name = "btnD";
this.btnD.Size = new System.Drawing.Size(75, 23);
this.btnD.TabIndex = 3;
this.btnD.Text = "D";
this.btnD.UseVisualStyleBackColor = true;
this.btnD.Click += new System.EventHandler(this.btnD_Click);
//
// btnE
//
this.btnE.Location = new System.Drawing.Point(12, 157);
this.btnE.Name = "btnE";
this.btnE.Size = new System.Drawing.Size(75, 23);
this.btnE.TabIndex = 4;
this.btnE.Text = "E";
this.btnE.UseVisualStyleBackColor = true;
this.btnE.Click += new System.EventHandler(this.btnE_Click);
//
// btnF
//
this.btnF.Location = new System.Drawing.Point(93, 157);
this.btnF.Name = "btnF";
this.btnF.Size = new System.Drawing.Size(75, 23);
this.btnF.TabIndex = 5;
this.btnF.Text = "F";
this.btnF.UseVisualStyleBackColor = true;
this.btnF.Click += new System.EventHandler(this.btnF_Click);
//
// labDisplay
//
this.labDisplay.AutoSize = true;
this.labDisplay.Font = new System.Drawing.Font("宋体", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.labDisplay.Location = new System.Drawing.Point(205, 94);
this.labDisplay.Name = "labDisplay";
this.labDisplay.Size = new System.Drawing.Size(44, 48);
this.labDisplay.TabIndex = 6;
this.labDisplay.Text = "A";
//
// Display
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.labDisplay);
this.Controls.Add(this.btnF);
this.Controls.Add(this.btnE);
this.Controls.Add(this.btnD);
this.Controls.Add(this.btnC);
this.Controls.Add(this.btnB);
this.Controls.Add(this.btnA);
this.Name = "Display";
this.Text = "Display";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnA;
private System.Windows.Forms.Button btnB;
private System.Windows.Forms.Button btnC;
private System.Windows.Forms.Button btnD;
private System.Windows.Forms.Button btnE;
private System.Windows.Forms.Button btnF;
private System.Windows.Forms.Label labDisplay;
}
}
...{
partial class Display
...{
/**//// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /**//// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
...{
if (disposing && (components != null))
...{
components.Dispose();
}
base.Dispose(disposing);
} Windows 窗体设计器生成的代码 ///
/**//// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btnA = new System.Windows.Forms.Button();
this.btnB = new System.Windows.Forms.Button();
this.btnC = new System.Windows.Forms.Button();
this.btnD = new System.Windows.Forms.Button();
this.btnE = new System.Windows.Forms.Button();
this.btnF = new System.Windows.Forms.Button();
this.labDisplay = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// btnA
//
this.btnA.Location = new System.Drawing.Point(12, 61);
this.btnA.Name = "btnA";
this.btnA.Size = new System.Drawing.Size(75, 23);
this.btnA.TabIndex = 0;
this.btnA.Text = "A";
this.btnA.UseVisualStyleBackColor = true;
this.btnA.Click += new System.EventHandler(this.btnA_Click);
//
// btnB
//
this.btnB.Location = new System.Drawing.Point(93, 61);
this.btnB.Name = "btnB";
this.btnB.Size = new System.Drawing.Size(75, 23);
this.btnB.TabIndex = 1;
this.btnB.Text = "B";
this.btnB.UseVisualStyleBackColor = true;
this.btnB.Click += new System.EventHandler(this.btnB_Click);
//
// btnC
//
this.btnC.Location = new System.Drawing.Point(12, 110);
this.btnC.Name = "btnC";
this.btnC.Size = new System.Drawing.Size(75, 23);
this.btnC.TabIndex = 2;
this.btnC.Text = "C";
this.btnC.UseVisualStyleBackColor = true;
this.btnC.Click += new System.EventHandler(this.btnC_Click);
//
// btnD
//
this.btnD.Location = new System.Drawing.Point(93, 110);
this.btnD.Name = "btnD";
this.btnD.Size = new System.Drawing.Size(75, 23);
this.btnD.TabIndex = 3;
this.btnD.Text = "D";
this.btnD.UseVisualStyleBackColor = true;
this.btnD.Click += new System.EventHandler(this.btnD_Click);
//
// btnE
//
this.btnE.Location = new System.Drawing.Point(12, 157);
this.btnE.Name = "btnE";
this.btnE.Size = new System.Drawing.Size(75, 23);
this.btnE.TabIndex = 4;
this.btnE.Text = "E";
this.btnE.UseVisualStyleBackColor = true;
this.btnE.Click += new System.EventHandler(this.btnE_Click);
//
// btnF
//
this.btnF.Location = new System.Drawing.Point(93, 157);
this.btnF.Name = "btnF";
this.btnF.Size = new System.Drawing.Size(75, 23);
this.btnF.TabIndex = 5;
this.btnF.Text = "F";
this.btnF.UseVisualStyleBackColor = true;
this.btnF.Click += new System.EventHandler(this.btnF_Click);
//
// labDisplay
//
this.labDisplay.AutoSize = true;
this.labDisplay.Font = new System.Drawing.Font("宋体", 36F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.labDisplay.Location = new System.Drawing.Point(205, 94);
this.labDisplay.Name = "labDisplay";
this.labDisplay.Size = new System.Drawing.Size(44, 48);
this.labDisplay.TabIndex = 6;
this.labDisplay.Text = "A";
//
// Display
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.Add(this.labDisplay);
this.Controls.Add(this.btnF);
this.Controls.Add(this.btnE);
this.Controls.Add(this.btnD);
this.Controls.Add(this.btnC);
this.Controls.Add(this.btnB);
this.Controls.Add(this.btnA);
this.Name = "Display";
this.Text = "Display";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button btnA;
private System.Windows.Forms.Button btnB;
private System.Windows.Forms.Button btnC;
private System.Windows.Forms.Button btnD;
private System.Windows.Forms.Button btnE;
private System.Windows.Forms.Button btnF;
private System.Windows.Forms.Label labDisplay;
}
}
(DispLay.cs 是工程的具体实现类,可以算做是逻辑应用层,具体的操作都在这里呢)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ConsoleEnglist
...{
public partial class Display : Form
...{
public Display()
...{
InitializeComponent();
} private void btnA_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnA.Text;
} private void btnB_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnB.Text;
} private void btnC_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnC.Text;
} private void btnD_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnD.Text;
} private void btnE_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnE.Text;
} private void btnF_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnF.Text;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace ConsoleEnglist
...{
public partial class Display : Form
...{
public Display()
...{
InitializeComponent();
} private void btnA_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnA.Text;
} private void btnB_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnB.Text;
} private void btnC_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnC.Text;
} private void btnD_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnD.Text;
} private void btnE_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnE.Text;
} private void btnF_Click(object sender, EventArgs e)
...{
this.labDisplay.Text = btnF.Text;
}
}
}
OK,编译一下,通过后,我们就开始做自动化测试了。(下面很重要呀)
第三:创建自动化测试。
当前程序不要Close,在.NET2005的快捷图标中选择“新建测试”如下图:
(如果没找到请做一下操作 视图----工肯栏----测试工具)
弹出下面的选择面板,请选择“单元测试向导”,按“确定”。如下图:
(具它的测试模板,我会在以后的文章中一一说明 )
接下来就是要会弹出一个窗体寻问您要创建的测试工程叫什么名字,这个你可以随便写,我的这个是默认的叫TestProject1。然后按“确定”按钮。这时会弹出一个窗体让你对当前工程(指的是一开始写的那个小程序)进行选择,因为我们要做自动化的测试,我们点选Display哪个类(因为我们这个程序只有两个类,一个是UI程序的入口类Program,另一个就是Display,Program对我们做下面的测试一点也没关系,所以我们就只选Display。如果您的程序还要用到别的类,切记也一定要选中呀)。如下图:
这时会生成一个测试工程,测试工程里有面有两个类文件,其中一个是DisplayTest.cs,一个是VSCodeGenAccessors.cs 这两个文件都很重要,VSCodeGenAccessors.cs里面放的是一些工程的UI,变量,方法等的引用,而DisplayTest.cs里面就是自动生成的测试代码。VSCodeGenAccessors.cs的内容看起来很繁琐,你不要怛心,其实很简单我会之后专门介绍一下这个类。DisplayTest.cs内容对我们的要求来说是不是很好用的。但是可以参考一下。接下来我们就要改一下DisplayTest.cs的内容。
(DisplayTest.cs)
// 以下代码由 Microsoft Visual Studio 2005 生成。
// 测试所有者应该检查每个测试的有效性。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using ConsoleEnglist;
namespace TestProject1
...{
/**//// <summary>
///这是 ConsoleEnglist.Display 的测试类,旨在
///包含所有 ConsoleEnglist.Display 单元测试
///</summary>
[TestClass()]
public class DisplayTest
...{
private TestContext testContextInstance; /**//// <summary>
///获取或设置测试上下文,上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
...{
get
...{
return testContextInstance;
}
set
...{
testContextInstance = value;
}
}
附加测试属性#region 附加测试属性
//
//编写测试时,可使用以下附加属性:
//
//使用 ClassInitialize 在运行类中的第一个测试前先运行代码
//
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//使用 ClassCleanup 在运行完类中的所有测试后再运行代码
//
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//使用 TestInitialize 在运行每个测试前先运行代码
//
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//使用 TestCleanup 在运行完每个测试后运行代码
//
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion [DeploymentItem("ConsoleEnglist.exe")]
[TestMethod()]
public void chick()
...{
int i = 0;
while (true)
...{
if (i >= 1)
...{
break;
}
Display target = new Display();
target.Show();
//创建窗体实例,并显示出来
TestProject1.ConsoleEnglist_DisplayAccessor accessor = new
TestProject1.ConsoleEnglist_DisplayAccessor(target);
object sender = null; // TODO: 初始化为适当的值
EventArgs e = null; // TODO: 初始化为适当的值
accessor.btnA_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnB_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnC_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnD_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnE_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnF_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
i++;
}
}
}
}
// 测试所有者应该检查每个测试的有效性。
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
using System.Collections.Generic;
using ConsoleEnglist;
namespace TestProject1
...{
/**//// <summary>
///这是 ConsoleEnglist.Display 的测试类,旨在
///包含所有 ConsoleEnglist.Display 单元测试
///</summary>
[TestClass()]
public class DisplayTest
...{
private TestContext testContextInstance; /**//// <summary>
///获取或设置测试上下文,上下文提供
///有关当前测试运行及其功能的信息。
///</summary>
public TestContext TestContext
...{
get
...{
return testContextInstance;
}
set
...{
testContextInstance = value;
}
}
附加测试属性#region 附加测试属性
//
//编写测试时,可使用以下附加属性:
//
//使用 ClassInitialize 在运行类中的第一个测试前先运行代码
//
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//使用 ClassCleanup 在运行完类中的所有测试后再运行代码
//
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//使用 TestInitialize 在运行每个测试前先运行代码
//
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//使用 TestCleanup 在运行完每个测试后运行代码
//
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion [DeploymentItem("ConsoleEnglist.exe")]
[TestMethod()]
public void chick()
...{
int i = 0;
while (true)
...{
if (i >= 1)
...{
break;
}
Display target = new Display();
target.Show();
//创建窗体实例,并显示出来
TestProject1.ConsoleEnglist_DisplayAccessor accessor = new
TestProject1.ConsoleEnglist_DisplayAccessor(target);
object sender = null; // TODO: 初始化为适当的值
EventArgs e = null; // TODO: 初始化为适当的值
accessor.btnA_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnB_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnC_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnD_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnE_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
accessor.btnF_Click(sender, e);
target.Refresh();
System.Threading.Thread.Sleep(1000);
i++;
}
}
}
}
(选择这个运行测试,可以对测试程序进行调试,只要您在测试代码中设断点。)
运行过程中您会看到程序会被重新执行一遍(自动),同时在左下方有个测试结果,你可以看到你的程序是否通过了测试。哈哈你已经学会了自动化测试的入门,你可以自己写一些复杂的测试工程。试试,如果您有什么不会,可以给我留言,或者给我发Email:ttlhong1981@163.com 记得给一些评论呀!
- 在.NET2005里用Test做自动化测试(WinForm)
- 在VS.NET2005里设置制表符
- 用python做自动化测试--序
- 用pytest做服务端自动化协议测试
- 网站做测试自动化
- 如何做自动化测试
- 如何做自动化测试
- 自动化测试在 stress test 和 repeat test 方面的应用和重要性
- 在WinForm里绘图
- 在做自动化测试之前你需要知道的
- 在做自动化测试之前你需要知道的
- 在做自动化测试之前你需要知道的
- 在做自动化测试之前你需要知道的
- 在做自动化测试之前你需要知道的
- 分享个最近在做的自动化测试框架
- 在做自动化测试之前你需要知道的
- 在做自动化测试之前你需要知道的
- 在做自动化测试之前你需要知道的
- VB新手入门之一
- 工程施工中应用多媒体技术
- VB新手入门之二
- vc读写Excel文件的方法
- VB新手入门之三
- 在.NET2005里用Test做自动化测试(WinForm)
- 妈妈的,dnn又出问题了
- algorithm 修改性的序列操作源码
- [转]完美的loading-吐血整理!
- 一个好玩的图像效果
- javaScript高级应用(二)
- Asp.Net2.0中GridView的DataFormatString="{0:yyyy-MM-dd}"不起作用解决办法
- asp.net三种重定向方法的总结
- VB新手入门之四