dotnet testdotnet test

本主題適用於:✓ .NET Core 1.x SDK .NET Core 2.x SDKThis topic applies to: ✓ .NET Core 1.x SDK .NET Core 2.x SDK


dotnet test - 用來執行單元測試的 .NET 測試驅動程式。dotnet test - .NET test driver used to execute unit tests.


dotnet test [<PROJECT>] [-a|--test-adapter-path] [--blame] [-c|--configuration] [--collect] [-d|--diag] [-f|--framework] [--filter]
    [-l|--logger] [--no-build] [--no-restore] [-o|--output] [-r|--results-directory] [-s|--settings] [-t|--list-tests] 
    [-v|--verbosity] [-- <RunSettings arguments>]

dotnet test [-h|--help]


dotnet test 命令是用來在指定的專案中執行單元測試。The dotnet test command is used to execute unit tests in a given project. dotnet test 命令會啟動為專案指定的測試執行器主控台應用程式。The dotnet test command launches the test runner console application specified for a project. 測試執行器會執行針對單元測試架構 (例如 MSTest、NUnit 或 xUnit) 定義的測試,並報告每項測試成功還是失敗。The test runner executes the tests defined for a unit test framework (for example, MSTest, NUnit, or xUnit) and reports the success or failure of each test. 如果所有測試都成功,則測試執行器會傳回 0 作為結束代碼;如果有任何測試失敗,則會傳回 1。If all tests are successful, the test runner returns 0 as an exit code; otherwise if any test fails, it returns 1. 測試執行器和單元測試程式庫會封裝為 NuGet 套件,並還原為專案的一般相依性。The test runner and the unit test library are packaged as NuGet packages and are restored as ordinary dependencies for the project.

測試專案會使用一般 <PackageReference> 元素指定測試執行器,如下列範例專案檔中所示:Test projects specify the test runner using an ordinary <PackageReference> element, as seen in the following sample project file:

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


    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
    <PackageReference Include="xunit" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />




測試專案的路徑。Path to the test project. 如果未指定,則會預設為目前目錄。If not specified, it defaults to current directory.


-a|--test-adapter-path <PATH_TO_ADAPTER>

在測試執行中,從指定的路徑使用自訂測試配接器。Use the custom test adapters from the specified path in the test run.


在歸責模式下執行測試。Runs the tests in blame mode. 這個選項有助於隔離造成測試主機損毀的問題。This option is helpful in isolating the problematic tests causing test host to crash. 它會以 Sequence.xml 的形式在目前目錄中建立一個輸出檔,用來擷取損毀前的測試執行順序。It creates an output file in the current directory as Sequence.xml that captures the order of tests execution before the crash.

-c|--configuration {Debug|Release}

定義組建組態。Defines the build configuration. 預設值是 Debug,但您的專案組態無法覆寫這個預設的 SDK 設定。The default value is Debug, but your project's configuration could override this default SDK setting.


測試回合啟用資料收集器。Enables data collector for the test run. 如需詳細資訊,請參閱監視及分析測試回合For more information, see Monitor and analyze test run.


針對測試平台啟用診斷模式,並將診斷訊息寫入至指定的檔案。Enables diagnostic mode for the test platform and write diagnostic messages to the specified file.

-f|--framework <FRAMEWORK>

尋找特定架構的測試二進位檔。Looks for test binaries for a specific framework.

--filter <EXPRESSION>

使用指定的運算式篩選出目前專案中的測試。Filters out tests in the current project using the given expression. 如需詳細資訊,請參閱篩選選項詳細資料一節。For more information, see the Filter option details section. 如需如何使用選擇性單元測試篩選的詳細資訊及範例,請參閱執行選擇性單元測試For more information and examples on how to use selective unit test filtering, see Running selective unit tests.


印出命令的簡短說明。Prints out a short help for the command.

-l|--logger <LoggerUri/FriendlyName>

指定測試結果的記錄器。Specifies a logger for test results.


不會在執行前建置測試專案。Doesn't build the test project before running it. 它也會隱含設定 --no-restore 旗標。It also implicit sets the --no-restore flag.


執行命令時,不會執行隱含還原。Doesn't execute an implicit restore when running the command.

