简体   繁体   English

单元测试一些Web服务方法

[英]Unit Testing Some Web Service Methods

My boss wants me to create an .aspx page, textboxes for simplicity for entering credit card information so we can test out some methods in our CreditCard service. 我的老板要我创建一个.aspx页面,文本框以简化输入信用卡信息,这样我们就可以在CreditCard服务中测试一些方法。

Thats fine but I would think we could do a Unit test for this. 多数民众赞成,但我认为我们可以为此进行单元测试。 The only thing is, instead of typing the stuff into a web form, we'd just change the variable values passed into the unit test. 唯一的问题是,我们只需更改传递给单元测试的变量值,而不是将内容输入到Web表单中。

Can anyone tell me if we're crazy for not doing a Unit test instead of an .aspx page for testing and entering in test data to test out some calls to some of our methods? 任何人都可以告诉我,如果我们因为没有进行单元测试而不是.aspx页面进行测试并输入测试数据以测试对我们某些方法的调用而感到疯狂吗?

He'll end up telling me Unit Test takes too much time to setup (as I've tried to tell him we need to do unit testing) which is a lame stupid excuse. 他最终会告诉我单元测试需要花费太多时间来设置(因为我试图告诉他我们需要进行单元测试),这是一个蹩脚的愚蠢借口。

You are crazy if you don't unit test your web service instead of writing a manual testing harness :) 如果您没有对Web服务进行单元测试而不是编写手动测试工具,那你就疯了:)

Basically, a web service is an API that is accessed over a remote protocol, so why not unit test it? 基本上,Web服务是通过远程协议访问的API,那么为什么不进行单元测试呢?

I feel a little bit guilty about leaving such a glib answer as I did before, so here's a more serious take on it: 我对留下如此狡猾的答案感到有点内疚,所以这里有一个更严肃的看法:

Let's examine what it would take to unit test the service. 让我们来看看单元测试服务需要做些什么。 A real unit test is an automated test that tests a single unit (in your case that would the web service without any backend systems such as databases etc.). 真正的单元测试是一种自动化测试,可以测试单个单元(在您的情况下,Web服务没有任何后端系统,如数据库等)。 As others have pointed out, proper unit testing of the service is probably too late in this case. 正如其他人所指出的那样,在这种情况下,对服务进行适当的单元测试可能为时已晚。

That doesn't mean that you can't use a unit testing framework (such as MSTest, xUnit.net, NUnit, etc.) to drive your service tests. 这并不意味着您不能使用单元测试框架 (例如MSTest,xUnit.net,NUnit等)来推动您的服务测试。 Let us contrast that scenario to developing a throw-away aspx page: 让我们将这个场景与开发一个抛弃的aspx页面进行对比:

  • In both cases I am going to assume that the web service is already deployed, configured and running, because that would probably be the case in the aspx scenario. 这两种情况下,我将假设Web服务已经部署,配置和运行,因为在aspx场景中可能就是这种情况。
  • In both cases you would have to add a service reference to the test project to generate a web service proxy. 这两种情况下,您都必须向测试项目添加服务引用以生成Web服务代理。
  • In both cases you would have to write code that applies values to the request parameters for the web service methods. 这两种情况下,您都必须编写将值应用于Web服务方法的请求参数的代码。
  • In both cases you need to invoke the web service operations. 这两种情况下,您都需要调用Web服务操作。

So what's different? 那有什么不同?

  • In the aspx scenario, you will need to collect values from form fields and assign those values to to the service method paramters. 在aspx场景中,您需要从表单字段中收集值并将这些值分配给服务方法参数。 Using a testing framework, you can write those values directly. 使用测试框架,您可以直接编写这些值。 It's actually easier to write the automated test. 实际上编写自动化测试更容易。 1-0 1-0
  • In the aspx scenario, you would need to write code that takes the response data and write it to the web page. 在aspx场景中,您需要编写获取响应数据并将其写入网页的代码。 In contrast, with a testing framework, you will need to write assertions. 相反,使用测试框架,您需要编写断言。 I would claim that it's easier to write assertions, but that's a bit subjective so I'll leave this one as a tie - still 1-0 我会声称写断言更容易,但这有点主观,所以我会把这个作为一个平局 - 仍然是1-0
  • In the automated test scenario, you will need to write many tests with different values, so that's more code you will need to write compared to the aspx option. 在自动化测试场景中,您需要编写许多具有不同值的测试,因此与aspx选项相比,您需要编写更多代码。 1-1 1-1
  • With an automated test suite, you can subsequently run the automated test suite against the database many times a day with no additional effort, whereas you would need to manually input and manually verify the results in the aspx scenario for every test run. 使用自动化测试套件,您可以随后每天多次针对数据库运行自动化测试套件,而无需额外工作,而您需要在每次测试运行时手动输入并手动验证 aspx场景中的结果。 That's a huge win in testing effort. 这是测试工作的巨大胜利。 2-1 (and that's conservative) 2-1 (那是保守的)

In conclusion, I would say that if you don't insist on doing real unit-testing in this case, but simply utilize a unit testing framework to write automated tests, you should be better off with the automated tests than with the aspx page. 总而言之,我想说如果你不坚持在这种情况下进行真正的单元测试,而只是利用单元测试框架来编写自动化测试,那么自动化测试应该比使用aspx页面更好。 The development effort will be more or less the same. 开发工作或多或少都是一样的。

For your next project, you can then see if you can use TDD from the beginning, but that's another battle. 对于你的下一个项目,你可以看看你是否可以从一开始就使用TDD,但这是另一场战斗。

Good luck. 祝好运。

If it's an ASMX web service, you might try enabling the HttpPost protocol in your Web.config: 如果它是ASMX Web服务,您可以尝试在Web.config中启用HttpPost协议:

