You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WebApplicationFactory - InvalidOperationException: Solution root could not be located using application root - When Testing From Docker Container
#27613
/// <summary>/// Set the content root to path relative to sln, e.g. Src/WebApp/// Configure the webhost using appsettings.json from the path where the assembly is located for /// the startup class. The Autofac container is configured here also./// </summary>/// <seealso cref="Microsoft.AspNetCore.Mvc.Testing.ConfigureWebHost(IWebHostBuilder)"/></seealso>protectedoverridevoidConfigureWebHost(IWebHostBuilderbuilder){stringpath=Assembly.GetAssembly(typeof(WebApiTestFactory<TStartupClass>)).Location;builder.UseSolutionRelativeContentRoot(_contentRoot).ConfigureAppConfiguration(cb =>{cb.AddJsonFile($"{Path.GetDirectoryName(path)}/appsettings.json",optional:false).AddEnvironmentVariables();}).ConfigureServices(services =>services.AddAutofac());base.ConfigureWebHost(builder);}
This uses UseSolutionRelativeContentRoot to set the path for content root that is relative to the directory containing *.sln files.
My integration test runs successfully within local development environment with a startup class inherited from the SUT startup.
However, when I run the test within a docker container an InvalidOperationException is thrown: Solution root could not be located using application root
I also have an xunit.runner.json to disable shadow copy:
{
"shadowCopy": false
}
Any ideas how I can solve this?
Exception
A total of 1 test files matched the specified pattern.
/Tests/FunctionalTests/WebApp.FunctionalTests/bin/Debug/netcoreapp3.1/WebApp.FunctionalTests.dll
[xUnit.net 00:00:00.00] xUnit.net VSTest Adapter v2.4.1 (64-bit .NET Core 3.1.9)
[xUnit.net 00:00:00.88] Discovering: WebApp.FunctionalTests
[xUnit.net 00:00:00.94] Discovered: WebApp.FunctionalTests
[xUnit.net 00:00:00.95] Starting: WebApp.FunctionalTests
[xUnit.net 00:00:01.17] WebApp.FunctionalTests.ApiTest.WebApp_ApiController_DownloadImage [FAIL]
[xUnit.net 00:00:01.17] System.InvalidOperationException : Solution root could not be located using application root /Tests/FunctionalTests/WebApp.FunctionalTests/bin/Debug/netcoreapp3.1/.
[xUnit.net 00:00:01.18] Stack Trace:
[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(IWebHostBuilder builder, String solutionRelativePath, String applicationBasePath, String solutionName)
[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.SetContentRoot(IWebHostBuilder builder)[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
[xUnit.net 00:00:01.18] at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()
[xUnit.net 00:00:01.18] /Tests/FunctionalTests/WebApp.FunctionalTests/ApiTest.cs(45,0): at WebApp.FunctionalTests.ApiTest.WebApp_ApiController_DownloadImage()
[xUnit.net 00:00:01.18] --- End of stack trace from previous location where exception was thrown ---
[xUnit.net 00:00:01.19] Finished: WebApp.FunctionalTests
X WebApp.FunctionalTests.ApiTest.WebApp_ApiController_DownloadImage [93ms]
Error Message:
System.InvalidOperationException : Solution root could not be located using application root /Tests/FunctionalTests/WebApp.FunctionalTests/bin/Debug/netcoreapp3.1/.
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.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers) at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()
at WebApp.FunctionalTests.ApiTest.WebApp_ApiController_DownloadImage() in /Tests/FunctionalTests/WebApp.FunctionalTests/ApiTest.cs:line 45
--- End of stack trace from previous location where exception was thrown ---
Test Run Failed.
Total tests: 1
Failed: 1
Total time: 2.0426 Seconds
/usr/share/dotnet/sdk/3.1.403/Microsoft.TestPlatform.targets(32,5): error MSB4181: The "Microsoft.TestPlatform.Build.Tasks.VSTestTask" task returned false but did not log an error. [/Tests/FunctionalTests/WebApp.FunctionalTests/WebApp.FunctionalTests.csproj]
Also tried adding WebApplicationFactoryContentRoot attribute in Test Project:
usingSystem;usingSystem.IO;usingSystem.Reflection;usingSystem.Text;usingSystem.Threading.Tasks;usingMicrosoft.AspNetCore.Hosting;usingAutofac.Extensions.DependencyInjection;usingMicrosoft.Extensions.DependencyInjection;usingMicrosoft.AspNetCore.Mvc.Testing;usingXunit;usingXunit.Abstractions;usingWebApp.S3.Contracts;usingWebApp.Functional.Utilities;/// Key should match FullName assembly attributed of TStartup : WebApplicationFactory<TStartup> /// https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.mvc.testing.webapplicationfactorycontentrootattribute?view=aspnetcore-3.0[assembly:WebApplicationFactoryContentRoot(key:"WebApp.FunctionalTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",contentRootPath:"../../../../../../Src/WebApp",contentRootTest:"Program.cs",priority:"1")]namespaceWebApp.FunctionalTests{publicclassApiTest{privateTestingBase<WebAppTestStartup>_testing;privatestring_settingsFile;privatestringGetSettingsFile(){conststringAspNetCoreEnvironment="ASPNETCORE_ENVIRONMENT";conststringDefaultSettingsFile="appsettings.Local.json";varenvVar=System.Environment.GetEnvironmentVariable(AspNetCoreEnvironment);if(envVar!=null)return$"appsettings.{envVar}.json";elsereturnDefaultSettingsFile;}/// <summary>/// Create Web Application Factory with content root at Src/WebApp using/// appsettings file derived from ASPNETCORE_ENVIRONMENT variable. If/// the variable is unset then defaults to appsettings.Local.json/// </summary>/// <param name="output">xUnit output stream</param>publicApiTest(ITestOutputHelperoutput){conststringcontentRoot="Src/WebApp";_settingsFile=GetSettingsFile();stringstartupPath=Assembly.GetAssembly(typeof(WebApiTestFactory<WebAppTestStartup>)).Location;stringsettingsFilePath=$"{Path.GetDirectoryName(startupPath)}/{_settingsFile}";_testing=newTestingBase<WebAppTestStartup>(output,contentRoot,settingsFilePath);}[Fact]publicasyncTaskWebApp_ApiController_DownloadImage(){conststringTestData="test";conststringObjectName="objectName";byte[]Payload=Encoding.UTF8.GetBytes(TestData);// upload a sample imageusing(varclient=_testing.Factory.CreateClient())using(varscope=_testing.Factory.Server.Host.Services.CreateScope())using(varbyteStream=newMemoryStream(Payload))using(varstream=newBufferedStream(byteStream)){vars3Client=scope.ServiceProvider.GetRequiredService<IS3Service>();awaits3Client.UploadAsync(stream,ObjectName);varresponse=awaitclient.GetAsync(ApiRoutes.Get.Image(ObjectName));response.EnsureSuccessStatusCode();stringresult=awaitresponse.Content.ReadAsStringAsync();Assert.Equal(TestData,result);}}}}
I have since created a smaller repository that highlights this issue with a much simpler Dockerfile. As far as I can tell the docker image file structure mirrors the structure of the local development environment.
git clone https://github.com/dcs3spp/WebApplicationFactoryContentRoot.git
cd WebApplicationFactoryContentRoot/Tests/FunctionalTests/WebApp.FunctionalTests.UseSolutionRelativeContentRoot
# run test locally succeeds
dotnet test# move to root folder to to build and run docker container containing this testcd ../../../
# build docker image called docker-webapp
DOCKER_BUILDKIT=1 docker build -f Docker/Dockerfile -t docker-webapp .# run a docker container containing this test - fails given solution root error
docker run --rm --entrypoint=bash -it docker-webapp
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I am learning ASP.NET Core 3.1 and have managed to create my own WebApplicationFactory class with the ConfigureWebHost method overriden as follows:
This uses
UseSolutionRelativeContentRoot
to set the path for content root that is relative to the directory containing *.sln files.My integration test runs successfully within local development environment with a startup class inherited from the SUT startup.
However, when I run the test within a docker container an
InvalidOperationException
is thrown:Solution root could not be located using application root
I also have an xunit.runner.json to disable shadow copy:
Any ideas how I can solve this?
Exception
Test Project
Also added Microsoft.AspNetCore.Mvc.Testing dependency (as detailed here) to the test project and still get the same result:
Also tried adding WebApplicationFactoryContentRoot attribute in Test Project:
I have since created a smaller repository that highlights this issue with a much simpler Dockerfile. As far as I can tell the docker image file structure mirrors the structure of the local development environment.
Beta Was this translation helpful? Give feedback.
All reactions