VS2012 測試WebService


測試本機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特性聲明時的第一個參數即服務器名稱。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM