尝试从xunit项目的调试文件夹启动exe时出错。
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet\'.
Failed to run as a self-contained app.
- The application was run as a self-contained app because 'C:\Users\me\Desktop\dotnet-testing-cli-issue\MyApp.Console.Tests\bin\Debug\net7.0\MyApp.Console.runtimeconfig.json' did not specify a framework.
- If this should be a framework-dependent app, specify the appropriate framework in 'C:\Users\me\Desktop\dotnet-testing-cli-issue\MyApp.Console.Tests\bin\Debug\net7.0\MyApp.Console.runtimeconfig.json'.
MyApp.Console.csproj:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
</Project>
Program.cs
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
MyApp.Console.Tests.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CliWrap" Version="3.6.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MyApp.Console\MyApp.Console.csproj" />
</ItemGroup>
</Project>
Tests.cs
public class UnitTest1
{
[Fact]
public async Task Test1Async()
{
var result = await Cli.Wrap("MyApp.Console.exe")
.WithArguments(new[] { "--foo", "bar" })
.ExecuteAsync();
}
}
完整复制:https://github.com/VictorioBerra/netcore-console-test-exe
这也可能是一个X-Y问题。我想测试我的控制台应用程序,包括一个使用通用主机的应用程序,即appsettings。
如何启动并测试它?
1条答案
按热度按时间vhmi4jdf1#
如果您构建解决方案并打开“MyApp.Console.Tests\bin\Debug\net7.0”和“MyApp.Console.Tests\bin\Debug\net7.0”文件夹并比较内容,您将看到后者的文件比前者多得多-这些文件是自包含应用附带的运行时的一部分,并且需要运行它。您可以设置一些精心设计的构建操作来执行正确的应用程序发布到某个路径,并在测试项目中使用它,但我认为这太复杂,而且可能太脆弱。
您可以切换到“旧”
Program.Main
样式(如果项目是使用dotnet new console
CLI命令创建的-提供--use-program-main
,因此无需手动更改),运行Program.Main(args)
,然后通过Console.Out
捕获“当前”控制台输出:和用法:
打开PR并举例说明。
备注:
Program
文件,那么您可以执行一些反射来调用生成的Program.<Main>$
,尽管这会更脆弱一些,因为生成模式可能会改变(尽管有一个选项可以通过使用Assembly.EntryPoint
来缓解这种情况-请参见this answer)。