I would like to learn how to test code that I wrote that relies on an external resource.
There is a website that I scrape data from. I then parse it and save to a strongly typed domain object.
I have unit tests that test this but because they rely on an external resource - having an internet connection that has access to the website - I can't depend on the tests being able to execute at all times.
I don't want to moq the website (there is no interface anyhow), or setup local html source text files. What I would prefer is in my nunit Setup() method to create a web server that can dish up the urls just like the external website would do. Perhaps it would redirect to localhost instead of the website.
I'm not looking for a specific solution. I instead would like guidance on best practice for handling this testing situation. Especially if there is a recommended .Net library or guidance from Microsoft.
Related
I'm learning ASP.Net MVC and I've read that it is easy to unit test as controllers can be instantiated and have their methods called without needing to deploy to a web server, and that it is easy to mock dependent objects etc. (see http://msdn.microsoft.com/en-us/library/gg416511(VS.98).aspx and multiple other references).
It's also possible to run acceptance level tests on the MVC App through the browser using Selenium or Waitin using your own favourite framework to author and control the tests.
What I'd like to know is if it's possible to run ATDD tests at an acceptance test level by calling the controller methods in a deployed, running MVC application, e.g. from Specflow, rather than having to test through the UI?
Or more generally, is it possible to programatically call the API of the ASP.Net MVC application from a test?
It seems like there is a gap in what it is possible to test between unit testing and browser based UI testing. Has anyone been able to bridge that gap?
Your question isn't clear but if you are asking what I think you are:
MVC controllers (generally) obey REST.
You can call any action on a controller (via a web server like IIS) by sending it a HTTP request. It will return a HTTP response containing the relevant data and data type(HTML, json, XML, etc.). So yes you can programatically call a controller like an API (if by API you mean something you send HTTP requests to and get HTTP responses from) therefore yes you can test the controller without using the UI.
I am exposing a service using asp.net web API. each get/put/post calls internally corresponding WCF method after doing some logic like converting passed objects.
(using apicontroller, not odatacontroller)
I would like to test the written 'logic' but I can't make the WCF call yet as it is being written by some other team (for which I will write stub). I'm planning to write a test hook in each get/put.
My requirement is to write simple client code (not using paid tool, but a simple .exe which can test a running service) to test the logic written.
How do I go about it?
If you want to test Web API i suggest using this neat tool called RestSharp, its free not paid
Here is the URL : http://restsharp.org/
You can get it through Nuget Package Manager.
Once you install it, you would be able to test your service pretty easily.
It has examples to get you started on the first page.
I have been recently added to a project where I will need to be doing functional and load testing for some existing Web Services (SOAP).
I was wondering if it is possible to call a web service from a console application for testing purposes? I do not have permissions to code in the existing Project so I need to create a new one, which is what my real issue is, I do not know how to call the web services (or the web service code) from outside of their Solution.
I know this will make things more difficult, especially for recording test results, but I have no choice as I am a contractor and can not get the required permissions. I can pull code and see the source code, I just don't want to code in the existing project as my local changes will get blown away every time there is new code to pull down.
I have done some basic testing using SoapUI, but the open Source version does not meet all the requirements I have and would rather just write my own tests then try and find a free 3rd party solution (as I also have no budget).
Thanks,
cwlovell13
You can write a simple WCF client to consume existing Web Services (SOAP).
Maybe you can reference this .
Accessing Services Using a WCF Client
I am trying to test a web service.
My first try was using unit tests which got really complicated because of the need to mock things like HttpContext (that actually was the main reason).
So now I'm trying a different angle - I know I can send a httpRequest using a c# application, but can this application for example maintain the cookies I'll receive from the web service?
Is it a possible and reasonable way to test a web service?
Thank you.
The logic 'hidden' behind the web service should be testable as unit-tests. The fact the results are returned across the wire is more of an integration test. Maintaining cookies across calls seems like an integration test to me.
BTW, designing stateless web service calls is desireable where possible.
Yes, That's possible for every practical use. As for the cookies example, see here.
What is the best way to test web service using NUnit.
I want it to be automated, i.e, I don't want to have a separate process to host the web service for the testing code to consume.
The web service is just a plain-old-.net class. You can instantiate it directly and call its methods in a unit test.
That won't allow you to test http specific aspects of web services like authentication at the protocol level, but I would say that there's no getting around using a web server for that.
It depends on what you want to test. It's possible to do full integration tests and there is some value in that (checking serialization, for instance). One simple way to get good test coverage with minimal work is as follows:
Write one or more plain old classes that do the real work (use TDD if desired)
Test these classes in isolation
Have your WebMethods delegate to these classes.
Depends. If it's an asmx, you can use HostableWebCore on Vista and higher. If it's WCF, just self-host by creating an instance of ServiceHost in your process. You could directly instantiate the service impl, but if you have any HTTP-isms (HttpContext, Request/Reponse access, cookies, etc), you'll have to mock them.