Visual Studio: Unit Testing a web project in the same solution - visual-studio

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

If you are trying to debug both the test and the service, consider doing this
Launch to debug your Web service in Visual Studio.
Click Debug -> Detach All. IIS Express will keep running.
Set a break point and start Debuging your Unit Test.
(Skip this if you don't need to debug the web service) Click Debug -> Attach to Process. Find iisexpress.exe and attach.
However, you lose Edit and Continue on your web service which was detached.

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.

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.

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

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.

Related

Launching integration test (MSTest) after starting the API without using two instances of Visual Studio

I have a .NET WebApi and an MSTest Project that contains the integration tests to call the API via HttpClient.
Because I did not find a way how to let the MSTests run while the API is started, I start Visual Studio 2022 two times.
One instance with the launched API and then another instance where I can run the tests.
But this is not a nice solution, because pretty often both projects are trying to "build" in the same location.
This leads to error messages looking like this:
There must be a better way, but defining multiple startup projects also does not work.
The best would be to start my Integration Tests a short while automatically after launching the main API Project.
How would you do this?

Test Project in Visual Studio 2010 for a Windows Service Project

I am working now in a Windows Service Project and I am having a lot of headaches trying to debug the service, I found a lot of article on how to debug a service but none of them worked for me :(
How to: Debug Windows Service Applications
There is no way after attach the process to enter in my breakpoints (set breakpoints everywhere)
Then I found another creating a windows form inside the project but that Solution is kind of dirty (but a solution).Testing a Windows Service From Within Visual Studio
So the real question is, is there any possible way to create a test project to test the service? (I don't have experience with testing, but I know is a good practice)
Do you want to debug the service or code that service runs?
In the past I would create one project for a service, one project for a library that contains the logic, and one project for a console app that would be used to test and debug the logic locally. That setup covered most of the debugging needs.
Hope this helps!

Starting asmx service when running unit tests

We have a VS 2010 Solution with many projects.
One of the projects contains asmx services.
We have a problem that our tests fail if the services are not running.
What is the best way to make sure that the services are started when the tests run? We need to get this to work both on the developers machine and on the build server, where we use TFS.
Found a solution, it was to use WebDev.WebServer40.exe

Debugging Visual Studio 2010 Unit Test and WCF Service in one IDE instance

I have created a WCF service in Visual Studio 2010 along with some supporting assemblies. I have also created a test project which contains multiple unit tests for the service and the supporting assemblies. Right now I have them all in one solution with the Test project having a service reference (http) to the WCF service.
If I debug the WCF service and select "Run checked tests" in a Test List I created, I can debug the WCF service without a problem. Note: I cannot select Debug Checked Tests while debugging the WCF service. (Because the IDE is already debugging?)
If I open the Test project in another instance of VS 2010, debug the WCF service and then select "Debug Checked Tests" - I can debug both my tests and the WCF service.
However - I would like to (and my question is) be able to debug my tests and my service in a single IDE. Is this possible?
I choose to host the wcf service in the same test process. So the test (maybe in the setup fixture) creates and opens the service host and at the end of the test, it closes/stops the service host. This will make debugging a breeze.
Are you running the WCF service in Cassini? If so, you should be fine, provided you have a service reference to the project from the calling code. It steps right through.
Hmm... at least it does in 2008, and I assume it does in 2010. I haven't tried it yet for 2k10; I'll go have a go at it and be back with a report.
Edit: Yep, it does.

Can I debug two local projects running on IIS (not Cassini)

I have a WCF service and a web application that both need to be hosted in local IIS virtual directories. I start up the WCF project and then when I try to debug the web app at the same time a popup tells me "Unable to start debugging on the web server. Unable to do an AutoAttach." The same problem happens if I try to manually attach to aspnet_wp.exe after the WCF service has already started.
Have you tried firing them both off somehow (basically to get processes) then attaching the debugger to each manually?
Thanks Wyatt,
I created a solution with both projects. Then I set the Startup projects to have both projects start without debugging. Then after they start, I manually attach to the process and get debugging on both sides.

Resources