代码之家  ›  专栏  ›  技术社区  ›  justin.m.chase

如何以编程方式运行NUnit

  •  17
  • justin.m.chase  · 技术社区  · 16 年前

    到目前为止,我已经:

    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”,那么仍然会得到相同的错误。

    让Assembly.Load(…)工作的秘诀是什么??

    2 回复  |  直到 15 年前
        1
  •  29
  •   Ricibald    9 年前

    如果你想在一个 控制台模式 添加 nunit-console-runner.dll 参考和使用:

    NUnit.ConsoleRunner.Runner.Main(new string[]
       {
          System.Reflection.Assembly.GetExecutingAssembly().Location, 
       });
    

    如果你想在一个 图形用户界面模式 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(); }
            }
        };
    }
    
        2
  •  3
  •   Ash    16 年前

    “让Assembly.Load开始工作的秘诀是什么?”

    如果要从文件加载程序集,请使用:

    Assembly a = System.Reflection.Assembly.LoadFrom(pathToFileOnDisk);
    

    (LoadFrom实际上在内部使用Assembly.Load)

    顺便问一下,你有什么理由可以;不要使用 NUnit-Console command line tool