在VS2010 Express中调试Nunit测试

ili*_*ian 9 nunit unit-testing visual-studio

我在VS2010 Express中编写了一系列单元测试,并测试它们有时会失败的测试.由于VS的快速版本不允许插件运行,我不能简单地启动TestDriven.Net或等效的并调试测试.为了尝试解决这个问题,我将测试程序集转换为控制台应用程序,并使main方法如下所示:

class CrappyHackToDebugUnitTestInVSExpress
{
  public static void Main()
  {
     AppDomain.CurrentDomain.ExecuteAssemblyByName(
          @"C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit-console.exe",
          new [] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });
  }
}
Run Code Online (Sandbox Code Playgroud)

理论上我应该能够运行它,在我的测试中设置断点.如果它工作,这将是一个可接受的工作,但我一直得到以下:

FileLoadException
Could not load file or assembly 'C:\\Program Files\\NUnit 2.5.5\\bin\\net-2.0\\nunit-console.exe' 
or one of its dependencies. The given assembly name or codebase was invalid. 
(Exception from HRESULT: 0x80131047)
Run Code Online (Sandbox Code Playgroud)

现在文件存在,当手动运行时,nunit-console运行正常.可能是我的问题?

Dmi*_*kiy 7

基本上,您需要将程序集转换为Windows窗体应用程序,添加对nunit-gui-runner.dll程序集的引用,并将Main方法更改为如下所示:

    [STAThread]
    static void Main()
    {
        NUnit.Gui.AppEntry.Main(new string[] { Assembly.GetExecutingAssembly().Location });
    }
Run Code Online (Sandbox Code Playgroud)

这是另一个例子:

...
using NUnit.Gui;

namespace __libs
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            NUnit.Gui.AppEntry.Main(new string[] { @"C:\test\bin\Debug\test.exe" });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这将允许您进入某些测试,但对于红绿循环不是很好,因此您只想在调试时使用它,而不是在其他情况下.


小智 6

我玩了你的概念,似乎问题不是直接来自加载文件,而是来自依赖项.

我使用了以下修改过的代码:

而错误实际上是找不到/ lib目录中的nunit.core.dll.

 try
        {
            String NUnitPath = @"C:\Program Files\NUnit 2.5.7\bin\net-2.0\nunit-console.exe";

            AssemblyName asmName = System.Reflection.AssemblyName.GetAssemblyName(NUnitPath);

            AppDomain.CurrentDomain.ExecuteAssemblyByName(asmName, new[] { Assembly.GetExecutingAssembly().Location, "/framework:4.0" });

        }
        catch (Exception ex)
        {
            Trace.WriteLine(ex.Message);
            Trace.WriteLine(ex.StackTrace);
        }
Run Code Online (Sandbox Code Playgroud)

(我喜欢获取System.Reflection.AssemblyName,因为您可以检查并查看所有内容都与原始文件路径相对应.)

快速批量复制(xcopy nunit.*.dll)到我的测试项目的调试目录中,它运行得很好.(发现所需的最小依赖性应该是微不足道的)

使用NUnit 2.5.7在VC#2010 Express中测试(断点有效,但我没有真正使用任何其他选项.)虽然我确信你可以从中创建一个可通过的构建选项.

干杯!

PS - 首先发布在这里,所以我有点未经测试,因为格式化'代码'块.提前抱歉..