NUnit-Console 命令行選項詳解


本文為 Dennis Gao 原創或翻譯技術文章,發表於博客園博客,未經作者本人允許禁止任何形式的轉載。

NUnit-Console 命令行選項

NUnit-Console 命令行選項列表
Options Description
/fixture=STR

Test fixture or namespace to be loaded (Deprecated) (Short format: /load=STR)

/run=STR

Name of the test case(s), fixture(s) or namespace(s) to run

/runlist=STR

Name of a file containing a list of the tests to run, one per line

/config=STR

Project configuration (e.g.: Debug) to load

/result=STR

Name of XML result file (Default: TestResult.xml) (Short format: /xml=STR)

/xmlConsole

Display XML to the console (Deprecated)

/noresult

Suppress XML result output (Short format: /noxml)

/output=STR

File to receive test output (Short format: /out=STR)

/err=STR

File to receive test error output

/work=STR

Work directory for output files

/labels

Label each test in stdOut

/trace=X

Set internal trace level: Off, Error, Warning, Info, Verbose

/include=STR

List of categories to include

/exclude=STR

List of categories to exclude

/framework=STR

Framework version to be used for tests

/process=X

Process model for tests: Single, Separate, Multiple

/domain=X

AppDomain Usage for tests: None, Single, Multiple

/apartment=X

Apartment for running tests: MTA (Default), STA

/noshadow

Disable shadow copy when running in separate domain

/nothread

Disable use of a separate thread for tests

/basepath=STR

Base path to be used when loading the assemblies

/privatebinpath=STR

Additional directories to be probed when loading assemblies, separated by semicolons

/timeout=X

Set timeout for each test case in milliseconds

/wait

Wait for input before closing console window

/nologo

Do not display the logo

/nodots

Do not display progress

/stoponerror

Stop after the first test failure or error

/cleanup

Erase any leftover cache files and exit

/help

Display help (Short format: /?)

指定運行哪些測試用例

運行指定程序集中的所有測試用例

nunit-console 命令行需要指定一個或多個文件方可運行。控制台程序會創建一個 XML 格式的測試執行結果。缺省的測試結果文件名為 TestResult.xml,放置在工作目錄中。

控制台程序必須指定一個程序集或者工程文件。

如果要運行 nunit.tests.dll 程序集中包含的測試用例,可使用下面的命令行:

nunit-console nunit.tests.dll

如果需要通過 Visual Studio 工程文件運行 nunit.tests.dll 中的的測試,可使用:

nunit-console nunit.tests.csproj

如果需要通過自定義的 NUnit 測試工程來運行同樣的測試,可使用:

nunit-console nunit.tests.nunit

運行指定程序集中指定的測試用例

可以通過 /run 選項指定測試的全名稱來運行程序集中的某個測試用例。

nunit-console /run:NUnit.Tests.AssertionTests nunit.tests.dll

被運行測試的名稱可以是一個測試用例、測試 Fixture 或者名空間。

也可以通過使用逗號分隔來指定多個測試。例如:

nunit-console /run:NUnit.Tests.AssertionTests,NUnit.Tests.ConstraintTests nunit.tests.dll

/fixture 選項已經被聲明為棄用,可以使用 /run 選項來代替。

通過單獨的文件來指定運行測試列表

可以通過創建一個包含需要運行的測試列表的文件,使用 /runlist 選項來執行測試:

nunit-console /runlist:testlist.txt nunit.tests.dll

"testlist.txt" 文件中包含了每個測試的全名稱,每行列舉一個測試。被運行測試的名稱可以是一個測試用例、測試 Fixture 或者名空間。

行首為 "#" 字符的為注釋。

指定多個程序集

通過控制台命令行接口,可以在一次運行中指定運行多個程序集中的測試。例如:

nunit-console assembly1.dll assembly2.dll assembly3.dll

注:可以指定多個程序集文件,但是不支持多個 NUnit 或 Visual Studio 工程文件。

默認情況下,每個程序集中會在單獨的 AppDomain 中執行。可以通過 /domain 選項來設置。

指定運行的 Configuration

可以通過 /config 選項控制測試運行的 Configuration。

nunit-console nunit.tests.csproj /config:Release

注:如果是直接加載程序集,則此選項無效。

通過測試類別 Category 來 Include 或 Exclude 測試

