Reputation: 14278
I have a solution with a WebAPI project and a VS Test project.
My tests make calls to the API using RestSharp via a Url rather than instantiating the controller itself and injecting things (context etc).
My question, is there a way to tell the test project to launch the web project via IIS Express when a test run begins? Currently I just run two instances of VS, one with the web projected with debugging started and the other running the test package
Upvotes: 8
Views: 7034
Reputation: 12395
I wouldn't recommend using the network to unit test Web API. You open yourself up to potential flakiness in the test and you end up testing a whole lot more than the service itself.
But if you really must do so, maybe to test that your client can exchange information with the API, then I'd suggest you look into self-hosting the service:
http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api
Self-hosting lets you start up a server for Web API with just a few lines of code and you could have your tests start up this server in the right places. In most cases, this should behave the same as having your service hosted in IIS Express. But there are some important distinctions. For example, you won't be able to use some System.Web concepts you may be used to (like HttpContext.Current).
Update:
I've written a blog post about testing Web API services that might help - https://learn.microsoft.com/en-us/archive/blogs/youssefm/writing-tests-for-an-asp-net-web-api-service
Hope that helps.
Upvotes: 2
Reputation: 1037
I know that this is late but I use this solution:
Start two instances of Visual Studios. Start debugging the API in one VS and then debug the test in another.
Upvotes: 0
Reputation: 3332
Running your web service or site in release non-debug by CTRL-F5
makes it run independent from Visual Studio and you are free to run your Tests from inside VS
Upvotes: 4
Reputation: 1825
If you are trying to debug both the test and the service, consider doing this
Debug
-> Detach All
. IIS Express will keep running.Debug
-> Attach to Process
. Find iisexpress.exe
and attach.However, you lose Edit and Continue on your web service which was detached.
Upvotes: 5
Reputation: 1166
I know this is an old post but I was just faced with this issue. I can provide a more detailed response if / when anyone reads this.
In short.. I created a console app referencing the unit test assembly and via reflection and a simple menu system you can run any one of your tests.
I then set multiple startup projects to be the Web project and the console project.
I can then F5 and debug both the unit test and the Web project from within the same session. No attaching to a process of multiple solutions needed.
Upvotes: 3