跳過到頁腳內容
.NET幫助

Test Console Application C#(開發者的工作原理)

測試在軟體開發領域中扮演關鍵的角色,是保證應用程式品質的重要元素。 在眾多可用的框架中,.NET 是製作 Windows 應用程式的最佳選擇。 本文將深入探討 .NET TestConsole 的複雜性,這是專為測試 .NET 應用程式而設計的工具。

在整個探索過程中,我們將撰寫一個全面的程式碼範例,說明 .NET TestConsole 的實際執行。 此外,我們將介紹 IronPDF,一個與 .NET TestConsole 無縫整合的典範 C# PDF 函式庫。 這個函式庫證明是非常有價值的,它能讓開發人員毫不費力地在 .NET TestConsole 環境中存取和產生 PDF。 加入我們的旅程,讓我們一起揭開 .NET TestConsole 與 IronPDF's C# PDF Functionality 的協同效應所產生的功能與可能性。

1.簡介:TestConsole

TestConsole 是一個多功能的測試函式庫,在 C# 程式語言中引入了獨特的單元測試方法。 傳統的單元測試方法在處理大型資料集和複雜斷言時經常面臨挑戰,導致難以辨識預期結果與實際結果之間的差異。 有鑑於此,TestConsole 提供了新穎的工作流程,從傳統的預測方式轉變為將格式化輸出與指定的"認可"標準輸出版本進行並排比較。

在這個函式庫中,尤其是 TestConsole,".Core"變體擴充了從原始 TestConsole 專案繼承的格式化功能,並在測試結果與預期有偏差的情況下,加入必要的測試核准功能。 TestConsole.Core 可與建立伺服器無縫整合,以觸發測試失敗。 在開發電腦上,預設情況下,它提供可重新配置的功能,以利用已安裝的檔案比較公用程式來顯示差異。 值得注意的是,此方法可簡化核准程序,讓開發人員在預期會出現差異時,可手動更新已核准的版本。

1.1.為何使用 TestConsole?

TestConsole.Core 從 ApprovalTests 擷取靈感,但其與眾不同之處在於可同時支援撰寫完整 Framework 與 .NET Core 測試套件。 該函式庫可滿足在不同環境中進行測試的需求,就像出版時的 ApprovalTests,主要是迎合完整框架的情境。 TestConsole.Core 中的語法雖然與 ApprovalTests 有相似之處,但仍有差異,尤其是關於檔案比較工具的選擇和內容的直接核准。

TestConsole.Core 是為了方便在 .NET Core 應用程式碼中進行測試而開發,它的出現是為了彌補 ApprovalTests 缺乏 .NET Standard 和 .NET Core 應用程式支援所留下的缺口。 TestConsole.Core 的測試核准功能著重於實現大型資料集的有效測試,可容納使用 Test Console Output 物件格式化的資料,並將其功能擴展至處理任何純文字輸入,為 C# 中的單元測試提供全面的解決方案。

1.2.安裝 TestConsole C#。

可使用 Visual Studio 內的 NuGet 套件管理員安裝測試主控台,或在 NuGet 套件管理員控制台執行下列指令。

Install-Package TestConsole -Version 2.6.0

或直接從 NuGet 上的 TestConsole 發行版下載。

2.TestConsole 的程式碼範例

在本節中,我們將介紹如何將控制台輸出轉換為報告。 以下原始碼使用測試控制台將 Enumerable 物件轉換成格式良好的報表。

using TestConsoleLib;
using System;
using System.Linq;

// Instantiate the output class from TestConsoleLib
var output = new Output();

// Generate a collection of anonymous objects containing value, square, and string length
var data = Enumerable.Range(0, 10)
    .Select(i => new { Value = i, Squared = i * i, String = new string('I', i) });

// Format the data into a table using TestConsoleLib's method
output.FormatTable(data);

// Retrieve the formatted report as a string
string report = output.Report;

// Print the formatted report to console
Console.WriteLine(report);
using TestConsoleLib;
using System;
using System.Linq;

// Instantiate the output class from TestConsoleLib
var output = new Output();

// Generate a collection of anonymous objects containing value, square, and string length
var data = Enumerable.Range(0, 10)
    .Select(i => new { Value = i, Squared = i * i, String = new string('I', i) });

// Format the data into a table using TestConsoleLib's method
output.FormatTable(data);

// Retrieve the formatted report as a string
string report = output.Report;

// Print the formatted report to console
Console.WriteLine(report);
Imports TestConsoleLib
Imports System
Imports System.Linq

' Instantiate the output class from TestConsoleLib
Private output = New Output()

' Generate a collection of anonymous objects containing value, square, and string length
Private data = Enumerable.Range(0, 10).Select(Function(i) New With {
	Key .Value = i,
	Key .Squared = i * i,
	Key .String = New String("I"c, i)
})

' Format the data into a table using TestConsoleLib's method
output.FormatTable(data)

' Retrieve the formatted report as a string
Dim report As String = output.Report

' Print the formatted report to console
Console.WriteLine(report)
$vbLabelText   $csharpLabel

此 C# 程式碼片段利用 TestConsoleLib 函式庫來示範並執行一個簡單的範例,使用 TestConsole 的 Output 類別來格式化和報告表格資料。 它首先創建一個 Output 類的實例,命名為 output。 其後,它會產生一個由 10 個元素組成的集合,其中包含匿名物件,其屬性代表一個整數值、它的平方和一個長度與整數值相對應的 'I's 字串。

然後調用 output.FormatTable() 方法將資料格式化為表格。 格式化後的結果會儲存在 report 字串變數中,最後會使用 Console.WriteLine() 將其列印到控制台。 這展示了 TestConsole 的功能和能力,可輕鬆格式化和呈現表格資料,以提高單元測試或除錯情境中的可讀性。

2.1.輸出

Test Console Application C# (How It Works For Developer):圖 1 - 先前程式碼的輸出

3.IronPDF。

IronPDF的官方網站為一個強大的C# PDF函式庫提供了一個全面的平台,該函式庫旨在簡化和增強在.NET應用程式中處理PDF文件的過程。 IronPDF 提供一系列全面的功能,讓開發人員可以在 C# 專案中輕鬆地建立、處理 PDF 檔案,並從 PDF 檔案中擷取內容。 IronPDF 注重靈活性和易用性,支持廣泛的功能,包括從 HTML、圖像或現有文件生成 PDF,以及加入動態內容(如圖表和表格)。

其功能延伸至合併、分割和操作 PDF 頁面,以及擷取文字和影像等功能。 無論是用於報表、文件或任何 PDF 相關工作,IronPDF 都能以可靠且多功能的解決方案脫穎而出,以最少的工作量將 PDF 功能整合至 C# 應用程式中。

3.1.建立測試控制台報告的 PDF 檔案

在本節中,我們將討論如何轉換 TestConsole 報告的輸出。

開始使用 IronPdf


安裝 IronPdf Library

使用 NuGet 套件管理員安裝

若要使用 NuGet Package Manager 將 IronPDF 整合至您的 Console 專案,請遵循下列步驟:

1.開啟 Visual Studio,在解決方案總管中,用滑鼠右鍵按一下專案。 2.從上下文功能表中選擇"管理 NuGet 套件..."。 3.前往瀏覽標籤,搜尋 IronPdf。 4.從搜尋結果中選擇 IronPDF 函式庫,然後按一下安裝按鈕。 5.接受任何許可協議提示。

如果您想透過套件管理員控制台將 IronPDF 包含在專案中,那麼請在套件管理員控制台執行下列指令:

Install-Package IronPdf

它將擷取 IronPDF 並安裝到您的專案中。

使用 NuGet 網站安裝

如需 IronPDF 的詳細概述,包括其功能、相容性和其他下載選項,請造訪 NuGet 網站上的 IronPDF 頁面,網址為 https://www.nuget.org/packages/IronPdf

透過 DLL 安裝

另外,您也可以使用 IronPDF 的 DLL 檔案,直接將 IronPDF 納入您的專案中。從此 IronPDF ZIP Package 下載包含 DLL 的 ZIP 檔案。 解壓縮,並將 DLL 包含在您的專案中。

安裝完成後,現在我們將重新製作上述範例的報告,但這次不是將它寫入主控台,而是從中建立 PDF 報告。

using TestConsole.OutputFormatting;
using TestConsoleLib;
using IronPdf;
using System;
using System.Linq;

// Instantiate the output class from TestConsoleLib
var output = new Output();

// Generate a collection of anonymous objects containing value, square, and string length
var data = Enumerable.Range(0, 10)
    .Select(i => new { Value = i, Squared = i * i, String = new string('I', i) });

// Format the data into a table and obtain the formatted output as a string
output.FormatTable(data);
string report = output.Report;

// Wrap the report in HTML pre-tags to maintain formatting
var htmlContent = $"<pre>{report}</pre>";

// Initialize IronPDF renderer and render the HTML content to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);

// Save the PDF to a file
pdf.SaveAs("test.pdf");
using TestConsole.OutputFormatting;
using TestConsoleLib;
using IronPdf;
using System;
using System.Linq;

// Instantiate the output class from TestConsoleLib
var output = new Output();

// Generate a collection of anonymous objects containing value, square, and string length
var data = Enumerable.Range(0, 10)
    .Select(i => new { Value = i, Squared = i * i, String = new string('I', i) });

// Format the data into a table and obtain the formatted output as a string
output.FormatTable(data);
string report = output.Report;

// Wrap the report in HTML pre-tags to maintain formatting
var htmlContent = $"<pre>{report}</pre>";

// Initialize IronPDF renderer and render the HTML content to PDF
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderHtmlAsPdf(htmlContent);

// Save the PDF to a file
pdf.SaveAs("test.pdf");
Imports TestConsole.OutputFormatting
Imports TestConsoleLib
Imports IronPdf
Imports System
Imports System.Linq

' Instantiate the output class from TestConsoleLib
Private output = New Output()

' Generate a collection of anonymous objects containing value, square, and string length
Private data = Enumerable.Range(0, 10).Select(Function(i) New With {
	Key .Value = i,
	Key .Squared = i * i,
	Key .String = New String("I"c, i)
})

' Format the data into a table and obtain the formatted output as a string
output.FormatTable(data)
Dim report As String = output.Report

' Wrap the report in HTML pre-tags to maintain formatting
Dim htmlContent = $"<pre>{report}</pre>"

' Initialize IronPDF renderer and render the HTML content to PDF
Dim renderer = New ChromePdfRenderer()
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)

' Save the PDF to a file
pdf.SaveAs("test.pdf")
$vbLabelText   $csharpLabel

此 C# 程式碼片段展示 TestConsoleLib 與 IronPDF 的整合,以產生包含格式化資料表的 PDF 文件。 一開始,它會從 TestConsoleLib 建立一個 Output 類別的實例,並使用從整數範圍產生的資料格式化一個表格。 格式化的輸出會儲存在 report 字串變數中,然後將其包圍在 HTML 預先標籤中以保留格式。

隨後,程式碼利用 IronPDF 的 ChromePdfRenderer 將 HTML 內容渲染為 PDF 文件。 最後,產生的 PDF 會儲存為 "test.pdf"。這段程式碼展示了 TestConsoleLib 在格式化方面與 IronPDF 在 PDF 產生方面的無縫結合,提供了在 C# 應用程式中將格式化資料納入 PDF 文件的直接解決方案。

3.1.1.輸出

Test Console Application C# (How It Works For Developer):圖 2 - 先前程式碼的輸出

4.結論

.NET TestConsole 是 C# 應用程式的關鍵測試函式庫,提供獨特的單元測試方法,可減輕與大型資料集和複雜斷言相關的挑戰。 TestConsole.Core 變體在多樣化的環境中擴展其實用性,彌補其他框架留下的缺口,並提供有效的工作流程,以便並排比較格式化的輸出。

它與 IronPDF(一個強大的 C# 函式庫)無縫整合,不僅有助於簡化測試,還能將其功能擴展至 PDF 的產生與操作。 這些工具可讓開發人員在 C# 專案中輕鬆處理複雜的測試,並強化文件的產生,提供全面且有效率的解決方案。

有關 IronPDF HTML 至 PDF 轉換的詳細完整教學,請參閱 《IronPDF 教學指南》

常見問題解答

如何在 C# 中建立控制台應用程式?

若要以 C# 建立控制台應用程式,您可以使用 Visual Studio 開啟一個新專案,並選擇「控制台應用程式」作為專案類型。然後,在 Main 方法內編寫 C# 程式碼,以執行您的應用程式邏輯。

.NET TestConsole 的目的是什麼?

.NET TestConsole 旨在測試 .NET 應用程式,提供獨特的工作流程,將格式化的輸出與核准的標準進行並排比較,以提高測試流程的效率。

如何在 C# 中將控制台應用程式輸出轉換為 PDF?

您可以使用 IronPDF 將 C# 的控制台應用程式輸出轉換為 PDF。首先,將控制台輸出擷取成格式化的 HTML 字串,然後使用 IronPDF 的 RenderHtmlAsPdf 方法從 HTML 內容建立 PDF。

在 C# 應用程式中使用 PDF 函式庫有何好處?

在 C# 應用程式中使用 IronPDF 之類的 PDF 函式庫,可讓開發人員從 PDF 檔案中產生、修改和擷取內容,實現從 HTML 內容建立 PDF、納入動態資料等功能。

在 .NET TestConsole 中,並排輸出比較如何運作?

.NET TestConsole 中的並排輸出比較包括將應用程式的格式化輸出與預先核准的標準進行比較,讓開發人員可以找出差異,並確保測試結果的準確性。

.NET TestConsole 可以與 .NET Core 搭配使用嗎?

是的,.NET TestConsole 可與完整 Framework 和 .NET Core 搭配使用,為測試應用程式提供跨不同 .NET 環境的彈性與相容性。

如何將 PDF 函式庫整合到我的 .NET 專案中?

若要將類似 IronPDF 的 PDF 函式庫整合到您的 .NET 專案中,您可以使用 Visual Studio 中的 NuGet 套件管理程式來安裝,或是下載函式庫的 DLL 並將其加入專案的參考檔中。

在軟體開發中使用測試函式庫的優點為何?

測試函式庫 (例如 .NET TestConsole) 可透過自動化測試核准、促進有效率的並排輸出比較,以及強化整體測試管理來簡化測試流程,進而改善軟體品質。

如何使用 .NET TestConsole 處理大型資料集?

.NET TestConsole 具備有效處理大型資料集的能力,其使用的比較方法可簡化複雜的斷言及確保大量資料輸出的準確測試。

在哪裡可以找到更多 IronPDF 的使用資訊?

有關使用 IronPDF 的更多資訊,請參閱 IronPDF 官方網站,該網站提供在 C# 專案中整合和使用 IronPDF 的全面指南、教學和文件。

Jacob Mellor, Team Iron 首席技术官
首席技术官

Jacob Mellor 是 Iron Software 的首席技術官,作為 C# PDF 技術的先鋒工程師。作為 Iron Software 核心代碼的原作者,他自開始以來塑造了公司產品架構,與 CEO Cameron Rimington 一起將其轉變為一家擁有超過 50 名員工的公司,為 NASA、特斯拉 和 全世界政府機構服務。

Jacob 持有曼徹斯特大學土木工程一級榮譽学士工程學位(BEng) (1998-2001)。他於 1999 年在倫敦開設了他的第一家軟件公司,並於 2005 年製作了他的首個 .NET 組件,專注於解決 Microsoft 生態系統內的複雜問題。

他的旗艦產品 IronPDF & Iron Suite .NET 庫在全球 NuGet 被安裝超過 3000 萬次,其基礎代碼繼續為世界各地的開發工具提供動力。擁有 25 年的商業經驗和 41 年的編碼專業知識,Jacob 仍專注於推動企業級 C#、Java 及 Python PDF 技術的創新,同時指導新一代技術領袖。