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:

o_webtest1.jpg

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

o_webtest2.jpg

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

o_webtest3.jpg


编写TestCase

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

 1None.gifusing System;
 2None.gifnamespace NBear.WebTest.TestCases
 3ExpandedBlockStart.gif{
 4InBlock.gif    [TestClass]
 5InBlock.gif    public class TestContractDescriptor
 6ExpandedSubBlockStart.gif    {
 7InBlock.gif        [SetUp]
 8InBlock.gif        public void SetUp()
 9ExpandedSubBlockStart.gif        {
10ExpandedSubBlockEnd.gif        }

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

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

25InBlock.gif
26InBlock.gif        [CleanUp]
27InBlock.gif        public void CleanUp()
28ExpandedSubBlockStart.gif        {
29ExpandedSubBlockEnd.gif        }

30ExpandedSubBlockEnd.gif    }

31ExpandedBlockEnd.gif}

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

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

o_webtest4.jpg

下载地址

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

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


Enjoy!

//the end


发布了187 篇原创文章 · 获赞 1 · 访问量 13万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章