views:

285

answers:

4

I'm just about done creating a web service that will be consumed by a non .NET internal custom system. I would like some advice on the best way to setup test classes and methods over an .asmx (best practices, how to test the calls, what not to do, etc.) specifically in a .NET 3.5 environment.

I will be using NUnit to do this testing. Is it as simple as creating a test project, adding the service to it and then create a test class and instance of that service..then start creating your test methods?

I need to test both the .asmx and .asmx.cs methods (unit test the methods) so that I know if I pass this to a teammate that it's going to work.

Maybe it's not possible to test an .asmx.cs directly and I'll just have to test via integration tests. I guess what I really would need is to mock my .asmx. Probably not possible.

+3  A: 

The best practice of a Unit test is not to test the asmx file, but the parts (units) behind the asmx file. If you can split up your code in small and separate pieces, then you can unit test those pieces.

If you want to test the asmx file itself, you're talking about an integration test. You can use NUnit for that in the way you described, but that's not really unit testing.

Lodewijk
I'm wanting to UNIT test the methods in the . asmx.cs. So my question has yet to be solved because in my unit test project, I guess I'm not sure how to make calls to the .asmx.cs. In other words if my service is called MyService.asmx, I want to create a unit test MyServiceTest that tests the .asmx.cs methods specifically. I need to both test as if I'm a consumer of the servcie AND unit test at the same time to know if I get any errors inside any .asmx.cs methods. Basically the point is I created an internal service and my boss is going to utilize it. Just got done with the methods.
CoffeeAddict
So I guess my questions still stands. I created MyTestProject solution and in it a C# project that will hold my test classes. Then added the web project to the same solution and want to test calls to the .asmx and also unit test the methods in the .asmx.cs
CoffeeAddict
Can you move the methods out of asmx.cs (at least the parts that need to be tested) into a new class and test that?
Davy8
If you really want to test the asmx (<nitpick>just don't call it a _Unit_ test</nitpick>), the best thing you can do is to start up the visual studio webdev server (aka cassini) in your testfixturesetup and call the service in your tests.
Lodewijk
A: 

Like Lodewijk stated, it is best practice to not unit test the asmx file. Instead, extract the logic from that file into class files which handle the behavior you are after. That way you can then unit test those classes in isolation from the UI. You may find that the real problem you have is that there is too much business logic in your UI layer.

If you want to test the asmx file itself you'll either want to consider manual testing, integration testing, or acceptance testing...but if you can move your logic to the business layer you'll probably find it much easier to test.

mezoid
I already have a BL project for that. Anyway thanks. These are wrappers around BL calls.
CoffeeAddict
A: 

Unfortunately this is a problem with .asmx web services, they are dependent on ASP.net, you're best approach would be to keep the .asmx web service as a stub and extract your web service logic into a clean dependency-free class and unit test that instead. The other alternative is to run Integration tests as well.

In the long run, if unit testing is important to you, you may be better off developing using a web service framework that was designed with unit testing from the start.

mythz
A: 

You might be missing the point of what other people are saying. The .asmx file should have no logic worth testing. If it truly is just a wrapper around business layer calls, then it adds nothing and doesn't need to be tested. If it does add something, extract that until the .asmx contains nothing but a pass-through call.

What does your .asmx file contain that cannot be extracted into separate, testable classes?

Bryan Watts
it's wrappers with additional logic such as logging, etc. It's a pass through to my PayPal wrapper project and calls wrappers there...but wrappers are not always just passing through without doing some logging, updating of SOME records on our side, etc.
CoffeeAddict
I could I guess extract into seperate classes. I'll just copy over the methods into some Test class and add that class to my test project then.
CoffeeAddict