<configuration>
  <system.web>
    <webServices>
      <protocols>
        <add name="HttpPost"/>
      </protocols>
    </webServices>
  </system.web>
</configuration>

This will enable the test form for the web service when you visit the .asmx page in your browser. 当您在浏览器中访问.asmx页面时,这将启用Web服务的测试表单。 It might not work well for complex types; 它可能不适用于复杂类型; but if you have complex types, it'll be easier to build the unit tests than a custom form anyway. 但是如果你有复杂的类型,那么构建单元测试比自定义表单更容易。

The argument that unit testing is harder than a web form seems wrong; 单元测试比网络形式更难的论点似乎是错误的; if you're developing a form, you have to write the web service client code anyway, in addition to building the page itself. 如果您正在开发表单,除了构建页面本身之外,您还必须编写Web服务客户端代码。

Your boss may wish to confirm that the web service can be called from an .aspx page as well as being able to try out some values. 您的老板可能希望确认可以从.aspx页面调用 Web服务以及能够尝试某些值。 (Does he want example calling code that someone else to use to create the real web page?) If your web service calls any external services and/or uses a database it will be hard to write automated unit tests for it anyway. (他是否需要使用其他人用来创建真实网页的示例调用代码?)如果您的Web服务调用任何外部服务和/或使用数据库,那么无论如何都很难编写自动单元测试。

As to writing a real unit test for the web service, I think you have already lost the battle this time…. 至于为Web服务编写一个真正的单元测试,我想你这次已经输掉了这场战斗......

Next time, try writing unit tests for each method the web services calls, just before or just after you write the method. 下一次,尝试在编写方法之前或之后为Web服务调用的每个方法编写单元测试。 There is no need to even tell your boss you are doing this, as it will result in producing working code quicker. 甚至没有必要告诉你的老板你这样做,因为这将导致更快地生成工作代码。

Once you have proved the unit tests help you write better code quickly you can try to introduce Test Driven Development and/or have the unit tests check into the source code control system and other people run them when they change the code. 一旦您证明单元测试可以帮助快速编写更好的代码,您可以尝试引入测试驱动开发和/或将单元测试检查到源代码控制系统,其他人在更改代码时运行它们。

You could always spend some of your own time tonight, after your boss has gone home, trying to write the unit tests. 在你的老板回家后,你可以在今晚度过一些自己的时间,试图编写单元测试。 Then only tell him what you have done when he ask why your code has no bugs in it. 然后只告诉他你在为什么你的代码没有错误时所做的事情。

It is a battle you'll surely loose. 这场战斗你肯定会松动。 You have to put yourself in your bosses shoes. 你必须把自己放在你的老板鞋里。 There are projects where unit testing could take up too much time, especially at the end of the development cycle when everything is rushed to be completed. 有些项目的单元测试可能会花费太多时间,特别是在开发周期结束时,一切都急于完成。 TDD has to be followed from the start or you will loose too much time implementing unit tests after you have already forgotten how a specific piece of code works (no, comments are usually not enough). 从一开始就必须遵循TDD ,否则在您已经忘记了特定代码片段的工作原理之后,您将花费太多时间来实现单元测试(不,评论通常是不够的)。

Just make it common practice for next projects that you do TDD. 只是让你做下一个TDD项目的常规做法。 After you have all of your code unit tested you could implement some type of functional testing with tools such as JMeter and Selenium . 在对所有代码单元进行测试后,您可以使用JMeterSelenium等工具实现某种类型的功能测试。

Instead you can write a simple .NET(or Java) test that calls the web service and checks various scenarios, along with the obvious benefit (it's testable) you will also have an automated way to check its functionality. 相反,您可以编写一个简单的.NET(或Java)测试来调用Web服务并检查各种场景,以及明显的好处(它是可测试的),您还可以通过自动方式检查其功能。

The time "wasted" on writing the unit tests will be regained by the time saved on testing the same scenario over and over again instead of just running the automated tests. 编写单元测试时“浪费”的时间将通过一次又一次地测试相同场景而节省的时间重新获得,而不是仅仅运行自动化测试。

If your boss is not convinced by that point him to studies that show TDD/unit tests effectiveness . 如果你的老板不相信他的那些研究表明TDD /单元测试的有效性

If all else fails why not use an automatic tool like soapUI at least you'll save yourself from manually testing the same functionality over and over again. 如果所有其他方法都失败了,为什么不使用像soapUI这样的自动工具,至少可以避免一次又一次地手动测试相同的功能。

In my opinion, if you create .aspx page and get value from web form, it would be more real time testing than unit testing. 在我看来,如果您创建.aspx页面并从Web表单获取价值,那么测试将比单元测试更实时。 I hope web service was already unit tested by the organization, which provide you this web service. 我希望Web服务已经过组织的单元测试,它为您提供此Web服务。 I think, you just need to create .aspx form and get done your work. 我想,你只需要创建.aspx表单并完成你的工作。

You can do unit testing for your entire development process satisfaction. 您可以对整个开发过程的满意度进行单元测试。 It is good idea that unit test should be done by the person who wrote the code of class/function/web method. 单元测试应该由编写类/函数/ web方法代码的人完成。

Let me know, if you have any question. 如果您有任何疑问,请告诉我。

Guessing that you already lost the battle (we feel for you). 猜猜你已经输掉了战斗(我们觉得你)。 There are better solutions than manually creating a consumer for your web service. 除了为Web服务手动创建使用者之外,还有更好的解决方案。

Check out SoapUI . 看看SoapUI It consumes your WSDL and lets you play with the xml requests. 它使用您的WSDL并允许您使用xml请求。 Very easy to plug into a web service to test it out if all they want is a POC. 如果他们想要的只是一个POC,很容易插入Web服务来测试它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM