如何从测试项目的调试文件夹中测试自包含 exe?

3

尝试从我的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个回答

1
如果你构建解决方案并打开"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捕获"当前"控制台输出:
public sealed class ConsoleCapture : IDisposable
{
    private readonly StringWriter _stringWriter = new();

    private readonly TextWriter _oldOut;
    private readonly TextWriter _oldErr;

    public ConsoleCapture()
    {
        _oldOut = Console.Out;
        _oldErr = Console.Error;

        Console.SetOut(_stringWriter);
        Console.SetError(_stringWriter);
    }

    public string GetAll()
    {
        return _stringWriter.ToString();
    }

    public void Dispose()
    {
        Console.SetOut(_oldOut);
        Console.SetError(_oldErr);

        _stringWriter.Dispose();
    }
}

使用方法:

using var console = new ConsoleCapture();
var res = Program.Main(args);

var consoleOutput = console.GetAll();

Assert.AreEqual(expectedExitCode, res);
Assert.Contains("Hello World", res);

打开了PR,附带示例。

备注


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接