[英]How to run a NUnit test?
I would like to have a standalone project that will test some features of a remote system connected through USB. 我想要一个独立的项目,该项目将测试通过USB连接的远程系统的某些功能。
So I want to use all the power of NUnit inside my application. 因此,我想在应用程序中使用NUnit的所有功能。
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.
NUnit团队在nuget上发布了NUnit Engine和NUnit Console 。
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. 从nuget安装Nuget Engine软件包以运行此示例。 The results will be in the
result
variable. 结果将在
result
变量中。 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 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. 从nuget安装NUnit Engine和NUnit Console软件包。 Add a reference to
nunit3-console.exe
in your project. 在您的项目中添加对
nunit3-console.exe
的引用。 The results will saved in the TestResult.xml
file. 结果将保存在
TestResult.xml
文件中。 I don't like this approach, because you can achieve the same using a simple batch file. 我不喜欢这种方法,因为您可以使用简单的批处理文件来实现相同的目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.