DotNet UnitTest AppConfigFiles

Issue

When runing MsTests, the app.config file created is copied to the output folder with then name of the Test Project (Ex: MyTestProject.dll.config) But the process hosting is testhost.dll and the file shoul dbe named testhost.dll.config.

To automatically copy the file with the proper name, a simple hack in the csproj will solve the issue :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <IsPackable>false</IsPackable>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>
    ...
	<Target Name="CopyAppConfig" AfterTargets="Build" DependsOnTargets="Build" Condition="'$(OutDir)' != ''">
		<CreateItem Include="$(OutputPath)$(AssemblyName).dll.config">
			<Output TaskParameter="Include" ItemName="FilesToCopy" />
		</CreateItem>
		<Copy SourceFiles="@(FilesToCopy)" DestinationFiles="$(OutputPath)testhost.dll.config" />
	</Target>
</Project>