Search code examples
asp.netasp.net-mvcvisual-studiovisual-studio-2017visual-studio-mac

Running tests after running a ASP.NET Core project without debugging stops running project before running tests


This might be a feature that just doesn't exist in Visual Studio for Mac but I thought I might ask here to see if I'm doing something wrong.

I have an ASP.NET Core MVC project set as the startup project.

I also have a set of Unit Tests targeting .NET Core 2.0 that use Selenium to drive UI tests. In order to run these tests, I need the ASP.NET Core MVC running first before the tests can run otherwise they will fail because the app is not running.

In the PC version of Visual Studio, the "Start without Debugging" option in the "Debug" menu starts the web app as expected. My browser opens to my app. Once I confirm it's running, I'm able to run the tests without any issue (through the test explorer).

In the Mac version of Visual Studio, the "Start without Debugging" option in the "Debug" menu starts the web app as expected. My browser opens to my app. Once I confirm it's running, I attempt to run the tests. The tests do run but before they run, it kills the running app process. Since the app is no longer running, the tests fail.

TL;DR:

My solution:

  • ASP.NET Core MVC project
  • .NET Core Unit test project using Selenium

Here are the steps on both instances of VS:

  • Click "Debug"(PC) or "Run"(Mac)
  • Click "Start Without Debugging"
  • Wait for the app to show up in browser (don't close it)
  • Go to Test Explorer (PC) or Unit Tests (Mac)
  • Run the suite of UI tests

Expected: Tests pass in both PC and Mac version

Actual: Tests fail in Mac version because web app process is killed before tests are run

Does anyone have any suggestions for how I can achieve what I'm trying to achieve?


Solution

  • Looking at the Visual Studio Mac source code there is a restriction that prevents this. Visual Studio for Mac keeps a track of all applications that it has run and when you try to run the unit test it will attempt to close these applications.

    Testing this with Visual Studio for Mac 7.1 a message dialog is displayed An application is already running and will have to be stopped. Do you want to continue? when you run the tests.

    Without changing the code you can only workaround this.

    Workarounds:

    1. Run a separate instance of Visual Studio for Mac and use one to launch your web application and the other to run the tests.
    2. Launch the web application from the tests itself when they are run.
    3. Create a run configuration that runs both the web project and the unit test project. However you would need to add some sort of delay into the unit tests since they will start at the same time as the web project.

    More detail about option 3. To create a run configuration right click the solution and select Options. Then in the Solution Options create a run configuration in the Run - Configurations section. Then select it and check the web project and unit test project. In the status bar you will need to your run configuration. This should work if you can run your unit test project in Visual Studio for Mac. A unit test project that uses NUnit can be set as the startup project and running it will run the tests.