Chuan Chuan Law

DevOps | Software Automation | Continuous Integration

Tag: .NET

How To Automate Web Services Testing

Testing web services is similar to testing API. Basically, all we need is a framework that:

  1. Calls the web services
  2. Checks the response
  3. Asset the value returned accordingly

The framework we choose is dependent on the framework of the web service itself, for example, if the web service is written in Java, we would want to use Java to test, if its written in .NET, we would use C# to test, etc.

In the example above, we:

  • Create a class extending WebClient

public class ExtendWebClient : WebClient

  •  Create a variable of class ExtendWebClient with overridden timeout

var client=new ExtendedClient(_TIMEOUT)

  • Set the header of the request

client.Headers.Add(HttpRequestHeader.ContentType,”text/xml”)

  • Set the XML data to be sent as string request

string request =”your XML here”

  • Calls the web service by passing the web services URL and XML (string)

 client.UploadString(new Uri(String.Format(“{0}/ADAuthenticate/ADAuthenticate.asmx”, _WEBSERVICEBASEURL)), request);

  • Web client will automatically return a success code (500) and error code otherwise

API Testing In General (PUT & DELETE cont.)

I have putting a few blogs about API testing in the past few months. I have been doing some research about how to test API, best tool to test API, etc and I guess there is no exact answer to this. However, SoapUI seems to appear in quite a number of searches. But I still do not choose SoapUI as it is not a pure Open Source tool.

So, I still think it all depends on the language of your API system, and there is a lot of built-in library to make a web request. All you need to do is to incorporate a unit testing framework to assert the result returned.

As I am working in a .NET environment, I have tried HttpClient and WebClient. I have used Curl for Ruby.

Example of POST and GET have been posted in the previous blogs.

Example of PUT is as below:

 

And DELETE:

 

Testing API in C# – WebClient – How To Get A Request

Below is how to Get a request using WebClient instead of HTTPClient

 

Testing API In C# – HttpClient – How To Get A Request

Back in days when I was working with Ruby on a MacBook Pro, I got Curl. However, now as I am using a PC in a .NET shop, I need to use compatible technology to test API – HttpClient is my solution.

Install the Web API Client Libraries via Package Manager Console

Install-Package Microsoft.AspNet.WebApi.Client

Sample code as below:


  • [Fact] is merely an XUnit notation indicating that this is a unit test
  • using creates an HttpClient instance which is only active within the scope
  • await suspends operation until the execution is completed
  • Method is declared as async as HttpClient methods perform I/O
  • ReadAsStringAsync() will read the output as string
  • JsonConvert.Deserializeobject<> will parse the string into the object model LanguageDictionary
  • You can use many methods from the HttpResponseMessage to verify the response from the API

 

Unable To Build After Upgrade To MVC4 Targeting 4.0 and VS2010

If you have upgraded your MVC3 project to MVC4 running in VS2010, and still targeting the 4.0 framework, you will have problem while running your build as below:

To fix this problem, you need to do the following:

  1. Unload the project in Visual Studio
    2.  Edit the .csproj file

3. Comment out the lines as below:

 

  4. Reload the project
  5. Re-build and you should see a success build!

 

Automating Email Testing For .Net – netDumsbter

Email testing in Ruby On Rails application is very easy because of the Action Mailer framework.

However, .NET does not have such similar framework. After doing a series of research, I finally found something that could help a little – netDumsbter.

netDumsbter merely is a fake .NET SMTP server derived from Dumsbter which is popular for Java based applications.

We will use System.Net.Mail to as the SMTP client to send emails.

The example below is how netDumsbter is used:


#Initialises a new SMTP server
SimpleSmtpServer _Server;

#Starting the server at a random port each time, else it will throw errors
Random _Rnd = new Random();
_Server = SimpleSmtpServer.Start(_Rnd.Next(50000, 60000));

#Initialise a SMTP client by using the .Net “System.Net.Mail” library
SmtpClient client = new SmtpClient(“localhost”, _Server.Port);

#Creating a new email message and send it using the System.Net.Mail function
var mailMessage = new MailMessag(“cfm@mendible.com”, “kbm@mendible.com”, “test”, “test tes test”);
client.Send(mailMessage);

#Prints out how many email received via the fake SMTP server. In this example, we will get 1
Console.Write(_Server.ReceivedEmailCount);

#Stopping the server
_Server.Stop();

Conclusion

– .Net does not provide a good framework for email testing automation like Ruby

– The combination of netDumster and System.Net.Mail is good for unit testing purposes but not integration or functional testing

Setting Up BDD Testing Framework For .NET Applications Using SpecFlow, NUnit And Selenium

1. Download and install the following files:

SpecFlow.msi

NuGet

NUnit.msi

Selenium webdriver for C#.zip

Resharper

2. Open up Visual Stdio 2010 and using NuGet, add reference for SpecFlow and NUnit.

3. To add reference for Selenium, you will need to right click->Add Reference-> browse to the Selenium folder and add all DLL files.

4. Include the following configuration for SpecFlow into App.config under the SpecFlow tag

unitTestProvider name=”NUnit”

5. Resharper is needed for integration of NUnit and Ms Visual Studio.

* Resharper 6 has problem integrating with Gallio (MbUnit) test

5. Right click on the project name->Add->New Item, you will see SpecFlow feature file

6. Use Class Library to create a normal Selenium project

7. To get Selenium working in IE, add the code below:

using OpenQA.Selenium.IE; (in library)

IWebDriver driver = new InternetExplorerDriver();

* Window switching does not work properly on IE8

* Have to set Tools->Internet Options->Security->Protected Mode to be the same for all zone for IE7 or higher on Windows Vista or Windows 7

© 2019 Chuan Chuan Law

Theme by Anders NorenUp ↑