如何运行NUnit测试? [英] How to run a NUnit test?
问题描述
我想有一个独立的项目,这将考验通过USB连接远程系统的某些功能。
所以我想用NUnit的一切权力我的应用程序中。
我目前写了这个:
使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Text;
使用的System.Web;
使用NUnit.Framework;
命名ConsoleApplication1
{
类节目
{
静态无效的主要(字串[] args)
{
控制台。的ReadLine();
}
}
[的TestFixture]
公共类MyTest的
{
[测试]
公共无效MyTest的()
{
INT I = 3;
Assert.AreEqual(3,I);
}
}
}
我如何运行我的测试-suite,我如何得到一个测试报告?
我知道两个可能的解决方案来实现你想要什么。 NUnit的团队发布的 NUnit的引擎并的 NUnit的控制台上中的NuGet。
使用NUnit的发动机
使用NUnit.Engine;
使用NUnit.Framework;
使用的System.Reflection;
使用的System.Xml;
使用系统;
公共类节目
{
静态无效的主要(字串[] args)
{
//设置选项
路径字符串= Assembly.GetExecutingAssembly()的位置;
TestPackage包=新TestPackage(路径);
package.AddSetting(WorkDirectory,Environment.CurrentDirectory);
//准备发动机
ITestEngine引擎= TestEngineActivator.CreateInstance();
VAR _filterService = engine.Services.GetService< ITestFilterService>();
ITestFilterBuilder建设者= _filterService.GetTestFilterBuilder();
TestFilter emptyFilter = builder.GetFilter();使用
(ITestRunner亚军= engine.GetRunner(包))
{
//执行测试
XmlNode的结果= runner.Run(NULL,emptyFilter);
}
}
[的TestFixture]
公共类MyTests
{
// ...
}
}
安装的从的NuGet才能运行这个例子的NuGet Engine套件。结果会在结果
变量。没有为大家谁愿意使用这个包的警告:
它不打算直接使用由谁只是想运行$用户b $ b测试。
块引用>
使用标准NUnit的控制台应用程序
使用NUnit.Framework;
使用的System.Reflection;
类节目
{
静态无效的主要(字串[] args)
{
路径字符串= Assembly.GetExecutingAssembly()位置。
NUnit.ConsoleRunner.Program.Main(新[] {路径});
}
[的TestFixture]
公共类MyTests
{
// ...
}
}
安装的 NUnit的发动机和从NUnit的的NuGet 控制台包。在项目中加入
nunit3-console.exe
的参考。结果将保存在TestResult.xml
文件。我不喜欢这种方式,因为你可以使用一个简单的批处理文件实现相同的。I would like to have a standalone project that will test some features of a remote system connected through USB.
So I want to use all the power of NUnit inside my application.
I currently wrote this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using NUnit.Framework; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Console.ReadLine(); } } [TestFixture] public class MyTest { [Test] public void MyTest() { int i = 3; Assert.AreEqual(3, i); } } }
How do I run my test-suite and how do I get a test-report?
解决方案I know two possible solutions to achieve what you want. The NUnit team published NUnit Engine and NUnit Console on nuget.
Using the NUnit Engine
using NUnit.Engine; using NUnit.Framework; using System.Reflection; using System.Xml; using System; public class Program { static void Main(string[] args) { // set up the options string path = Assembly.GetExecutingAssembly().Location; TestPackage package = new TestPackage(path); package.AddSetting("WorkDirectory", Environment.CurrentDirectory); // prepare the engine ITestEngine engine = TestEngineActivator.CreateInstance(); var _filterService = engine.Services.GetService<ITestFilterService>(); ITestFilterBuilder builder = _filterService.GetTestFilterBuilder(); TestFilter emptyFilter = builder.GetFilter(); using (ITestRunner runner = engine.GetRunner(package)) { // execute the tests XmlNode result = runner.Run(null, emptyFilter); } } [TestFixture] public class MyTests { // ... } }
Install the Nuget Engine package from nuget in order to run this example. The results will be in the
result
variable. There is a warning for everyone who wants to use this package:It is not intended for direct use by users who simply want to run tests.
Using the standard NUnit Console Application
using NUnit.Framework; using System.Reflection; class Program { static void Main(string[] args) { string path = Assembly.GetExecutingAssembly().Location; NUnit.ConsoleRunner.Program.Main(new[] { path }); } [TestFixture] public class MyTests { // ... } }
Install NUnit Engine and NUnit Console packages from nuget. Add a reference to
nunit3-console.exe
in your project. The results will saved in theTestResult.xml
file. I don't like this approach, because you can achieve the same using a simple batch file.这篇关于如何运行NUnit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!