WebService的UnitTest以及调试

对于有些特殊的项目,需要验证WebService提供的方法是否正确,比如通过Session传递数据的WebService。

看到这儿先声明一下是一些特殊的项目对WebService的特殊用途。

主要内容如下:

1、写一个WebSerivce

2、写一个WebService的单元测试

3、调试单元测试

 

先看一个简单的WebService:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

/**//// <summary>
///
Summary description for DefaultWebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class DefaultWebService : System.Web.Services.WebService
{
public DefaultWebService()
{

}

[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[WebMethod(EnableSession=true)]
[ScriptMethod]
public void ChanageSesseion(string sessionName,string msg)
{
if (null == Session[sessionName])
{
return;
}
Session[sessionName] = msg;
}
}

注意,这个WebService是可以供JavaScript调用的(也是一个特殊的WebService吧,呵呵!)

我们针对HelloMethod方法进行UnitTest

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
using System.Web;

namespace UnitTestProject
{


/**//// <summary>
///
This is a test class for DefaultWebServiceTest and is intended
///to contain all DefaultWebServiceTest Unit Tests
///</summary>
[TestClass()]
public class DefaultWebServiceTest
{
static string TestSessionName = "TESTSESSION";

private TestContext testContextInstance;

/**//// <summary>
///
Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}

Additional test attributes#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
[ClassInitialize()]
public static void MyClassInitialize(TestContext testContext)
{
HttpContext.Current.Session[TestSessionName] = "I'm test class."; //在这儿初始化Session
}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion


/**//// <summary>
///
A test for ChanageSesseion
///</summary>
// TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
// http:///Default.aspx). This is necessary for the unit test to be executed on the web server,
// whether you are testing a page, web service, or a WCF service.
[TestMethod()]
[HostType("ASP.NET")]
[AspNetDevelopmentServerHost("E:\\Project\\TestSolution\\WebSite", "/")]
[UrlToTest("http://localhost/WebSite")]
public void ChanageSesseionTest()
{
DefaultWebService_Accessor target = new DefaultWebService_Accessor(); // TODO: Initialize to an appropriate value
string sessionName = TestSessionName; // TODO: Initialize to an appropriate value
string expected = "I have changed you!";
target.ChanageSesseion(sessionName,expected);
if (null == HttpContext.Current.Session[sessionName])
{
Assert.Fail("Cann't use session in context");
}
else
{
string actual = HttpContext.Current.Session[sessionName].ToString();
Assert.AreEqual(expected, actual);
}
}
}
}

注意两几:

1、调用WebSite的webservice使用DefaultWebService_Accessor,调用Webapplication则使用DefaultWebService,VS创建单元测试时会自动生成。

2、TestMethod的UrlToTest属性,可以是IIS的地址也可以是VS自带的ASP.NET的浏览工具的地址,带端口号的。但是要设置成端口号被运行一次改变一次

3、AspNetDevelopmentServerHost属性中的项目地址是可以用%SystemRootPath%

4、Session之类的在标识[ClassInitialize()]属性的方法中初始化,方法接受一个TestContext的参数

 

接下来是调试单元测试:

1、使用VS自带的ASP.NET程序的浏览器,在 UrlToTest属性中配置

2、运行单元测试让它显示在TestResult窗口中

3、调试->附加到进程 附加到WebDev.WebServer.EXE进程中

4、在TestResult窗口中选择调试即可。

 

调试之前最好先备份Web.config文件。在调试过程中不小心按了VS主菜单的停止调试,VS会修改Web.config然后下一次就都无法运行单元测试了。这个时候只需要还原Web.config即可。