WebService 的UnitTest以及调试

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

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

主要内容如下:

1、写一个WebSerivce

2、写一个WebService的单元测试

3、调试单元测试

 

先看一个简单的WebService:

 

ContractedBlock.gif ExpandedBlockStart.gif Default web service
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Web;
 5using System.Web.Services;
 6using System.Web.Script.Services;
 7
 8ExpandedBlockStart.gifContractedBlock.gif/**//// <summary>
 9/// Summary description for DefaultWebService
10/// </summary>

11[WebService(Namespace = "http://tempuri.org/")]
12[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
13[ScriptService]
14public class DefaultWebService : System.Web.Services.WebService
15ExpandedBlockStart.gifContractedBlock.gif{
16    public DefaultWebService()
17ExpandedSubBlockStart.gifContractedSubBlock.gif    {
18
19    }

20
21    [WebMethod]    
22    public string HelloWorld()
23ExpandedSubBlockStart.gifContractedSubBlock.gif    {
24        return "Hello World";
25    }

26    [WebMethod(EnableSession=true)]
27    [ScriptMethod]
28    public void ChanageSesseion(string sessionName,string msg)
29ExpandedSubBlockStart.gifContractedSubBlock.gif    {
30        if (null == Session[sessionName])
31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
32            return;
33        }

34        Session[sessionName] = msg;
35    }

36}

 

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

我们针对HelloMethod方法进行UnitTest

 

ContractedBlock.gif ExpandedBlockStart.gif Default web service test
 1using Microsoft.VisualStudio.TestTools.UnitTesting;
 2using Microsoft.VisualStudio.TestTools.UnitTesting.Web;
 3using System.Web;
 4
 5namespace UnitTestProject
 6ExpandedBlockStart.gifContractedBlock.gif{
 7    
 8    
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
10    ///This is a test class for DefaultWebServiceTest and is intended
11    ///to contain all DefaultWebServiceTest Unit Tests
12    ///</summary>

13    [TestClass()]
14    public class DefaultWebServiceTest
15ExpandedSubBlockStart.gifContractedSubBlock.gif    {
16        static string TestSessionName = "TESTSESSION"
17
18        private TestContext testContextInstance;
19
20ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
21        ///Gets or sets the test context which provides
22        ///information about and functionality for the current test run.
23        ///</summary>

24        public TestContext TestContext
25ExpandedSubBlockStart.gifContractedSubBlock.gif        {
26            get
27ExpandedSubBlockStart.gifContractedSubBlock.gif            {
28                return testContextInstance;
29            }

30            set
31ExpandedSubBlockStart.gifContractedSubBlock.gif            {
32                testContextInstance = value;
33            }

34        }

35
36ContractedSubBlock.gifExpandedSubBlockStart.gif        Additional test attributes#region Additional test attributes
37        // 
38        //You can use the following additional attributes as you write your tests:
39        //
40        //Use ClassInitialize to run code before running the first test in the class
41        [ClassInitialize()]
42        public static void MyClassInitialize(TestContext testContext)
43ExpandedSubBlockStart.gifContractedSubBlock.gif        {
44            HttpContext.Current.Session[TestSessionName] = "I'm test class."//在这儿初始化Session
45        }

46        //
47        //Use ClassCleanup to run code after all tests in a class have run
48        //[ClassCleanup()]
49        //public static void MyClassCleanup()
50        //{
51        //}
52        //
53        //Use TestInitialize to run code before running each test
54        //[TestInitialize()]
55        //public void MyTestInitialize()
56        //{
57        //}
58        //
59        //Use TestCleanup to run code after each test has run
60        //[TestCleanup()]
61        //public void MyTestCleanup()
62        //{
63        //}
64        //
65        #endregion

66
67
68ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
69        ///A test for ChanageSesseion
70        ///</summary>

71        // TODO: Ensure that the UrlToTest attribute specifies a URL to an ASP.NET page (for example,
72        // http:///Default.aspx). This is necessary for the unit test to be executed on the web server,
73        // whether you are testing a page, web service, or a WCF service.
74        [TestMethod()]
75        [HostType("ASP.NET")]
76        [AspNetDevelopmentServerHost("E:\\Project\\TestSolution\\WebSite""/")]
77        [UrlToTest("http://localhost/WebSite")]
78        public void ChanageSesseionTest()
79ExpandedSubBlockStart.gifContractedSubBlock.gif        {
80            DefaultWebService_Accessor target = new DefaultWebService_Accessor(); // TODO: Initialize to an appropriate value
81            string sessionName = TestSessionName; // TODO: Initialize to an appropriate value
82            string expected = "I have changed you!";
83            target.ChanageSesseion(sessionName,expected);
84            if (null == HttpContext.Current.Session[sessionName])
85ExpandedSubBlockStart.gifContractedSubBlock.gif            {
86                Assert.Fail("Cann't use session in context");
87            }

88            else
89ExpandedSubBlockStart.gifContractedSubBlock.gif            {
90                string actual = HttpContext.Current.Session[sessionName].ToString();
91                Assert.AreEqual(expected, actual);
92            }

93        }

94    }

95}

96

 

注意两几:

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即可。

 

 

 

转载于:https://www.cnblogs.com/disappearwind/archive/2009/04/04/1429426.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值