运行没有调试的ASP.NET Core项目后运行测试会在运行测试之前停止运行项目

时间:2017-08-02 23:20:19

标签: asp.net asp.net-mvc visual-studio visual-studio-2017 visual-studio-mac

这可能是Visual Studio for Mac中不存在的功能,但我想我可能会在这里询问我是否做错了。

我将ASP.NET Core MVC项目设置为启动项目。

我还有一组针对.NET Core 2.0的单元测试,它使用Selenium来驱动UI测试。为了运行这些测试,我需要先运行ASP.NET Core MVC才能运行测试,否则它们会因为应用程序没有运行而失败。

在Visual Studio的PC版本中,"开始无需调试" " Debug"中的选项菜单按预期启动Web应用程序。我的浏览器打开我的应用程序。一旦我确认它正在运行,我就可以毫无问题地运行测试(通过测试资源管理器)。

在Mac版的Visual Studio中,"开始时无需调试" " Debug"中的选项菜单按预期启动Web应用程序。我的浏览器打开我的应用程序。一旦我确认它正在运行,我就会尝试运行测试。测试确实在运行之前运行,它会杀死正在运行的应用程序进程。由于应用程序不再运行,测试失败。

TL; DR:

我的解决方案:

  • ASP.NET核心MVC项目
  • 使用Selenium的.NET核心单元测试项目

以下是VS的两个实例的步骤:

  • 点击"调试"(PC)或"运行"(Mac)
  • 点击"开始无需调试"
  • 等待应用程序显示在浏览器中(不要关闭它)
  • 转到测试资源管理器(PC)或单元测试(Mac)
  • 运行UI测试套件

预期:测试通过PC和Mac版本

实际:Mac版测试失败,因为在运行测试之前Web应用程序进程被终止

有没有人对我如何实现我想要实现的目标有任何建议?

1 个答案:

答案 0 :(得分:1)

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.