簡體   English   中英

如何針對dotnet核心控制台應用程序運行驗收測試?

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

我試圖對dotnet核心控制台應用程序運行一些驗收測試。 我的測試連續失敗,因為控制台應用程序未正確啟動。 通過正確啟動,我的意思是我為運行控制台應用程序而產生的System.Diagnostics.Process並沒有執行我想要的操作。

這是我設置方案的方式:

通過運行以下命令創建控制台應用程序:

dotnet new console -o myconsoleapp

然后修改Program.Main以通過Environment.Exit(-1)返回-1退出代碼

通過運行以下命令創建測試項目:

dotnet new xunit -o myconsoleapp.tests

在測試項目中添加myconsoleapp作為參考:

dotnet add ./myconsoleapp.tests reference ./myconsoleapp

編寫驗收測試:

[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);
  }
}

設置場景之后,我運行測試:

dotnet test ./myconsoleapp.tests

運行測試始終會導致失敗。 為了診斷問題,我運行:

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

運行這兩個命令將返回以下錯誤消息:

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.

該錯誤消息很好地說明了所需的內容,但是我對dotnet核心並不了解,無法完成我的任務。

您可以使用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);
}

這可以在我的Windows 10 PC上使用dotnet core 3的預覽版。

我確定沒有任何答案。 這里的上下文是關鍵。 我最終做了類似於@Fabio建議的操作,但是@Marius也有一個合理的答案。 僅取決於您要測試的內容。

我想測試可執行文件是否相應運行。 我相信我通過說這是驗收測試來表達最初的問題是錯誤的,但這似乎更類似於功能測試。 在測試中生成可運行的可執行文件的努力超出了我的期望。 我去測試Program.Main代替。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM