使用 .NET CLI 組織和測試專案

本教學課程遵循教學課程:使用 Visual Studio Code 以 .NET 建立主控台應用程式,讓您超越建立簡單的主控台應用程式來開發進階且組織完善的應用程式。 教學課程在示範如何使用資料夾來組織您的程式碼之後,會示範如何使用 xUnit 基礎結構來擴充主控台應用程式。

注意

本教學課程建議您將應用程式專案和測試專案放在不同的資料夾中。 有些開發人員偏好將這些專案保留在同一個資料夾中。 如需詳細資訊,請參閱 GitHub 問題 dotnet/docs #26395

使用資料夾來組織程式碼

如果您想要將新類型引入主控台應用程式,則可以將包含類型的檔案新增至應用程式。 例如,如果您將包含 AccountInformationMonthlyReportRecords 類型的檔案新增至專案,則專案檔案結構會是平面,而且容易進行瀏覽︰

/MyProject
|__AccountInformation.cs
|__MonthlyReportRecords.cs
|__MyProject.csproj
|__Program.cs

不過,只有在專案規模相當小時,這個一般結構才會運作良好。 您可以想像將 20 種類型新增至專案時,會發生什麼事? 如果有多個可將專案根目錄弄亂的檔案,則絕對無法輕鬆地巡覽及維護專案。

若要組織專案,請建立新的資料夾,並將它命名為 Models 以保留類型檔案。 將類型檔案放入 Models 資料夾︰

/MyProject
|__/Models
   |__AccountInformation.cs
   |__MonthlyReportRecords.cs
|__MyProject.csproj
|__Program.cs

您也可以輕鬆地巡覽及維護以邏輯方式將檔案群組到資料夾的專案。 在下一節中,您會建立具有資料夾及單元測試的更複雜範例。

使用 NewTypes Pets 範例進行組織及測試

必要條件

建置範例

如需下列步驟,您可以遵循如何使用 NewTypes Pets Sample (NewTypes Pets 範例),或建立自己的檔案及資料夾。 類型會以邏輯方式組織成資料夾結構以允許稍後新增更多類型,而且測試也會以邏輯方式放在允許稍後新增更多測試的資料夾中。

這個範例包含 DogCat 這兩種類型,並讓它們實作公用介面 IPet。 針對 NewTypes 專案,您的目標是將寵物相關類型組織到 Pets 資料夾。 如果稍後新增另一組類型 (例如,WildAnimals),則會將它們放入 Pets 資料夾旁邊的 NewTypes 資料夾。 WildAnimals 資料夾可能會包含不是寵物之動物的類型,例如 SquirrelRabbit 類型。 因此,新增類型時,專案會井然有序。

使用所指出的檔案內容來建立下列資料夾結構︰

/NewTypes
|__/src
   |__/NewTypes
      |__/Pets
         |__Dog.cs
         |__Cat.cs
         |__IPet.cs
      |__Program.cs
      |__NewTypes.csproj

IPet.cs

using System;

namespace Pets
{
    public interface IPet
    {
        string TalkToOwner();
    }
}

Dog.cs

using System;

namespace Pets
{
    public class Dog : IPet
    {
        public string TalkToOwner() => "Woof!";
    }
}

Cat.cs

using System;

namespace Pets
{
    public class Cat : IPet
    {
        public string TalkToOwner() => "Meow!";
    }
}

Program.cs

using System;
using Pets;
using System.Collections.Generic;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            List<IPet> pets = new List<IPet>
            {
                new Dog(),
                new Cat()
            };

            foreach (var pet in pets)
            {
                Console.WriteLine(pet.TalkToOwner());
            }
        }
    }
}

NewTypes.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

</Project>

執行以下 命令:

dotnet run

取得下列輸出:

Woof!
Meow!

選擇性練習︰您可以擴充此專案,以新增 Bird 這類寵物類型。 讓小鳥的 TalkToOwner 方法提供 Tweet! 給擁有者。 重新執行應用程式。 輸出會包含 Tweet!

測試範例

NewTypes 專案已經就緒,而且組織方式是將寵物相關類型保留在資料夾中。 接下來,建立測試專案,並開始撰寫具有 xUnit 測試架構的測試。 單元測試可讓您自動檢查寵物類型的行為以確認它們正常運作。

巡覽回到 src 資料夾,並建立內含 NewTypesTests 資料夾的 test 資料夾。 在命令提示字元中,從 NewTypesTests 資料夾執行 dotnet new xunit。 此命令會產生兩個檔案︰NewTypesTests.csprojUnitTest1.cs

測試專案目前無法測試 NewTypes 中的類型,並且需要 NewTypes 專案的專案參考。 若要新增專案參考,請使用 dotnet add reference 命令︰

dotnet add reference ../../src/NewTypes/NewTypes.csproj

或者,您也可以選擇手動新增專案參考,方法是將 <ItemGroup> 節點新增至 NewTypesTests.csproj 檔案:

<ItemGroup>
  <ProjectReference Include="../../src/NewTypes/NewTypes.csproj" />
</ItemGroup>

NewTypesTests.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
    <PackageReference Include="xunit" Version="2.8.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.8.0" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="../../src/NewTypes/NewTypes.csproj"/>
  </ItemGroup>

</Project>

NewTypesTests.csproj 檔案包含下列套件參考:

  • Microsoft.NET.Test.Sdk,.NET 測試基礎結構
  • xunit,xUnit 測試基礎結構
  • xunit.runner.visualstudio,測試執行器
  • NewTypes,要測試的程式碼

UnitTest1.cs 的名稱變更為 PetTests.cs,並將檔案中的程式碼取代為下列程式碼:

using System;
using Xunit;
using Pets;

public class PetTests
{
    [Fact]
    public void DogTalkToOwnerReturnsWoof()
    {
        string expected = "Woof!";
        string actual = new Dog().TalkToOwner();

        Assert.NotEqual(expected, actual);
    }

    [Fact]
    public void CatTalkToOwnerReturnsMeow()
    {
        string expected = "Meow!";
        string actual = new Cat().TalkToOwner();

        Assert.NotEqual(expected, actual);
    }
}

選擇性練習︰如果您稍早已將產生 Tweet!Bird 類型新增至擁有者,請將測試方法新增至 PetTests.cs 檔案,並新增 BirdTalkToOwnerReturnsTweet 以確認 TalkToOwner 方法正確作用於 Bird 類型。

注意

雖然您預期 expectedactual 值相等,但是具有 Assert.NotEqual 檢查的初始判斷提示指定這些值「不相等」。 一開始一律會讓測試失敗,以檢查測試邏輯。 在您確認測試失敗之後,調整判斷提示以允許測試通過。

下列顯示完整專案結構:

/NewTypes
|__/src
   |__/NewTypes
      |__/Pets
         |__Dog.cs
         |__Cat.cs
         |__IPet.cs
      |__Program.cs
      |__NewTypes.csproj
|__/test
   |__NewTypesTests
      |__PetTests.cs
      |__NewTypesTests.csproj

test/NewTypesTests 目錄開始。 使用 dotnet test 命令執行測試。 這個命令會啟動專案檔中指定的測試執行器。

如預期,測試會失敗,而且主控台會顯示下列輸出︰

Test run for C:\Source\dotnet\docs\samples\snippets\core\tutorials\testing-with-cli\csharp\test\NewTypesTests\bin\Debug\net5.0\NewTypesTests.dll (.NETCoreApp,Version=v5.0)
Microsoft (R) Test Execution Command Line Tool Version 16.8.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
[xUnit.net 00:00:00.50]     PetTests.DogTalkToOwnerReturnsWoof [FAIL]
  Failed PetTests.DogTalkToOwnerReturnsWoof [6 ms]
  Error Message:
   Assert.NotEqual() Failure
Expected: Not "Woof!"
Actual:   "Woof!"
  Stack Trace:
     at PetTests.DogTalkToOwnerReturnsWoof() in C:\Source\dotnet\docs\samples\snippets\core\tutorials\testing-with-cli\csharp\test\NewTypesTests\PetTests.cs:line 13

Failed!  - Failed:     1, Passed:     1, Skipped:     0, Total:     2, Duration: 8 ms - NewTypesTests.dll (net5.0)

將您測試的判斷提示從 Assert.NotEqual 變更為 Assert.Equal

using System;
using Xunit;
using Pets;

public class PetTests
{
    [Fact]
    public void DogTalkToOwnerReturnsWoof()
    {
        string expected = "Woof!";
        string actual = new Dog().TalkToOwner();

        Assert.Equal(expected, actual);
    }

    [Fact]
    public void CatTalkToOwnerReturnsMeow()
    {
        string expected = "Meow!";
        string actual = new Cat().TalkToOwner();

        Assert.Equal(expected, actual);
    }
}

使用 dotnet test 命令重新執行測試,並取得下列輸出︰

Test run for C:\Source\dotnet\docs\samples\snippets\core\tutorials\testing-with-cli\csharp\test\NewTypesTests\bin\Debug\net5.0\NewTypesTests.dll (.NETCoreApp,Version=v5.0)
Microsoft (R) Test Execution Command Line Tool Version 16.8.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.

Passed!  - Failed:     0, Passed:     2, Skipped:     0, Total:     2, Duration: 2 ms - NewTypesTests.dll (net5.0)

通過測試。 與擁有者交談時,寵物類型的方法會傳回正確值。

您已了解使用 xUnit 來組織及測試專案的技術。 繼續使用這些技術,以將它們套用至您自己的專案。 祝各位程式撰寫愉快!