我正在尝试使用nunits测试引擎/测试运行器来运行特定的测试,但不知怎的,我无法让过滤器工作(如果我使用空过滤器,它会运行我的测试)。
这是到目前为止的代码:
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
答案 0 :(得分:2)
使用没有<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/StackOverflowAnswers/tree/master/NUnitTestRunner找到解决方案的源代码。