如何以编程方式运行NUnit [英] How to run NUnit programmatically

查看:52
本文介绍了如何以编程方式运行NUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些程序集引用NUnit并使用单个测试方法创建单个测试类.我可以获取此程序集的文件系统路径(例如"C:... \ test.dll").我想以编程方式使用NUnit在此程序集上运行.

I have some assembly that references NUnit and creates a single test class with a single test method. I am able to get the file system path to this assembly (e.g. "C:...\test.dll"). I would like to programmatically use NUnit to run against this assembly.

到目前为止,我有:

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",那么我仍然会遇到相同的错误.

However, calling runner.Load(path) throws a FileNotFound exception. I can see through the stack trace that the problem is with NUnit calling Assembly.Load(path) down the stack. 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.

我已经向AppDomain.Current.AssemblyResolve添加了一个事件处理程序,以查看是否可以手动解析该类型,但是我的处理程序从未被调用.

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.

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

What is the secret to getting Assembly.Load(...) to work??

推荐答案

如果要以控制台模式打开,请添加 nunit-console-runner.dll 参考并使用:

If you want to open in a console mode, add nunit-console-runner.dll reference and use:

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

如果要以 gui模式打开,请添加 nunit-gui-runner.dll 参考并使用:

If you want to open in a gui mode, add nunit-gui-runner.dll reference and use:

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.

另一个选择是将NUnit运行器集成到Visual Studio调试器输出中:

Another option is also to integrate NUnit runner in Visual Studio debugger output:

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(); }
        }
    };
}

这篇关于如何以编程方式运行NUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