測試本機webservice :
要點說明:
1.TestContext :使用 TestContext 類
2.AspNetDevelopmentServer:AspNetDevelopmentServerAttribute 類
3.TryUrlRedirection方法:WebServiceHelper.TryUrlRedirection
測試:
使用工具:VS2012
首先新建一個服務項目WebService1,然后新建測試項目
在測試項目里添加服務引用,在服務引用對話框里點高級,然后在兼容性下面點添加web引用,在彈出的添加web引用對話框里點擊此解決方案中的web服務,選中服務,點擊添加引用。

實現代碼:
View Code
public class UnitTest1 { 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; } } [TestMethod] [AspNetDevelopmentServer("HelloTest", @"E:\新建文件夾\WebService1\WebService1")] public void TestMethod1() { Service1 target = new Service1(); Assert.IsTrue( WebServiceHelper.TryUrlRedirection(target, testContextInstance, "HelloTest"), "Web service redirection failed" ); string expected = "Hello World"; string actual = target.HelloWorld(); Assert.AreEqual(expected, actual, "test"); } }
TestContext屬性手動寫進行就行了,系統會自動賦值;
AspNetDevelopmentServer特性 第一個參數是 可以理解為服務器名稱,第二個參數是 服務項目路徑,第三個參數為虛擬路徑
TryUrlRedirection方法 第一個參數為service1 的對象,第二個參數為TestContext 類型的變量,第三個參數為 AspNetDevelopmentServer特性聲明時的第一個參數即服務器名稱。