-o|--output <OUTPUT_DIRECTORY>

在其中尋找要執行的二進位檔的目錄。Directory in which to find the binaries to run.

-r|--results-directory <PATH>

要放置測試結果的目錄。The directory where the test results are going to be placed. 如果指定的目錄不存在,則會建立該目錄。If the specified directory doesn't exist, it's created.

-s|--settings <SETTINGS_FILE>

用來執行測試的 .runsettings 檔案。The .runsettings file to use for running the tests. 使用 .runsettings 檔案設定單元測試Configure unit tests by using a .runsettings file.


列出在目前專案中探索到的所有測試。List all of the discovered tests in the current project.

-v|--verbosity <LEVEL>

設定命令的詳細資訊層級。Sets the verbosity level of the command. 允許的值為 q[uiet]m[inimal]n[ormal]d[etailed]diag[nostic]Allowed values are q[uiet], m[inimal], n[ormal], d[etailed], and diag[nostic].

RunSettings arguments

針對測試傳遞為 RunSettings 設定的引數。Arguments passed as RunSettings configurations for the test. 指定為 "-- " 後 (注意 -- 後方的空格) 之 [name]=[value] 組的引數。Arguments are specified as [name]=[value] pairs after "-- " (note the space after --). 空格適用來分隔多個 [name]=[value] 組。A space is used to separate multiple [name]=[value] pairs.

範例:dotnet test -- MSTest.DeploymentEnabled=false MSTest.MapInconclusiveToFailed=TrueExample: dotnet test -- MSTest.DeploymentEnabled=false MSTest.MapInconclusiveToFailed=True

如需 RunSettings 的詳細資訊,請參閱 vstest.console.exe:傳遞 RunSettings 引數For more information about RunSettings, see vstest.console.exe: Passing RunSettings args.


執行目前目錄之專案中的測試:Run the tests in the project in the current directory:

dotnet test

執行 test1 專案中的測試︰Run the tests in the test1 project:

dotnet test ~/projects/test1/test1.csproj

在目前目錄中的專案中執行測試,並以 trx 格式產生測試結果檔案:Run the tests in the project in the current directory and generate a test results file in the trx format:

dotnet test --logger:trx

篩選選項詳細資料Filter option details

--filter <EXPRESSION>

<Expression> 的格式為 <property><operator><value>[|&<Expression>]<Expression> has the format <property><operator><value>[|&<Expression>].

<property>Test Case 的屬性。<property> is an attribute of the Test Case. 以下為熱門單元測試架構所支援的屬性:The following are the properties supported by popular unit test frameworks:

測試架構Test Framework 支援的屬性Supported properties
  • FullyQualifiedNameFullyQualifiedName
  • 名稱Name
  • ClassNameClassName
  • 優先權Priority
  • TestCategoryTestCategory
  • FullyQualifiedNameFullyQualifiedName
  • DisplayNameDisplayName
  • 特性Traits

<operator> 描述屬性和值之間的關聯性:The <operator> describes the relationship between the property and the value:

運算子Operator 功能Function
= 完全相符Exact match
!= 不完全相符Not exact match
~ ContainsContains

<value> 為字串。<value> is a string. 所有的查閱皆不區分大小寫。All the lookups are case insensitive.

沒有 <operator> 的運算式會自動被視為 FullyQualifiedName 屬性上的 contains (例如,dotnet test --filter xyz 等同於 dotnet test --filter FullyQualifiedName~xyz)。An expression without an <operator> is automatically considered as a contains on FullyQualifiedName property (for example, dotnet test --filter xyz is same as dotnet test --filter FullyQualifiedName~xyz).

運算式可以使用條件運算子聯結:Expressions can be joined with conditional operators:

運算子Operator 功能Function
| OROR      

使用條件運算子時,您可以使用括弧括住運算式 (例如,(Name~TestMethod1) | (Name~TestMethod2))。You can enclose expressions in parenthesis when using conditional operators (for example, (Name~TestMethod1) | (Name~TestMethod2)).

如需如何使用選擇性單元測試篩選的詳細資訊及範例,請參閱執行選擇性單元測試For more information and examples on how to use selective unit test filtering, see Running selective unit tests.

另請參閱See also