将 NUnit TestRunner 与 xml 过滤器一起使用
Using NUnit TestRunner with xml filter
我正在尝试使用 nunits 测试 engine/test 运行ner 进行 运行 特定测试,但不知何故我无法让过滤器工作(它 运行如果我使用空过滤器,我的测试)。
这是目前的代码:
var package = new TestPackage("path/to/dll");
package.AddSetting("WorkDirectory", Path.GetDirectoryName("path/to/dll"));
using (ITestEngine engine = TestEngineActivator.CreateInstance())
{
engine.WorkDirectory = Path.GetDirectoryName(dllPath);
using (NUnit.Engine.ITestRunner runner = engine.GetRunner(package))
{
var filter = new TestFilter("<filter><tests><test>UnitTestProject2.UnitTest1.TestMethod1</test></tests></filter>");
var result = runner.Run(new TestEventDispatcher(), filter);
runner.Unload();
return CreateResult(result);
}
}
如果我尝试 运行 此代码并检查我得到的结果:
<command-line><![CDATA[".." ]]></command-line><filter><tests><test>UnitTestProject2.UnitTest1.TestMethod1</test></tests></filter><test-suite type="Assembly" id="0" name="UnitTestProject2.dll" fullname="...dll" runstate="NotRunnable" testcasecount="1" result="Failed" label="Error" start-time="2018-02-20 20:31:57Z" end-time="2018-02-20 20:31:57Z" duration="0.001" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0"><failure><message><
![CDATA[The NUnit 3 driver encountered an error while executing reflected code.]]></message><stack-trace><![CDATA[
Server stack trace:
vid NUnit.Engine.Drivers.NUnit3FrameworkDriver.CreateObject(String typeName, Object[] args)
vid NUnit.Engine.Drivers.NUnit3FrameworkDriver.Run(ITestEventListener listener, String filter)
vid NUnit.Engine.Runners.DirectTestRunner.RunTests(ITestEventListener listener, TestFilter filter)
vid System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
vid System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
Exception rethrown at [0]:
vid System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
vid System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
vid NUnit.Engine.ITestEngineRunner.Run(ITestEventListener listener, TestFilter filter)
vid NUnit.Engine.Runners.ProcessRunner.RunTests(ITestEventListener listener, TestFilter filter)]]></stack-trace></failure></test-suite>
我是不是遗漏了什么明显的东西?我在这里通读了文档,听起来应该是正确的方法:https://github.com/nunit/docs/wiki/Test-Filters
使用没有 <tests>
节点的过滤器,即:
<filter><test>UnitTestProject.Class1.Test123</test></filter>
对于多个测试,使用:
<filter><or><test>NUnitTestRunner.Class1.Test123</test><test>NUnitTestRunner.Class1.Test456</test></or></filter>
或者,您可以使用 ITestFilterBuilder
:
示例:
using (ITestEngine engine = TestEngineActivator.CreateInstance())
{
var filterService = engine.Services.GetService<ITestFilterService>();
var builder = filterService.GetTestFilterBuilder();
builder.AddTest("UnitTestProject.Class1.Test123");
var filter = builder.GetFilter();
using (ITestRunner runner = engine.GetRunner(package))
{
var result = runner.Run(listener: null, filter: filter);
}
}
两种解决方案都通过了测试:total="1" passed="1"
您可以在以下位置查看解决方案的源代码:https://github.com/dariusz-wozniak/WhosebugAnswers/tree/master/NUnitTestRunner。
我正在尝试使用 nunits 测试 engine/test 运行ner 进行 运行 特定测试,但不知何故我无法让过滤器工作(它 运行如果我使用空过滤器,我的测试)。
这是目前的代码:
var package = new TestPackage("path/to/dll");
package.AddSetting("WorkDirectory", Path.GetDirectoryName("path/to/dll"));
using (ITestEngine engine = TestEngineActivator.CreateInstance())
{
engine.WorkDirectory = Path.GetDirectoryName(dllPath);
using (NUnit.Engine.ITestRunner runner = engine.GetRunner(package))
{
var filter = new TestFilter("<filter><tests><test>UnitTestProject2.UnitTest1.TestMethod1</test></tests></filter>");
var result = runner.Run(new TestEventDispatcher(), filter);
runner.Unload();
return CreateResult(result);
}
}
如果我尝试 运行 此代码并检查我得到的结果:
<command-line><![CDATA[".." ]]></command-line><filter><tests><test>UnitTestProject2.UnitTest1.TestMethod1</test></tests></filter><test-suite type="Assembly" id="0" name="UnitTestProject2.dll" fullname="...dll" runstate="NotRunnable" testcasecount="1" result="Failed" label="Error" start-time="2018-02-20 20:31:57Z" end-time="2018-02-20 20:31:57Z" duration="0.001" total="1" passed="0" failed="1" inconclusive="0" skipped="0" asserts="0"><failure><message><
![CDATA[The NUnit 3 driver encountered an error while executing reflected code.]]></message><stack-trace><![CDATA[
Server stack trace:
vid NUnit.Engine.Drivers.NUnit3FrameworkDriver.CreateObject(String typeName, Object[] args)
vid NUnit.Engine.Drivers.NUnit3FrameworkDriver.Run(ITestEventListener listener, String filter)
vid NUnit.Engine.Runners.DirectTestRunner.RunTests(ITestEventListener listener, TestFilter filter)
vid System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Object[]& outArgs)
vid System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg)
Exception rethrown at [0]:
vid System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
vid System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
vid NUnit.Engine.ITestEngineRunner.Run(ITestEventListener listener, TestFilter filter)
vid NUnit.Engine.Runners.ProcessRunner.RunTests(ITestEventListener listener, TestFilter filter)]]></stack-trace></failure></test-suite>
我是不是遗漏了什么明显的东西?我在这里通读了文档,听起来应该是正确的方法:https://github.com/nunit/docs/wiki/Test-Filters
使用没有 <tests>
节点的过滤器,即:
<filter><test>UnitTestProject.Class1.Test123</test></filter>
对于多个测试,使用:
<filter><or><test>NUnitTestRunner.Class1.Test123</test><test>NUnitTestRunner.Class1.Test456</test></or></filter>
或者,您可以使用 ITestFilterBuilder
:
示例:
using (ITestEngine engine = TestEngineActivator.CreateInstance())
{
var filterService = engine.Services.GetService<ITestFilterService>();
var builder = filterService.GetTestFilterBuilder();
builder.AddTest("UnitTestProject.Class1.Test123");
var filter = builder.GetFilter();
using (ITestRunner runner = engine.GetRunner(package))
{
var result = runner.Run(listener: null, filter: filter);
}
}
两种解决方案都通过了测试:total="1" passed="1"
您可以在以下位置查看解决方案的源代码:https://github.com/dariusz-wozniak/WhosebugAnswers/tree/master/NUnitTestRunner。