Gaurav’s Monologue

on Software Architecture, .NET Framework and Connected System Technology

Web Services Test Automation

Posted by intrepiddeveloper on November 28, 2008

Generate the Service Proxy and Configuration from the exposed WSDL of the service being tested. The simplest option is to use SvcUtil and drop the proxy and config in your test projects solution folder.

svcutil.exe /t:code https://MyWebService.com/Service.svc / out:C:\[project path ]\Proxy.cs /config:C:\ [project path ]\App.config

I run a simple scripted bat file to do this task. The other option is to automate this step with a build task so when you compile your solution the build will generate and drop the Proxy and Configuration at the right location.

Now that you have a service proxy to code against, you can use testing frameworks like NUnit [www.nunit.org/] to write simple unit tests for each service operation or write more detailed scenario tests which make a series of service calls to accomplish a functional business scenario.

This is a sample shell class for hosting the tests as below.

public class TestWebServices {
    public TestWebServicesClient ClientProxy { get; set; }
    public ChannelFactory<itestwebservices> factory { get; set; }
    public ITestWebServices Proxy { get; set; }

[SetUp]
public void TestSetUp()
{
     ServicePointManager.ServerCertificateValidationCallback +=
     new System.Net.Security.RemoteCertificateValidationCallback(RemoteCertValidate);
}

/// <summary>
/// All Clean-up work goes here
/// </summary>
[TearDown]
public void TestTearDown()
{

}

//This is to take care of SSL certification validation which are not issued by Trusted Root CA. Recommended for testing  only.
protected bool RemoteCertValidate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
     //Cert Validation Logic
     return true;
}

[TestFixtureTearDown]
public void CloseChannel()
{
     factory.Close();
}
[TestFixture]
public class Agent : BaseTest
{
    [TestFixtureSetUp]
    public void Initialize()
    {
     try
       {
            factory = new ChannelFactory</itestwebservices><itestwebservices>("ConfigurationBinding");
            factory.Credentials.UserName.UserName = "admin";
            factory.Credentials.UserName.Password = "admin";
            factory.Credentials.ServiceCertificate.Authentication.RevocationMode = X509RevocationMode.NoCheck;
            Proxy = factory.CreateChannel();
        }
    catch (Exception ex)
    {
        Assert.Fail(string.Format("Test Setup threw an Exception: {0} ", ex.Message));
    }
   }

    [Test()]
    public void TestNumber01();
    [Test()]
    public void TestNumber02();
    [Test()]
    public void TestScenarioNumber01();
  }

}

And finally you can run this test assembly in the stand alone NUnit console.

NUnitConsole


Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.