Sunday, October 12, 2008

How to test web services using Nunit

Hi Guys,

this is my first technical blog, so apologies If I make any mistake, do revert me back in case you didnt understood what is written below.

I used Nunit in unconventional way(as a Automation Tester) on my project.

We can do functional testing using Nunit.all we need for this is proxy classes, 
service classes which invoke web service methods.

Suppose you have web service named Login which accepts login credentials as input parameters along with some enviormental values. 
On successful response If Login service responds  in user's first name to be sent back.

Key things to be tested in web service is 
  • Request object content(what all fields are required for request object) for manual tester sometimes it seems to technical or too low level, but for functional testing this is need of project.
  • Responset object content(what all fields are sent in response obj) .

Nunit test should be of 3 steps
  1. fill request object needed for service method call(in our case Login (request Object))
  2. call service method- in our case  verifiedCustomer successfulCustomerLogin = Login(request Obj);
  3. Assert on Response fields.
Example

namespace OurFunctionalTests.Tests
{
    [TestFixture]
    public class LoginWebServiceTest
    {

        private static readonly string DATE_OF_BIRTH = new DateTime(1960, 02, 01).ToShortDateString();


        
        [Test, Category("SmokeTest")]
        public void SucessfulLoginCustomer()
        {

// Register user whose login credentials to be used - setting up Test data
            registerUserAndSetEmail();

// fill request object needed for service method call
   LoginRequest  loginRequestObj = new Credentials(){
                                            EmailAddress = alreadyRegisteredCustomersEmailId,
                                            Password = "Test12",
                                            EnvIdentity = new EnvIdentityType()
                                                   {EntryId = 12, ManagedId = 1}};

// call service method- in our case  verifiedCustomer successfulCustomerLogin = Login(request Obj);
            VerifiedCustomer successfulLoginCustomerDetails =
                new LoginServiceClient().Login(loginRequestObj );


// Assert on   Response Object Content  -  Jack is User name sent in successful Login response   Assert.AreEqual("Jack",successfulLoginCustomerDetails.FirstName.ToString());

// Clear/flush Test data - delete query to database
            new CommonMethods().flushAddedRegisteredUser(alreadyRegisteredCustomersEmailId);
        }

No comments: