本教學課程會逐步引導您完成建置範例解決方案的互動式體驗,以瞭解單元測試概念。 如果您想要使用預先建置的解決方案來遵循教學課程,請在開始之前檢視或下載範例程式代碼。 如需下載指示,請參閱 範例和教學課程。
本文是關於測試 .NET Core 專案。 如果您要測試 ASP.NET Core 專案,請參閱 ASP.NET Core 中的整合測試。
先決條件
- .NET 8 SDK 或更高版本。
- 選擇您喜好的文字編輯器或程式碼編輯器
建立來源專案
開啟終端機視窗。 建立名為 unit-testing-vb-nunit 的目錄來保存解決方案。 在此新目錄內,執行下列命令,為類別庫和測試專案建立新的方案檔:
dotnet new sln
接下來,建立 PrimeService 目錄。 下列大綱顯示到目前為止的檔案結構:
/unit-testing-vb-nunit
unit-testing-vb-nunit.sln
/PrimeService
將 PrimeService 設為目前的目錄,然後執行下列命令來建立來源專案:
dotnet new classlib -lang VB
將 Class1.VB 重新命名為 PrimeService.VB。 您建立一個 PrimeService 類別的失敗實作:
Namespace Prime.Services
Public Class PrimeService
Public Function IsPrime(candidate As Integer) As Boolean
Throw New NotImplementedException("Please create a test first.")
End Function
End Class
End Namespace
將目錄切換回 unit-testing-vb-using-mstest 目錄。 執行下列命令,將類別庫專案新增至方案:
dotnet sln add .\PrimeService\PrimeService.vbproj
建立測試專案
接下來,建立 PrimeService.Tests 目錄。 下列大綱顯示目錄結構:
/unit-testing-vb-nunit
unit-testing-vb-nunit.sln
/PrimeService
Source Files
PrimeService.vbproj
/PrimeService.Tests
將 PrimeService.Tests 目錄設為目前目錄,並使用下列命令建立新專案:
dotnet new nunit -lang VB
dotnet new 命令會建立使用 NUnit 作為測試連結庫的測試專案。 產生的範本會在 PrimeServiceTests.vbproj 檔案中設定測試執行器:
<ItemGroup>
<PackageReference Include="nunit" Version="4.4.0" />
<PackageReference Include="NUnit3TestAdapter" Version="5.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.0.1" />
</ItemGroup>
備註
在 .NET 9 之前,產生的程式代碼可能會參考舊版的 NUnit 測試架構。 您可以使用 dotnet CLI 來更新套件。 或者,開啟 PrimeService.Tests.vbproj 檔案,並將套件參考專案群組的內容取代為上述程序代碼。
測試專案需要其他套件才能建立和執行單元測試。
dotnet new 在前一步中新增了 NUnit 及 NUnit 測試配接器。 現在,將 PrimeService 類別庫新增為專案的另一個相依性。 使用 dotnet reference add 命令:
dotnet reference add ../PrimeService/PrimeService.vbproj
您可以在 GitHub 上的 範例存放庫 中看到整個檔案。
您有下列最終解答佈局:
/unit-testing-vb-nunit
unit-testing-vb-nunit.sln
/PrimeService
Source Files
PrimeService.vbproj
/PrimeService.Tests
Test Source Files
PrimeService.Tests.vbproj
在 unit-testing-vb-nunit 目錄中執行下列命令:
dotnet sln add .\PrimeService.Tests\PrimeService.Tests.vbproj
建立第一個測試
您撰寫一個失敗的測試,讓它通過,接著重複這個過程。 在 PrimeService.Tests 目錄中,將 UnitTest1.vb 檔案重新命名為 PrimeService_IsPrimeShould.VB ,並以下列程式代碼取代其整個內容:
Imports NUnit.Framework
Namespace PrimeService.Tests
<TestFixture>
Public Class PrimeService_IsPrimeShould
Private _primeService As Prime.Services.PrimeService = New Prime.Services.PrimeService()
<Test>
Sub IsPrime_InputIs1_ReturnFalse()
Dim result As Boolean = _primeService.IsPrime(1)
Assert.That(result, [Is].False, $"1 should not be prime")
End Sub
End Class
End Namespace
屬性 <TestFixture> 表示包含測試的類別。
<Test> 屬性表示由測試運行器執行的方法。 從 unit-testing-vb-nunit 執行 dotnet test 以建置測試和類別庫,然後執行測試。 NUnit 測試執行器包含執行測試的程式進入點。
dotnet test 使用您建立的單元測試專案啟動測試執行器。
您的測試失敗。 您尚未創建實作。 在可運作的 PrimeService 類別中撰寫最簡單的程式代碼,讓此測試通過:
Public Function IsPrime(candidate As Integer) As Boolean
If candidate = 1 Then
Return False
End If
Throw New NotImplementedException("Please create a test first.")
End Function
在 unit-testing-vb-nunit 目錄中,再次執行 dotnet test 。
dotnet test 命令會執行 PrimeService 項目的組建,然後執行 PrimeService.Tests 項目的組建。 建置這兩個項目之後,它會執行此單一測試。 它通過了。
新增更多功能
既然您已通過一次測試,現在是時候撰寫更多了。 關於質數,有一些其他的特殊情況:0、-1。 你可以使用<Test>屬性將這些案例新增為新的測試,但這很快就會變得乏味。 還有其他 xUnit 屬性可讓您撰寫一組類似的測試。
<TestCase>屬性代表執行相同程式代碼但具有不同輸入自變數的測試套件。 您可以使用 <TestCase> 屬性來指定這些輸入的值。
不要建立新的測試,請套用這兩個屬性來建立一系列測試,以測試小於兩個值的數個值,這是最低質數:
<TestFixture>
Public Class PrimeService_IsPrimeShould
Private _primeService As Prime.Services.PrimeService = New Prime.Services.PrimeService()
<TestCase(-1)>
<TestCase(0)>
<TestCase(1)>
Sub IsPrime_ValuesLessThan2_ReturnFalse(value As Integer)
Dim result As Boolean = _primeService.IsPrime(value)
Assert.That(result, [Is].False, $"{value} should not be prime")
End Sub
<TestCase(2)>
<TestCase(3)>
<TestCase(5)>
<TestCase(7)>
Public Sub IsPrime_PrimesLessThan10_ReturnTrue(value As Integer)
Dim result As Boolean = _primeService.IsPrime(value)
Assert.That(result, [Is].True, $"{value} should be prime")
End Sub
<TestCase(4)>
<TestCase(6)>
<TestCase(8)>
<TestCase(9)>
Public Sub IsPrime_NonPrimesLessThan10_ReturnFalse(value As Integer)
Dim result As Boolean = _primeService.IsPrime(value)
Assert.That(result, [Is].False, $"{value} should not be prime")
End Sub
End Class
執行 dotnet test,其中兩個測試會失敗。 若要讓所有測試都通過,請在 if 檔案中的 Main 方法開頭變更 子句。
if candidate < 2
在主程式庫中新增更多測試、更多理論和更多代碼,以繼續迭代。 您已獲得測試的完成版本 ,及函式庫 的完整實作。
您已建立一個小型程式庫及其一組單元測試。 您已建構解決方案,以便新增新的套件和測試是一般工作流程的一部分。 您大部分的時間和精力都集中在解決應用程式的目標上。