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特性声明时的第一个参数即服务器名称。

posted @ 2013-04-26 18:20  ClrsDream  阅读(4369)  评论(0编辑  收藏  举报