NUnit 通過 CategoryAttribute 屬性來指定測試所屬的類別。可以通過 /include/exclude 選項來選擇包含或排除指定的類別 Category。

下面的命令只運行 BaseLine 類別中的測試:

nunit-console myassembly.dll /include:BaseLine

下面的命令會運行除了 Database 類別外的所有測試:

nunit-console myassembly.dll /exclude:Database

從 NUnit 2.4.6 版本開始,可以在 /include 或 /exclude 中使用 Category 表達式。下面的表格中為一些示例:

NUnit Category Expression Examples
Expression Action
A|B|C Selects tests having any of the categories A, B or C.
A,B,C Selects tests having any of the categories A, B or C.
A+B+C Selects only tests having all three of the categories assigned
A+B|C Selects tests with both A and B OR with category C.
A+B-C Selects tests with both A and B but not C.
-A Selects tests not having category A assigned
A+(B|C) Selects tests having both category A and either of B or C
A+B,C Selects tests having both category A and either of B or C

注:通過上面的例子可以看出,逗號和 "|" 擁有相同涵義,但逗號的優先級更高。

優先級順序為:

  1. - 用於排除
  2. , 高優先級的或操作
  3. + 與操作
  4. | 低優先級的或操作

控制測試運行

指定 .NET Framework 版本

大多數應用程序都在某個特定版本的 CLR 下運行。少數的設計為可以在多版本下均可運行。不管哪種情況,為測試指定運行的 CLR 版本是非常重要的。

通過使用 /framework 選項可以指定運行時的版本。如果指定的版本與 NUnit 使用的版本不同,則測試將在一個獨立的進程中被執行。

nunit-console myassembly.dll /framework:net-4.0

控制進程的使用

通過 /process 選項可以控制 NUnit 如何在進程中加載測試。

  • Single :所有的測試運行在 nunit-console 進程中。默認選項。
  • Separate :創建單獨的進程中運行測試。
  • Multiple :為每個程序集 Assembly 創建獨立的進程運行測試。

控制 AppDomain 的使用

通過 /domain 選項可以控制運行測試所在的 AppDomain。

  • None :不創建任何 AppDomain,所有測試運行在 Primary Domain 中。通常需要將 NUnit 程序集拷貝至工作目錄。
  • Single :創建新的 AppDomain 來運行測試。
  • Multiple :為每個程序集 Assembly 創建獨立的 AppDomain。

如果在命令行中列舉了多個程序集,則默認選項為 Multiple。否則為 Single 選項。

控制 Apartment 的使用

通過 /apartment 選項可以指定運行測試線程的 ApartmentState (STA 或 MTA)。默認為 MTA。

指定超時時長

通過 /timeout 選項可以指定一個 int 值來設置運行測試的超時時長。如果任何測試超過了指定的超時時長,則該測試將被取消運行並同時報告一個錯誤信息。

也可以使用 TimeoutAttribute 屬性來指定超時時長。

注:如果未使用該選項,則無超時限制。

控制測試的輸出

重定向輸出

通常在控制台顯示的輸出可以重定向到一個文件。

下面的命令將標准輸入重定向到 TestResult.txt 文件:

nunit-console nunit.tests.dll /out:TestResult.txt

下面的命令將標准錯誤重定向到 StdErr.txt 文件:

nunit-console nunit.tests.dll /err:StdErr.txt

對測試輸出進行標記

每個測試的輸出會跟在前一個測試的輸出之后。可以使用 /labels 選項創建一個標識,在每個測試輸出的開始處顯示。

指定 XML 結果文件名

控制台程序會創建一個 XML 形式的測試結果。可以使用 /result 將輸出文件名改為 "console-test.xml":

nunit-console /result:console-test.xml nunit.tests.dll

可以使用 /noresult 選項禁止 XML 輸出。

注:為了兼容早起版本,NUnit 2.6 版本仍然可識別 /xml/noxml 選項。

指定輸出的目錄

默認情況下,所有的輸出文件都被創建在當前工作目錄。可以通過 /work 選項來指定具體的位置。

例如,下面的命令將會使 TestResult.xml 和 Output.txt 文件在 "results" 目錄中被創建。

nunit-console /work:results /out:Output.txt nunit.tests.dll

參考資料

  1. NUnit-Console Command Line Options

本文為 Dennis Gao 原創或翻譯技術文章,發表於博客園博客,未經作者本人允許禁止任何形式的轉載。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM