C# 如何以编程方式运行 NUnit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/195061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:34:11  来源:igfitidea点击:

How to run NUnit programmatically

c#.netnunitassembly.load

提问by justin.m.chase

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.

我有一些程序集引用 NUnit 并使用单个测试方法创建单个测试类。我能够获得该程序集的文件系统路径(例如“C:...\test.dll”)。我想以编程方式使用 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. 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.

但是,调用 runner.Load(path) 会引发 FileNotFound 异常。我可以通过堆栈跟踪看到问题在于 NUnit 在堆栈中调用 Assembly.Load(path)。如果我将路径更改为“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(...) 工作的秘诀是什么?

采纳答案by Ricibald

If you want to open in a console mode, add nunit-console-runner.dllreference 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.dllreference 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 运行器:

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

回答by Ash

"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 tooland just pass it the path to your test assembly? You could then just use the System.Diagnostics.Process to run this from within your client application, might be simpler?

顺便说一句,您有什么理由不能使用NUnit-Console 命令行工具并将其传递给您的测试程序集的路径吗?然后您可以使用 System.Diagnostics.Process 从您的客户端应用程序中运行它,可能更简单?