我有一些引用NUnit的程序集,并使用单个测试方法创建一个测试类.我能够获得此程序集的文件系统路径(例如"C:...\test.dll").我想以编程方式使用NUnit来运行此程序集.
到目前为止,我有:
var runner = new SimpleTestRunner(); runner.Load(path); var result = runner.Run(NullListener.NULL);
但是,调用runner.Load(path)会抛出FileNotFound异常.我可以通过堆栈跟踪看到问题是NUnit在堆栈中调用Assembly.Load(path).如果我将路径更改为"Test,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null",那么我仍会得到相同的错误.
我已经向AppDomain.Current.AssemblyResolve添加了一个事件处理程序,以查看我是否可以手动解析此类型,但我的处理程序永远不会被调用.
让Assembly.Load(...)工作的秘诀是什么?
如果要在控制台模式下打开,请添加nunit-console-runner.dll引用并使用:
NUnit.ConsoleRunner.Runner.Main(new string[] { System.Reflection.Assembly.GetExecutingAssembly().Location, });
如果你想以gui模式打开,添加nunit-gui-runner.dll引用并使用:
NUnit.Gui.AppEntry.Main(new string[] { System.Reflection.Assembly.GetExecutingAssembly().Location, "/run" });
这是最好的方法,因为您不必指定任何路径.
另一个选项是在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(); } } }; }