views:

460

answers:

8

Should we unit test web service or really be looking to unit test the code that the web service is invoking for us and leave the web service alone or at least until integration testing, etc.... ?

EDIT: Further clarification / thought

My thought is that testing the web service is really integration testing not unit testing?? I ask because our web service at this point (under development) is coded in such a way there is no way to unit test the code it is invoking. So I am wondering if it is worth while / smart to refactor it now in order to be able to unit test the code free of the web service? I would like to know the general consensus on if it's that important to separate the two or if it's really OK to unit test the web service and call it good/wise.

If I separate them I would look to test both but I am just not sure if separation is worth it. My hunch is that I should.

+2  A: 

Why not do both? You can unit test the web service code, as well as unit test it from the point of view of a client of the web service.

1800 INFORMATION
Like the answer and like the nickname 1800 INFORMATION
Perpetualcoder
+5  A: 

Unit testing the code that the web service is invoking is definitely a good idea since it ensures the "inside" of your code is stable (and well designed). However, it's also a good idea to test the web service calls, especially if a few of them are called in succession to accomplish a certain task. This will ensure that the web services that you've provided are usable, as well as, work properly when called along with other web service calls. (Not sure if you're writing these tests before or after writing your code, but you should really consider writing your web service tests before implementing the actual calls so that you ensure that they are usable in advance of writing the code behind them.)

Joe L.
A: 

We do both.

We unit test the various code elements.

Plus, we use the unit test framework to execute tests against the web service as a whole. This is rather complex because we have to create (and load) a database, start a server, and then execute requests against that server.

S.Lott
You're abusing the term "unit test". By your definition, there would be no difference between "automated testing" in general and "unit testing".
Wim Coenen
I'm unclear on "unit". Is "Unit" a single "file"? "class"? "module"? "package"? or "application"? Which is the one-and-only true and unchanging definition of "unit"? Since we can't see why a package "unit" test is different from a class "unit" test, we think we're doing "unit" testing and we're definitely using the unittest framework because it's so effective. You can call it "integration" testing, too, if it makes you happy.
S.Lott
The absence of a very strict criterion does not automatically mean that the distinction is useless. http://en.wikipedia.org/wiki/Continuum_fallacy
Wim Coenen
@wcoenen: Never said the distinction was useless. Indeed, I firmly believe that "unit" is defined at different levels of detail. Others -- it appears -- object to the idea that "unit" can have different interpretations depending on the level of detail encompassed by the testing. As long as the unit under test is treated as "opaque" and only the formal API's are used, I don't see how one can't have multiple levels of unit testing.
S.Lott
A: 

Testing the web service API is easy (it's got an API) and valuable. It's not a unit test, though - it's an "integration", "sub-system", or "system" test (depends on who you ask).

There's no need to delay the testing until some magical period called "integration testing" though, just get some simple tests now and reap the benefit early.

orip
True on the magical period. I was using it as a conversation piece. I agree we should be testing ASAP. :)
klabranche
A: 

If you can, try consuming your web service using some of the development tools your customers will use (Delphi, C#, VB.Net, ColdFusion, hand crafted XML, etc...). Within reason, of course.

1) Different tools may have problems consuming your web service. Better for you to run in to this before your customers do.

2) If a customer runs in to a problem, you can easily prove that your web service is working as expected. In the past year or so, this has stopped finger pointing in its tracks at least dozen times.

The worst was the developer in a different time zone who was hand crafting the SOAP calls and parsing the responses. Every time he ran in to a problem, he would insist that it was a problem on our end and demand (seriously) that we prove otherwise. I made a dead simple Delphi app to consume the web service, demonstrate that the methods worked as expected and even displayed the XML for each request and response.

Bruce McGee
A: 

re your updated question.

The integration testing and unit testing are only superficially similar, so yes, they should be done and thought of separately.

Refactoring existing code to make it testable can be risky. It's up to you to decide if the benefits outweigh the time and effort it will take. In my case, I'd certainly try, even if you do it a little bit at a time.

On the bright side, a web service has a defined interface, so you don't really have to change anything to add integration testing. Go crazy. If you can, try to do this before you roll the web service out to customers. There's a good chance that using the web service lead to changes in the interface, and you don't want this to mess up customers too much.

Bruce McGee
We are in the very early stages of development on this so I am not worried about the refactoring risk. I pose the idea to refactor in order to unit test it. As far as a web service having a defined interface, can't you have the same for a library? I see both as having the same potential risk for changing their interface and I don't see how the web service is better protected from this than a library is.
klabranche
If you're extremely careful about never changing your library code interfaces, then they would be about the same. From my experience, I see a lot of library code breakage between releases, where authors of COM objects or web services are more likely to leave the existing interfaces alone and introduce new ones instead.
Bruce McGee
A: 

I like the idea of writing unit tests which call your web service through one of its public interfaces. For instance, a given WCF web service may expose HTTP, TCP, and "web" bindings. Such a unit tests proves that the web service can be called through a binding.

Integration testing would involve testing all of the bindings of the service, testing with particular client scenarios, and with particular client tools. For instance, it would be important to show that a Java client can be created with IBM's Rational Web Developer that can access the service when using WS-Security.

John Saunders
A: 

Under my concept, the WS is just a mere encapsulation of a Method of a Central Business Layer Object, in other words, the Web Method is just a "gate" to access methods deeper in the model.

With the former said, i do both operations:

  1. Inside the Server, I create a Winform App who do Load Testing on the Business Layer Method.

  2. Outside the Server (namely a Machine out of the LAN where the Web App "lives"), I create a Tester (Winform or Web) that consumes the WS, doing that way the Load Testing.

That Way I can evaluate the performance of my solution considering and discarding the "Web Effect" (i.e. The time for the data to travel and reach the WS, the WS object creation, etc).

All the above said is of course IMHO. At least that worked a lot for me!

Haj.-

HJ42