简体   繁体   English

如何针对dotnet核心控制台应用程序运行验收测试?

[英]How to run acceptance test against dotnet core console app?

I am trying to run some acceptance tests against a dotnet core console application. 我试图对dotnet核心控制台应用程序运行一些验收测试。 My test continually fails because the console application is not properly started. 我的测试连续失败,因为控制台应用程序未正确启动。 By properly started I mean the the System.Diagnostics.Process I spawn to run the console app is not doing what I would like. 通过正确启动,我的意思是我为运行控制台应用程序而产生的System.Diagnostics.Process并没有执行我想要的操作。

Here is how I setup the scenario: 这是我设置方案的方式:

Create the console application by running: 通过运行以下命令创建控制台应用程序:

dotnet new console -o myconsoleapp

Then modify Program.Main to return a -1 exit code via Environment.Exit(-1) 然后修改Program.Main以通过Environment.Exit(-1)返回-1退出代码

Create the test project by running: 通过运行以下命令创建测试项目:

dotnet new xunit -o myconsoleapp.tests

Add myconsoleapp as a reference in the tests projects: 在测试项目中添加myconsoleapp作为参考:

dotnet add ./myconsoleapp.tests reference ./myconsoleapp

Write the acceptance test: 编写验收测试:

[Fact]
public void AppExistsWithProperExitCode(){
  var @params = "myconsoleapp.dll";
  using(var sut = Process.Start("dotnet", @params))
  {
    sut.WaitForExit();
    var actual = sut.ExitCode;
    Assert.Equal(-1,actual);
  }
}

After the scenario is setup I run the test: 设置场景之后,我运行测试:

dotnet test ./myconsoleapp.tests

Running the tests always results in a failure. 运行测试始终会导致失败。 In an attempt to diagnose the problem I run: 为了诊断问题,我运行:

dotnet build ./myconsoleapp.tests
dotnet ./myconsoleapp.tests/bin/Debug/netcoreapp2.2/myconsoleapp.dll

Running those two commands returns the following error message: 运行这两个命令将返回以下错误消息:

A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'myconsoleapp.tests/bin/Debug/netcoreapp2.2/'.
Failed to run as a self-contained app. If this should be a framework-dependent app, add the myconsoleapp.tests/bin/Debug\netcoreapp2.2/myconsoleapp.runtimeconfig.json file specifying the appropriate framework.

The error message is pretty explanatory of what is required, but I am not well versed enough in dotnet core to accomplish my task. 该错误消息很好地说明了所需的内容,但是我对dotnet核心并不了解,无法完成我的任务。

You can create a self contained executable with dotnet publish -c Release -r win10-x64 (for a Windows exe) and then change your test code to 您可以使用dotnet publish -c Release -r win10-x64 (对于Windows exe)创建自包含的可执行文件,然后将测试代码更改为

using(var sut = Process.Start(@"<path-to-the-executable>"))
{
  sut.WaitForExit();
  var actual = sut.ExitCode;
  Assert.Equal(-1,actual);
}

This worked on my Windows 10 pc with the preview version of dotnet core 3. 这可以在我的Windows 10 PC上使用dotnet core 3的预览版。

I have determined that there isn't any one answer. 我确定没有任何答案。 Context is key here. 这里的上下文是关键。 I ended up doing something similar to what @Fabio suggested, but @Marius had a reasonable answer as well. 我最终做了类似于@Fabio建议的操作,但是@Marius也有一个合理的答案。 Just depends on what exactly you are trying to test. 仅取决于您要测试的内容。

I wanted to test that the executable ran accordingly. 我想测试可执行文件是否相应运行。 I believe I phrased the original question wrong by saying this was an acceptance test, but it seems more akin to a functional test. 我相信我通过说这是验收测试来表达最初的问题是错误的,但这似乎更类似于功能测试。 The effort to produce a run-able executable within the test was more than I was looking for. 在测试中生成可运行的可执行文件的努力超出了我的期望。 I went with testing Program.Main instead. 我去测试Program.Main代替。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 dotnet run 时如何调试 .NET 核心控制台应用程序 - How to Debug a .NET Core Console App When Using dotnet run 如何在 Docker 中使用 Selenium 运行 dotnet core 应用程序 - How to run dotnet core app with Selenium in Docker dotnet 核心应用程序以管理员身份运行 - dotnet core app run as administrator 如何使用别名从命令行运行 dotnet 控制台应用程序 - How to run a dotnet console app from the command line using an alias 在测试中在后台运行dotnet控制台 - Run dotnet console in background from test 是否可以将 dotnet 核心控制台应用程序用作库 - Is it possible to use dotnet core console app as library 如何根据参数参数运行显示控制台 window 的 dotnet Core 控制台应用程序? - How to run dotnet Core console application that shows the console window or not based on argument parameters? 使用控制台应用程序.NET Core在并行C#中运行两个dotnet进程 - Run two dotnet processes in parallel C# with Console app .NET Core 将AspNet Core添加到标准Dotnet Core控制台应用程序 - Adding AspNet Core to standard Dotnet Core Console app 如何在 dotnet core 3 控制台应用程序中更改时重新加载“appsettings.json” - How to reload 'appsettings.json' upon change in a dotnet core 3 console app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM