[英]How to run NUnit programmatically
I have some assembly that references NUnit and creates a single test class with a single test method. 我有一些引用NUnit的程序集,并使用单个测试方法创建一个测试类。 I am able to get the file system path to this assembly (eg "C:...\\test.dll").
我能够获得此程序集的文件系统路径(例如“C:... \\ test.dll”)。 I would like to programmatically use NUnit to run against this assembly.
我想以编程方式使用NUnit来运行此程序集。
So far I have: 到目前为止,我有:
var runner = new SimpleTestRunner();
runner.Load(path);
var result = runner.Run(NullListener.NULL);
However, calling runner.Load(path) throws a FileNotFound exception. 但是,调用runner.Load(path)会抛出FileNotFound异常。 I can see through the stack trace that the problem is with NUnit calling Assembly.Load(path) down the stack.
我可以通过堆栈跟踪看到问题是NUnit在堆栈中调用Assembly.Load(path)。 If I change path to be something like "Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" then I still get the same error.
如果我将路径更改为“Test,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”,那么我仍会得到相同的错误。
I have added an event handler to AppDomain.Current.AssemblyResolve to see if I could manually resolve this type but my handler never gets called. 我已经向AppDomain.Current.AssemblyResolve添加了一个事件处理程序,以查看我是否可以手动解析此类型,但我的处理程序永远不会被调用。
What is the secret to getting Assembly.Load(...) to work?? 让Assembly.Load(...)工作的秘诀是什么?
If you want to open in a console mode , add nunit-console-runner.dll reference and use: 如果要在控制台模式下打开,请添加nunit-console-runner.dll引用并使用:
NUnit.ConsoleRunner.Runner.Main(new string[]
{
System.Reflection.Assembly.GetExecutingAssembly().Location,
});
If you want to open in a gui mode , add nunit-gui-runner.dll reference and use: 如果你想以gui模式打开,添加nunit-gui-runner.dll引用并使用:
NUnit.Gui.AppEntry.Main(new string[]
{
System.Reflection.Assembly.GetExecutingAssembly().Location,
"/run"
});
This is the best approach because you don't have to specify any path. 这是最好的方法,因为您不必指定任何路径。
Another option is also to integrate NUnit runner in Visual Studio debugger output: 另一个选项是在Visual Studio调试器输出中集成NUnit runner:
public static void Main()
{
var assembly = Assembly.GetExecutingAssembly().FullName;
new TextUI (new DebugTextWriter()).Execute(new[] { assembly, "-wait" });
}
public class DebugTextWriter : StreamWriter
{
public DebugTextWriter()
: base(new DebugOutStream(), Encoding.Unicode, 1024)
{
this.AutoFlush = true;
}
class DebugOutStream : Stream
{
public override void Write(byte[] buffer, int offset, int count)
{
Debug.Write(Encoding.Unicode.GetString(buffer, offset, count));
}
public override bool CanRead { get { return false; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return true; } }
public override void Flush() { Debug.Flush(); }
public override long Length { get { throw new InvalidOperationException(); } }
public override int Read(byte[] buffer, int offset, int count) { throw new InvalidOperationException(); }
public override long Seek(long offset, SeekOrigin origin) { throw new InvalidOperationException(); }
public override void SetLength(long value) { throw new InvalidOperationException(); }
public override long Position
{
get { throw new InvalidOperationException(); }
set { throw new InvalidOperationException(); }
}
};
}
"What is the secret to getting Assembly.Load to work?" “让Assembly.Load工作的秘诀是什么?”
System.Reflection.Assembly.Load takes an string containing an assembly name, not a path to a file. System.Reflection.Assembly.Load采用包含程序集名称的字符串,而不是文件的路径。
If you want to load an assembly from a file use: 如果要从文件加载程序集,请使用:
Assembly a = System.Reflection.Assembly.LoadFrom(pathToFileOnDisk);
(LoadFrom actually uses Assembly.Load internally) (LoadFrom实际上在内部使用Assembly.Load)
By the way, is there any reason why you can;t use the NUnit-Console command line tool and just pass it the path to your test assembly? 顺便说一下,有什么理由可以使用NUnit-Console命令行工具并将它传递给测试程序集的路径吗? You could then just use the System.Diagnostics.Process to run this from within your client application, might be simpler?
然后,您可以使用System.Diagnostics.Process在客户端应用程序中运行它,可能更简单?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.