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

查看:107
本文介绍了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禁用了卷影复制.

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:\Users\mihai\Desktop\publish\.
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.

同时,我已经通过

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

关于如何克服这一点并从publish文件夹运行测试的任何想法?

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.

推荐答案

请参阅文章如您所见, WebApplicationFactory 类需要推断内容根路径. WebApplicationFactory WebApplicationFactoryContentRootAttribute 中找到路径(如果已定义).如果未找到该属性,则搜索解决方案( .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 文件复制到了发布"文件夹的父文件夹中.我猜只是创建虚拟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天全站免登陆