Visual Studio: Unit Testing a Web Project in the Same Solution

I have a solution with a WebAPI project and a VS Test project.

My tests are making API calls with RestSharp through the Url rather than instantiating the controller itself and injecting things (context, etc.).

My question is, is there a way to tell the test project to start the web project through IIS Express when the test run starts? I am currently just running two instances of VS, one with network, projected with debug, and the other running a test suite

+3


source to share


3 answers


I would not recommend using networking in a unit test Web API. You open yourself up to potential doughiness and you end up testing a lot more than the service itself.

But if you really have to do this, perhaps to verify that your client can communicate with the API, then I suggest you go to the self-hosting service:

http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api

Self-hosting lets you run a server for a web API with just a few lines of code, and you can run your tests on that server where you want. In most cases, this should behave the same as your service hosted on IIS Express. But there are some important differences. For example, you won't be able to use some of the System.Web concepts that you can use (like HttpContext.Current).



Update:

I wrote a blog post about testing web API services that might help - http://blogs.msdn.com/b/youssefm/archive/2013/01/28/writing-tests-for-an-asp-net- webapi-service.aspx

Hope it helps.

+1


source


I know this is an old post, but I just ran into this problem. I can provide a more detailed answer if / when anyone reads this.

In short .. I created a console application referencing the unit test build and through reflection and a simple menu system you can run any of your tests.



Then I install multiple startup projects as a web project and a console project.

Then I can F5 and debug both the unit test and the web project from the same session. Multiple solutions are not required to join the process.

+1


source


If you are trying to debug both test and service, consider this

  • Start to debug a web service in Visual Studio.
  • Click Debug

    โ†’ Detach All

    . IIS Express will work.
  • Set a breakpoint and run Debug Unit Test.
  • (Skip this if you do not need to debug a web service) Press Debug

    โ†’ Attach to Process

    . Find iisexpress.exe

    and attach.

However, you lost Edit and Continue on your webservice that got disconnected.

+1


source







All Articles