XUnit - InvalidOperationException:无法使用应用程序根定位解决方案根 [英] XUnit - InvalidOperationException : Solution root could not be located using application root

查看:20
本文介绍了XUnit - InvalidOperationException:无法使用应用程序根定位解决方案根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Web-API .NET Core 2.2 项目,我正在尝试对其进行集成测试.

I have a Web-API .NET Core 2.2 project and I am trying to do integration tests on it.

我遵循了 微软.当从测试运行器和命令行启动它们时,测试通过,它们应该被正确配置,我能够通过 WebApplicationFactory - HTTP 客户端调用控制器.我已经通过 xunit.runner.json 禁用了 shadow copy 到目前为止一切顺利.

I have followed the guide from Microsoft. The tests pass when starting them from the test runner and from the command line, they should be configured correctly, I am able to call the controllers via the WebApplicationFactory - HTTP client. I have disabled shadow copy via xunit.runner.json So far so good.

我正在尝试使用 Azure DevOps 来部署我的项目,并且在发布步骤中,我添加了一个测试步骤,用于获取构建工件、下载它并运行测试.基本上,在本地机器上复制,这将转化为:

I am trying to use Azure DevOps to deploy my project and at the release step, I've added a test step that takes the build artifact, downloads it and runs the test. Basically, replicated on the local machine, this would translate into:

dotnet publish testProject.csproj <path_on_disk>
cd <path_on_disk>
vstest.console.exe testProject.dll

从已发布的文件夹运行测试时,出现以下错误

When running the tests from a published folder, I get the following error

Failed   wInvoiceIntegrationTests.Controllers.Authentication.AuthenticationControllerTests.AuthenticationController_Register_InputDoesNotRespectsModel_DoesNotRegistersUser(email: "abcd@@.com", password: "Abcdefgh1")
Error Message:
 System.InvalidOperationException : Solution root could not be located using application root C:UsersmihaiDesktoppublish.
Stack Trace:
   at Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(IWebHostBuilder builder, String solutionRelativePath, String applicationBasePath, String solutionName)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.SetContentRoot(IWebHostBuilder builder)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
   at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)

我没有需要提供的静态文件,也不想提供,我只是想测试控制器.路径正确,就是测试dll所在的位置,项目dll也包含在内.

I have no static files that I need to serve nor do I want to, I am just trying to test the controller. The path is correct, that's where the test dlls are and the project dlls are also included.

与此同时,在构建阶段,我通过

In the meantime I've managed to get the tests to run on azure, at the build stage via

ls *Tests*/*.csproj | xargs -L1 dotnet test --logger:trx

关于如何克服这个问题并从发布文件夹运行测试的任何想法?

Any ideas on how to overcome this and run the tests from the publish folder?

基本上从输出目录中的命令行和 VS 测试资源管理器运行测试是可以的,但从发布目录运行是不行的.

Basically running the tests from the command line in the output directory and from the VS test explorer is ok, but running from the publish directory is not ok.

推荐答案

请参阅文章的这一部分 https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2#how-the-test-infrastructure-infers-the-app-content-root-path-1

Please see this part of the article https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-2.2#how-the-test-infrastructure-infers-the-app-content-root-path-1

如您所见,WebApplicationFactory 类需要推断内容根路径.WebApplicationFactoryWebApplicationFactoryContentRootAttribute 中查找路径(如果已定义).如果未找到该属性,则搜索解决方案 (.sln) 文件,将包含解决方案文件的文件夹设置为内容根路径.您似乎既没有在测试项目中定义 WebApplicationFactoryContentRootAttribute,也没有在包含已发布 dll (testProject.dll) 的目录的父目录中定义 .sln 文件.

As you can see WebApplicationFactory class needs to infer the content root path. WebApplicationFactory finds the path from WebApplicationFactoryContentRootAttribute if defined. If that attribute is not found then solution (.sln) file is searched, the folder containing solution file is set to be the content root path. It seems neither you define WebApplicationFactoryContentRootAttribute in your test project nor the parent directory of the directory containing published dlls (testProject.dll) contains a .sln file.

作为在运行测试之前的解决方法,我将 sln 文件从我的项目复制到publish"文件夹的父文件夹中.我想创建虚拟 sln 文件也有帮助.我像这样从父目录运行我的测试命令:

As workaround just before running tests I copied sln file from my project into the parent folder of "publish" folder. I guess just creating dummy sln file could help as well. I run my test command from the parent directory like this :

dotnet test ./<publish folder>/testProject.dll

注意 ./是复制 sln 文件的父目录,在您的示例中,发布文件夹"是path_on_disk"

Note ./ is the parent directory where sln file is copied an "publish folder" is "path_on_disk" in your example

在我的情况下,出现此附加错误后显示缺少目录.我刚刚在父目录中创建了具有确切名称的空目录,并修复了错误.希望这也能帮到你.

In my case after this additional errors came up showing missing directory. I have just created empty directory with exact name in the parent directory and it fixed the errors. Hope this could help you as well.

这篇关于XUnit - InvalidOperationException:无法使用应用程序根定位解决方案根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