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
ExpressionAction
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 原创或翻译技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载。

原文地址:https://www.cnblogs.com/gaochundong/p/nunit_console_command_line_options.html