XUnit - InvalidOperationException : Solution root could not be located using application root

爱⌒轻易说出口 提交于 2021-02-08 06:20:07

问题


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

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.

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)

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?

Edit: 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.


回答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

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.

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

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.



来源:https://stackoverflow.com/questions/54495319/xunit-invalidoperationexception-solution-root-could-not-be-located-using-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!