Automate test of web service communication

2019-04-30 10:39发布

I have an application that sends messages to an external web service. I build and deploy this application using MSBuild and Cruisecontrol.NET. As CCNET build and deploys the app it also runs a set of test using NUnit. I'd now like to test the web service communication as well.

My idea is that as part of the build process a web service should be generated (based on the external web services WSDL) and deployed to the build servers local web server. All the web service should do is to receive the message and place it on the file system so I then can check it using ordinary NUnit for example. This would also make development easier as new developers would only have to run the build script and be up and running (not have to spend time to set up a connection to the third party service).

Are there any existing utilities out there that easily mock a web service based on a WSDL? Anyone done something similar using MSBuild?

Are there other ways of testing this scenario?

5条回答
迷人小祖宗
2楼-- · 2019-04-30 11:10

I just started looking into http://www.soapui.org/ and it seems like it will work nicely for testing web services.

Also, maybe look at adding an abstraction layer in your web service, each service call would directly call a testable method (outside of the web scope)? I just did this with a bigger project I'm working on, and it's testability is working nicely.

查看更多
走好不送
3楼-- · 2019-04-30 11:13

At my work place we are using Typemock and nUnit for our unit testing.

查看更多
\"骚年 ilove
4楼-- · 2019-04-30 11:17

In general, a very good way to test things like this is to use mock objects.

At work, we use the product TypeMock to test things like Web Service communication and other outside dependencies. It costs money, so for that reason it may not be suitable for your needs, but I think it's a fantastic product. I can tell you from personal experience that it integrates very well with NUnit and CCNet.

It's got a really simple syntax where you basically say "when this method/property is called, I want you to return this value instead." It's great for testing things like network failures, files not being present, and of course, web services.

查看更多
ゆ 、 Hurt°
5楼-- · 2019-04-30 11:23

This might also be something - MockingBird. Look useful.

查看更多
混吃等死
6楼-- · 2019-04-30 11:34

Take a look at NMock2. It's a open-source mocking product and allows you to create "virtual" implementations for interfaces that support rich and deep interaction.

For example, if your WS interface is called IService and has a Data GetData() method, you can create a mock that requires the method to be called once and returns a new Data object:

var testService = mockery.NewMock<IService>();
Expect
    .Once
    .On(testService)
    .Method("GetService")
    .WithNoArguments()
    .Will(
        Return.Value(new Data());

At the end of the test, call mockery.VerifyAllExpectationsHaveBeenMet() to assure that the GetData method was actually called.

P.S.: don't confuse the "NMock2" project with the "NMock RC2", which is also called "nmock2" on sourceforge. NMock2-the-project seems to have superseded NMock.

查看更多
登录 后发表回答