C# 如何从命令提示符执行 NUnit 测试用例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11085822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to execute NUnit test cases from command prompt
提问by Harshavardhan Konakanchi
How can I execute a test case from Command Console using NUnit? I had set of Selenium Tests written in C# based on NUnit framework. I need to execute the test cases simply by running from command console.
如何使用 NUnit 从命令控制台执行测试用例?我有一套基于 NUnit 框架用 C# 编写的 Selenium 测试。我只需要通过从命令控制台运行来执行测试用例。
In JUnit we can run test case from cmd as
在 JUnit 中,我们可以从 cmd 运行测试用例作为
java junit.swingui.TestRunner test.Run
How can we do above in NUnit?
我们如何在 NUnit 中做到以上几点?
采纳答案by vcsjones
Use nunit-console.exeto run tests from the command line.
使用nunit-console.exe运行在命令行测试。
For example:
例如:
nunit-console.exe /xml:results.xml path/to/test/assembly.dll
This will run the unit tests and save the results in the results.xml file, which you can work with easily.
这将运行单元测试并将结果保存在 results.xml 文件中,您可以轻松使用该文件。
See the documentationfor all of the various command line switches that are available.
有关所有可用的各种命令行开关,请参阅文档。
回答by Boris Sclauzero
I've just find another nice solution:
我刚刚找到了另一个不错的解决方案:
Adding the following command to the "Build Events" / "Post-Build Events", will run the tests in Nunit-Gui automatically after the project has been built.
将以下命令添加到“构建事件”/“构建后事件”,将在项目构建后自动运行 Nunit-Gui 中的测试。
I hope this can be useful:
我希望这很有用:
"C:\Program Files (x86)\NUnit 2.6.3\bin\nunit-x86.exe" $(TargetPath) /run
回答by Sergii Zhevzhyk
I would like to add a few words about the latest version of NUnit. The name of the console application has changedto nunit3-console.exein NUnit 3. Information about all possible options can be found in the official documentation. For example, run all tests in the assembly (the results are saved into the TestResult.xmlfile by default).
我想补充几句关于最新版本的 NUnit。NUnit 3 中控制台应用程序的名称已更改为nunit3-console.exe。有关所有可能选项的信息可以在官方文档中找到。例如,运行程序集中的所有测试(结果TestResult.xml默认保存到文件中)。
nunit3-console.exe path/to/test/assembly.dll
回答by Pratik Patel
Visual Studio: 2017, 2019(Preview) On Mac use below command:
Visual Studio: 2017, 2019(Preview) 在 Mac 上使用以下命令:
nunit-console <path/to/project>/<project-name>/bin/Debug/<project-solution-name>.dll
For example:
例如:
nunit-console /Users/pratik/Projects/selenium-mac13/selenium-test/bin/Debug/selenium-test.dll
nunit-console /Users/pratik/Projects/selenium-mac13/selenium-test/bin/Debug/selenium-test.dll
回答by Hemlata Gehlot
nunit3-console.exe "path of the testfile (dll)"
nunit3-console.exe“测试文件(dll)的路径”
回答by J Wood
- Working on a Windows 10 Desktop with Visual Studio
- I had a set of tests in C# where I'd set the test method with Category==API.
To run the tests (Nunit3-console) remotely via Bamboo, I added this Bamboo Powershell script:
Invoke-Command -Credential $credentials -ComputerName $Server -ScriptBlock{ $pathToDdrive = "D:" $pathtoDLL = Join-Path $pathToDdrive -ChildPath "RestOfThePathToDLL" cd D:\...\NUnit.ConsoleRunner.3.10.0\tools .\nunit3-console.exe $pathToDLL --where "cat=='API'" }
- 使用 Visual Studio 在 Windows 10 桌面上工作
- 我在 C# 中有一组测试,我将使用 Category==API 设置测试方法。
为了通过 Bamboo 远程运行测试(Nunit3-console),我添加了这个 Bamboo Powershell 脚本:
Invoke-Command -Credential $credentials -ComputerName $Server -ScriptBlock{ $pathToDdrive = "D:" $pathtoDLL = Join-Path $pathToDdrive -ChildPath "RestOfThePathToDLL" cd D:\...\NUnit.ConsoleRunner.3.10.0\tools .\nunit3-console.exe $pathToDLL --where "cat=='API'" }

