[译]使用WatiN,TestDriven.NET,使用Visual Studio 2008的NUnit(团队系统)进行测试
By robot-v1.0
本文链接 https://www.kyfws.com/applications/a-test-harness-with-watin-testdriven-net-nunit-wit-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2909 个词 阅读量 0[译]使用WatiN,TestDriven.NET,使用Visual Studio 2008的NUnit(团队系统)进行测试
原文地址:https://www.codeproject.com/Articles/29352/A-Test-Harness-with-WatiN-TestDriven-NET-NUnit-wit
原文作者:Khandakar Fazley Rabbi
译文由本站 robot-v1.0 翻译
前言
UI and Functional Testing through different Tools using .NET Technology
使用.NET技术通过不同的工具进行UI和功能测试
- 下载SimpleLogin-13.25 KB(Download SimpleLogin - 13.25 KB)
- 下载SimpleTest-199.48 KB(Download SimpleTest - 199.48 KB)
介绍(Introduction)
在这个Web应用程序时代,Web 2.0变得非常流行,现在我们正在寻求向Web 3.0的进一步发展.通过这一方面,使用许多不同的工具已经大大改善了测试. “改善"一词可以表示为"变得更容易”.有趣的是,许多程序员(包括我在内)经常不关心UI测试.但是请考虑一下Web 2.0应用程序中的许多Ajax样式方法.它们具有拖放,自动完成,模式弹出窗口等功能,也应该以某种方式进行测试.世界各地的许多开发人员都可能熟悉WATIR框架(发音为(In this era of web application, web 2.0 became very popular and now we are looking for further development towards web 3.0. By this side testing has been improved a lot with many different tools. The word “Improved” can be meant by “becoming easier”. Interestingly, many programmers (including me) often do not care about UI Testing. But think about many Ajax style approaches in a web 2.0 application. They have drag n drop, auto complete, modal popup, etc., which should also be tested in some way. Many developers around the world may be well-acquainted with the framework WATIR (pronounced)水(water)),从而帮助Rails程序员充分利用其UI和功能测试. " Jeroen van Menen"创建了一个不错的工具WatiN,用于测试受Watir启发的Web应用程序.() which helped Rails programmers to leverage their UI and Functional Testing. “Jeroen van Menen” has created a nice tool WatiN for testing Web applications inspired on Watir.)
背景(Background)
WatiN:它已经发展成为易于使用,功能丰富且稳定的框架. WatiN是用C#开发的,旨在为您提供一种使用Internet Explorer和FireFox自动化测试的简便方法.有关Watin的更多信息,请访问(WatiN: It has grown into an easy to use, feature rich and stable framework. WatiN is developed in C# and aims to bring you an easy way to automate tests with Internet Explorer & FireFox. For more information about Watin, please visit) http://watin.sourceforge.net/(http://watin.sourceforge.net/) .(.)
NUnit(NUnit):这是所有.NET语言的单元测试框架.最初从(: It is a unit-testing framework for all .NET languages. Initially ported from) JUnit的(JUnit) ,当前的生产版本2.4版是该基于xUnit的Microsoft .NET单元测试工具的第五个主要版本.它完全用C#编写,并且经过完全重新设计,以利用许多.NET语言功能,例如自定义属性和其他与反射相关的功能. NUnit将xUnit引入所有.NET语言.从下载最新版本(, the current production release, version 2.4, is the fifth major release of this xUnit based unit testing tool for Microsoft .NET. It is written entirely in C# and has been completely redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities. NUnit brings xUnit to all .NET languages. Download the latest version from) 这里(here) .(.)
TestDriven.NET(TestDriven.NET):通过单击即可轻松在Visual Studio解决方案中的任何位置运行单元测试.它支持所有版本的Microsoft Visual Studio,并且与最佳的.NET开发工具集成.本示例使用"(: It makes it easy to run unit tests with a single click, anywhere in your Visual Studio solutions. It supports all versions of Microsoft Visual Studio and it integrates with the best .NET development tools. This example uses “)** TestDriven.NET-2.14.2190 Beta(TestDriven.NET-2.14.2190 Beta) **".(*”.*)
使用代码(Using the Code)
这是一个非常基本的示例,将通过集成不同的工具来展示UI和功能测试的方式.让我们从一个简单的控制台应用程序开始:配置WaTiN与nUnit交互(nUnit使用STA ApartmentState).公寓是进程中用于共享相同线程访问要求的对象的逻辑容器.同一单元中的所有对象都可以接收来自单元中任何线程的呼叫. .NET Framework不使用公寓,并且托管对象本身负责以线程安全的方式使用所有共享资源.(This is very elementary example that will show the way of UI & Functional Testing by integration different tools. Let’s start with a simple console application: To configure WaTiN to interact with nUnit (nUnit uses the STA ApartmentState). An apartment is a logical container within a process for objects sharing the same thread access requirements. All objects in the same apartment can receive calls from any thread in the apartment. The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.)
因为COM类使用单元,所以在COM互操作情况下调用COM对象时,公共语言运行库需要创建和初始化单元.托管线程可以创建并输入仅允许一个线程的单线程单元(STA),或包含一个或多个线程的多线程单元(MTA).您可以通过设置(Because COM classes use apartments, the common language runtime needs to create and initialize an apartment when calling a COM object in a COM interop situation. A managed thread can create and enter a single-threaded apartment (STA) that allows only one thread, or a multithreaded apartment (MTA) that contains one or more threads. You can control the type of apartment created by setting the) 公寓状态(ApartmentState) 线程的属性为其中一个值(property of the thread to one of the values of the) ApartmentState
枚举.因为给定线程只能初始化一次COM单元,所以您在第一次调用非托管代码后就无法更改单元类型.(enumeration. Because a given thread can only initialize a COM apartment once, you cannot change the apartment type after the first call to the unmanaged code.)
好,您需要先添加(Well, you need to first add)**应用配置(App.Config)**文件到项目中,如下所示:(file to the project which will look like:)
现在,您需要为项目添加一些引用.两者都加(You now need to add a few references to the project. Add both) WatiN.Core
和(and) nunit.Framework
对项目的引用.(references to the project.)
然后,我修改了主类,该类将负责打开和关闭浏览器. .代码如下:(Then I modified my main class that would take care of opening and closing out my browser. . The code follows:)
然后,我进行测试以检查登录名(正确).源代码如下:(My tests then for checking the login (correct). The source code is as follows:)
从上面的代码,您可以看到服务在端口2434上运行.我创建了一个简单的单独的Web应用程序项目,该项目在该端口上运行.该项目的源代码如下:(From the above code, you can see a service is running on the port 2434. I have created a simple separate web application project which runs on that port. The source code of that project is below:)
Default.aspx(Default.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="SimpleLogin._Default" %>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Simple Page</title>
</head>
<body>
<form id="form1" runat="server" action="Default.aspx">
<div> <table align="center" class="style1">
<tr>
<td class="style2">
UserName:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr> <tr>
<td class="style2"> Password:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="Button" OnClientClick="document.getElementById('form1').submit()"/>
</td> </tr>
<tr>
<td class="style2">
</td>
<td>
</td>
</tr><tr>
<td class="style2">
</td>
<td>
</td>
</tr>
<tr><td class="style2">
</td>
<td>
</td>
</tr>
<tr>
<td class="style2">
</td>
<td>
<asp:Label ID="Label1" runat="server"></asp:Label>
</td>
</tr>
</table> </div>
</form>
</body>
</html>
和(And)
Default.aspx.cs(Default.aspx.cs)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace SimpleLogin
{
public partial class _Default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
this.Label1.Text = "";
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.TextBox1.Text.Equals("rabbi") && this.TextBox2.Text.Equals("rabbi"))
this.Label1.Text = "Welcome Rabbi";
else
this.Label1.Text = "Unknown User";
}
}
}
从那里,现在我已经安装了TestDriven.NET的插件,可以构建项目,然后右键单击并选择" Test With …",然后选择NUnit.(From there, now that I have TestDriven.NET’s plug in installed, I can build the project and then right click and select “Test With…” and then select NUnit.)
这将弹出NUnit,并允许我单击运行按钮以运行测试.(This will pop-up NUnit and allow me to click on the run button to run through the tests.)
结论(Conclusion)
在这一阶段,我们应该知道如何基本使用WatiN框架来测试Web应用程序.我希望您能像我一样对这个框架和本文有所帮助.就个人而言,我没有使用任何其他Web应用程序测试框架.(At this stage, we should know how to basically use WatiN framework to test web applications. I hope you find this framework and this article useful, as I did. Personally, I haven’t used any other web application testing framework.)
历史(History)
- 15(15)日(th)2008年9月:初始职位(September, 2008: Initial post)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
Javascript XML CSS C# .NET3.0 .NET3.5 .NET2.0 ASP.NET WCF ASP 新闻 翻译