NBear WebTest - 分享一个基于Web的UnitTest工具

简介

这是一个ASP.NET 3.5的Web Application程序,实现了类似NUnit的简单但实用的UnitTest功能。写这个小工具的目的是在NBear5的开发中需要方便的在完全真实的模拟环境中测试所有组件功能的在ASP.NET下,尤其是Partial Trust模式下的运行效果,现有的UnitTest工具中似乎对这方面的支持都比较有限,所以,自己花两天时间写了一个。相比NUnit,本工具提供的UnitTest功能比较基础,但是,对一般的UnitTest来说应该完全够用了。如果您正在开发和测试一些ASP.NET下的Web组件,推荐一试。程序本身就是一个Web Application,所以,自然是包含了全部源代码的。源码对除.Net Framework 3.5之外的DLL没有任何依赖,也可以做成VS的Project Template方便重复使用。


版权

本文内容及相关代码遵守BSD开源协议,首发于http://www.cnblogs.com/teddyma/archive/2008/11/10/1330535.html


使用说明

UI界面

运行Default.aspx后,程序员自动列出所有的TestCases:



选择需要运行的TestCases,点击Run Tests后,TestCases被运行:



展开Tree可以看到运行的输出结果:




编写TestCase

要编写TestCase只需要在TestCases目录下新建.cs文件,并给你的测试Class和测试方法分别标注TestMethodAttribute。下面是程序默认附带的TestClass1类的源代码,可以看到和NUnit等工具的定义基本类似,SetUpAttribute和CleanUpAttribute仅支持Classs范围的,也就是说在一个Class中的所有TestMethod被执行的前后分别执行,其他的如TestClassAttribute,TestMethodAttribute和
ExpectedExceptionAttribute等就非常直观的,随便猜就能猜到意思了:

 1using System;
 2namespace NBear.WebTest.TestCases
 3{
 4    [TestClass]
 5    public class TestContractDescriptor
 6    {
 7        [SetUp]
 8        public void SetUp()
 9        {
10        }

11
12        [TestMethod]
13        public void TestMethod1()
14        {
15            Output.WriteLine("hello world");
16            Assert.IsTrue(true);
17        }

18
19        [TestMethod]
20        [ExpectedException(typeof(NotImplementedException))]
21        public void TestMethod2()
22        {
23            throw new NotImplementedException();
24        }

25
26        [CleanUp]
27        public void CleanUp()
28        {
29        }

30    }

31}

Output类用于输出结果,支持Write(string), WriteLine(string)和WriteNewLine()。

Assert类用于Check,支持如下Check方法:



下载地址

打包下载:https://files.cnblogs.com/teddyma/WebTest.zip

SVN下载:http://svn.cnblogs.com:8080/svn/NBear/trunk/src/WebTest/


Enjoy!

//the end


posted @ 2008-11-10 15:57  Teddy's Knowledge Base  Views(4333)  Comments(15Edit  收藏  举报